Skip to content

Commit

Permalink
Merge branch 'grad-release' of https://github.com/bcgov/EDUC-GRAD-BUS…
Browse files Browse the repository at this point in the history
…INESS-API into develop/alex-GRAD2-2249-2
  • Loading branch information
arybakov-cgi committed Nov 18, 2023
2 parents 72a6c12 + 8f2e052 commit 00494c1
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 35 deletions.
2 changes: 1 addition & 1 deletion api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</parent>
<groupId>ca.bc.gov</groupId>
<artifactId>educ-grad-business-api</artifactId>
<version>1.8.23</version>
<version>1.8.24</version>
<name>educ-grad-business-api</name>
<description>GRAD Business API for external clients</description>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ public ResponseEntity<byte[]> 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);
Expand All @@ -110,7 +112,9 @@ public ResponseEntity<byte[]> 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);
Expand All @@ -131,7 +135,9 @@ public ResponseEntity<byte[]> 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);
Expand All @@ -152,7 +158,9 @@ public ResponseEntity<byte[]> 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);
Expand Down Expand Up @@ -211,8 +219,10 @@ public ResponseEntity<byte[]> 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);
Expand Down Expand Up @@ -265,14 +275,17 @@ public ResponseEntity<byte[]> 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);
}
}


@Transactional
public ResponseEntity<byte[]> getStudentTranscriptPDFByType(String pen, String type, String accessToken) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());

}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -211,19 +255,14 @@ private String readFile(String jsonPath) throws Exception {
return sb.toString();
}

private byte[] readBinaryFile(String path) throws Exception {
ClassLoader classLoader = getClass().getClassLoader();
InputStream inputStream = classLoader.getResourceAsStream(path);
return inputStream.readAllBytes();
}

@Test
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);
Expand All @@ -241,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);
Expand All @@ -260,29 +300,45 @@ void testgetAmalgamatedSchoolReportPDFByMincode() throws Exception {
ResponseEntity<byte[]> 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<byte[]> 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";
Expand All @@ -298,6 +354,7 @@ void testSchoolReportPDFByMincode_witherror2() throws Exception {
ResponseEntity<byte[]> byteData = gradBusinessService.getSchoolReportPDFByMincode(mincode, type, "accessToken");
assertNotNull(byteData);
assertTrue(byteData.getBody().length > 0);
assertTrue(byteData.getStatusCode().is5xxServerError());
}

@Test
Expand All @@ -316,18 +373,41 @@ void testGetStudentDemographicsByPen() throws Exception {
ResponseEntity<byte[]> 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);
Expand Down Expand Up @@ -386,22 +466,24 @@ void testStudentTranscriptPDFByTypeByPen() throws Exception {
}

@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<List<Student>> responseType = new ParameterizedTypeReference<>() {
};
Expand All @@ -414,19 +496,23 @@ void testStudentCredentialPDFByType_witherror() throws Exception {

ResponseEntity<byte[]> 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<List<Student>> responseType = new ParameterizedTypeReference<>() {
Expand All @@ -441,6 +527,13 @@ void testStudentCredentialPDFByType_witherror2() {
ResponseEntity<byte[]> 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();
}

}

0 comments on commit 00494c1

Please sign in to comment.