Skip to content

Commit

Permalink
Grad2-3209 Amalgamated endpoint finds reports using schoolId (#163)
Browse files Browse the repository at this point in the history
* Reading school details from Trax api by mincode and send schoolOfRecordId to retrieve amalgamated school reports.

* Replaced student api endpoint webclient call with restservice call

* clean up

* test case coverage

* test case coverage 2

* test case coverage 2 correction

* test case coverage 3
  • Loading branch information
githubmamatha authored Jan 13, 2025
1 parent 4944430 commit 8b811fa
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ public ResponseEntity<byte[]> 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<byte[]> amalgamatedSchoolReportByMincode(@PathVariable String mincode, @RequestParam(name = "type") String type, @RequestHeader(name="Authorization") String accessToken) {
return gradBusinessService.getAmalgamatedSchoolReportPDFByMincode(mincode, type, accessToken.replace(BEARER, ""));
public ResponseEntity<byte[]> amalgamatedSchoolReportByMincode(@PathVariable String mincode, @RequestParam(name = "type") String type) {
return gradBusinessService.getAmalgamatedSchoolReportPDFByMincode(mincode, type);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -234,34 +235,36 @@ public ResponseEntity<byte[]> getDistrictReportPDFByDistcode(String distCode, St
}
}

public ResponseEntity<byte[]> getAmalgamatedSchoolReportPDFByMincode(String mincode, String type, String accessToken) {
public ResponseEntity<byte[]> getAmalgamatedSchoolReportPDFByMincode(String mincode, String type) {
logger.debug("******** Retrieve List of Students for Amalgamated School Report ******");
List<UUID> studentList = webClient.get().uri(String.format(educGradStudentApiConstants.getStudentsForAmalgamatedReport(), mincode, type)).headers(h -> h.setBearerAuth(accessToken)).retrieve().bodyToMono(new ParameterizedTypeReference<List<UUID>>() {
}).block();
List<InputStream> locations = new ArrayList<>();
if (studentList != null && !studentList.isEmpty()) {
logger.debug("******** Fetched {} students ******", studentList.size());
List<List<UUID>> 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<School> 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<UUID> studentList = jsonTransformer.convertValue(response, new TypeReference<>() {});
if (studentList != null && !studentList.isEmpty()) {
logger.debug("******** Fetched {} students ******", studentList.size());
List<List<UUID>> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.*;
Expand Down Expand Up @@ -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<UUID> 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<List<UUID>>() {})).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<byte[]> byteData = gradBusinessService.getAmalgamatedSchoolReportPDFByMincode(mincode, type, "accessToken");
ResponseEntity<byte[]> 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<byte[]> 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<byte[]> byteData = gradBusinessService.getAmalgamatedSchoolReportPDFByMincode(mincode, type);
assertNotNull(byteData);
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, byteData.getStatusCode());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");

}

Expand Down

0 comments on commit 8b811fa

Please sign in to comment.