From 9b4ef447ffff094ab90d88dc4ad5db9f35e3bee4 Mon Sep 17 00:00:00 2001 From: acialini Date: Fri, 1 Dec 2023 10:48:34 +0100 Subject: [PATCH 1/4] [PRDP-255] feat: Refactored RegenerateReceiptPdf class to meet now endpoint definition --- .../pdf/helpdesk/RegenerateReceiptPdf.java | 61 ++++++++++++++----- .../pdf/helpdesk/ReceiptToReviewedTest.java | 2 +- .../helpdesk/RegenerateReceiptPdfTest.java | 30 +++------ 3 files changed, 57 insertions(+), 36 deletions(-) diff --git a/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/RegenerateReceiptPdf.java b/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/RegenerateReceiptPdf.java index c8c2f35..0c000a6 100644 --- a/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/RegenerateReceiptPdf.java +++ b/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/RegenerateReceiptPdf.java @@ -2,6 +2,7 @@ import com.microsoft.azure.functions.*; import com.microsoft.azure.functions.annotation.AuthorizationLevel; +import com.microsoft.azure.functions.annotation.BindingName; import com.microsoft.azure.functions.annotation.FunctionName; import com.microsoft.azure.functions.annotation.HttpTrigger; import it.gov.pagopa.receipt.pdf.helpdesk.client.BizEventCosmosClient; @@ -13,7 +14,7 @@ import it.gov.pagopa.receipt.pdf.helpdesk.exception.BizEventNotFoundException; import it.gov.pagopa.receipt.pdf.helpdesk.exception.ReceiptNotFoundException; import it.gov.pagopa.receipt.pdf.helpdesk.model.PdfGeneration; -import it.gov.pagopa.receipt.pdf.helpdesk.model.request.RegenerateReceiptRequest; +import it.gov.pagopa.receipt.pdf.helpdesk.model.ProblemJson; import it.gov.pagopa.receipt.pdf.helpdesk.service.GenerateReceiptPdfService; import it.gov.pagopa.receipt.pdf.helpdesk.service.impl.GenerateReceiptPdfServiceImpl; import org.slf4j.Logger; @@ -21,7 +22,6 @@ import java.io.IOException; import java.nio.file.Path; -import java.util.NoSuchElementException; import java.util.Optional; import static it.gov.pagopa.receipt.pdf.helpdesk.utils.GenerateReceiptUtils.*; @@ -63,20 +63,18 @@ public RegenerateReceiptPdf(){ @FunctionName("RegenerateReceiptPdf") public HttpResponseMessage run ( @HttpTrigger(name = "RegenerateReceiptPdfTrigger", - methods = {HttpMethod.PUT}, - route = "regenerateReceiptPdf", + methods = {HttpMethod.POST}, + route = "/receipts//regenerate-receipt-pdf", authLevel = AuthorizationLevel.ANONYMOUS) - HttpRequestMessage> request, + HttpRequestMessage> request, + @BindingName("bizevent-id") String eventId, final ExecutionContext context) { try { - RegenerateReceiptRequest regenerateReceiptRequest = request.getBody().get(); + if (eventId != null) { - if (regenerateReceiptRequest.getEventId() != null) { - - BizEvent bizEvent = bizEventCosmosClient.getBizEventDocument( - regenerateReceiptRequest.getEventId()); + BizEvent bizEvent = bizEventCosmosClient.getBizEventDocument(eventId); //Retrieve receipt's data from CosmosDB Receipt receipt = getReceipt(context, bizEvent, receiptCosmosClient, logger); @@ -102,11 +100,22 @@ public HttpResponseMessage run ( request.createResponseBuilder(HttpStatus.OK) .body("OK") .build() : - request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR).build(); + request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR) + .body(ProblemJson.builder() + .title(HttpStatus.INTERNAL_SERVER_ERROR.name()) + .detail("Receipt could not be updated with the new attachments") + .status(HttpStatus.INTERNAL_SERVER_ERROR.value()) + .build()) + .build(); } catch (Exception e) { logger.error(e.getMessage(), e); - return request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR) + request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR) + .body(ProblemJson.builder() + .title(HttpStatus.INTERNAL_SERVER_ERROR.name()) + .detail("Error during receipt generation: " + e.getMessage()) + .status(HttpStatus.INTERNAL_SERVER_ERROR.value()) + .build()) .build(); } finally { deleteTempFolder(workingDirPath, logger); @@ -116,16 +125,38 @@ public HttpResponseMessage run ( } - return request.createResponseBuilder(HttpStatus.BAD_REQUEST) + return request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR) + .body(ProblemJson.builder() + .title(HttpStatus.BAD_REQUEST.name()) + .detail("Missing valid eventId paramater") + .status(HttpStatus.BAD_REQUEST.value()) + .build()) .build(); - } catch (NoSuchElementException | ReceiptNotFoundException | BizEventNotFoundException exception) { + } catch (ReceiptNotFoundException | BizEventNotFoundException exception) { logger.error(exception.getMessage(), exception); - return request.createResponseBuilder(HttpStatus.BAD_REQUEST) + String message = "Missing required informations"; + if (exception.getClass().equals(ReceiptNotFoundException.class)) { + message = "Receipt not found with event-id " + eventId; + } else if (exception.getClass().equals(BizEventNotFoundException.class)) { + message = "BizEvent not found with event-id " + eventId; + } + return request + .createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR) + .body(ProblemJson.builder() + .title(HttpStatus.BAD_REQUEST.name()) + .detail(message) + .status(HttpStatus.BAD_REQUEST.value()) + .build()) .build(); } catch (IOException e) { logger.error(e.getMessage(), e); return request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR) + .body(ProblemJson.builder() + .title(HttpStatus.INTERNAL_SERVER_ERROR.name()) + .detail("Unexpected error while managing the receipt file") + .status(HttpStatus.INTERNAL_SERVER_ERROR.value()) + .build()) .build(); } } diff --git a/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/ReceiptToReviewedTest.java b/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/ReceiptToReviewedTest.java index 5d97c08..1b204f9 100644 --- a/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/ReceiptToReviewedTest.java +++ b/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/ReceiptToReviewedTest.java @@ -226,7 +226,7 @@ void requestWithValidBizEventIdButReceiptWrongStatus() throws ReceiptNotFoundExc // test execution AtomicReference responseMessage = new AtomicReference<>(); assertDoesNotThrow(() -> responseMessage.set(function.run(request, documentdb))); - assertEquals(HttpStatus.BAD_REQUEST, responseMessage.get().getStatus()); + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseMessage.get().getStatus()); verifyNoInteractions(documentdb); } diff --git a/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/RegenerateReceiptPdfTest.java b/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/RegenerateReceiptPdfTest.java index 57254dc..93b8035 100644 --- a/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/RegenerateReceiptPdfTest.java +++ b/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/RegenerateReceiptPdfTest.java @@ -73,10 +73,8 @@ void regeneratePDFSuccess() { doReturn(true).when(generateReceiptPdfServiceMock).verifyAndUpdateReceipt(any(), any()); RegenerateReceiptRequest regenerateReceiptRequest = new RegenerateReceiptRequest(); - regenerateReceiptRequest.setEventId("1"); - HttpRequestMessage> request = mock(HttpRequestMessage.class); - when(request.getBody()).thenReturn(Optional.of(regenerateReceiptRequest)); + HttpRequestMessage> request = mock(HttpRequestMessage.class); doAnswer((Answer) invocation -> { com.microsoft.azure.functions.HttpStatus status = (com.microsoft.azure.functions.HttpStatus) invocation.getArguments()[0]; @@ -84,7 +82,7 @@ void regeneratePDFSuccess() { }).when(request).createResponseBuilder(any(com.microsoft.azure.functions.HttpStatus.class)); // test execution - assertEquals(200,assertDoesNotThrow(() -> sut.run(request, executionContextMock)).getStatusCode()); + assertEquals(200,assertDoesNotThrow(() -> sut.run(request, "1", executionContextMock)).getStatusCode()); verify(receiptCosmosClientMock).getReceiptDocument(anyString()); verify(generateReceiptPdfServiceMock).generateReceipts(any(), any(), any()); @@ -97,8 +95,7 @@ void regeneratePDFInsuccessMissingRequestData() { RegenerateReceiptRequest regenerateReceiptRequest = new RegenerateReceiptRequest(); - HttpRequestMessage> request = mock(HttpRequestMessage.class); - when(request.getBody()).thenReturn(Optional.of(regenerateReceiptRequest)); + HttpRequestMessage> request = mock(HttpRequestMessage.class); doAnswer((Answer) invocation -> { com.microsoft.azure.functions.HttpStatus status = (com.microsoft.azure.functions.HttpStatus) invocation.getArguments()[0]; @@ -106,7 +103,7 @@ void regeneratePDFInsuccessMissingRequestData() { }).when(request).createResponseBuilder(any(com.microsoft.azure.functions.HttpStatus.class)); // test execution - assertEquals(400,assertDoesNotThrow(() -> sut.run(request, executionContextMock)).getStatusCode()); + assertEquals(500,assertDoesNotThrow(() -> sut.run(request, null, executionContextMock)).getStatusCode()); verifyNoInteractions(receiptCosmosClientMock); verifyNoInteractions(generateReceiptPdfServiceMock); @@ -125,8 +122,7 @@ void regeneratePDFErrorMissingEventData() { RegenerateReceiptRequest regenerateReceiptRequest = new RegenerateReceiptRequest(); regenerateReceiptRequest.setEventId("1"); - HttpRequestMessage> request = mock(HttpRequestMessage.class); - when(request.getBody()).thenReturn(Optional.of(regenerateReceiptRequest)); + HttpRequestMessage> request = mock(HttpRequestMessage.class); doAnswer((Answer) invocation -> { com.microsoft.azure.functions.HttpStatus status = (com.microsoft.azure.functions.HttpStatus) invocation.getArguments()[0]; @@ -134,7 +130,7 @@ void regeneratePDFErrorMissingEventData() { }).when(request).createResponseBuilder(any(com.microsoft.azure.functions.HttpStatus.class)); // test execution - assertEquals(400,assertDoesNotThrow(() -> sut.run(request, executionContextMock)).getStatusCode()); + assertEquals(500,assertDoesNotThrow(() -> sut.run(request, "1", executionContextMock)).getStatusCode()); verify(receiptCosmosClientMock).getReceiptDocument(anyString()); } @@ -146,11 +142,8 @@ void regeneratePDFErrorReceiptNotFound() { doReturn(bizEvent).when(bizEventCosmosClient).getBizEventDocument(anyString()); doThrow(new ReceiptNotFoundException("KO")).when(receiptCosmosClientMock).getReceiptDocument(anyString()); - RegenerateReceiptRequest regenerateReceiptRequest = new RegenerateReceiptRequest(); - regenerateReceiptRequest.setEventId("1"); - HttpRequestMessage> request = mock(HttpRequestMessage.class); - when(request.getBody()).thenReturn(Optional.of(regenerateReceiptRequest)); + HttpRequestMessage> request = mock(HttpRequestMessage.class); doAnswer((Answer) invocation -> { com.microsoft.azure.functions.HttpStatus status = (com.microsoft.azure.functions.HttpStatus) invocation.getArguments()[0]; @@ -158,7 +151,7 @@ void regeneratePDFErrorReceiptNotFound() { }).when(request).createResponseBuilder(any(com.microsoft.azure.functions.HttpStatus.class)); // test execution - assertEquals(400,assertDoesNotThrow(() -> sut.run(request, executionContextMock)).getStatusCode()); + assertEquals(500,assertDoesNotThrow(() -> sut.run(request, "1", executionContextMock)).getStatusCode()); verify(receiptCosmosClientMock).getReceiptDocument(anyString()); @@ -176,11 +169,8 @@ void regeneratePDFErrorReceiptGenerationException() { throw new Exception(); }); - RegenerateReceiptRequest regenerateReceiptRequest = new RegenerateReceiptRequest(); - regenerateReceiptRequest.setEventId("1"); - HttpRequestMessage> request = mock(HttpRequestMessage.class); - when(request.getBody()).thenReturn(Optional.of(regenerateReceiptRequest)); + HttpRequestMessage> request = mock(HttpRequestMessage.class); doAnswer((Answer) invocation -> { com.microsoft.azure.functions.HttpStatus status = (com.microsoft.azure.functions.HttpStatus) invocation.getArguments()[0]; @@ -188,7 +178,7 @@ void regeneratePDFErrorReceiptGenerationException() { }).when(request).createResponseBuilder(any(com.microsoft.azure.functions.HttpStatus.class)); // test execution - assertEquals(500,assertDoesNotThrow(() -> sut.run(request, executionContextMock)).getStatusCode()); + assertEquals(500,assertDoesNotThrow(() -> sut.run(request, "1", executionContextMock)).getStatusCode()); verify(receiptCosmosClientMock).getReceiptDocument(anyString()); verify(generateReceiptPdfServiceMock).generateReceipts(any(), any(), any()); From 264f6cf3683ed7de13cf55b2697fbd0433ebcbfd Mon Sep 17 00:00:00 2001 From: acialini Date: Fri, 1 Dec 2023 12:00:29 +0100 Subject: [PATCH 2/4] [PRDP-255] feat: Updated RegenerateReceiptPdf --- .../gov/pagopa/receipt/pdf/helpdesk/RegenerateReceiptPdf.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/RegenerateReceiptPdf.java b/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/RegenerateReceiptPdf.java index 0c000a6..cd3f6b7 100644 --- a/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/RegenerateReceiptPdf.java +++ b/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/RegenerateReceiptPdf.java @@ -64,7 +64,7 @@ public RegenerateReceiptPdf(){ public HttpResponseMessage run ( @HttpTrigger(name = "RegenerateReceiptPdfTrigger", methods = {HttpMethod.POST}, - route = "/receipts//regenerate-receipt-pdf", + route = "/receipts/{bizevent-id}/regenerate-receipt-pdf", authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage> request, @BindingName("bizevent-id") String eventId, From 82a17bf0bf91acea8caa08228b0b6da9d0da1067 Mon Sep 17 00:00:00 2001 From: acialini Date: Fri, 1 Dec 2023 12:13:05 +0100 Subject: [PATCH 3/4] [PRDP-255] feat: Updated helm values --- helm/values-dev.yaml | 3 ++- helm/values-prod.yaml | 1 + helm/values-uat.yaml | 1 + 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/helm/values-dev.yaml b/helm/values-dev.yaml index 3a2b576..ddc412b 100644 --- a/helm/values-dev.yaml +++ b/helm/values-dev.yaml @@ -115,7 +115,8 @@ microservice-chart: envSecret: APPLICATIONINSIGHTS_CONNECTION_STRING: "ai-d-connection-string" COSMOS_RECEIPTS_CONN_STRING: "cosmos-receipt-connection-string" - RECEIPT_QUEUE_CONN_STRING: "receipts-storage-account-connection-string" + RECEIPT_QUEUE_CONN_STRING: "receipts-storage-account-connection-string", + RECEIPTS_STORAGE_CONN_STRING: "receipts-storage-account-connection-string", COSMOS_BIZ_EVENT_CONN_STRING: "cosmos-biz-event-d-connection-string" COSMOS_RECEIPT_KEY: "cosmos-receipt-pkey" COSMOS_BIZ_EVENT_KEY: "cosmos-bizevent-pkey" diff --git a/helm/values-prod.yaml b/helm/values-prod.yaml index e9b388f..4d947a3 100644 --- a/helm/values-prod.yaml +++ b/helm/values-prod.yaml @@ -115,6 +115,7 @@ microservice-chart: envSecret: APPLICATIONINSIGHTS_CONNECTION_STRING: "ai-p-connection-string" COSMOS_RECEIPTS_CONN_STRING: "cosmos-receipt-connection-string" + RECEIPTS_STORAGE_CONN_STRING: "receipts-storage-account-connection-string", RECEIPT_QUEUE_CONN_STRING: "receipts-storage-account-connection-string" COSMOS_BIZ_EVENT_CONN_STRING: "cosmos-biz-event-p-connection-string" COSMOS_RECEIPT_KEY: "cosmos-receipt-pkey" diff --git a/helm/values-uat.yaml b/helm/values-uat.yaml index 9410e0c..a6eeecc 100644 --- a/helm/values-uat.yaml +++ b/helm/values-uat.yaml @@ -115,6 +115,7 @@ microservice-chart: envSecret: APPLICATIONINSIGHTS_CONNECTION_STRING: "ai-u-connection-string" COSMOS_RECEIPTS_CONN_STRING: "cosmos-receipt-connection-string" + RECEIPTS_STORAGE_CONN_STRING: "receipts-storage-account-connection-string", RECEIPT_QUEUE_CONN_STRING: "receipts-storage-account-connection-string" COSMOS_BIZ_EVENT_CONN_STRING: "cosmos-biz-event-u-connection-string" COSMOS_RECEIPT_KEY: "cosmos-receipt-pkey" From 3d4e25bc954208b7dd4fec89b08e37a6f17a9854 Mon Sep 17 00:00:00 2001 From: acialini Date: Fri, 1 Dec 2023 17:06:25 +0100 Subject: [PATCH 4/4] [PRDP-255] feat: removed unnecessary logic in pdf generation --- .../pdf/helpdesk/RegenerateReceiptPdf.java | 18 +- .../impl/GenerateReceiptPdfServiceImpl.java | 67 +--- .../helpdesk/RegenerateReceiptPdfTest.java | 5 +- .../GenerateReceiptPdfServiceImplTest.java | 287 ++++-------------- 4 files changed, 87 insertions(+), 290 deletions(-) diff --git a/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/RegenerateReceiptPdf.java b/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/RegenerateReceiptPdf.java index cd3f6b7..222e20f 100644 --- a/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/RegenerateReceiptPdf.java +++ b/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/RegenerateReceiptPdf.java @@ -79,8 +79,9 @@ public HttpResponseMessage run ( //Retrieve receipt's data from CosmosDB Receipt receipt = getReceipt(context, bizEvent, receiptCosmosClient, logger); + //Verify receipt status - if (receipt.getEventData() != null) { + if (receipt.getEventData() != null && isHasAllAttachments(receipt)) { logger.info("[{}] Generating pdf for Receipt with id {} and bizEvent with id {}", context.getFunctionName(), @@ -161,4 +162,19 @@ public HttpResponseMessage run ( } } + private static boolean isHasAllAttachments(Receipt receipt) { + String debtorCF = receipt.getEventData().getDebtorFiscalCode(); + String payerCF = receipt.getEventData().getPayerFiscalCode(); + boolean hasAllAttachments; + if (payerCF == null) { + hasAllAttachments = receipt.getMdAttach() != null && receipt.getMdAttach().getUrl() != null; + } else if (debtorCF.equals(payerCF)) { + hasAllAttachments = receipt.getMdAttach() != null && receipt.getMdAttach().getUrl() != null; + } else { + hasAllAttachments = receipt.getMdAttach() != null && receipt.getMdAttach().getUrl() != null && + receipt.getMdAttachPayer() != null && receipt.getMdAttachPayer().getUrl() != null; + } + return hasAllAttachments; + } + } \ No newline at end of file diff --git a/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/service/impl/GenerateReceiptPdfServiceImpl.java b/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/service/impl/GenerateReceiptPdfServiceImpl.java index 31256f6..95342b9 100644 --- a/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/service/impl/GenerateReceiptPdfServiceImpl.java +++ b/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/service/impl/GenerateReceiptPdfServiceImpl.java @@ -1,12 +1,10 @@ package it.gov.pagopa.receipt.pdf.helpdesk.service.impl; -import com.fasterxml.jackson.core.JsonProcessingException; import it.gov.pagopa.receipt.pdf.helpdesk.client.PdfEngineClient; import it.gov.pagopa.receipt.pdf.helpdesk.client.ReceiptBlobClient; import it.gov.pagopa.receipt.pdf.helpdesk.client.impl.PdfEngineClientImpl; import it.gov.pagopa.receipt.pdf.helpdesk.client.impl.ReceiptBlobClientImpl; import it.gov.pagopa.receipt.pdf.helpdesk.entity.event.BizEvent; -import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.ReasonError; import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.Receipt; import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.ReceiptMetadata; import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.enumeration.ReasonErrorCode; @@ -31,10 +29,10 @@ import java.io.FileInputStream; import java.net.URL; import java.nio.file.Path; -import java.time.LocalDate; -import java.time.format.DateTimeFormatter; import java.util.Objects; +import static org.apache.http.HttpStatus.SC_OK; + public class GenerateReceiptPdfServiceImpl implements GenerateReceiptPdfService { private final Logger logger = LoggerFactory.getLogger(GenerateReceiptPdfServiceImpl.class); @@ -76,34 +74,23 @@ public PdfGeneration generateReceipts(Receipt receipt, BizEvent bizEvent, Path w if (payerCF.equals(debtorCF)) { pdfGeneration.setGenerateOnlyDebtor(true); //Generate debtor's complete PDF - if (receiptAlreadyCreated(receipt.getMdAttach())) { - pdfGeneration.setDebtorMetadata(PdfMetadata.builder().statusCode(ALREADY_CREATED).build()); - return pdfGeneration; - } - PdfMetadata generationResult = generateAndSavePDFReceipt(bizEvent, PAYER_TEMPLATE_SUFFIX, false, workingDirPath); + PdfMetadata generationResult = generateAndSavePDFReceipt(bizEvent, receipt.getMdAttach().getName(), false, workingDirPath); pdfGeneration.setDebtorMetadata(generationResult); return pdfGeneration; } //Generate payer's complete PDF - if (receiptAlreadyCreated(receipt.getMdAttachPayer())) { - pdfGeneration.setPayerMetadata(PdfMetadata.builder().statusCode(ALREADY_CREATED).build()); - } else { + PdfMetadata generationResult = generateAndSavePDFReceipt(bizEvent, receipt.getMdAttachPayer().getName(), false, workingDirPath); + pdfGeneration.setPayerMetadata(generationResult); - PdfMetadata generationResult = generateAndSavePDFReceipt(bizEvent, PAYER_TEMPLATE_SUFFIX, false, workingDirPath); - pdfGeneration.setPayerMetadata(generationResult); - } } else { pdfGeneration.setGenerateOnlyDebtor(true); } //Generate debtor's partial PDF - if (receiptAlreadyCreated(receipt.getMdAttach())) { - pdfGeneration.setDebtorMetadata(PdfMetadata.builder().statusCode(ALREADY_CREATED).build()); - } else { - PdfMetadata generationResult = generateAndSavePDFReceipt(bizEvent, DEBTOR_TEMPLATE_SUFFIX, true, workingDirPath); - pdfGeneration.setDebtorMetadata(generationResult); - } + PdfMetadata generationResult = generateAndSavePDFReceipt(bizEvent, receipt.getMdAttach().getName(), true, workingDirPath); + pdfGeneration.setDebtorMetadata(generationResult); + return pdfGeneration; } @@ -120,20 +107,8 @@ public boolean verifyAndUpdateReceipt(Receipt receipt, PdfGeneration pdfGenerati return false; } - if (debtorMetadata.getStatusCode() == HttpStatus.SC_OK) { - ReceiptMetadata receiptMetadata = new ReceiptMetadata(); - receiptMetadata.setName(debtorMetadata.getDocumentName()); - receiptMetadata.setUrl(debtorMetadata.getDocumentUrl()); - - receipt.setMdAttach(receiptMetadata); - } else if (debtorMetadata.getStatusCode() != ALREADY_CREATED) { - ReasonError reasonError = new ReasonError(debtorMetadata.getStatusCode(), debtorMetadata.getErrorMessage()); - receipt.setReasonErr(reasonError); - result = false; - } - if (pdfGeneration.isGenerateOnlyDebtor()) { - if (debtorMetadata.getStatusCode() == ReasonErrorCode.ERROR_TEMPLATE_PDF.getCode()) { + if (debtorMetadata.getStatusCode() != SC_OK) { String errMsg = String.format("Debtor receipt generation fail with status %s", debtorMetadata.getStatusCode()); throw new ReceiptGenerationNotToRetryException(errMsg); } @@ -146,20 +121,8 @@ public boolean verifyAndUpdateReceipt(Receipt receipt, PdfGeneration pdfGenerati return false; } - if (payerMetadata.getStatusCode() == HttpStatus.SC_OK) { - ReceiptMetadata receiptMetadata = new ReceiptMetadata(); - receiptMetadata.setName(payerMetadata.getDocumentName()); - receiptMetadata.setUrl(payerMetadata.getDocumentUrl()); - - receipt.setMdAttachPayer(receiptMetadata); - } else if (payerMetadata.getStatusCode() != ALREADY_CREATED) { - ReasonError reasonErrorPayer = new ReasonError(payerMetadata.getStatusCode(), payerMetadata.getErrorMessage()); - receipt.setReasonErrPayer(reasonErrorPayer); - result = false; - } - - if (debtorMetadata.getStatusCode() == ReasonErrorCode.ERROR_TEMPLATE_PDF.getCode() - || payerMetadata.getStatusCode() == ReasonErrorCode.ERROR_TEMPLATE_PDF.getCode()) { + if (debtorMetadata.getStatusCode() != SC_OK + || payerMetadata.getStatusCode() != SC_OK) { String errMsg = String.format("Receipt generation fail for debtor (status: %s) and/or payer (status: %s)", debtorMetadata.getStatusCode(), payerMetadata.getStatusCode()); throw new ReceiptGenerationNotToRetryException(errMsg); @@ -167,11 +130,9 @@ public boolean verifyAndUpdateReceipt(Receipt receipt, PdfGeneration pdfGenerati return result; } - private PdfMetadata generateAndSavePDFReceipt(BizEvent bizEvent, String templateSuffix, boolean partialTemplate, Path workingDirPath) { + private PdfMetadata generateAndSavePDFReceipt(BizEvent bizEvent, String blobName, boolean partialTemplate, Path workingDirPath) { try { ReceiptPDFTemplate template = buildTemplateService.buildTemplate(bizEvent, partialTemplate); - String dateFormatted = LocalDate.now().format(DateTimeFormatter.ofPattern("yyMMdd")); - String blobName = String.format("%s-%s-%s-%s", TEMPLATE_PREFIX, dateFormatted, bizEvent.getId(), templateSuffix); PdfEngineResponse pdfEngineResponse = generatePDFReceipt(template, workingDirPath); return saveToBlobStorage(pdfEngineResponse, blobName); } catch (PDFReceiptGenerationException e) { @@ -201,7 +162,7 @@ private PdfMetadata saveToBlobStorage(PdfEngineResponse pdfEngineResponse, Strin return PdfMetadata.builder() .documentName(blobStorageResponse.getDocumentName()) .documentUrl(blobStorageResponse.getDocumentUrl()) - .statusCode(HttpStatus.SC_OK) + .statusCode(SC_OK) .build(); } @@ -216,7 +177,7 @@ private PdfEngineResponse generatePDFReceipt(ReceiptPDFTemplate template, Path w PdfEngineResponse pdfEngineResponse = pdfEngineClient.generatePDF(request, workingDirPath); - if (pdfEngineResponse.getStatusCode() != HttpStatus.SC_OK) { + if (pdfEngineResponse.getStatusCode() != SC_OK) { String errMsg = String.format("PDF-Engine response KO (%s): %s", pdfEngineResponse.getStatusCode(), pdfEngineResponse.getErrorMessage()); throw new GeneratePDFException(errMsg, pdfEngineResponse.getStatusCode()); } diff --git a/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/RegenerateReceiptPdfTest.java b/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/RegenerateReceiptPdfTest.java index 93b8035..14d15c6 100644 --- a/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/RegenerateReceiptPdfTest.java +++ b/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/RegenerateReceiptPdfTest.java @@ -9,6 +9,7 @@ import it.gov.pagopa.receipt.pdf.helpdesk.entity.event.BizEvent; import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.EventData; import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.Receipt; +import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.ReceiptMetadata; import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.enumeration.ReceiptStatusType; import it.gov.pagopa.receipt.pdf.helpdesk.exception.ReceiptNotFoundException; import it.gov.pagopa.receipt.pdf.helpdesk.model.PdfGeneration; @@ -72,8 +73,6 @@ void regeneratePDFSuccess() { doReturn(new PdfGeneration()).when(generateReceiptPdfServiceMock).generateReceipts(any(), any(), any()); doReturn(true).when(generateReceiptPdfServiceMock).verifyAndUpdateReceipt(any(), any()); - RegenerateReceiptRequest regenerateReceiptRequest = new RegenerateReceiptRequest(); - HttpRequestMessage> request = mock(HttpRequestMessage.class); doAnswer((Answer) invocation -> { @@ -192,6 +191,8 @@ private Receipt buildReceiptWithStatus(ReceiptStatusType receiptStatusType, int .debtorFiscalCode("cd debtor") .payerFiscalCode("cf payer") .build()) + .mdAttach(ReceiptMetadata.builder().name("DEBTOR_NAME").url("DEBTOR_URL").build()) + .mdAttachPayer(ReceiptMetadata.builder().name("PAYER_NAME").url("PAYER_URL").build()) .eventId("biz-event-id") .status(receiptStatusType) .numRetry(numRetry) diff --git a/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/service/impl/GenerateReceiptPdfServiceImplTest.java b/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/service/impl/GenerateReceiptPdfServiceImplTest.java index e8a61bc..be1fef1 100644 --- a/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/service/impl/GenerateReceiptPdfServiceImplTest.java +++ b/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/service/impl/GenerateReceiptPdfServiceImplTest.java @@ -21,6 +21,7 @@ import org.apache.commons.io.FileUtils; import org.apache.http.HttpStatus; import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -30,6 +31,7 @@ import java.nio.file.Path; import static it.gov.pagopa.receipt.pdf.helpdesk.service.impl.GenerateReceiptPdfServiceImpl.ALREADY_CREATED; +import static org.apache.http.HttpStatus.SC_OK; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.Mockito.*; @@ -87,10 +89,10 @@ public void teardown() throws IOException { @Test void generateReceiptsPayerNullWithSuccess() throws Exception { - Receipt receiptOnly = getReceiptWithOnlyDebtor(false); + Receipt receiptOnly = getReceiptWithOnlyDebtor(true); BizEvent bizEventOnly = getBizEventWithOnlyDebtor(); - doReturn(getPdfEngineResponse(HttpStatus.SC_OK, outputPdfDebtor.getPath())) + doReturn(getPdfEngineResponse(SC_OK, outputPdfDebtor.getPath())) .when(pdfEngineClientMock).generatePDF(any(), any()); doReturn(getBlobStorageResponse(com.microsoft.azure.functions.HttpStatus.CREATED.value())) .when(receiptBlobClientMock).savePdfToBlobStorage(any(), anyString()); @@ -105,7 +107,7 @@ void generateReceiptsPayerNullWithSuccess() throws Exception { assertNull(pdfGeneration.getDebtorMetadata().getErrorMessage()); assertNotNull(pdfGeneration.getDebtorMetadata().getDocumentName()); assertNotNull(pdfGeneration.getDebtorMetadata().getDocumentUrl()); - assertEquals(HttpStatus.SC_OK, pdfGeneration.getDebtorMetadata().getStatusCode()); + assertEquals(SC_OK, pdfGeneration.getDebtorMetadata().getStatusCode()); assertNull(pdfGeneration.getPayerMetadata()); verify(buildTemplateServiceMock).buildTemplate(any(), anyBoolean()); @@ -115,10 +117,10 @@ void generateReceiptsPayerNullWithSuccess() throws Exception { @Test void generateReceiptsSameDebtorPayerWithSuccess() throws Exception { - Receipt receiptOnly = getReceiptWithDebtorPayer(VALID_CF_DEBTOR, false, false); + Receipt receiptOnly = getReceiptWithDebtorPayer(VALID_CF_DEBTOR, true, true); BizEvent bizEventOnly = getBizEventWithDebtorPayer(VALID_CF_DEBTOR); - doReturn(getPdfEngineResponse(HttpStatus.SC_OK, outputPdfDebtor.getPath())) + doReturn(getPdfEngineResponse(SC_OK, outputPdfDebtor.getPath())) .when(pdfEngineClientMock).generatePDF(any(), any()); doReturn(getBlobStorageResponse(com.microsoft.azure.functions.HttpStatus.CREATED.value())) .when(receiptBlobClientMock).savePdfToBlobStorage(any(), anyString()); @@ -133,7 +135,7 @@ void generateReceiptsSameDebtorPayerWithSuccess() throws Exception { assertNull(pdfGeneration.getDebtorMetadata().getErrorMessage()); assertNotNull(pdfGeneration.getDebtorMetadata().getDocumentName()); assertNotNull(pdfGeneration.getDebtorMetadata().getDocumentUrl()); - assertEquals(HttpStatus.SC_OK, pdfGeneration.getDebtorMetadata().getStatusCode()); + assertEquals(SC_OK, pdfGeneration.getDebtorMetadata().getStatusCode()); assertNull(pdfGeneration.getPayerMetadata()); verify(buildTemplateServiceMock).buildTemplate(any(), anyBoolean()); @@ -143,11 +145,11 @@ void generateReceiptsSameDebtorPayerWithSuccess() throws Exception { @Test void generateReceiptsDifferentDebtorPayerWithSuccess() throws Exception { - Receipt receiptOnly = getReceiptWithDebtorPayer(VALID_CF_PAYER, false, false); + Receipt receiptOnly = getReceiptWithDebtorPayer(VALID_CF_PAYER, true, true); BizEvent bizEventOnly = getBizEventWithDebtorPayer(VALID_CF_PAYER); - doReturn(getPdfEngineResponse(HttpStatus.SC_OK, outputPdfDebtor.getPath()), - getPdfEngineResponse(HttpStatus.SC_OK, outputPdfPayer.getPath())) + doReturn(getPdfEngineResponse(SC_OK, outputPdfDebtor.getPath()), + getPdfEngineResponse(SC_OK, outputPdfPayer.getPath())) .when(pdfEngineClientMock).generatePDF(any(), any()); doReturn(getBlobStorageResponse(com.microsoft.azure.functions.HttpStatus.CREATED.value()), getBlobStorageResponse(com.microsoft.azure.functions.HttpStatus.CREATED.value())) @@ -163,95 +165,21 @@ void generateReceiptsDifferentDebtorPayerWithSuccess() throws Exception { assertNull(pdfGeneration.getDebtorMetadata().getErrorMessage()); assertNotNull(pdfGeneration.getDebtorMetadata().getDocumentName()); assertNotNull(pdfGeneration.getDebtorMetadata().getDocumentUrl()); - assertEquals(HttpStatus.SC_OK, pdfGeneration.getDebtorMetadata().getStatusCode()); + assertEquals(SC_OK, pdfGeneration.getDebtorMetadata().getStatusCode()); assertNotNull(pdfGeneration.getPayerMetadata()); assertNull(pdfGeneration.getPayerMetadata().getErrorMessage()); assertNotNull(pdfGeneration.getPayerMetadata().getDocumentName()); assertNotNull(pdfGeneration.getPayerMetadata().getDocumentUrl()); - assertEquals(HttpStatus.SC_OK, pdfGeneration.getPayerMetadata().getStatusCode()); + assertEquals(SC_OK, pdfGeneration.getPayerMetadata().getStatusCode()); verify(buildTemplateServiceMock, times(2)).buildTemplate(any(), anyBoolean()); verify(pdfEngineClientMock, times(2)).generatePDF(any(), any()); verify(receiptBlobClientMock, times(2)).savePdfToBlobStorage(any(), anyString()); } - @Test - void generateReceiptsPayerNullReceiptAlreadyCreatedWithSuccess() throws TemplateDataMappingException { - Receipt receiptOnly = getReceiptWithOnlyDebtor(true); - BizEvent bizEventOnly = getBizEventWithOnlyDebtor(); - - PdfGeneration pdfGeneration = sut.generateReceipts(receiptOnly, bizEventOnly, Path.of("/tmp")); - - assertNotNull(pdfGeneration); - assertTrue(pdfGeneration.isGenerateOnlyDebtor()); - assertNotNull(pdfGeneration.getDebtorMetadata()); - assertNull(pdfGeneration.getDebtorMetadata().getErrorMessage()); - assertNull(pdfGeneration.getDebtorMetadata().getDocumentName()); - assertNull(pdfGeneration.getDebtorMetadata().getDocumentUrl()); - assertEquals(ALREADY_CREATED, pdfGeneration.getDebtorMetadata().getStatusCode()); - assertNull(pdfGeneration.getPayerMetadata()); - - verify(buildTemplateServiceMock, never()).buildTemplate(any(), anyBoolean()); - verify(pdfEngineClientMock, never()).generatePDF(any(), any()); - verify(receiptBlobClientMock, never()).savePdfToBlobStorage(any(), anyString()); - } - - @Test - void generateReceiptsSameDebtorPayerAndDebtorReceiptAlreadyCreatedWithSuccess() throws TemplateDataMappingException { - Receipt receiptOnly = getReceiptWithDebtorPayer(VALID_CF_DEBTOR, true, false); - BizEvent bizEventOnly = getBizEventWithDebtorPayer(VALID_CF_DEBTOR); - - PdfGeneration pdfGeneration = sut.generateReceipts(receiptOnly, bizEventOnly, Path.of("/tmp")); - - assertNotNull(pdfGeneration); - assertTrue(pdfGeneration.isGenerateOnlyDebtor()); - assertNotNull(pdfGeneration.getDebtorMetadata()); - assertNull(pdfGeneration.getDebtorMetadata().getErrorMessage()); - assertNull(pdfGeneration.getDebtorMetadata().getDocumentName()); - assertNull(pdfGeneration.getDebtorMetadata().getDocumentUrl()); - assertEquals(ALREADY_CREATED, pdfGeneration.getDebtorMetadata().getStatusCode()); - assertNull(pdfGeneration.getPayerMetadata()); - - verify(buildTemplateServiceMock, never()).buildTemplate(any(), anyBoolean()); - verify(pdfEngineClientMock, never()).generatePDF(any(), any()); - verify(receiptBlobClientMock, never()).savePdfToBlobStorage(any(), anyString()); - } - - @Test - void generateReceiptsDifferentDebtorPayerAndPayerReceiptAlreadyCreatedWithSuccess() throws Exception { - Receipt receiptOnly = getReceiptWithDebtorPayer(VALID_CF_PAYER, false, true); - BizEvent bizEventOnly = getBizEventWithDebtorPayer(VALID_CF_PAYER); - - doReturn(getPdfEngineResponse(HttpStatus.SC_OK, outputPdfDebtor.getPath())) - .when(pdfEngineClientMock).generatePDF(any(), any()); - doReturn(getBlobStorageResponse(com.microsoft.azure.functions.HttpStatus.CREATED.value())) - .when(receiptBlobClientMock).savePdfToBlobStorage(any(), anyString()); - doReturn(new ReceiptPDFTemplate()) - .when(buildTemplateServiceMock).buildTemplate(any(), anyBoolean()); - - PdfGeneration pdfGeneration = sut.generateReceipts(receiptOnly, bizEventOnly,Path.of("/tmp")); - - assertNotNull(pdfGeneration); - assertFalse(pdfGeneration.isGenerateOnlyDebtor()); - assertNotNull(pdfGeneration.getDebtorMetadata()); - assertNull(pdfGeneration.getDebtorMetadata().getErrorMessage()); - assertNotNull(pdfGeneration.getDebtorMetadata().getDocumentName()); - assertNotNull(pdfGeneration.getDebtorMetadata().getDocumentUrl()); - assertEquals(HttpStatus.SC_OK, pdfGeneration.getDebtorMetadata().getStatusCode()); - assertNotNull(pdfGeneration.getPayerMetadata()); - assertNull(pdfGeneration.getPayerMetadata().getErrorMessage()); - assertNull(pdfGeneration.getPayerMetadata().getDocumentName()); - assertNull(pdfGeneration.getPayerMetadata().getDocumentUrl()); - assertEquals(ALREADY_CREATED, pdfGeneration.getPayerMetadata().getStatusCode()); - - verify(buildTemplateServiceMock).buildTemplate(any(), anyBoolean()); - verify(pdfEngineClientMock).generatePDF(any(), any()); - verify(receiptBlobClientMock).savePdfToBlobStorage(any(), anyString()); - } - @Test void generateReceiptsPayerNullFailPDFEngineCallReturn500() throws Exception { - Receipt receiptOnly = getReceiptWithOnlyDebtor(false); + Receipt receiptOnly = getReceiptWithOnlyDebtor(true); BizEvent bizEventOnly = getBizEventWithOnlyDebtor(); doReturn(getPdfEngineResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR, "")) @@ -277,7 +205,7 @@ void generateReceiptsPayerNullFailPDFEngineCallReturn500() throws Exception { @Test void generateReceiptsPayerNullFailBuildTemplateData() throws Exception { - Receipt receiptOnly = getReceiptWithOnlyDebtor(false); + Receipt receiptOnly = getReceiptWithOnlyDebtor(true); BizEvent bizEventOnly = getBizEventWithOnlyDebtor(); doThrow(new TemplateDataMappingException("error message", ReasonErrorCode.ERROR_TEMPLATE_PDF.getCode())) @@ -301,10 +229,10 @@ void generateReceiptsPayerNullFailBuildTemplateData() throws Exception { @Test void generateReceiptsPayerNullFailSaveToBlobStorageThrowsException() throws Exception { - Receipt receiptOnly = getReceiptWithOnlyDebtor(false); + Receipt receiptOnly = getReceiptWithOnlyDebtor(true); BizEvent bizEventOnly = getBizEventWithOnlyDebtor(); - doReturn(getPdfEngineResponse(HttpStatus.SC_OK, outputPdfDebtor.getPath())) + doReturn(getPdfEngineResponse(SC_OK, outputPdfDebtor.getPath())) .when(pdfEngineClientMock).generatePDF(any(), any()); doThrow(RuntimeException.class).when(receiptBlobClientMock).savePdfToBlobStorage(any(), anyString()); doReturn(new ReceiptPDFTemplate()) @@ -328,10 +256,10 @@ void generateReceiptsPayerNullFailSaveToBlobStorageThrowsException() throws Exce @Test void generateReceiptsPayerNullFailSaveToBlobStorageReturn500() throws Exception { - Receipt receiptOnly = getReceiptWithOnlyDebtor(false); + Receipt receiptOnly = getReceiptWithOnlyDebtor(true); BizEvent bizEventOnly = getBizEventWithOnlyDebtor(); - doReturn(getPdfEngineResponse(HttpStatus.SC_OK, outputPdfDebtor.getPath())) + doReturn(getPdfEngineResponse(SC_OK, outputPdfDebtor.getPath())) .when(pdfEngineClientMock).generatePDF(any(), any()); doReturn(getBlobStorageResponse(com.microsoft.azure.functions.HttpStatus.INTERNAL_SERVER_ERROR.value())) .when(receiptBlobClientMock).savePdfToBlobStorage(any(), anyString()); @@ -356,11 +284,11 @@ void generateReceiptsPayerNullFailSaveToBlobStorageReturn500() throws Exception @Test void verifyPayerNullOrSameDebtorPayerWithSuccess() throws ReceiptGenerationNotToRetryException { - Receipt receipt = buildReceiptForVerify(false, false); + Receipt receipt = buildReceiptForVerify(true, true); PdfGeneration pdfGeneration = PdfGeneration.builder() .debtorMetadata(PdfMetadata.builder() - .statusCode(HttpStatus.SC_OK) + .statusCode(SC_OK) .documentName(DEBTOR_DOCUMENT_NAME) .documentUrl(DEBTOR_DOCUMENT_URL) .build()) @@ -373,25 +301,22 @@ void verifyPayerNullOrSameDebtorPayerWithSuccess() throws ReceiptGenerationNotTo assertNotNull(receipt.getMdAttach()); assertNotNull(receipt.getMdAttach().getUrl()); assertNotNull(receipt.getMdAttach().getName()); - assertEquals(DEBTOR_DOCUMENT_NAME, receipt.getMdAttach().getName()); - assertEquals(DEBTOR_DOCUMENT_URL, receipt.getMdAttach().getUrl()); - assertNull(receipt.getMdAttachPayer()); - assertNull(receipt.getReasonErr()); - assertNull(receipt.getReasonErrPayer()); + assertEquals(RECEIPT_METADATA_ORIGINAL_NAME, receipt.getMdAttach().getName()); + assertEquals(RECEIPT_METADATA_ORIGINAL_URL, receipt.getMdAttach().getUrl()); } @Test void verifyDifferentDebtorPayerWithSuccess() throws ReceiptGenerationNotToRetryException { - Receipt receipt = buildReceiptForVerify(false, false); + Receipt receipt = buildReceiptForVerify(true, true); PdfGeneration pdfGeneration = PdfGeneration.builder() .debtorMetadata(PdfMetadata.builder() - .statusCode(HttpStatus.SC_OK) + .statusCode(SC_OK) .documentName(DEBTOR_DOCUMENT_NAME) .documentUrl(DEBTOR_DOCUMENT_URL) .build()) .payerMetadata(PdfMetadata.builder() - .statusCode(HttpStatus.SC_OK) + .statusCode(SC_OK) .documentName(PAYER_DOCUMENT_NAME) .documentUrl(PAYER_DOCUMENT_URL) .build()) @@ -404,13 +329,13 @@ void verifyDifferentDebtorPayerWithSuccess() throws ReceiptGenerationNotToRetryE assertNotNull(receipt.getMdAttach()); assertNotNull(receipt.getMdAttach().getUrl()); assertNotNull(receipt.getMdAttach().getName()); - assertEquals(DEBTOR_DOCUMENT_NAME, receipt.getMdAttach().getName()); - assertEquals(DEBTOR_DOCUMENT_URL, receipt.getMdAttach().getUrl()); + assertEquals(RECEIPT_METADATA_ORIGINAL_NAME, receipt.getMdAttach().getName()); + assertEquals(RECEIPT_METADATA_ORIGINAL_URL, receipt.getMdAttach().getUrl()); assertNotNull(receipt.getMdAttachPayer()); assertNotNull(receipt.getMdAttachPayer().getUrl()); assertNotNull(receipt.getMdAttachPayer().getName()); - assertEquals(PAYER_DOCUMENT_NAME, receipt.getMdAttachPayer().getName()); - assertEquals(PAYER_DOCUMENT_URL, receipt.getMdAttachPayer().getUrl()); + assertEquals(RECEIPT_METADATA_ORIGINAL_NAME, receipt.getMdAttachPayer().getName()); + assertEquals(RECEIPT_METADATA_ORIGINAL_URL, receipt.getMdAttachPayer().getUrl()); assertNull(receipt.getReasonErr()); assertNull(receipt.getReasonErrPayer()); } @@ -438,7 +363,7 @@ void verifyPayerNullOrSameDebtorPayerAlreadyCreatedSuccess() throws ReceiptGener PdfGeneration pdfGeneration = PdfGeneration.builder() .debtorMetadata(PdfMetadata.builder() - .statusCode(ALREADY_CREATED) + .statusCode(SC_OK) .build()) .generateOnlyDebtor(true) .build(); @@ -468,16 +393,8 @@ void verifyPayerNullOrSameDebtorPayerFailReceiptGenerationInError() throws Recei .generateOnlyDebtor(true) .build(); - boolean result = sut.verifyAndUpdateReceipt(receipt, pdfGeneration); + assertThrows(ReceiptGenerationNotToRetryException.class, () -> sut.verifyAndUpdateReceipt(receipt, pdfGeneration)); - assertFalse(result); - assertNull(receipt.getMdAttach()); - assertNull(receipt.getMdAttachPayer()); - assertNotNull(receipt.getReasonErr()); - assertNotNull(receipt.getReasonErr().getMessage()); - assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, receipt.getReasonErr().getCode()); - assertEquals(ERROR_MESSAGE, receipt.getReasonErr().getMessage()); - assertNull(receipt.getReasonErrPayer()); } @Test @@ -494,60 +411,20 @@ void verifyPayerNullOrSameDebtorPayerFailThrowsReceiptGenerationNotToRetryExcept assertThrows(ReceiptGenerationNotToRetryException.class, () -> sut.verifyAndUpdateReceipt(receipt, pdfGeneration)); - assertNull(receipt.getMdAttach()); - assertNull(receipt.getMdAttachPayer()); - assertNotNull(receipt.getReasonErr()); - assertNotNull(receipt.getReasonErr().getMessage()); - assertEquals(ReasonErrorCode.ERROR_TEMPLATE_PDF.getCode(), receipt.getReasonErr().getCode()); - assertEquals(ERROR_MESSAGE, receipt.getReasonErr().getMessage()); - assertNull(receipt.getReasonErrPayer()); - } - - @Test - void verifyDifferentDebtorPayerAndDebtorAlreadyGeneratedSuccess() throws ReceiptGenerationNotToRetryException { - Receipt receipt = buildReceiptForVerify(true, false); - - PdfGeneration pdfGeneration = PdfGeneration.builder() - .debtorMetadata(PdfMetadata.builder() - .statusCode(ALREADY_CREATED) - .build()) - .payerMetadata(PdfMetadata.builder() - .statusCode(HttpStatus.SC_OK) - .documentName(PAYER_DOCUMENT_NAME) - .documentUrl(PAYER_DOCUMENT_URL) - .build()) - .generateOnlyDebtor(false) - .build(); - - boolean result = sut.verifyAndUpdateReceipt(receipt, pdfGeneration); - - assertTrue(result); - assertNotNull(receipt.getMdAttach()); - assertNotNull(receipt.getMdAttach().getUrl()); - assertNotNull(receipt.getMdAttach().getName()); - assertEquals(RECEIPT_METADATA_ORIGINAL_NAME, receipt.getMdAttach().getName()); - assertEquals(RECEIPT_METADATA_ORIGINAL_URL, receipt.getMdAttach().getUrl()); - assertNotNull(receipt.getMdAttachPayer()); - assertNotNull(receipt.getMdAttachPayer().getUrl()); - assertNotNull(receipt.getMdAttachPayer().getName()); - assertEquals(PAYER_DOCUMENT_NAME, receipt.getMdAttachPayer().getName()); - assertEquals(PAYER_DOCUMENT_URL, receipt.getMdAttachPayer().getUrl()); - assertNull(receipt.getReasonErr()); - assertNull(receipt.getReasonErrPayer()); } @Test void verifyDifferentDebtorPayerAndPayerAlreadyGeneratedSuccess() throws ReceiptGenerationNotToRetryException { - Receipt receipt = buildReceiptForVerify(false, true); + Receipt receipt = buildReceiptForVerify(true, true); PdfGeneration pdfGeneration = PdfGeneration.builder() .debtorMetadata(PdfMetadata.builder() - .statusCode(HttpStatus.SC_OK) + .statusCode(SC_OK) .documentName(DEBTOR_DOCUMENT_NAME) .documentUrl(DEBTOR_DOCUMENT_URL) .build()) .payerMetadata(PdfMetadata.builder() - .statusCode(ALREADY_CREATED) + .statusCode(SC_OK) .build()) .generateOnlyDebtor(false) .build(); @@ -558,8 +435,8 @@ void verifyDifferentDebtorPayerAndPayerAlreadyGeneratedSuccess() throws ReceiptG assertNotNull(receipt.getMdAttach()); assertNotNull(receipt.getMdAttach().getUrl()); assertNotNull(receipt.getMdAttach().getName()); - assertEquals(DEBTOR_DOCUMENT_NAME, receipt.getMdAttach().getName()); - assertEquals(DEBTOR_DOCUMENT_URL, receipt.getMdAttach().getUrl()); + assertEquals(RECEIPT_METADATA_ORIGINAL_NAME, receipt.getMdAttach().getName()); + assertEquals(RECEIPT_METADATA_ORIGINAL_URL, receipt.getMdAttach().getUrl()); assertNotNull(receipt.getMdAttachPayer()); assertNotNull(receipt.getMdAttachPayer().getUrl()); assertNotNull(receipt.getMdAttachPayer().getName()); @@ -579,36 +456,24 @@ void verifyDifferentDebtorPayerFailDebtorGenerationInError() throws ReceiptGener .errorMessage(ERROR_MESSAGE) .build()) .payerMetadata(PdfMetadata.builder() - .statusCode(HttpStatus.SC_OK) + .statusCode(SC_OK) .documentName(PAYER_DOCUMENT_NAME) .documentUrl(PAYER_DOCUMENT_URL) .build()) .generateOnlyDebtor(false) .build(); - boolean result = sut.verifyAndUpdateReceipt(receipt, pdfGeneration); + assertThrows(ReceiptGenerationNotToRetryException.class, () -> sut.verifyAndUpdateReceipt(receipt, pdfGeneration)); - assertFalse(result); - assertNull(receipt.getMdAttach()); - assertNotNull(receipt.getReasonErr()); - assertNotNull(receipt.getReasonErr().getMessage()); - assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, receipt.getReasonErr().getCode()); - assertEquals(ERROR_MESSAGE, receipt.getReasonErr().getMessage()); - assertNotNull(receipt.getMdAttachPayer()); - assertNotNull(receipt.getMdAttachPayer().getUrl()); - assertNotNull(receipt.getMdAttachPayer().getName()); - assertEquals(PAYER_DOCUMENT_NAME, receipt.getMdAttachPayer().getName()); - assertEquals(PAYER_DOCUMENT_URL, receipt.getMdAttachPayer().getUrl()); - assertNull(receipt.getReasonErrPayer()); } @Test void verifyDifferentDebtorPayerFailPayerGenerationInError() throws ReceiptGenerationNotToRetryException { - Receipt receipt = buildReceiptForVerify(false, false); + Receipt receipt = buildReceiptForVerify(true, true); PdfGeneration pdfGeneration = PdfGeneration.builder() .debtorMetadata(PdfMetadata.builder() - .statusCode(HttpStatus.SC_OK) + .statusCode(SC_OK) .documentName(DEBTOR_DOCUMENT_NAME) .documentUrl(DEBTOR_DOCUMENT_URL) .build()) @@ -619,25 +484,13 @@ void verifyDifferentDebtorPayerFailPayerGenerationInError() throws ReceiptGenera .generateOnlyDebtor(false) .build(); - boolean result = sut.verifyAndUpdateReceipt(receipt, pdfGeneration); + assertThrows(ReceiptGenerationNotToRetryException.class, () -> sut.verifyAndUpdateReceipt(receipt, pdfGeneration)); - assertFalse(result); - assertNotNull(receipt.getMdAttach()); - assertNotNull(receipt.getMdAttach().getUrl()); - assertNotNull(receipt.getMdAttach().getName()); - assertEquals(DEBTOR_DOCUMENT_NAME, receipt.getMdAttach().getName()); - assertEquals(DEBTOR_DOCUMENT_URL, receipt.getMdAttach().getUrl()); - assertNull(receipt.getMdAttachPayer()); - assertNull(receipt.getReasonErr()); - assertNotNull(receipt.getReasonErrPayer()); - assertNotNull(receipt.getReasonErrPayer().getMessage()); - assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, receipt.getReasonErrPayer().getCode()); - assertEquals(ERROR_MESSAGE, receipt.getReasonErrPayer().getMessage()); } @Test void verifyDifferentDebtorPayerFailGenerationInErrorForBoth() throws ReceiptGenerationNotToRetryException { - Receipt receipt = buildReceiptForVerify(false, false); + Receipt receipt = buildReceiptForVerify(true, true); String errorMessagePayer = "error message payer"; PdfGeneration pdfGeneration = PdfGeneration.builder() @@ -652,30 +505,19 @@ void verifyDifferentDebtorPayerFailGenerationInErrorForBoth() throws ReceiptGene .generateOnlyDebtor(false) .build(); - boolean result = sut.verifyAndUpdateReceipt(receipt, pdfGeneration); + assertThrows(ReceiptGenerationNotToRetryException.class, () -> sut.verifyAndUpdateReceipt(receipt, pdfGeneration)); - assertFalse(result); - assertNull(receipt.getMdAttach()); - assertNull(receipt.getMdAttachPayer()); - assertNotNull(receipt.getReasonErr()); - assertNotNull(receipt.getReasonErr().getMessage()); - assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, receipt.getReasonErr().getCode()); - assertEquals(ERROR_MESSAGE, receipt.getReasonErr().getMessage()); - assertNotNull(receipt.getReasonErrPayer()); - assertNotNull(receipt.getReasonErrPayer().getMessage()); - assertEquals(HttpStatus.SC_BAD_REQUEST, receipt.getReasonErrPayer().getCode()); - assertEquals(errorMessagePayer, receipt.getReasonErrPayer().getMessage()); } @Test void verifyDifferentDebtorPayerFailPayerReceiptMetadataNull() throws ReceiptGenerationNotToRetryException { - Receipt receipt = buildReceiptForVerify(false, false); + Receipt receipt = buildReceiptForVerify(true, true); PdfGeneration pdfGeneration = PdfGeneration.builder() .debtorMetadata(PdfMetadata.builder() - .statusCode(HttpStatus.SC_OK) - .documentName(DEBTOR_DOCUMENT_NAME) - .documentUrl(DEBTOR_DOCUMENT_URL) + .statusCode(SC_OK) + .documentName(RECEIPT_METADATA_ORIGINAL_NAME) + .documentUrl(RECEIPT_METADATA_ORIGINAL_URL) .build()) .generateOnlyDebtor(false) .build(); @@ -686,16 +528,13 @@ void verifyDifferentDebtorPayerFailPayerReceiptMetadataNull() throws ReceiptGene assertNotNull(receipt.getMdAttach()); assertNotNull(receipt.getMdAttach().getUrl()); assertNotNull(receipt.getMdAttach().getName()); - assertEquals(DEBTOR_DOCUMENT_NAME, receipt.getMdAttach().getName()); - assertEquals(DEBTOR_DOCUMENT_URL, receipt.getMdAttach().getUrl()); - assertNull(receipt.getMdAttachPayer()); - assertNull(receipt.getReasonErr()); - assertNull(receipt.getReasonErrPayer()); + assertEquals(RECEIPT_METADATA_ORIGINAL_NAME, receipt.getMdAttach().getName()); + assertEquals(RECEIPT_METADATA_ORIGINAL_URL, receipt.getMdAttach().getUrl()); } @Test void verifyDifferentDebtorPayerFailThrowsReceiptGenerationNotToRetryException() { - Receipt receipt = buildReceiptForVerify(false, false); + Receipt receipt = buildReceiptForVerify(true, true); String errorMessagePayer = "error message payer"; PdfGeneration pdfGeneration = PdfGeneration.builder() @@ -712,21 +551,11 @@ void verifyDifferentDebtorPayerFailThrowsReceiptGenerationNotToRetryException() assertThrows(ReceiptGenerationNotToRetryException.class, () -> sut.verifyAndUpdateReceipt(receipt, pdfGeneration)); - assertNull(receipt.getMdAttach()); - assertNull(receipt.getMdAttachPayer()); - assertNotNull(receipt.getReasonErr()); - assertNotNull(receipt.getReasonErr().getMessage()); - assertEquals(ReasonErrorCode.ERROR_TEMPLATE_PDF.getCode(), receipt.getReasonErr().getCode()); - assertEquals(ERROR_MESSAGE, receipt.getReasonErr().getMessage()); - assertNotNull(receipt.getReasonErrPayer()); - assertNotNull(receipt.getReasonErrPayer().getMessage()); - assertEquals(HttpStatus.SC_BAD_REQUEST, receipt.getReasonErrPayer().getCode()); - assertEquals(errorMessagePayer, receipt.getReasonErrPayer().getMessage()); } @Test void verifyDifferentDebtorPayerFailBothThrowsReceiptGenerationNotToRetryException() { - Receipt receipt = buildReceiptForVerify(false, false); + Receipt receipt = buildReceiptForVerify(true, true); String errorMessagePayer = "error message payer"; PdfGeneration pdfGeneration = PdfGeneration.builder() @@ -743,16 +572,6 @@ void verifyDifferentDebtorPayerFailBothThrowsReceiptGenerationNotToRetryExceptio assertThrows(ReceiptGenerationNotToRetryException.class, () -> sut.verifyAndUpdateReceipt(receipt, pdfGeneration)); - assertNull(receipt.getMdAttach()); - assertNull(receipt.getMdAttachPayer()); - assertNotNull(receipt.getReasonErr()); - assertNotNull(receipt.getReasonErr().getMessage()); - assertEquals(ReasonErrorCode.ERROR_TEMPLATE_PDF.getCode(), receipt.getReasonErr().getCode()); - assertEquals(ERROR_MESSAGE, receipt.getReasonErr().getMessage()); - assertNotNull(receipt.getReasonErrPayer()); - assertNotNull(receipt.getReasonErrPayer().getMessage()); - assertEquals(ReasonErrorCode.ERROR_TEMPLATE_PDF.getCode(), receipt.getReasonErrPayer().getCode()); - assertEquals(errorMessagePayer, receipt.getReasonErrPayer().getMessage()); } private Receipt buildReceiptForVerify(boolean debtorAlreadyCreated, boolean payerAlreadyCreated) { @@ -780,7 +599,7 @@ private BlobStorageResponse getBlobStorageResponse(int status) { private PdfEngineResponse getPdfEngineResponse(int status, String pdfPath) { PdfEngineResponse pdfEngineResponse = new PdfEngineResponse(); pdfEngineResponse.setTempPdfPath(pdfPath); - if (status != HttpStatus.SC_OK) { + if (status != SC_OK) { pdfEngineResponse.setErrorMessage("error"); } pdfEngineResponse.setStatusCode(status);