Skip to content

Commit

Permalink
Merge pull request #129 from bcgov/develop/alex-GRAD2-2410-2
Browse files Browse the repository at this point in the history
Develop/alex grad2 2410 2
  • Loading branch information
kamal-mohammed authored Nov 28, 2023
2 parents 0bdd273 + a597378 commit b023f52
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ca.bc.gov.educ.api.gradbusiness.exception;

import lombok.Data;

@Data
public class ServiceException extends RuntimeException {

private final int statusCode;

public ServiceException(String message, int value) {
super(message);
this.statusCode = value;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ca.bc.gov.educ.api.gradbusiness.service;

import ca.bc.gov.educ.api.gradbusiness.exception.ServiceException;
import ca.bc.gov.educ.api.gradbusiness.model.dto.Student;
import ca.bc.gov.educ.api.gradbusiness.util.EducGradBusinessApiConstants;
import ca.bc.gov.educ.api.gradbusiness.util.EducGradBusinessUtil;
Expand All @@ -22,10 +23,10 @@
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;

import java.io.FileOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.util.*;
import java.util.concurrent.CompletableFuture;

Expand All @@ -41,7 +42,7 @@ public class GradBusinessService {
private static final String APPLICATION_JSON = "application/json";
private static final String APPLICATION_PDF = "application/pdf";
private static final String ACCEPT = "*/*";
private static final String TMP = "/tmp";
private static final String TMP = File.separator + "tmp";
/**
* The Web client.
*/
Expand Down Expand Up @@ -272,9 +273,17 @@ public ResponseEntity<byte[]> getStudentTranscriptPDFByType(String pen, String t
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();
byte[] result = webClient.post().uri(educGraduationApiConstants.getStudentTranscriptReportByRequest())
.headers(h -> h.addAll(headers)).body(BodyInserters.fromValue(reportRequest.toString())).retrieve()
.onStatus(
HttpStatus.NO_CONTENT::equals,
response -> response.bodyToMono(String.class).thenReturn(new ServiceException("NO_CONTENT", response.statusCode().value()))
)
.bodyToMono(byte[].class).block();
assert result != null;
return handleBinaryResponse(result, pen + " Transcript Report.pdf", MediaType.APPLICATION_PDF);
} catch (ServiceException e) {
return handleBinaryResponse(new byte[0], pen + " Transcript Report.pdf", MediaType.APPLICATION_PDF);
} catch (Exception e) {
return getInternalServerErrorResponse(e);
}
Expand Down Expand Up @@ -337,8 +346,9 @@ protected ResponseEntity<byte[]> getInternalServerErrorResponse(Throwable t) {

private ResponseEntity<byte[]> handleBinaryResponse(byte[] resultBinary, String reportFile, MediaType contentType) {
ResponseEntity<byte[]> response;

if(resultBinary.length > 0) {
String fileType = contentType.getSubtype().toUpperCase();
logger.debug("Sending {} response {} KB", fileType, resultBinary.length/(1024));
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", "inline; filename=" + reportFile);
response = ResponseEntity
Expand All @@ -354,9 +364,15 @@ private ResponseEntity<byte[]> handleBinaryResponse(byte[] resultBinary, String

private void saveBinaryResponseToFile(byte[] resultBinary, String reportFile) throws IOException {
if(resultBinary.length > 0) {
try (OutputStream out = new FileOutputStream(TMP + "/" + reportFile)) {
out.write(resultBinary);
String pathToFile = TMP + File.separator + reportFile;
logger.debug("Save generated PDF {} on the file system", reportFile);
File fileToSave = new File(pathToFile);
if(fileToSave.exists()) {
boolean isDeleted = fileToSave.delete();
logger.debug("{} to delete existing PDF {}", isDeleted, reportFile);
}
Files.write(fileToSave.toPath(), resultBinary);
logger.debug("PDF {} saved successfully", pathToFile);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ca.bc.gov.educ.api.gradbusiness;

import ca.bc.gov.educ.api.gradbusiness.exception.ServiceException;
import ca.bc.gov.educ.api.gradbusiness.model.dto.Student;
import ca.bc.gov.educ.api.gradbusiness.service.GradBusinessService;
import ca.bc.gov.educ.api.gradbusiness.util.EducGradBusinessApiConstants;
Expand Down Expand Up @@ -478,13 +479,29 @@ void testStudentTranscriptPDFByTypeByPen() throws Exception {
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.onStatus(any(), any())).thenReturn(this.responseMock);
when(this.responseMock.bodyToMono(byte[].class)).thenReturn(Mono.just(transcriptPdfSample));

when(this.tokenUtils.getAccessToken()).thenReturn("accessToken");

ResponseEntity<byte[]> transcriptPdf = gradBusinessService.getStudentTranscriptPDFByType(pen, "xml", null,"accessToken");
assertNotNull(transcriptPdf.getBody());
assertEquals(transcriptPdfSample,transcriptPdf.getBody());

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.onStatus(any(), any())).thenReturn(this.responseMock);
when(this.responseMock.bodyToMono(byte[].class)).thenThrow(new ServiceException("NO_CONTENT", 204));

when(this.tokenUtils.getAccessToken()).thenReturn("accessToken");

transcriptPdf = gradBusinessService.getStudentTranscriptPDFByType(pen, "xml", null,"accessToken");
assertNotNull(transcriptPdf);
assertNull(transcriptPdf.getBody());
}

@Test
Expand Down

0 comments on commit b023f52

Please sign in to comment.