diff --git a/api/pom.xml b/api/pom.xml index 552f933..970cf2a 100644 --- a/api/pom.xml +++ b/api/pom.xml @@ -11,7 +11,7 @@ ca.bc.gov educ-grad-business-api - 1.8.23 + 1.8.24 educ-grad-business-api GRAD Business API for external clients diff --git a/api/src/main/java/ca/bc/gov/educ/api/gradbusiness/controller/GradBusinessController.java b/api/src/main/java/ca/bc/gov/educ/api/gradbusiness/controller/GradBusinessController.java index 945ade6..1cb73c6 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/gradbusiness/controller/GradBusinessController.java +++ b/api/src/main/java/ca/bc/gov/educ/api/gradbusiness/controller/GradBusinessController.java @@ -130,12 +130,20 @@ public ResponseEntity schoolReportByMincode(@PathVariable String mincode @GetMapping(EducGraduationApiConstants.STUDENT_CREDENTIAL_PDF) @PreAuthorize("hasAuthority('SCOPE_GET_GRADUATION_DATA')") - @Operation(summary = "Get School Report pdf from graduation by mincode and report type", description = "Get School Report pdf from graduation by mincode and report type", tags = { "Graduation Data" }) + @Operation(summary = "Get Student Transcript or TVR Report pdf by PEN and report type", description = "Get Student Transcript or TVR Report pdf by PEN and report type", tags = { "Graduation Data" }) @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK")}) public ResponseEntity studentCredentialByType(@PathVariable String pen, @PathVariable String type, @RequestHeader(name="Authorization") String accessToken) { return gradBusinessService.getStudentCredentialPDFByType(pen, type, accessToken.replace(BEARER, "")); } + @GetMapping(EducGraduationApiConstants.STUDENT_TRANSCRIPT_PDF) + @PreAuthorize("hasAuthority('SCOPE_GET_GRADUATION_DATA')") + @Operation(summary = "Get Transcript Report pdf by PEN and optional xml type parameter", description = "Get Transcript Report pdf by PEN and optional xml type parameter", tags = { "Graduation Data" }) + @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK")}) + public ResponseEntity studentTranscriptByType(@PathVariable String pen, @RequestParam(required = false) String type, @RequestHeader(name="Authorization") String accessToken) { + return gradBusinessService.getStudentTranscriptPDFByType(pen, type, accessToken.replace(BEARER, "")); + } + @GetMapping(EducGraduationApiConstants.AMALGAMATED_SCHOOL_REPORT_PDF) @PreAuthorize("hasAuthority('SCOPE_GET_GRADUATION_DATA')") @Operation(summary = "Get School Report pdf from graduation by mincode and report type", description = "Get School Report pdf from graduation by mincode and report type", tags = { "Graduation Data" }) diff --git a/api/src/main/java/ca/bc/gov/educ/api/gradbusiness/service/GradBusinessService.java b/api/src/main/java/ca/bc/gov/educ/api/gradbusiness/service/GradBusinessService.java index b5a9bfe..a35ed20 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/gradbusiness/service/GradBusinessService.java +++ b/api/src/main/java/ca/bc/gov/educ/api/gradbusiness/service/GradBusinessService.java @@ -1,10 +1,11 @@ package ca.bc.gov.educ.api.gradbusiness.service; import ca.bc.gov.educ.api.gradbusiness.model.dto.Student; -import ca.bc.gov.educ.api.gradbusiness.util.EducGradBusinessUtil; import ca.bc.gov.educ.api.gradbusiness.util.EducGradBusinessApiConstants; +import ca.bc.gov.educ.api.gradbusiness.util.EducGradBusinessUtil; import ca.bc.gov.educ.api.gradbusiness.util.EducGraduationApiConstants; import io.github.resilience4j.retry.annotation.Retry; +import jakarta.transaction.Transactional; import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -19,7 +20,6 @@ import org.springframework.web.reactive.function.BodyInserters; import org.springframework.web.reactive.function.client.WebClient; -import jakarta.transaction.Transactional; import java.io.IOException; import java.io.InputStream; import java.util.*; @@ -35,6 +35,7 @@ public class GradBusinessService { private static final String BEARER = "Bearer "; private static final String APPLICATION_JSON = "application/json"; private static final String APPLICATION_PDF = "application/pdf"; + private static final String ACCEPT = "*/*"; /** * The Web client. */ @@ -86,7 +87,9 @@ public ResponseEntity prepareReportDataByPen(String pen, String type, St type = Optional.ofNullable(type).orElse(""); try { byte[] result = webClient.get().uri(String.format(educGraduationApiConstants.getGraduateReportDataByPenUrl(), pen) + "?type=" + type).headers(h -> h.setBearerAuth(accessToken)).retrieve().bodyToMono(byte[].class).block(); - assert result != null; + if(result == null) { + result = new byte[0]; + } return handleBinaryResponse(result, "graduation_report_data.json", MediaType.APPLICATION_JSON); } catch (Exception e) { return getInternalServerErrorResponse(e); @@ -108,7 +111,9 @@ public ResponseEntity prepareReportDataByGraduation(String graduationDat headers.put(HttpHeaders.ACCEPT, Collections.singletonList(APPLICATION_JSON)); headers.put(HttpHeaders.CONTENT_TYPE, Collections.singletonList(APPLICATION_JSON)); byte[] result = webClient.post().uri(educGraduationApiConstants.getGraduateReportDataByGraduation() + "?type=" + type).headers(h -> h.addAll(headers)).body(BodyInserters.fromValue(graduationData)).retrieve().bodyToMono(byte[].class).block(); - assert result != null; + if(result == null) { + result = new byte[0]; + } return handleBinaryResponse(result, "graduation_report_data.json", MediaType.APPLICATION_JSON); } catch (Exception e) { return getInternalServerErrorResponse(e); @@ -129,7 +134,9 @@ public ResponseEntity prepareXmlTranscriptReportDataByXmlRequest(String headers.put(HttpHeaders.ACCEPT, Collections.singletonList(APPLICATION_JSON)); headers.put(HttpHeaders.CONTENT_TYPE, Collections.singletonList(APPLICATION_JSON)); byte[] result = webClient.post().uri(educGraduationApiConstants.getXmlTranscriptReportData()).headers(h -> h.addAll(headers)).body(BodyInserters.fromValue(xmlRequest)).retrieve().bodyToMono(byte[].class).block(); - assert result != null; + if(result == null) { + result = new byte[0]; + } return handleBinaryResponse(result, "xml_transcript_report_data.json", MediaType.APPLICATION_JSON); } catch (Exception e) { return getInternalServerErrorResponse(e); @@ -150,7 +157,9 @@ public ResponseEntity getStudentDemographicsByPen(String pen, String acc headers.put(HttpHeaders.ACCEPT, Collections.singletonList(APPLICATION_JSON)); headers.put(HttpHeaders.CONTENT_TYPE, Collections.singletonList(APPLICATION_JSON)); byte[] result = webClient.get().uri(String.format(educGradStudentApiConstants.getPenDemographicStudentApiUrl(), pen)).headers(h -> h.setBearerAuth(accessToken)).retrieve().bodyToMono(byte[].class).block(); - assert result != null; + if(result == null) { + result = new byte[0]; + } return handleBinaryResponse(result, "student_demog_data.json", MediaType.APPLICATION_JSON); } catch (Exception e) { return getInternalServerErrorResponse(e); @@ -209,8 +218,10 @@ public ResponseEntity getSchoolReportPDFByMincode(String mincode, String headers.put(HttpHeaders.ACCEPT, Collections.singletonList(APPLICATION_PDF)); headers.put(HttpHeaders.CONTENT_TYPE, Collections.singletonList(APPLICATION_PDF)); InputStreamResource result = webClient.get().uri(String.format(educGraduationApiConstants.getSchoolReportByMincode(), mincode,type)).headers(h -> h.setBearerAuth(accessToken)).retrieve().bodyToMono(InputStreamResource.class).block(); - assert result != null; - byte[] res = IOUtils.toByteArray(result.getInputStream()); + byte[] res = new byte[0]; + if(result != null) { + res = result.getInputStream().readAllBytes(); + } return handleBinaryResponse(res, EducGradBusinessUtil.getFileNameSchoolReports(mincode,year,month,type), MediaType.APPLICATION_PDF); } catch (Exception e) { return getInternalServerErrorResponse(e); @@ -253,7 +264,6 @@ public ResponseEntity getAmalgamatedSchoolReportPDFByMincode(String minc return null; } - @Transactional public ResponseEntity getStudentCredentialPDFByType(String pen, String type, String accessToken) { List stud = getStudentByPenFromStudentAPI(pen,accessToken); @@ -264,8 +274,10 @@ public ResponseEntity getStudentCredentialPDFByType(String pen, String t headers.put(HttpHeaders.ACCEPT, Collections.singletonList(APPLICATION_PDF)); headers.put(HttpHeaders.CONTENT_TYPE, Collections.singletonList(APPLICATION_PDF)); InputStreamResource result = webClient.get().uri(String.format(educGraduationApiConstants.getStudentCredentialByType(), studObj.getStudentID(),type)).headers(h -> h.setBearerAuth(accessToken)).retrieve().bodyToMono(InputStreamResource.class).block(); - assert result != null; - byte[] res = IOUtils.toByteArray(result.getInputStream()); + byte[] res = new byte[0]; + if(result != null) { + res = result.getInputStream().readAllBytes(); + } return handleBinaryResponse(res, EducGradBusinessUtil.getFileNameStudentCredentials(studObj.getMincode(),pen,type), MediaType.APPLICATION_PDF); } catch (Exception e) { return getInternalServerErrorResponse(e); @@ -273,7 +285,32 @@ public ResponseEntity getStudentCredentialPDFByType(String pen, String t } - - - + @Transactional + public ResponseEntity getStudentTranscriptPDFByType(String pen, String type, String accessToken) { + try { + byte[] reportData = prepareReportDataByPen(pen, type, accessToken).getBody(); + StringBuilder reportRequest = new StringBuilder(); + String reportOptions = "\"options\": {\n" + + " \"cacheReport\": false,\n" + + " \"convertTo\": \"pdf\",\n" + + " \"overwrite\": false,\n" + + " \"reportName\": \"transcript\",\n" + + " \"reportFile\": \""+pen+" Transcript Report.pdf\"\n" + + " },\n"; + reportRequest.append("{\n"); + reportRequest.append(reportOptions); + reportRequest.append("\"data\":\n"); + reportRequest.append(new String(reportData)).append("\n"); + reportRequest.append("}\n"); + HttpHeaders headers = new HttpHeaders(); + headers.put(HttpHeaders.AUTHORIZATION, Collections.singletonList(BEARER + accessToken)); + headers.put(HttpHeaders.ACCEPT, Collections.singletonList(ACCEPT)); + headers.put(HttpHeaders.CONTENT_TYPE, Collections.singletonList(APPLICATION_JSON)); + byte[] result = webClient.post().uri(educGraduationApiConstants.getStudentTranscriptReportByRequest()).headers(h -> h.addAll(headers)).body(BodyInserters.fromValue(reportRequest.toString())).retrieve().bodyToMono(byte[].class).block(); + assert result != null; + return handleBinaryResponse(result, pen + " Transcript Report.pdf", MediaType.APPLICATION_PDF); + } catch (Exception e) { + return getInternalServerErrorResponse(e); + } + } } diff --git a/api/src/main/java/ca/bc/gov/educ/api/gradbusiness/util/EducGraduationApiConstants.java b/api/src/main/java/ca/bc/gov/educ/api/gradbusiness/util/EducGraduationApiConstants.java index dd0082f..4539f55 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/gradbusiness/util/EducGraduationApiConstants.java +++ b/api/src/main/java/ca/bc/gov/educ/api/gradbusiness/util/EducGraduationApiConstants.java @@ -26,6 +26,7 @@ public class EducGraduationApiConstants { public static final String SCHOOL_REPORT_PDF = "/schoolreport/{mincode}"; public static final String AMALGAMATED_SCHOOL_REPORT_PDF = "/amalgamated/schoolreport/{mincode}"; public static final String STUDENT_CREDENTIAL_PDF = "/studentcredential/{pen}/type/{type}"; + public static final String STUDENT_TRANSCRIPT_PDF = "/studenttranscript/{pen}"; //Default Date format constants public static final String DEFAULT_CREATED_BY = "API_GRADUATION"; @@ -50,5 +51,8 @@ public class EducGraduationApiConstants { @Value("${endpoint.grad-graduation-report-api.student-credential-by-type.url}") private String studentCredentialByType; + @Value("${endpoint.grad-report-api.transcript-by-request.url}") + private String studentTranscriptReportByRequest; + } diff --git a/api/src/main/resources/application.yaml b/api/src/main/resources/application.yaml index 2a8ba81..24257d0 100644 --- a/api/src/main/resources/application.yaml +++ b/api/src/main/resources/application.yaml @@ -115,6 +115,8 @@ endpoint: grad-report-api: report-data-by-xml: url: ${REPORT_API}api/v1/reports/xmltranscriptreport + transcript-by-request: + url: ${REPORT_API}api/v1/reports/transcriptreport grad-student-api: demographic: url: ${GRAD_STUDENT_API}api/v1/student/demog/pen/%s diff --git a/api/src/test/java/ca/bc/gov/educ/api/gradbusiness/EducGradBusinessApiApplicationTests.java b/api/src/test/java/ca/bc/gov/educ/api/gradbusiness/EducGradBusinessApiApplicationTests.java index 24e62c7..7e5c70e 100644 --- a/api/src/test/java/ca/bc/gov/educ/api/gradbusiness/EducGradBusinessApiApplicationTests.java +++ b/api/src/test/java/ca/bc/gov/educ/api/gradbusiness/EducGradBusinessApiApplicationTests.java @@ -24,6 +24,7 @@ import reactor.core.publisher.Mono; import java.io.BufferedReader; +import java.io.ByteArrayInputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; @@ -114,6 +115,25 @@ void testReportDataByPen() throws Exception { json = new String(byteData.getBody()); assertEquals(json,reportData); + when(this.webClient.get()).thenReturn(this.requestHeadersUriMock); + when(this.requestHeadersUriMock.uri(String.format(educGraduationApiConstants.getGraduateReportDataByPenUrl(),"128385861") + "?type=CERT")).thenReturn(this.requestHeadersMock); + when(this.requestHeadersMock.headers(any(Consumer.class))).thenReturn(this.requestHeadersMock); + when(this.requestHeadersMock.retrieve()).thenReturn(this.responseMock); + when(this.responseMock.bodyToMono(byte[].class)).thenReturn(Mono.just(new byte[0])); + + byteData = gradBusinessService.prepareReportDataByPen(pen, "CERT", "accessToken"); + assertNotNull(byteData); + assertNull(byteData.getBody()); + + when(this.webClient.get()).thenReturn(this.requestHeadersUriMock); + when(this.requestHeadersUriMock.uri(String.format(educGraduationApiConstants.getGraduateReportDataByPenUrl(),"128385861") + "?type=CERT")).thenReturn(this.requestHeadersMock); + when(this.requestHeadersMock.headers(any(Consumer.class))).thenReturn(this.requestHeadersMock); + when(this.requestHeadersMock.retrieve()).thenReturn(this.responseMock); + when(this.responseMock.bodyToMono(byte[].class)).thenReturn(null); + + byteData = gradBusinessService.prepareReportDataByPen(pen, "CERT", "accessToken"); + assertNotNull(byteData); + assertTrue(byteData.getStatusCode().is5xxServerError()); } @@ -171,6 +191,30 @@ void testReportDataByGraduationData() throws Exception { json = new String(byteData.getBody()); assertEquals(json,reportData); + when(this.webClient.post()).thenReturn(this.requestBodyUriMock); + when(this.requestBodyUriMock.uri(educGraduationApiConstants.getGraduateReportDataByGraduation() + "?type=CERT")).thenReturn(this.requestBodyUriMock); + when(this.requestBodyUriMock.headers(any(Consumer.class))).thenReturn(this.requestBodyMock); + when(this.requestBodyMock.contentType(any())).thenReturn(this.requestBodyMock); + when(this.requestBodyMock.body(any(BodyInserter.class))).thenReturn(this.requestHeadersMock); + when(this.requestHeadersMock.retrieve()).thenReturn(this.responseMock); + when(this.responseMock.bodyToMono(byte[].class)).thenReturn(Mono.just(new byte[0])); + + byteData = gradBusinessService.prepareReportDataByGraduation(studentGradData, "CERT", "accessToken"); + assertNotNull(byteData); + assertNull(byteData.getBody()); + + when(this.webClient.post()).thenReturn(this.requestBodyUriMock); + when(this.requestBodyUriMock.uri(educGraduationApiConstants.getGraduateReportDataByGraduation() + "?type=CERT")).thenReturn(this.requestBodyUriMock); + when(this.requestBodyUriMock.headers(any(Consumer.class))).thenReturn(this.requestBodyMock); + when(this.requestBodyMock.contentType(any())).thenReturn(this.requestBodyMock); + when(this.requestBodyMock.body(any(BodyInserter.class))).thenReturn(this.requestHeadersMock); + when(this.requestHeadersMock.retrieve()).thenReturn(this.responseMock); + when(this.responseMock.bodyToMono(byte[].class)).thenReturn(null); + + byteData = gradBusinessService.prepareReportDataByGraduation(studentGradData, "CERT", "accessToken"); + assertNotNull(byteData); + assertTrue(byteData.getStatusCode().is5xxServerError()); + } @org.junit.jupiter.api.Test @@ -216,8 +260,9 @@ void testSchoolReportPDFByMincode() throws Exception { String mincode = "128385861"; String type = "NONGRADPRJ"; - InputStream is = getClass().getClassLoader().getResourceAsStream("json/xmlTranscriptReportRequest.json"); - InputStreamResource pdf = new InputStreamResource(is); + + byte[] samplePdf = readBinaryFile("data/sample.pdf"); + InputStreamResource pdf = new InputStreamResource(new ByteArrayInputStream(samplePdf)); when(this.webClient.get()).thenReturn(this.requestHeadersUriMock); when(this.requestHeadersUriMock.uri(String.format(educGraduationApiConstants.getSchoolReportByMincode(),mincode,type))).thenReturn(this.requestHeadersMock); @@ -235,8 +280,9 @@ void testgetAmalgamatedSchoolReportPDFByMincode() throws Exception { String mincode = "128385861"; String type = "TVRNONGRAD"; - InputStream is = getClass().getClassLoader().getResourceAsStream("json/xmlTranscriptReportRequest.json"); - InputStreamResource pdf = new InputStreamResource(is); + + byte[] samplePdf = readBinaryFile("data/sample.pdf"); + InputStreamResource pdf = new InputStreamResource(new ByteArrayInputStream(samplePdf)); UUID studentID = UUID.randomUUID(); when(this.webClient.get()).thenReturn(this.requestHeadersUriMock); @@ -254,29 +300,45 @@ void testgetAmalgamatedSchoolReportPDFByMincode() throws Exception { ResponseEntity byteData = gradBusinessService.getAmalgamatedSchoolReportPDFByMincode(mincode, type, "accessToken"); assertNotNull(byteData); assertNotNull(byteData.getBody()); + + pdf = new InputStreamResource(new ByteArrayInputStream(new byte[0])); + + when(this.webClient.get()).thenReturn(this.requestHeadersUriMock); + when(this.requestHeadersUriMock.uri(String.format(educGraduationApiConstants.getStudentCredentialByType(),studentID,"ACHV"))).thenReturn(this.requestHeadersMock); + when(this.requestHeadersMock.headers(any(Consumer.class))).thenReturn(this.requestHeadersMock); + when(this.requestHeadersMock.retrieve()).thenReturn(this.responseMock); + when(this.responseMock.bodyToMono(InputStreamResource.class)).thenReturn(Mono.just(pdf)); + + byteData = gradBusinessService.getAmalgamatedSchoolReportPDFByMincode(mincode, type, "accessToken"); + assertNotNull(byteData); + assertNotNull(byteData.getBody()); + assertTrue(byteData.getStatusCode().is5xxServerError()); + } @Test - void testSchoolReportPDFByMincode_witherror() throws Exception { + void testSchoolReportPDFByMincode_NotFound() throws Exception { String mincode = "128385861"; String type = "NONGRADPRJ"; - InputStream is = getClass().getClassLoader().getResourceAsStream("json/xmlTranscriptReportRequest.json"); + + byte[] samplePdf = new byte[0]; + InputStreamResource pdf = new InputStreamResource(new ByteArrayInputStream(samplePdf)); when(this.webClient.get()).thenReturn(this.requestHeadersUriMock); when(this.requestHeadersUriMock.uri(String.format(educGraduationApiConstants.getSchoolReportByMincode(),mincode,type))).thenReturn(this.requestHeadersMock); when(this.requestHeadersMock.headers(any(Consumer.class))).thenReturn(this.requestHeadersMock); when(this.requestHeadersMock.retrieve()).thenReturn(this.responseMock); - when(this.responseMock.bodyToMono(InputStreamResource.class)).thenReturn(null); + when(this.responseMock.bodyToMono(InputStreamResource.class)).thenReturn(Mono.just(pdf)); ResponseEntity byteData = gradBusinessService.getSchoolReportPDFByMincode(mincode, type, "accessToken"); assertNotNull(byteData); - assertTrue(byteData.getBody().length > 0); + assertNull(byteData.getBody()); } @Test - void testSchoolReportPDFByMincode_witherror2() throws Exception { + void testSchoolReportPDFByMincode_Error500() throws Exception { String mincode = "128385861"; String type = "NONGRADPRJ"; @@ -292,6 +354,7 @@ void testSchoolReportPDFByMincode_witherror2() throws Exception { ResponseEntity byteData = gradBusinessService.getSchoolReportPDFByMincode(mincode, type, "accessToken"); assertNotNull(byteData); assertTrue(byteData.getBody().length > 0); + assertTrue(byteData.getStatusCode().is5xxServerError()); } @Test @@ -310,18 +373,41 @@ void testGetStudentDemographicsByPen() throws Exception { ResponseEntity byteData = gradBusinessService.getStudentDemographicsByPen(pen,"accessToken"); assertNotNull(byteData); assertTrue(byteData.getBody().length > 0); + + when(this.webClient.get()).thenReturn(this.requestHeadersUriMock); + when(this.requestHeadersUriMock.uri(String.format(educGradStudentApiConstants.getPenDemographicStudentApiUrl(),pen))).thenReturn(this.requestHeadersMock); + when(this.requestHeadersMock.headers(any(Consumer.class))).thenReturn(this.requestHeadersMock); + when(this.requestHeadersMock.retrieve()).thenReturn(this.responseMock); + when(this.responseMock.bodyToMono(byte[].class)).thenReturn(Mono.just(new byte[0])); + + byteData = gradBusinessService.getStudentDemographicsByPen(pen,"accessToken"); + assertNotNull(byteData); + assertNull(byteData.getBody()); + + when(this.webClient.get()).thenReturn(this.requestHeadersUriMock); + when(this.requestHeadersUriMock.uri(String.format(educGradStudentApiConstants.getPenDemographicStudentApiUrl(),pen))).thenReturn(this.requestHeadersMock); + when(this.requestHeadersMock.headers(any(Consumer.class))).thenReturn(this.requestHeadersMock); + when(this.requestHeadersMock.retrieve()).thenReturn(this.responseMock); + when(this.responseMock.bodyToMono(byte[].class)).thenThrow(); + + byteData = gradBusinessService.getStudentDemographicsByPen(pen,"accessToken"); + assertNotNull(byteData); + assertTrue(byteData.getBody().length > 0); + assertTrue(byteData.getStatusCode().is5xxServerError()); } @Test void testStudentCredentialPDFByType() throws Exception { String pen = "128385861"; - String type = "TRAN"; - InputStream is = getClass().getClassLoader().getResourceAsStream("json/xmlTranscriptReportRequest.json"); - InputStreamResource pdf = new InputStreamResource(is); + String type = "GRADREG"; + + byte[] samplePdf = readBinaryFile("data/sample.pdf"); + InputStreamResource pdf = new InputStreamResource(new ByteArrayInputStream(samplePdf)); Student sObj = new Student(); sObj.setStudentID(UUID.randomUUID().toString()); + sObj.setPen(pen); sObj.setMincode("123123112"); when(this.webClient.get()).thenReturn(this.requestHeadersUriMock); @@ -344,23 +430,60 @@ void testStudentCredentialPDFByType() throws Exception { assertTrue(byteData.getBody().length > 0); } + @org.junit.jupiter.api.Test + void testStudentTranscriptPDFByTypeByPen() throws Exception { + + String pen = "128385861"; + + String reportData = readFile("json/studentTranscriptReportData.json"); + assertNotNull(reportData); + + when(this.webClient.get()).thenReturn(this.requestHeadersUriMock); + when(this.requestHeadersUriMock.uri(String.format(educGraduationApiConstants.getGraduateReportDataByPenUrl(),"128385861") + "?type=")).thenReturn(this.requestHeadersMock); + when(this.requestHeadersMock.headers(any(Consumer.class))).thenReturn(this.requestHeadersMock); + when(this.requestHeadersMock.retrieve()).thenReturn(this.responseMock); + when(this.responseMock.bodyToMono(byte[].class)).thenReturn(Mono.just(reportData.getBytes())); + + ResponseEntity byteData = gradBusinessService.prepareReportDataByPen(pen, null, "accessToken"); + assertNotNull(byteData); + assertTrue(byteData.getBody().length > 0); + String json = new String(byteData.getBody()); + assertEquals(json,reportData); + + byte[] transcriptPdfSample = readBinaryFile("data/sample.pdf"); + + when(this.webClient.post()).thenReturn(this.requestBodyUriMock); + when(this.requestBodyUriMock.uri(educGraduationApiConstants.getStudentTranscriptReportByRequest())).thenReturn(this.requestBodyUriMock); + when(this.requestBodyUriMock.headers(any(Consumer.class))).thenReturn(this.requestBodyMock); + when(this.requestBodyMock.contentType(any())).thenReturn(this.requestBodyMock); + when(this.requestBodyMock.body(any(BodyInserter.class))).thenReturn(this.requestHeadersMock); + when(this.requestHeadersMock.retrieve()).thenReturn(this.responseMock); + when(this.responseMock.bodyToMono(byte[].class)).thenReturn(Mono.just(transcriptPdfSample)); + + ResponseEntity transcriptPdf = gradBusinessService.getStudentTranscriptPDFByType(pen, "xml", "accessToken"); + assertNotNull(transcriptPdf.getBody()); + assertEquals(transcriptPdfSample,transcriptPdf.getBody()); + } + @Test - void testStudentCredentialPDFByType_witherror() throws Exception { + void testStudentCredentialPDFByType_NotFound() throws Exception { String pen = "128385861"; - String type = "TRAN"; - InputStream is = getClass().getClassLoader().getResourceAsStream("json/xmlTranscriptReportRequest.json"); - InputStreamResource pdf = new InputStreamResource(is); + String type = "NONGRADREG"; + + byte[] samplePdf = new byte[0]; + InputStreamResource pdf = new InputStreamResource(new ByteArrayInputStream(samplePdf)); Student sObj = new Student(); sObj.setStudentID(UUID.randomUUID().toString()); + sObj.setPen(pen); sObj.setMincode("123123112"); when(this.webClient.get()).thenReturn(this.requestHeadersUriMock); when(this.requestHeadersUriMock.uri(String.format(educGraduationApiConstants.getStudentCredentialByType(),sObj.getStudentID(),type))).thenReturn(this.requestHeadersMock); when(this.requestHeadersMock.headers(any(Consumer.class))).thenReturn(this.requestHeadersMock); when(this.requestHeadersMock.retrieve()).thenReturn(this.responseMock); - when(this.responseMock.bodyToMono(InputStreamResource.class)).thenReturn(null); + when(this.responseMock.bodyToMono(InputStreamResource.class)).thenReturn(Mono.just(pdf)); final ParameterizedTypeReference> responseType = new ParameterizedTypeReference<>() { }; @@ -373,19 +496,23 @@ void testStudentCredentialPDFByType_witherror() throws Exception { ResponseEntity byteData = gradBusinessService.getStudentCredentialPDFByType(pen, type, "accessToken"); assertNotNull(byteData); - assertTrue(byteData.getBody().length > 0); + assertNull(byteData.getBody()); + } @Test - void testStudentCredentialPDFByType_witherror2() { + void testStudentCredentialPDFByType_Error500() throws Exception { String pen = "128385861"; String type = "TRAN"; String studentID = UUID.randomUUID().toString(); - InputStream is = getClass().getClassLoader().getResourceAsStream("json/xml_report_sample.xml"); + + byte[] samplePdf = readBinaryFile("data/sample.pdf"); + InputStreamResource pdf = new InputStreamResource(new ByteArrayInputStream(samplePdf)); Student sObj = new Student(); sObj.setStudentID(studentID); + sObj.setPen(pen); sObj.setMincode("123123112"); final ParameterizedTypeReference> responseType = new ParameterizedTypeReference<>() { @@ -400,6 +527,13 @@ void testStudentCredentialPDFByType_witherror2() { ResponseEntity byteData = gradBusinessService.getStudentCredentialPDFByType(pen, type, "accessToken"); assertNotNull(byteData); assertTrue(byteData.getBody().length > 0); + assertTrue(byteData.getStatusCode().is5xxServerError()); + } + + private byte[] readBinaryFile(String path) throws Exception { + ClassLoader classLoader = getClass().getClassLoader(); + InputStream inputStream = classLoader.getResourceAsStream(path); + return inputStream.readAllBytes(); } } diff --git a/api/src/test/java/ca/bc/gov/educ/api/gradbusiness/EducGradBusinessApiControllerTests.java b/api/src/test/java/ca/bc/gov/educ/api/gradbusiness/EducGradBusinessApiControllerTests.java index bec8df7..f5e9375 100644 --- a/api/src/test/java/ca/bc/gov/educ/api/gradbusiness/EducGradBusinessApiControllerTests.java +++ b/api/src/test/java/ca/bc/gov/educ/api/gradbusiness/EducGradBusinessApiControllerTests.java @@ -3,8 +3,6 @@ import ca.bc.gov.educ.api.gradbusiness.controller.GradBusinessController; import ca.bc.gov.educ.api.gradbusiness.model.dto.Student; import ca.bc.gov.educ.api.gradbusiness.service.GradBusinessService; -import org.junit.After; -import org.junit.Before; import org.junit.FixMethodOrder; import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; @@ -27,7 +25,6 @@ import java.util.UUID; import static org.junit.Assert.assertNotNull; -import static org.mockito.MockitoAnnotations.openMocks; @FixMethodOrder(MethodSorters.NAME_ASCENDING) @RunWith(SpringRunner.class) @@ -58,9 +55,9 @@ void testReportDataByPen() throws Exception { .contentType(MediaType.APPLICATION_JSON) .body(reportData.getBytes()); - Mockito.when(gradBusinessService.prepareReportDataByPen(pen, null, "abc")).thenReturn(response); - gradBusinessController.transcriptReportDataByPen(pen, null, "abc"); - Mockito.verify(gradBusinessService).prepareReportDataByPen(pen, null, "abc"); + Mockito.when(gradBusinessService.prepareReportDataByPen(pen, null, "accessToken")).thenReturn(response); + gradBusinessController.transcriptReportDataByPen(pen, null, "accessToken"); + Mockito.verify(gradBusinessService).prepareReportDataByPen(pen, null, "accessToken"); reportData = readFile("json/studentCertificateReportData.json"); assertNotNull(reportData); @@ -73,9 +70,9 @@ void testReportDataByPen() throws Exception { .contentType(MediaType.APPLICATION_JSON) .body(reportData.getBytes()); - Mockito.when(gradBusinessService.prepareReportDataByPen(pen, "CERT", "abc")).thenReturn(response); - gradBusinessController.certificateReportDataByPen(pen, "abc"); - Mockito.verify(gradBusinessService).prepareReportDataByPen(pen, "CERT", "abc"); + Mockito.when(gradBusinessService.prepareReportDataByPen(pen, "CERT", "accessToken")).thenReturn(response); + gradBusinessController.certificateReportDataByPen(pen, "accessToken"); + Mockito.verify(gradBusinessService).prepareReportDataByPen(pen, "CERT", "accessToken"); } @@ -97,9 +94,9 @@ void testReportDataByGraduationData() throws Exception { .body(reportData.getBytes()); - Mockito.when(gradBusinessService.prepareReportDataByGraduation(studentGradData, null, "abc")).thenReturn(response); - gradBusinessController.transcriptReportDataFromGraduation(studentGradData, null, "abc"); - Mockito.verify(gradBusinessService).prepareReportDataByGraduation(studentGradData, null, "abc"); + Mockito.when(gradBusinessService.prepareReportDataByGraduation(studentGradData, null, "accessToken")).thenReturn(response); + gradBusinessController.transcriptReportDataFromGraduation(studentGradData, null, "accessToken"); + Mockito.verify(gradBusinessService).prepareReportDataByGraduation(studentGradData, null, "accessToken"); reportData = readFile("json/studentCertificateReportData.json"); assertNotNull(reportData); @@ -112,9 +109,9 @@ void testReportDataByGraduationData() throws Exception { .contentType(MediaType.APPLICATION_JSON) .body(reportData.getBytes()); - Mockito.when(gradBusinessService.prepareReportDataByGraduation(studentGradData, "CERT", "abc")).thenReturn(response); - gradBusinessController.certificateReportDataFromGraduation(studentGradData, "abc"); - Mockito.verify(gradBusinessService).prepareReportDataByGraduation(studentGradData, "CERT", "abc"); + Mockito.when(gradBusinessService.prepareReportDataByGraduation(studentGradData, "CERT", "accessToken")).thenReturn(response); + gradBusinessController.certificateReportDataFromGraduation(studentGradData, "accessToken"); + Mockito.verify(gradBusinessService).prepareReportDataByGraduation(studentGradData, "CERT", "accessToken"); } @Test @@ -134,9 +131,9 @@ void testXmlTranscriptData() throws Exception { .contentType(MediaType.APPLICATION_XML) .body(xmlTranscriptReportData.getBytes()); - Mockito.when(gradBusinessService.prepareXmlTranscriptReportDataByXmlRequest(xmlReportRequest, "abc")).thenReturn(response); - gradBusinessController.transcriptXmlReportDataFromXmlRequest(xmlReportRequest, "abc"); - Mockito.verify(gradBusinessService).prepareXmlTranscriptReportDataByXmlRequest(xmlReportRequest, "abc"); + Mockito.when(gradBusinessService.prepareXmlTranscriptReportDataByXmlRequest(xmlReportRequest, "accessToken")).thenReturn(response); + gradBusinessController.transcriptXmlReportDataFromXmlRequest(xmlReportRequest, "accessToken"); + Mockito.verify(gradBusinessService).prepareXmlTranscriptReportDataByXmlRequest(xmlReportRequest, "accessToken"); } @@ -147,9 +144,9 @@ void testGetGradStudentByPenFromStudentAPI() throws Exception { obj.setPen("12312321"); obj.setStudentID(UUID.randomUUID().toString()); - Mockito.when(gradBusinessService.getStudentByPenFromStudentAPI("12312321", "abc")).thenReturn(List.of(obj)); - gradBusinessController.getGradStudentByPenFromStudentAPI("12312321", "abc"); - Mockito.verify(gradBusinessService).getStudentByPenFromStudentAPI("12312321", "abc"); + Mockito.when(gradBusinessService.getStudentByPenFromStudentAPI("12312321", "accessToken")).thenReturn(List.of(obj)); + gradBusinessController.getGradStudentByPenFromStudentAPI("12312321", "accessToken"); + Mockito.verify(gradBusinessService).getStudentByPenFromStudentAPI("12312321", "accessToken"); } @@ -165,9 +162,9 @@ void testGetGradStudentDemographicsByPen() throws Exception { .contentType(MediaType.APPLICATION_XML) .body(greBPack); - Mockito.when(gradBusinessService.getStudentDemographicsByPen("12312321", "abc")).thenReturn(response); - gradBusinessController.getGradStudentDemographicsByPen("12312321", "abc"); - Mockito.verify(gradBusinessService).getStudentDemographicsByPen("12312321", "abc"); + Mockito.when(gradBusinessService.getStudentDemographicsByPen("12312321", "accessToken")).thenReturn(response); + gradBusinessController.getGradStudentDemographicsByPen("12312321", "accessToken"); + Mockito.verify(gradBusinessService).getStudentDemographicsByPen("12312321", "accessToken"); } @@ -183,9 +180,9 @@ void testSchoolReportByMincode() throws Exception { .contentType(MediaType.APPLICATION_XML) .body(greBPack); - Mockito.when(gradBusinessService.getSchoolReportPDFByMincode("12312321", "GRAD","abc")).thenReturn(response); - gradBusinessController.schoolReportByMincode("12312321","GRAD", "abc"); - Mockito.verify(gradBusinessService).getSchoolReportPDFByMincode("12312321","GRAD", "abc"); + Mockito.when(gradBusinessService.getSchoolReportPDFByMincode("12312321", "GRAD","accessToken")).thenReturn(response); + gradBusinessController.schoolReportByMincode("12312321","GRAD", "accessToken"); + Mockito.verify(gradBusinessService).getSchoolReportPDFByMincode("12312321","GRAD", "accessToken"); } @@ -201,9 +198,9 @@ void testAmalgamatedSchoolReportByMincode() throws Exception { .contentType(MediaType.APPLICATION_XML) .body(greBPack); - Mockito.when(gradBusinessService.getAmalgamatedSchoolReportPDFByMincode("12312321", "TVRNONGRAD","abc")).thenReturn(response); - gradBusinessController.amalgamatedSchoolReportByMincode("12312321","TVRNONGRAD", "abc"); - Mockito.verify(gradBusinessService).getAmalgamatedSchoolReportPDFByMincode("12312321","TVRNONGRAD", "abc"); + Mockito.when(gradBusinessService.getAmalgamatedSchoolReportPDFByMincode("12312321", "TVRNONGRAD","accessToken")).thenReturn(response); + gradBusinessController.amalgamatedSchoolReportByMincode("12312321","TVRNONGRAD", "accessToken"); + Mockito.verify(gradBusinessService).getAmalgamatedSchoolReportPDFByMincode("12312321","TVRNONGRAD", "accessToken"); } @@ -219,9 +216,27 @@ void testStudentCredentialByType() throws Exception { .contentType(MediaType.APPLICATION_XML) .body(greBPack); - Mockito.when(gradBusinessService.getStudentCredentialPDFByType("12312321","TRAN", "abc")).thenReturn(response); - gradBusinessController.studentCredentialByType("12312321", "TRAN","abc"); - Mockito.verify(gradBusinessService).getStudentCredentialPDFByType("12312321","TRAN", "abc"); + Mockito.when(gradBusinessService.getStudentCredentialPDFByType("12312321","TRAN", "accessToken")).thenReturn(response); + gradBusinessController.studentCredentialByType("12312321", "TRAN","accessToken"); + Mockito.verify(gradBusinessService).getStudentCredentialPDFByType("12312321","TRAN", "accessToken"); + + } + + @Test + void testStudentTranscriptPDFByType() throws Exception { + byte[] greBPack = "Any String you want".getBytes(); + + HttpHeaders headers = new HttpHeaders(); + headers.add("Content-Disposition", "inline; filename=StudentTranscript.pdf"); + ResponseEntity response = ResponseEntity + .ok() + .headers(headers) + .contentType(MediaType.APPLICATION_XML) + .body(greBPack); + + Mockito.when(gradBusinessService.getStudentTranscriptPDFByType("12312321","xml", "accessToken")).thenReturn(response); + gradBusinessController.studentTranscriptByType("12312321", "xml","accessToken"); + Mockito.verify(gradBusinessService).getStudentTranscriptPDFByType("12312321","xml", "accessToken"); } diff --git a/api/src/test/resources/application.yaml b/api/src/test/resources/application.yaml index f5bcddd..35f1c1d 100644 --- a/api/src/test/resources/application.yaml +++ b/api/src/test/resources/application.yaml @@ -84,6 +84,8 @@ endpoint: grad-report-api: report-data-by-xml: url: https://educ-grad-report-api-77c02f-dev.apps.silver.devops.gov.bc.ca/api/v1/reports/xmltranscriptreport + transcript-by-request: + url: https://educ-grad-report-api-77c02f-dev.apps.silver.devops.gov.bc.ca/api/v1/reports/transcriptreport grad-student-api: demographic: url: https://educ-grad-student-api-77c02f-dev.apps.silver.devops.gov.bc.ca/api/v1/student/demog/pen/%s diff --git a/api/src/test/resources/data/sample.pdf b/api/src/test/resources/data/sample.pdf new file mode 100644 index 0000000..805a21e Binary files /dev/null and b/api/src/test/resources/data/sample.pdf differ