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 1839b94..bf6afab 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 @@ -148,8 +148,8 @@ public ResponseEntity studentTranscriptByType(@PathVariable String pen, @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" }) @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK")}) - public ResponseEntity amalgamatedSchoolReportByMincode(@PathVariable String mincode, @RequestParam(name = "type") String type, @RequestHeader(name="Authorization") String accessToken) { - return gradBusinessService.getAmalgamatedSchoolReportPDFByMincode(mincode, type, accessToken.replace(BEARER, "")); + public ResponseEntity amalgamatedSchoolReportByMincode(@PathVariable String mincode, @RequestParam(name = "type") String type) { + return gradBusinessService.getAmalgamatedSchoolReportPDFByMincode(mincode, type); } 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 dafa620..4c35b54 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 @@ -5,6 +5,7 @@ import ca.bc.gov.educ.api.gradbusiness.model.dto.Student; import ca.bc.gov.educ.api.gradbusiness.model.dto.District; import ca.bc.gov.educ.api.gradbusiness.util.*; +import com.fasterxml.jackson.core.type.TypeReference; import io.github.resilience4j.retry.annotation.Retry; import jakarta.transaction.Transactional; import org.apache.commons.collections4.ListUtils; @@ -234,34 +235,36 @@ public ResponseEntity getDistrictReportPDFByDistcode(String distCode, St } } - public ResponseEntity getAmalgamatedSchoolReportPDFByMincode(String mincode, String type, String accessToken) { + public ResponseEntity getAmalgamatedSchoolReportPDFByMincode(String mincode, String type) { logger.debug("******** Retrieve List of Students for Amalgamated School Report ******"); - List studentList = webClient.get().uri(String.format(educGradStudentApiConstants.getStudentsForAmalgamatedReport(), mincode, type)).headers(h -> h.setBearerAuth(accessToken)).retrieve().bodyToMono(new ParameterizedTypeReference>() { - }).block(); List locations = new ArrayList<>(); - if (studentList != null && !studentList.isEmpty()) { - logger.debug("******** Fetched {} students ******", studentList.size()); - List> partitions = ListUtils.partition(studentList, 200); - getStudentAchievementReports(partitions, locations); - Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("PST"), Locale.CANADA); - int year = cal.get(Calendar.YEAR); - String month = "00"; - String fileName = EducGradBusinessUtil.getReportsFileNameForSchoolAndDistrict(mincode, year, month, type, MediaType.APPLICATION_PDF); - try { - logger.debug("******** Merging Documents Started ******"); - byte[] res = EducGradBusinessUtil.mergeDocumentsPDFs(locations); - logger.debug("******** Merged {} Documents ******", locations.size()); - HttpHeaders headers = new HttpHeaders(); - headers.put(HttpHeaders.AUTHORIZATION, Collections.singletonList(BEARER + accessToken)); - headers.put(HttpHeaders.ACCEPT, Collections.singletonList(APPLICATION_PDF)); - headers.put(HttpHeaders.CONTENT_TYPE, Collections.singletonList(APPLICATION_PDF)); - saveBinaryResponseToFile(res, fileName); - return handleBinaryResponse(res, fileName, MediaType.APPLICATION_PDF); - } catch (Exception e) { - return getInternalServerErrorResponse(e); + Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("PST"), Locale.CANADA); + int year = cal.get(Calendar.YEAR); + String month = "00"; + + try { + List schoolDetails = schoolService.getSchoolDetails(mincode); + if (schoolDetails.isEmpty()) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).build(); + } + String schoolOfRecordId = schoolDetails.get(0).getSchoolId(); + var response = restService.get(String.format(educGradStudentApiConstants.getStudentsForAmalgamatedReport(), schoolOfRecordId, type), List.class); + List studentList = jsonTransformer.convertValue(response, new TypeReference<>() {}); + if (studentList != null && !studentList.isEmpty()) { + logger.debug("******** Fetched {} students ******", studentList.size()); + List> partitions = ListUtils.partition(studentList, 200); + getStudentAchievementReports(partitions, locations); } + String fileName = EducGradBusinessUtil.getReportsFileNameForSchoolAndDistrict(mincode, year, month, type, MediaType.APPLICATION_PDF); + logger.debug("******** Merging Documents Started ******"); + byte[] res = EducGradBusinessUtil.mergeDocumentsPDFs(locations); + logger.debug("******** Merged {} Documents ******", locations.size()); + saveBinaryResponseToFile(res, fileName); + return handleBinaryResponse(res, fileName, MediaType.APPLICATION_PDF); + } catch (Exception e) { + logger.error("Error getting amalgamated report PDF by mincode: {}", e.getMessage()); + return getInternalServerErrorResponse(e); } - return null; } @Transactional 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 2bb9cc8..d8c0c84 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 @@ -7,6 +7,7 @@ import ca.bc.gov.educ.api.gradbusiness.service.RESTService; import ca.bc.gov.educ.api.gradbusiness.service.SchoolService; 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 ca.bc.gov.educ.api.gradbusiness.util.TokenUtils; import org.junit.FixMethodOrder; @@ -31,9 +32,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; -import java.util.Collections; -import java.util.List; -import java.util.UUID; +import java.util.*; import java.util.function.Consumer; import static org.junit.jupiter.api.Assertions.*; @@ -287,43 +286,60 @@ void testSchoolReportPDFByMincode() throws Exception { } @Test - void testgetAmalgamatedSchoolReportPDFByMincode() throws Exception { + void testAmalgamatedSchoolReportPDFByMincode() throws Exception { String mincode = "128385861"; String type = "TVRNONGRAD"; + String schoolOfRecordId = "14453395-ecf0-7f81-998f-506940d94c2d"; + String uuId = String.valueOf(UUID.randomUUID()); + List studentList = new ArrayList<>(); + studentList.add(UUID.randomUUID()); + studentList.add(UUID.randomUUID()); byte[] samplePdf = readBinaryFile("data/sample.pdf"); InputStreamResource pdf = new InputStreamResource(new ByteArrayInputStream(samplePdf)); - when(this.tokenUtils.getAccessToken()).thenReturn("accessToken"); - - UUID studentID = UUID.randomUUID(); - when(this.webClient.get()).thenReturn(this.requestHeadersUriMock); - when(this.requestHeadersUriMock.uri(String.format(educGradStudentApiConstants.getStudentsForAmalgamatedReport(),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(new ParameterizedTypeReference>() {})).thenReturn(Mono.just(List.of(studentID))); - 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)); + var schoolList = List.of(School.builder().mincode(mincode).schoolId(String.valueOf(UUID.randomUUID())).build()); + when(schoolService.getSchoolDetails(any(String.class))).thenReturn(schoolList); + when(this.restService.get(String.format(educGradStudentApiConstants.getStudentsForAmalgamatedReport(), schoolOfRecordId, type), List.class)).thenReturn(studentList); + when(this.restService.get(anyString(), eq(InputStreamResource.class))).thenReturn(pdf); + when(this.restService.get(String.format(educGraduationApiConstants.getStudentCredentialByType(), uuId, "ACHV"), InputStreamResource.class)).thenReturn(pdf); - ResponseEntity byteData = gradBusinessService.getAmalgamatedSchoolReportPDFByMincode(mincode, type, "accessToken"); + ResponseEntity byteData = gradBusinessService.getAmalgamatedSchoolReportPDFByMincode(mincode, type); assertNotNull(byteData); + assertEquals(HttpStatus.NO_CONTENT, byteData.getStatusCode()); + } - pdf = new InputStreamResource(new ByteArrayInputStream(new byte[0])); + @Test + void testAmalgamatedSchoolReportPDFByMincode_NotFound() { - 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)); + String mincode = "128385861"; + String type = "TVRNONGRAD"; + String uuId = String.valueOf(UUID.randomUUID()); - byteData = gradBusinessService.getAmalgamatedSchoolReportPDFByMincode(mincode, type, "accessToken"); + byte[] samplePdf = new byte[0]; + InputStreamResource pdf = new InputStreamResource(new ByteArrayInputStream(samplePdf)); + + when(this.restService.get(String.format(educGraduationApiConstants.getStudentCredentialByType(), uuId, "ACHV"), InputStreamResource.class)).thenReturn(pdf); + when(this.restService.get(anyString(), eq(InputStreamResource.class))).thenReturn(pdf); + + ResponseEntity byteData = gradBusinessService.getAmalgamatedSchoolReportPDFByMincode(mincode, type); assertNotNull(byteData); + assertEquals(HttpStatus.NOT_FOUND, byteData.getStatusCode()); + } + + @Test + void testAmalgamatedSchoolReportPDFByMincode_Error500() { + + String mincode = "128385861"; + String type = "TVRNONGRAD"; + when(schoolService.getSchoolDetails(mincode)).thenThrow(new RuntimeException()); + + ResponseEntity byteData = gradBusinessService.getAmalgamatedSchoolReportPDFByMincode(mincode, type); + assertNotNull(byteData); + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, byteData.getStatusCode()); } @Test 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 b5990c5..8094559 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 @@ -203,9 +203,9 @@ void testAmalgamatedSchoolReportByMincode() { .contentType(MediaType.APPLICATION_XML) .body(greBPack); - Mockito.when(gradBusinessService.getAmalgamatedSchoolReportPDFByMincode("12312321", "TVRNONGRAD","accessToken")).thenReturn(response); - gradBusinessController.amalgamatedSchoolReportByMincode("12312321","TVRNONGRAD", "accessToken"); - Mockito.verify(gradBusinessService).getAmalgamatedSchoolReportPDFByMincode("12312321","TVRNONGRAD", "accessToken"); + Mockito.when(gradBusinessService.getAmalgamatedSchoolReportPDFByMincode("12312321", "TVRNONGRAD")).thenReturn(response); + gradBusinessController.amalgamatedSchoolReportByMincode("12312321","TVRNONGRAD"); + Mockito.verify(gradBusinessService).getAmalgamatedSchoolReportPDFByMincode("12312321","TVRNONGRAD"); }