From 29f0f2edcf630e90c7aaa622f66a95909c34c29b Mon Sep 17 00:00:00 2001 From: acialini Date: Tue, 19 Dec 2023 14:36:52 +0100 Subject: [PATCH 1/3] [PRDP-298] feat: relax checks on anonymous debtor --- .../pdf/generator/GenerateReceiptPdf.java | 2 +- .../impl/GenerateReceiptPdfServiceImpl.java | 24 +++++++------- .../GenerateReceiptPdfServiceImplTest.java | 32 +++++++++++++++++++ 3 files changed, 46 insertions(+), 12 deletions(-) diff --git a/src/main/java/it/gov/pagopa/receipt/pdf/generator/GenerateReceiptPdf.java b/src/main/java/it/gov/pagopa/receipt/pdf/generator/GenerateReceiptPdf.java index 7fecc7e1..ede72d10 100644 --- a/src/main/java/it/gov/pagopa/receipt/pdf/generator/GenerateReceiptPdf.java +++ b/src/main/java/it/gov/pagopa/receipt/pdf/generator/GenerateReceiptPdf.java @@ -136,7 +136,7 @@ public void processGenerateReceipt( } String debtorCF = receipt.getEventData().getDebtorFiscalCode(); - if (debtorCF == null) { + if (debtorCF == null && receipt.getEventData().getPayerFiscalCode() == null) { String errorMessage = String.format( "Error processing receipt with id %s : debtor's fiscal code is null", receipt.getEventId() diff --git a/src/main/java/it/gov/pagopa/receipt/pdf/generator/service/impl/GenerateReceiptPdfServiceImpl.java b/src/main/java/it/gov/pagopa/receipt/pdf/generator/service/impl/GenerateReceiptPdfServiceImpl.java index 68c8a1c4..bdc96414 100644 --- a/src/main/java/it/gov/pagopa/receipt/pdf/generator/service/impl/GenerateReceiptPdfServiceImpl.java +++ b/src/main/java/it/gov/pagopa/receipt/pdf/generator/service/impl/GenerateReceiptPdfServiceImpl.java @@ -99,7 +99,7 @@ public PdfGeneration generateReceipts(Receipt receipt, BizEvent bizEvent, Path w //Generate debtor's partial PDF if (receiptAlreadyCreated(receipt.getMdAttach())) { pdfGeneration.setDebtorMetadata(PdfMetadata.builder().statusCode(ALREADY_CREATED).build()); - } else { + } else if (!"ANONIMO".equals(debtorCF)) { PdfMetadata generationResult = generateAndSavePDFReceipt(bizEvent, receipt, DEBTOR_TEMPLATE_SUFFIX, true, workingDirPath); pdfGeneration.setDebtorMetadata(generationResult); } @@ -119,16 +119,18 @@ 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 (receipt.getEventData() != null && !"ANONIMO".equals(receipt.getEventData().getDebtorFiscalCode())) { + 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()) { diff --git a/src/test/java/it/gov/pagopa/receipt/pdf/generator/service/impl/GenerateReceiptPdfServiceImplTest.java b/src/test/java/it/gov/pagopa/receipt/pdf/generator/service/impl/GenerateReceiptPdfServiceImplTest.java index 9911a3aa..7e4a6325 100644 --- a/src/test/java/it/gov/pagopa/receipt/pdf/generator/service/impl/GenerateReceiptPdfServiceImplTest.java +++ b/src/test/java/it/gov/pagopa/receipt/pdf/generator/service/impl/GenerateReceiptPdfServiceImplTest.java @@ -199,6 +199,37 @@ void generateReceiptsDifferentDebtorPayerWithSuccess() throws Exception { verify(receiptBlobClientMock, times(2)).savePdfToBlobStorage(any(), anyString()); } + @Test + void generateReceiptsDifferentDebtorPayerWithSuccessOnDebtAnonym() throws Exception { + Receipt receiptOnly = getReceiptWithDebtorPayer(VALID_CF_PAYER, false, false); + BizEvent bizEventOnly = getBizEventWithDebtorPayer(VALID_CF_PAYER); + + receiptOnly.getEventData().setDebtorFiscalCode("ANONIMO"); + + doReturn(getPdfEngineResponse(HttpStatus.SC_OK, outputPdfDebtor.getPath()), + getPdfEngineResponse(HttpStatus.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())) + .when(receiptBlobClientMock).savePdfToBlobStorage(any(), anyString()); + doReturn(new ReceiptPDFTemplate()) + .when(buildTemplateServiceMock).buildTemplate(any(), anyBoolean(), any(Receipt.class)); + + PdfGeneration pdfGeneration = sut.generateReceipts(receiptOnly, bizEventOnly,Path.of("/tmp")); + + assertNotNull(pdfGeneration); + assertFalse(pdfGeneration.isGenerateOnlyDebtor()); + assertNotNull(pdfGeneration.getPayerMetadata()); + assertNull(pdfGeneration.getPayerMetadata().getErrorMessage()); + assertNotNull(pdfGeneration.getPayerMetadata().getDocumentName()); + assertNotNull(pdfGeneration.getPayerMetadata().getDocumentUrl()); + assertEquals(HttpStatus.SC_OK, pdfGeneration.getPayerMetadata().getStatusCode()); + + verify(buildTemplateServiceMock, times(1)).buildTemplate(any(), anyBoolean(), any(Receipt.class)); + verify(pdfEngineClientMock, times(1)).generatePDF(any(), any()); + verify(receiptBlobClientMock, times(1)).savePdfToBlobStorage(any(), anyString()); + } + @Test void generateReceiptsPayerNullReceiptAlreadyCreatedWithSuccess() throws TemplateDataMappingException { Receipt receiptOnly = getReceiptWithOnlyDebtor(true); @@ -784,6 +815,7 @@ private Receipt buildReceiptForVerify(boolean debtorAlreadyCreated, boolean paye .id("id") .mdAttach(buildMetadata(debtorAlreadyCreated)) .mdAttachPayer(buildMetadata(payerAlreadyCreated)) + .eventData(EventData.builder().debtorFiscalCode("DEBTOR").build()) .numRetry(0) .generated_at(1L) .inserted_at(1L) From a029f63296d0fce39e45ea2c9e6981184ac9c012 Mon Sep 17 00:00:00 2001 From: acialini Date: Tue, 19 Dec 2023 14:52:52 +0100 Subject: [PATCH 2/3] [PRDP-298] feat: Updated BuildTemplateServiceImpl --- .../pdf/generator/service/impl/BuildTemplateServiceImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/it/gov/pagopa/receipt/pdf/generator/service/impl/BuildTemplateServiceImpl.java b/src/main/java/it/gov/pagopa/receipt/pdf/generator/service/impl/BuildTemplateServiceImpl.java index b7459492..9cb24b85 100644 --- a/src/main/java/it/gov/pagopa/receipt/pdf/generator/service/impl/BuildTemplateServiceImpl.java +++ b/src/main/java/it/gov/pagopa/receipt/pdf/generator/service/impl/BuildTemplateServiceImpl.java @@ -89,7 +89,7 @@ public ReceiptPDFTemplate buildTemplate(BizEvent bizEvent, boolean isGeneratingD .requestedByDebtor(requestedByDebtor) .processedByPagoPA(getProcessedByPagoPA(bizEvent)) .build()) - .user(requestedByDebtor ? + .user(requestedByDebtor || "ANONIMO".equals(receipt.getEventData().getDebtorFiscalCode()) ? null : User.builder() .data(UserData.builder() From 7fbddaf8b21fed368388372b635b82f1a104fdcc Mon Sep 17 00:00:00 2001 From: acialini Date: Tue, 19 Dec 2023 17:10:05 +0100 Subject: [PATCH 3/3] [PRDP-298] feat: Updated BuildTemplateServiceImpl --- .../service/impl/BuildTemplateServiceImpl.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/main/java/it/gov/pagopa/receipt/pdf/generator/service/impl/BuildTemplateServiceImpl.java b/src/main/java/it/gov/pagopa/receipt/pdf/generator/service/impl/BuildTemplateServiceImpl.java index 9cb24b85..a9bba1c8 100644 --- a/src/main/java/it/gov/pagopa/receipt/pdf/generator/service/impl/BuildTemplateServiceImpl.java +++ b/src/main/java/it/gov/pagopa/receipt/pdf/generator/service/impl/BuildTemplateServiceImpl.java @@ -89,7 +89,7 @@ public ReceiptPDFTemplate buildTemplate(BizEvent bizEvent, boolean isGeneratingD .requestedByDebtor(requestedByDebtor) .processedByPagoPA(getProcessedByPagoPA(bizEvent)) .build()) - .user(requestedByDebtor || "ANONIMO".equals(receipt.getEventData().getDebtorFiscalCode()) ? + .user(requestedByDebtor ? null : User.builder() .data(UserData.builder() @@ -104,10 +104,12 @@ public ReceiptPDFTemplate buildTemplate(BizEvent bizEvent, boolean isGeneratingD .type(getRefNumberType(bizEvent)) .value(getRefNumberValue(bizEvent)) .build()) - .debtor(Debtor.builder() - .fullName(getDebtorFullName(bizEvent)) - .taxCode(getDebtorTaxCode(bizEvent)) - .build()) + .debtor("ANONIMO".equals(receipt.getEventData().getDebtorFiscalCode()) ? + null : + Debtor.builder() + .fullName(getDebtorFullName(bizEvent)) + .taxCode(getDebtorTaxCode(bizEvent)) + .build()) .payee(Payee.builder() .name(getPayeeName(bizEvent)) .taxCode(getPayeeTaxCode(bizEvent))