Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PRDP-180] feat: Avoid to retry receipt generation in case of missing property #59

Merged
merged 1 commit into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import it.gov.pagopa.receipt.pdf.generator.entity.receipt.Receipt;
import it.gov.pagopa.receipt.pdf.generator.entity.receipt.enumeration.ReceiptStatusType;
import it.gov.pagopa.receipt.pdf.generator.exception.BizEventNotValidException;
import it.gov.pagopa.receipt.pdf.generator.exception.ReceiptGenerationNotToRetryException;
import it.gov.pagopa.receipt.pdf.generator.exception.ReceiptNotFoundException;
import it.gov.pagopa.receipt.pdf.generator.model.PdfGeneration;
import it.gov.pagopa.receipt.pdf.generator.service.GenerateReceiptPdfService;
Expand Down Expand Up @@ -163,29 +164,38 @@ public void processGenerateReceipt(
}

//Verify PDF generation success
boolean success = generateReceiptPdfService.verifyAndUpdateReceipt(receipt, pdfGeneration);
if (success) {
receipt.setStatus(ReceiptStatusType.GENERATED);
receipt.setGenerated_at(System.currentTimeMillis());
logger.info("[{}] Receipt with id {} being saved with status {}",
context.getFunctionName(),
receipt.getEventId(),
receipt.getStatus());
} else {
ReceiptStatusType receiptStatusType;
//Verify if the max number of retry have been passed
if (receipt.getNumRetry() > MAX_NUMBER_RETRY) {
receiptStatusType = ReceiptStatusType.FAILED;
boolean success;
try {
success = generateReceiptPdfService.verifyAndUpdateReceipt(receipt, pdfGeneration);
if (success) {
receipt.setStatus(ReceiptStatusType.GENERATED);
receipt.setGenerated_at(System.currentTimeMillis());
logger.info("[{}] Receipt with id {} being saved with status {}",
context.getFunctionName(),
receipt.getEventId(),
receipt.getStatus());
} else {
receiptStatusType = ReceiptStatusType.RETRY;
receipt.setNumRetry(receipt.getNumRetry() + 1);
requeueMessage.setValue(bizEventMessage);
ReceiptStatusType receiptStatusType;
//Verify if the max number of retry have been passed
if (receipt.getNumRetry() > MAX_NUMBER_RETRY) {
receiptStatusType = ReceiptStatusType.FAILED;
} else {
receiptStatusType = ReceiptStatusType.RETRY;
receipt.setNumRetry(receipt.getNumRetry() + 1);
requeueMessage.setValue(bizEventMessage);
}
receipt.setStatus(receiptStatusType);
logger.error("[{}] Error generating receipt for Receipt {} will be saved with status {}",
context.getFunctionName(),
receipt.getId(),
receiptStatusType);
}
receipt.setStatus(receiptStatusType);
logger.error("[{}] Error generating receipt for Receipt {} will be saved with status {}",
} catch (ReceiptGenerationNotToRetryException e) {
receipt.setStatus(ReceiptStatusType.FAILED);
logger.error("[{}] PDF Receipt generation for Receipt {} failed. This error will not be retried, the receipt will be saved with status {}",
context.getFunctionName(),
receipt.getId(),
receiptStatusType);
ReceiptStatusType.FAILED, e);
}
documentdb.setValue(receipt);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package it.gov.pagopa.receipt.pdf.generator.exception;

/**
* Thrown in case the PDF Receipt generation fail with an error that is useless to be retried.
* Next generation will produce the same error.
*/
public class ReceiptGenerationNotToRetryException extends Exception {

/**
* Constructs new exception with provided message
*
* @param message Detail message
*/
public ReceiptGenerationNotToRetryException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import it.gov.pagopa.receipt.pdf.generator.entity.event.BizEvent;
import it.gov.pagopa.receipt.pdf.generator.entity.receipt.Receipt;
import it.gov.pagopa.receipt.pdf.generator.exception.ReceiptGenerationNotToRetryException;
import it.gov.pagopa.receipt.pdf.generator.model.PdfGeneration;

import java.nio.file.Path;
Expand All @@ -24,6 +25,7 @@ public interface GenerateReceiptPdfService {
* @param receipt the Receipt that hold the status of the elaboration
* @param pdfGeneration {@link PdfGeneration} object with the result of the PDF generation
* @return true if the process succeeded, otherwise false
* @throws ReceiptGenerationNotToRetryException when the receipt generation fail with an error that will not be retried
*/
boolean verifyAndUpdateReceipt(Receipt receipt, PdfGeneration pdfGeneration);
boolean verifyAndUpdateReceipt(Receipt receipt, PdfGeneration pdfGeneration) throws ReceiptGenerationNotToRetryException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import it.gov.pagopa.receipt.pdf.generator.entity.receipt.ReceiptMetadata;
import it.gov.pagopa.receipt.pdf.generator.entity.receipt.enumeration.ReasonErrorCode;
import it.gov.pagopa.receipt.pdf.generator.exception.GeneratePDFException;
import it.gov.pagopa.receipt.pdf.generator.exception.ReceiptGenerationNotToRetryException;
import it.gov.pagopa.receipt.pdf.generator.exception.PDFReceiptGenerationException;
import it.gov.pagopa.receipt.pdf.generator.exception.SavePDFToBlobException;
import it.gov.pagopa.receipt.pdf.generator.model.PdfGeneration;
Expand Down Expand Up @@ -110,7 +111,7 @@ public PdfGeneration generateReceipts(Receipt receipt, BizEvent bizEvent, Path w
* {@inheritDoc}
*/
@Override
public boolean verifyAndUpdateReceipt(Receipt receipt, PdfGeneration pdfGeneration) {
public boolean verifyAndUpdateReceipt(Receipt receipt, PdfGeneration pdfGeneration) throws ReceiptGenerationNotToRetryException {
PdfMetadata debtorMetadata = pdfGeneration.getDebtorMetadata();
boolean result = true;
if (debtorMetadata == null) {
Expand All @@ -131,6 +132,10 @@ public boolean verifyAndUpdateReceipt(Receipt receipt, PdfGeneration pdfGenerati
}

if (pdfGeneration.isGenerateOnlyDebtor()) {
if (debtorMetadata.getStatusCode() == ReasonErrorCode.ERROR_TEMPLATE_PDF.getCode()) {
String errMsg = String.format("Debtor receipt generation fail with status %s", debtorMetadata.getStatusCode());
throw new ReceiptGenerationNotToRetryException(errMsg);
}
return result;
}

Expand All @@ -151,6 +156,13 @@ public boolean verifyAndUpdateReceipt(Receipt receipt, PdfGeneration pdfGenerati
receipt.setReasonErrPayer(reasonErrorPayer);
result = false;
}

if (debtorMetadata.getStatusCode() == ReasonErrorCode.ERROR_TEMPLATE_PDF.getCode()
|| payerMetadata.getStatusCode() == ReasonErrorCode.ERROR_TEMPLATE_PDF.getCode()) {
String errMsg = String.format("Receipt generation fail for debtor (status: %s) and/or payer (status: %s)",
debtorMetadata.getStatusCode(), payerMetadata.getStatusCode());
throw new ReceiptGenerationNotToRetryException(errMsg);
}
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import it.gov.pagopa.receipt.pdf.generator.entity.receipt.Receipt;
import it.gov.pagopa.receipt.pdf.generator.entity.receipt.enumeration.ReceiptStatusType;
import it.gov.pagopa.receipt.pdf.generator.exception.BizEventNotValidException;
import it.gov.pagopa.receipt.pdf.generator.exception.ReceiptGenerationNotToRetryException;
import it.gov.pagopa.receipt.pdf.generator.exception.ReceiptNotFoundException;
import it.gov.pagopa.receipt.pdf.generator.model.PdfGeneration;
import it.gov.pagopa.receipt.pdf.generator.service.GenerateReceiptPdfService;
Expand All @@ -15,13 +16,21 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.*;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import static uk.org.webcompere.systemstubs.SystemStubs.withEnvironmentVariable;
import static uk.org.webcompere.systemstubs.SystemStubs.withEnvironmentVariables;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;

@ExtendWith(MockitoExtension.class)
class GenerateReceiptPdfTest {
Expand Down Expand Up @@ -361,6 +370,30 @@ void generatePDFFailAndMaxNumRetryReached() {
verify(requeueMessageMock, never()).setValue(any());
}

@Test
@SneakyThrows
void generatePDFFailVerifyThrowsReceiptGenerationNotToRetryException() {
int numRetry = 0;
Receipt receipt = buildReceiptWithStatus(ReceiptStatusType.RETRY, numRetry);

doReturn(receipt).when(receiptCosmosClientMock).getReceiptDocument(anyString());
doReturn(new PdfGeneration()).when(generateReceiptPdfServiceMock).generateReceipts(any(), any(), any());
doThrow(ReceiptGenerationNotToRetryException.class).when(generateReceiptPdfServiceMock).verifyAndUpdateReceipt(any(), any());

sut.processGenerateReceipt(BIZ_EVENT_VALID_MESSAGE, documentReceiptsMock, requeueMessageMock, executionContextMock);

assertEquals(ReceiptStatusType.FAILED, receipt.getStatus());
assertEquals(ORIGINAL_GENERATED_AT, receipt.getGenerated_at());
assertEquals(numRetry, receipt.getNumRetry());
assertNull(receipt.getReasonErr());

verify(receiptCosmosClientMock).getReceiptDocument(anyString());
verify(generateReceiptPdfServiceMock).generateReceipts(any(), any(), any());
verify(generateReceiptPdfServiceMock).verifyAndUpdateReceipt(any(), any());
verify(documentReceiptsMock).setValue(any());
verify(requeueMessageMock, never()).setValue(any());
}

private Receipt buildReceiptWithStatus(ReceiptStatusType receiptStatusType, int numRetry) {
return Receipt.builder()
.eventData(EventData.builder()
Expand Down
Loading
Loading