From f9e8abc2d6c8e2901f027b1ee39d40b85c9f5914 Mon Sep 17 00:00:00 2001 From: arybakov Date: Mon, 27 Nov 2023 16:09:44 -0700 Subject: [PATCH 01/12] GRAD2-2306 HD-21559-GRAD - P3 - GRAD is creating blank transcripts --- .../exception/ServiceException.java | 39 +++++++++++++++++++ .../service/GradBusinessService.java | 11 +++++- 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 api/src/main/java/ca/bc/gov/educ/api/gradbusiness/exception/ServiceException.java diff --git a/api/src/main/java/ca/bc/gov/educ/api/gradbusiness/exception/ServiceException.java b/api/src/main/java/ca/bc/gov/educ/api/gradbusiness/exception/ServiceException.java new file mode 100644 index 0000000..dc86376 --- /dev/null +++ b/api/src/main/java/ca/bc/gov/educ/api/gradbusiness/exception/ServiceException.java @@ -0,0 +1,39 @@ +package ca.bc.gov.educ.api.gradbusiness.exception; + +import lombok.Data; + +@Data +public class ServiceException extends RuntimeException { + + private int statusCode; + + public ServiceException() { + super(); + } + + public ServiceException(String message) { + super(message); + } + + public ServiceException(String message, Throwable cause) { + super(message, cause); + } + + public ServiceException(Throwable cause) { + super(cause); + } + + protected ServiceException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } + + public ServiceException(String message, int value) { + super(message); + this.statusCode = value; + } + + public ServiceException(String s, int value, Exception e) { + super(s, e); + this.statusCode = value; + } +} 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 d208502..3c8690c 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,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; @@ -272,9 +273,17 @@ public ResponseEntity 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); } From 3428a9745205ed91a504dc00dfa1bf1642bff551 Mon Sep 17 00:00:00 2001 From: arybakov Date: Mon, 27 Nov 2023 16:19:31 -0700 Subject: [PATCH 02/12] GRAD2-2306 HD-21559-GRAD - P3 - GRAD is creating blank transcripts --- .../api/gradbusiness/EducGradBusinessApiApplicationTests.java | 1 + 1 file changed, 1 insertion(+) 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 993d1f8..6e7b616 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 @@ -478,6 +478,7 @@ 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"); From f9c536265cf721fb7b124890d660e4356a809cdd Mon Sep 17 00:00:00 2001 From: arybakov Date: Mon, 27 Nov 2023 16:23:58 -0700 Subject: [PATCH 03/12] Fix code smell --- .../exception/ServiceException.java | 26 +------------------ 1 file changed, 1 insertion(+), 25 deletions(-) diff --git a/api/src/main/java/ca/bc/gov/educ/api/gradbusiness/exception/ServiceException.java b/api/src/main/java/ca/bc/gov/educ/api/gradbusiness/exception/ServiceException.java index dc86376..29f3f31 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/gradbusiness/exception/ServiceException.java +++ b/api/src/main/java/ca/bc/gov/educ/api/gradbusiness/exception/ServiceException.java @@ -5,35 +5,11 @@ @Data public class ServiceException extends RuntimeException { - private int statusCode; - - public ServiceException() { - super(); - } - - public ServiceException(String message) { - super(message); - } - - public ServiceException(String message, Throwable cause) { - super(message, cause); - } - - public ServiceException(Throwable cause) { - super(cause); - } - - protected ServiceException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { - super(message, cause, enableSuppression, writableStackTrace); - } + private final int statusCode; public ServiceException(String message, int value) { super(message); this.statusCode = value; } - public ServiceException(String s, int value, Exception e) { - super(s, e); - this.statusCode = value; - } } From 4fef7a7e1684aac706eae35b61593dbec4775dad Mon Sep 17 00:00:00 2001 From: arybakov Date: Mon, 27 Nov 2023 16:34:41 -0700 Subject: [PATCH 04/12] Fix code smell --- .../EducGradBusinessApiApplicationTests.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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 6e7b616..1a0f2ec 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 @@ -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; @@ -486,6 +487,21 @@ void testStudentTranscriptPDFByTypeByPen() throws Exception { ResponseEntity 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 From ad4d0bf72db63ca8310dd2db67c63abebb71ac3d Mon Sep 17 00:00:00 2001 From: arybakov Date: Tue, 28 Nov 2023 11:47:31 -0700 Subject: [PATCH 05/12] More debugging added --- .../educ/api/gradbusiness/service/GradBusinessService.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) 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 3c8690c..8c6443c 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 @@ -346,8 +346,8 @@ protected ResponseEntity getInternalServerErrorResponse(Throwable t) { private ResponseEntity handleBinaryResponse(byte[] resultBinary, String reportFile, MediaType contentType) { ResponseEntity response; - if(resultBinary.length > 0) { + logger.debug("Sending {} response {}M", contentType.getSubtype().toUpperCase(), resultBinary.length/(1024 * 1024)); HttpHeaders headers = new HttpHeaders(); headers.add("Content-Disposition", "inline; filename=" + reportFile); response = ResponseEntity @@ -363,9 +363,12 @@ private ResponseEntity handleBinaryResponse(byte[] resultBinary, String private void saveBinaryResponseToFile(byte[] resultBinary, String reportFile) throws IOException { if(resultBinary.length > 0) { - try (OutputStream out = new FileOutputStream(TMP + "/" + reportFile)) { + String pathToFile = TMP + "/" + reportFile; + logger.debug("Save generated PDF {} on the file system", reportFile); + try (OutputStream out = new FileOutputStream(pathToFile)) { out.write(resultBinary); } + logger.debug("PDF {} saved successfully", pathToFile); } } } From dac9c89ef6ed9ae7c0e6572d76cc0a8222d9d29a Mon Sep 17 00:00:00 2001 From: arybakov Date: Tue, 28 Nov 2023 12:31:59 -0700 Subject: [PATCH 06/12] More debugging added --- .../gov/educ/api/gradbusiness/service/GradBusinessService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 8c6443c..9600969 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 @@ -347,7 +347,7 @@ protected ResponseEntity getInternalServerErrorResponse(Throwable t) { private ResponseEntity handleBinaryResponse(byte[] resultBinary, String reportFile, MediaType contentType) { ResponseEntity response; if(resultBinary.length > 0) { - logger.debug("Sending {} response {}M", contentType.getSubtype().toUpperCase(), resultBinary.length/(1024 * 1024)); + logger.debug("Sending {} response {} KB", contentType.getSubtype().toUpperCase(), resultBinary.length/(1024)); HttpHeaders headers = new HttpHeaders(); headers.add("Content-Disposition", "inline; filename=" + reportFile); response = ResponseEntity From e61e9f996e78d1eecda017641e4f7a023b8ea30d Mon Sep 17 00:00:00 2001 From: arybakov Date: Tue, 28 Nov 2023 12:38:37 -0700 Subject: [PATCH 07/12] More debugging added --- .../api/gradbusiness/service/GradBusinessService.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) 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 9600969..e41f433 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 @@ -23,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.IOException; import java.io.InputStream; -import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.*; import java.util.concurrent.CompletableFuture; @@ -365,9 +365,10 @@ private void saveBinaryResponseToFile(byte[] resultBinary, String reportFile) th if(resultBinary.length > 0) { String pathToFile = TMP + "/" + reportFile; logger.debug("Save generated PDF {} on the file system", reportFile); - try (OutputStream out = new FileOutputStream(pathToFile)) { - out.write(resultBinary); - } +// try (OutputStream out = new FileOutputStream(pathToFile)) { +// out.write(resultBinary); +// } + Files.write(Path.of(pathToFile), resultBinary); logger.debug("PDF {} saved successfully", pathToFile); } } From 0c46ef6b3c97efa9ba2815bbf5f3ccb38f074616 Mon Sep 17 00:00:00 2001 From: arybakov Date: Tue, 28 Nov 2023 12:51:57 -0700 Subject: [PATCH 08/12] More debugging added --- .../gradbusiness/service/GradBusinessService.java | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) 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 e41f433..d6cc3f0 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 @@ -9,6 +9,7 @@ import io.github.resilience4j.retry.annotation.Retry; import jakarta.transaction.Transactional; import org.apache.commons.collections4.ListUtils; +import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -23,10 +24,9 @@ import org.springframework.web.reactive.function.BodyInserters; import org.springframework.web.reactive.function.client.WebClient; +import java.io.File; import java.io.IOException; import java.io.InputStream; -import java.nio.file.Files; -import java.nio.file.Path; import java.util.*; import java.util.concurrent.CompletableFuture; @@ -347,7 +347,8 @@ protected ResponseEntity getInternalServerErrorResponse(Throwable t) { private ResponseEntity handleBinaryResponse(byte[] resultBinary, String reportFile, MediaType contentType) { ResponseEntity response; if(resultBinary.length > 0) { - logger.debug("Sending {} response {} KB", contentType.getSubtype().toUpperCase(), resultBinary.length/(1024)); + 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 @@ -365,10 +366,7 @@ private void saveBinaryResponseToFile(byte[] resultBinary, String reportFile) th if(resultBinary.length > 0) { String pathToFile = TMP + "/" + reportFile; logger.debug("Save generated PDF {} on the file system", reportFile); -// try (OutputStream out = new FileOutputStream(pathToFile)) { -// out.write(resultBinary); -// } - Files.write(Path.of(pathToFile), resultBinary); + FileUtils.writeByteArrayToFile(new File(pathToFile), resultBinary); logger.debug("PDF {} saved successfully", pathToFile); } } From 7cb23918873c3ec964cb699826b741d5527e0956 Mon Sep 17 00:00:00 2001 From: arybakov Date: Tue, 28 Nov 2023 12:57:10 -0700 Subject: [PATCH 09/12] More debugging added --- .../educ/api/gradbusiness/service/GradBusinessService.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 d6cc3f0..066c2db 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 @@ -42,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.pathSeparator + "tmp"; /** * The Web client. */ @@ -364,7 +364,7 @@ private ResponseEntity handleBinaryResponse(byte[] resultBinary, String private void saveBinaryResponseToFile(byte[] resultBinary, String reportFile) throws IOException { if(resultBinary.length > 0) { - String pathToFile = TMP + "/" + reportFile; + String pathToFile = TMP + File.pathSeparator + reportFile; logger.debug("Save generated PDF {} on the file system", reportFile); FileUtils.writeByteArrayToFile(new File(pathToFile), resultBinary); logger.debug("PDF {} saved successfully", pathToFile); From 742e5bf4d6aded62c391688075dee2052c2bce53 Mon Sep 17 00:00:00 2001 From: arybakov Date: Tue, 28 Nov 2023 13:12:07 -0700 Subject: [PATCH 10/12] More debugging added --- .../educ/api/gradbusiness/service/GradBusinessService.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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 066c2db..ec120c5 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 @@ -366,7 +366,12 @@ private void saveBinaryResponseToFile(byte[] resultBinary, String reportFile) th if(resultBinary.length > 0) { String pathToFile = TMP + File.pathSeparator + reportFile; logger.debug("Save generated PDF {} on the file system", reportFile); - FileUtils.writeByteArrayToFile(new File(pathToFile), resultBinary); + File fileToSave = new File(pathToFile); + if(fileToSave.exists()) { + boolean isDeleted = fileToSave.delete(); + logger.debug("{} to delete existing PDF {}", isDeleted, reportFile); + } + FileUtils.writeByteArrayToFile(fileToSave, resultBinary); logger.debug("PDF {} saved successfully", pathToFile); } } From 6b169d060d074543dd44b6cedde4d7ae708a7bb5 Mon Sep 17 00:00:00 2001 From: arybakov Date: Tue, 28 Nov 2023 13:42:29 -0700 Subject: [PATCH 11/12] More debugging added --- .../educ/api/gradbusiness/service/GradBusinessService.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 ec120c5..f985fba 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 @@ -9,7 +9,6 @@ import io.github.resilience4j.retry.annotation.Retry; import jakarta.transaction.Transactional; import org.apache.commons.collections4.ListUtils; -import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -27,6 +26,7 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.nio.file.Files; import java.util.*; import java.util.concurrent.CompletableFuture; @@ -371,7 +371,7 @@ private void saveBinaryResponseToFile(byte[] resultBinary, String reportFile) th boolean isDeleted = fileToSave.delete(); logger.debug("{} to delete existing PDF {}", isDeleted, reportFile); } - FileUtils.writeByteArrayToFile(fileToSave, resultBinary); + Files.write(fileToSave.toPath(), resultBinary); logger.debug("PDF {} saved successfully", pathToFile); } } From a5973784b7caaadcb54cfb625a41c4b236a8d67b Mon Sep 17 00:00:00 2001 From: arybakov Date: Tue, 28 Nov 2023 13:46:31 -0700 Subject: [PATCH 12/12] More debugging added --- .../educ/api/gradbusiness/service/GradBusinessService.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 f985fba..3e71b16 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 @@ -42,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 = File.pathSeparator + "tmp"; + private static final String TMP = File.separator + "tmp"; /** * The Web client. */ @@ -364,7 +364,7 @@ private ResponseEntity handleBinaryResponse(byte[] resultBinary, String private void saveBinaryResponseToFile(byte[] resultBinary, String reportFile) throws IOException { if(resultBinary.length > 0) { - String pathToFile = TMP + File.pathSeparator + reportFile; + String pathToFile = TMP + File.separator + reportFile; logger.debug("Save generated PDF {} on the file system", reportFile); File fileToSave = new File(pathToFile); if(fileToSave.exists()) {