diff --git a/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/utils/BizEventToReceiptUtils.java b/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/utils/BizEventToReceiptUtils.java index b969392..d9411c5 100644 --- a/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/utils/BizEventToReceiptUtils.java +++ b/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/utils/BizEventToReceiptUtils.java @@ -36,9 +36,11 @@ public class BizEventToReceiptUtils { private static final String REMITTANCE_INFORMATION_REGEX = "/TXT/(.*)"; private static final List listOrigin; + private static final List listUnwantedRemittanceInfo; static { listOrigin = Arrays.asList(System.getenv().getOrDefault("LIST_VALID_ORIGINS", "IO").split(",")); + listUnwantedRemittanceInfo = Arrays.asList(System.getenv().getOrDefault("UNWANTED_REMITTANCE_INFO", "pagamento multibeneficiario").split(",")); } public static Receipt getEvent( @@ -327,7 +329,11 @@ public static void tokenizeReceipt(BizEventToReceiptService service, List transferList = bizEvent.getTransferList(); diff --git a/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/utils/BizEventToReceiptUtilsTest.java b/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/utils/BizEventToReceiptUtilsTest.java index 94003d1..1e7a420 100644 --- a/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/utils/BizEventToReceiptUtilsTest.java +++ b/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/utils/BizEventToReceiptUtilsTest.java @@ -2,7 +2,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.microsoft.azure.functions.HttpStatus; -import it.gov.pagopa.receipt.pdf.helpdesk.client.impl.BizEventCosmosClientImpl; +import it.gov.pagopa.receipt.pdf.helpdesk.client.BizEventCosmosClient; import it.gov.pagopa.receipt.pdf.helpdesk.client.impl.ReceiptCosmosClientImpl; import it.gov.pagopa.receipt.pdf.helpdesk.client.impl.ReceiptQueueClientImpl; import it.gov.pagopa.receipt.pdf.helpdesk.entity.event.*; @@ -18,19 +18,23 @@ import org.mockito.junit.jupiter.MockitoExtension; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import uk.org.webcompere.systemstubs.environment.EnvironmentVariables; +import uk.org.webcompere.systemstubs.jupiter.SystemStub; +import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension; import java.time.LocalDateTime; import java.util.List; import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -@ExtendWith(MockitoExtension.class) +@ExtendWith({MockitoExtension.class, SystemStubsExtension.class}) class BizEventToReceiptUtilsTest { + public static final String VALID_IO_CHANNEL = "IO"; + public static final String INVALID_REMITTANCE_INFORMATION = "pagamento multibeneficiario"; private final String EVENT_ID = "a valid id"; - private final String PAYER_FISCAL_CODE = "AAAAAA00A00A000P"; - private final String DEBTOR_FISCAL_CODE = "AAAAAA00A00A000D"; + private final String PAYER_FISCAL_CODE = "AAAAAA00A00A000D"; + private final String DEBTOR_FISCAL_CODE = "AAAAAA00A00A000P"; private final String TOKENIZED_DEBTOR_FISCAL_CODE = "tokenizedDebtorFiscalCode"; private final String TOKENIZED_PAYER_FISCAL_CODE = "tokenizedPayerFiscalCode"; public static final String REMITTANCE_INFORMATION_PAYMENT_INFO = "TARI 2021"; @@ -39,53 +43,87 @@ class BizEventToReceiptUtilsTest { public static final String TRANSFER_AMOUNT_HIGHEST = "10000.00"; public static final String TRANSFER_AMOUNT_MEDIUM = "20.00"; public static final String TRANSFER_AMOUNT_LOWEST = "10.00"; - + public static final String AUTHENTICATED_CHANNELS = "IO,OTHER VALID ORIGIN,ANOTHER VALID"; @Mock private PDVTokenizerServiceRetryWrapper pdvTokenizerServiceMock; + @Mock + private ReceiptCosmosClientImpl receiptCosmosClient; + @Mock + private BizEventCosmosClient bizEventCosmosClientMock; + @Mock + private ReceiptQueueClientImpl queueClient; + @SystemStub + private EnvironmentVariables environmentVariables = new EnvironmentVariables( + "AUTHENTICATED_CHANNELS", AUTHENTICATED_CHANNELS); + private final Logger logger = LoggerFactory.getLogger(BizEventToReceiptUtilsTest.class); @Test void createReceiptSuccessWithPaymentInfo() throws PDVTokenizerException, JsonProcessingException { when(pdvTokenizerServiceMock.generateTokenForFiscalCodeWithRetry(DEBTOR_FISCAL_CODE)).thenReturn(TOKENIZED_DEBTOR_FISCAL_CODE); + when(pdvTokenizerServiceMock.generateTokenForFiscalCodeWithRetry(PAYER_FISCAL_CODE)).thenReturn(TOKENIZED_PAYER_FISCAL_CODE); BizEventToReceiptServiceImpl receiptService = new BizEventToReceiptServiceImpl( - pdvTokenizerServiceMock, mock(ReceiptQueueClientImpl.class), mock(BizEventCosmosClientImpl.class), mock(ReceiptCosmosClientImpl.class)); + pdvTokenizerServiceMock, queueClient, bizEventCosmosClientMock, receiptCosmosClient); - Receipt receipt = BizEventToReceiptUtils.createReceipt(generateValidBizEvent(false,false), receiptService, logger); + Receipt receipt = BizEventToReceiptUtils.createReceipt(generateValidBizEvent(false, false), receiptService, logger); assertEquals(EVENT_ID, receipt.getEventId()); assertNotNull(receipt.getId()); assertEquals(TOKENIZED_DEBTOR_FISCAL_CODE, receipt.getEventData().getDebtorFiscalCode()); + assertEquals(TOKENIZED_PAYER_FISCAL_CODE, receipt.getEventData().getPayerFiscalCode()); assertEquals(REMITTANCE_INFORMATION_PAYMENT_INFO, receipt.getEventData().getCart().get(0).getSubject()); } @Test void createReceiptSuccessWithoutPaymentInfoButWithTransferList() throws PDVTokenizerException, JsonProcessingException { when(pdvTokenizerServiceMock.generateTokenForFiscalCodeWithRetry(DEBTOR_FISCAL_CODE)).thenReturn(TOKENIZED_DEBTOR_FISCAL_CODE); + when(pdvTokenizerServiceMock.generateTokenForFiscalCodeWithRetry(PAYER_FISCAL_CODE)).thenReturn(TOKENIZED_PAYER_FISCAL_CODE); BizEventToReceiptServiceImpl receiptService = new BizEventToReceiptServiceImpl( - pdvTokenizerServiceMock, mock(ReceiptQueueClientImpl.class), mock(BizEventCosmosClientImpl.class), mock(ReceiptCosmosClientImpl.class)); + pdvTokenizerServiceMock, queueClient, bizEventCosmosClientMock, receiptCosmosClient); + + Receipt receipt = BizEventToReceiptUtils.createReceipt(generateValidBizEvent(false, true), receiptService, logger); + + assertEquals(EVENT_ID, receipt.getEventId()); + assertNotNull(receipt.getId()); + assertEquals(TOKENIZED_DEBTOR_FISCAL_CODE, receipt.getEventData().getDebtorFiscalCode()); + assertEquals(TOKENIZED_PAYER_FISCAL_CODE, receipt.getEventData().getPayerFiscalCode()); + assertEquals(REMITTANCE_INFORMATION_TRANSFER_LIST_FORMATTED, receipt.getEventData().getCart().get(0).getSubject()); + } + + @Test + void createReceiptSuccessWithPaymentInfoRemittanceInvalidButWithTransferList() throws PDVTokenizerException, JsonProcessingException { + when(pdvTokenizerServiceMock.generateTokenForFiscalCodeWithRetry(DEBTOR_FISCAL_CODE)).thenReturn(TOKENIZED_DEBTOR_FISCAL_CODE); + when(pdvTokenizerServiceMock.generateTokenForFiscalCodeWithRetry(PAYER_FISCAL_CODE)).thenReturn(TOKENIZED_PAYER_FISCAL_CODE); - Receipt receipt = BizEventToReceiptUtils.createReceipt(generateValidBizEvent(false,true), receiptService, logger); + BizEventToReceiptServiceImpl receiptService = new BizEventToReceiptServiceImpl( + pdvTokenizerServiceMock, queueClient, bizEventCosmosClientMock, receiptCosmosClient); + BizEvent bizEvent = generateValidBizEvent(false, true); + bizEvent.getPaymentInfo().setRemittanceInformation(INVALID_REMITTANCE_INFORMATION); + Receipt receipt = BizEventToReceiptUtils.createReceipt(bizEvent, receiptService, logger); assertEquals(EVENT_ID, receipt.getEventId()); assertNotNull(receipt.getId()); assertEquals(TOKENIZED_DEBTOR_FISCAL_CODE, receipt.getEventData().getDebtorFiscalCode()); + assertEquals(TOKENIZED_PAYER_FISCAL_CODE, receipt.getEventData().getPayerFiscalCode()); assertEquals(REMITTANCE_INFORMATION_TRANSFER_LIST_FORMATTED, receipt.getEventData().getCart().get(0).getSubject()); } @Test void createReceiptSuccessWithoutRemittanceInformation() throws PDVTokenizerException, JsonProcessingException { when(pdvTokenizerServiceMock.generateTokenForFiscalCodeWithRetry(DEBTOR_FISCAL_CODE)).thenReturn(TOKENIZED_DEBTOR_FISCAL_CODE); + when(pdvTokenizerServiceMock.generateTokenForFiscalCodeWithRetry(PAYER_FISCAL_CODE)).thenReturn(TOKENIZED_PAYER_FISCAL_CODE); BizEventToReceiptServiceImpl receiptService = new BizEventToReceiptServiceImpl( - pdvTokenizerServiceMock, mock(ReceiptQueueClientImpl.class), mock(BizEventCosmosClientImpl.class), mock(ReceiptCosmosClientImpl.class)); + pdvTokenizerServiceMock, queueClient, bizEventCosmosClientMock, receiptCosmosClient); - Receipt receipt = BizEventToReceiptUtils.createReceipt(generateValidBizEvent(true,false), receiptService, logger); + Receipt receipt = BizEventToReceiptUtils.createReceipt(generateValidBizEvent(true, false), receiptService, logger); assertEquals(EVENT_ID, receipt.getEventId()); assertNotNull(receipt.getId()); assertEquals(TOKENIZED_DEBTOR_FISCAL_CODE, receipt.getEventData().getDebtorFiscalCode()); + assertEquals(TOKENIZED_PAYER_FISCAL_CODE, receipt.getEventData().getPayerFiscalCode()); assertNull(receipt.getEventData().getCart().get(0).getSubject()); } @@ -94,9 +132,9 @@ void createReceiptSuccessWithTokenizerFailed() throws PDVTokenizerException, Jso when(pdvTokenizerServiceMock.generateTokenForFiscalCodeWithRetry(DEBTOR_FISCAL_CODE)).thenThrow(new PDVTokenizerException("exception", HttpStatus.I_AM_A_TEAPOT.value())); BizEventToReceiptServiceImpl receiptService = new BizEventToReceiptServiceImpl( - pdvTokenizerServiceMock, mock(ReceiptQueueClientImpl.class), mock(BizEventCosmosClientImpl.class), mock(ReceiptCosmosClientImpl.class)); + pdvTokenizerServiceMock, queueClient, bizEventCosmosClientMock, receiptCosmosClient); - Receipt receipt = BizEventToReceiptUtils.createReceipt(generateValidBizEvent(false,false), receiptService, logger); + Receipt receipt = BizEventToReceiptUtils.createReceipt(generateValidBizEvent(false, false), receiptService, logger); assertEquals(EVENT_ID, receipt.getEventId()); assertNotNull(receipt.getId()); @@ -104,7 +142,55 @@ void createReceiptSuccessWithTokenizerFailed() throws PDVTokenizerException, Jso assertEquals(ReceiptStatusType.FAILED, receipt.getStatus()); } - private BizEvent generateValidBizEvent( boolean withoutRemittanceInformation, boolean withTransferList){ + @Test + void createReceiptSuccessWithChannelOriginInTransactionInfo() throws PDVTokenizerException, JsonProcessingException { + when(pdvTokenizerServiceMock.generateTokenForFiscalCodeWithRetry(DEBTOR_FISCAL_CODE)).thenReturn(TOKENIZED_DEBTOR_FISCAL_CODE); + when(pdvTokenizerServiceMock.generateTokenForFiscalCodeWithRetry(PAYER_FISCAL_CODE)).thenReturn(TOKENIZED_PAYER_FISCAL_CODE); + + BizEventToReceiptServiceImpl receiptService = new BizEventToReceiptServiceImpl( + pdvTokenizerServiceMock, queueClient, bizEventCosmosClientMock, receiptCosmosClient); + + Receipt receipt = BizEventToReceiptUtils.createReceipt(generateValidBizEventWithChannelOriginInTransactionInfo(), receiptService, logger); + + assertEquals(EVENT_ID, receipt.getEventId()); + assertNotNull(receipt.getId()); + assertEquals(TOKENIZED_DEBTOR_FISCAL_CODE, receipt.getEventData().getDebtorFiscalCode()); + assertEquals(TOKENIZED_PAYER_FISCAL_CODE, receipt.getEventData().getPayerFiscalCode()); + } + + @Test + void payerNotGeneratedWithoutChannelOrigin() throws PDVTokenizerException, JsonProcessingException { + when(pdvTokenizerServiceMock.generateTokenForFiscalCodeWithRetry(DEBTOR_FISCAL_CODE)).thenReturn(TOKENIZED_DEBTOR_FISCAL_CODE); + + BizEventToReceiptServiceImpl receiptService = new BizEventToReceiptServiceImpl( + pdvTokenizerServiceMock, queueClient, bizEventCosmosClientMock, receiptCosmosClient); + + Receipt receipt = BizEventToReceiptUtils.createReceipt(generateValidBizEventWithoutChannelOrigin(), receiptService, logger); + + assertEquals(EVENT_ID, receipt.getEventId()); + assertNotNull(receipt.getId()); + assertEquals(TOKENIZED_DEBTOR_FISCAL_CODE, receipt.getEventData().getDebtorFiscalCode()); + assertNull(receipt.getEventData().getPayerFiscalCode()); + } + + @Test + void payerNotGeneratedWithInvalidChannelOrigin() throws PDVTokenizerException, JsonProcessingException { + environmentVariables.set("AUTHENTICATED_CHANNELS", "DIFFERENT ORIGIN"); + when(pdvTokenizerServiceMock.generateTokenForFiscalCodeWithRetry(DEBTOR_FISCAL_CODE)).thenReturn(TOKENIZED_DEBTOR_FISCAL_CODE); + + BizEventToReceiptServiceImpl receiptService = new BizEventToReceiptServiceImpl( + pdvTokenizerServiceMock, queueClient, bizEventCosmosClientMock, receiptCosmosClient); + + Receipt receipt = BizEventToReceiptUtils.createReceipt(generateValidBizEvent(false, false), receiptService, logger); + + assertEquals(EVENT_ID, receipt.getEventId()); + assertNotNull(receipt.getId()); + assertEquals(TOKENIZED_DEBTOR_FISCAL_CODE, receipt.getEventData().getDebtorFiscalCode()); + assertNull(receipt.getEventData().getPayerFiscalCode()); + assertEquals(REMITTANCE_INFORMATION_PAYMENT_INFO, receipt.getEventData().getCart().get(0).getSubject()); + } + + private BizEvent generateValidBizEvent(boolean withoutRemittanceInformation, boolean withTransferList) { BizEvent item = new BizEvent(); Payer payer = new Payer(); @@ -115,12 +201,13 @@ private BizEvent generateValidBizEvent( boolean withoutRemittanceInformation, bo TransactionDetails transactionDetails = new TransactionDetails(); Transaction transaction = new Transaction(); transaction.setCreationDate(String.valueOf(LocalDateTime.now())); + transaction.setOrigin(VALID_IO_CHANNEL); transactionDetails.setTransaction(transaction); PaymentInfo paymentInfo = new PaymentInfo(); paymentInfo.setTotalNotice("1"); - if(!withoutRemittanceInformation){ - if(withTransferList){ + if (!withoutRemittanceInformation) { + if (withTransferList) { List transferList = List.of( Transfer.builder() .amount(TRANSFER_AMOUNT_LOWEST) @@ -149,4 +236,92 @@ private BizEvent generateValidBizEvent( boolean withoutRemittanceInformation, bo return item; } + + private BizEvent generateValidBizEventWithChannelOriginInTransactionInfo() { + BizEvent item = new BizEvent(); + + Payer payer = new Payer(); + payer.setEntityUniqueIdentifierValue(PAYER_FISCAL_CODE); + Debtor debtor = new Debtor(); + debtor.setEntityUniqueIdentifierValue(DEBTOR_FISCAL_CODE); + + TransactionDetails transactionDetails = new TransactionDetails(); + Transaction transaction = new Transaction(); + transaction.setCreationDate(String.valueOf(LocalDateTime.now())); + transactionDetails.setTransaction(transaction); + InfoTransaction infoTransaction = new InfoTransaction(); + infoTransaction.setClientId(VALID_IO_CHANNEL); + transactionDetails.setInfo(infoTransaction); + + PaymentInfo paymentInfo = new PaymentInfo(); + paymentInfo.setTotalNotice("1"); + + List transferList = List.of( + Transfer.builder() + .amount(TRANSFER_AMOUNT_LOWEST) + .remittanceInformation("not to show") + .build(), + Transfer.builder() + .amount(TRANSFER_AMOUNT_MEDIUM) + .remittanceInformation("not to show") + .build(), + Transfer.builder() + .amount(TRANSFER_AMOUNT_HIGHEST) + .remittanceInformation(REMITTANCE_INFORMATION_TRANSFER_LIST) + .build() + ); + item.setTransferList(transferList); + + item.setEventStatus(BizEventStatusType.DONE); + item.setId(EVENT_ID); + item.setPayer(payer); + item.setDebtor(debtor); + item.setTransactionDetails(transactionDetails); + item.setPaymentInfo(paymentInfo); + + return item; + } + + private BizEvent generateValidBizEventWithoutChannelOrigin() { + BizEvent item = new BizEvent(); + + Payer payer = new Payer(); + payer.setEntityUniqueIdentifierValue(PAYER_FISCAL_CODE); + Debtor debtor = new Debtor(); + debtor.setEntityUniqueIdentifierValue(DEBTOR_FISCAL_CODE); + + TransactionDetails transactionDetails = new TransactionDetails(); + Transaction transaction = new Transaction(); + transaction.setCreationDate(String.valueOf(LocalDateTime.now())); + transactionDetails.setTransaction(transaction); + + PaymentInfo paymentInfo = new PaymentInfo(); + paymentInfo.setTotalNotice("1"); + + List transferList = List.of( + Transfer.builder() + .amount(TRANSFER_AMOUNT_LOWEST) + .remittanceInformation("not to show") + .build(), + Transfer.builder() + .amount(TRANSFER_AMOUNT_MEDIUM) + .remittanceInformation("not to show") + .build(), + Transfer.builder() + .amount(TRANSFER_AMOUNT_HIGHEST) + .remittanceInformation(REMITTANCE_INFORMATION_TRANSFER_LIST) + .build() + ); + item.setTransferList(transferList); + + item.setEventStatus(BizEventStatusType.DONE); + item.setId(EVENT_ID); + item.setPayer(payer); + item.setDebtor(debtor); + item.setTransactionDetails(transactionDetails); + item.setPaymentInfo(paymentInfo); + + return item; + } + } \ No newline at end of file