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-335] Introduced remittance information check with regex against unwanted strings #66

Merged
merged 2 commits into from
Jan 23, 2024
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 @@ -36,9 +36,11 @@ public class BizEventToReceiptUtils {
private static final String REMITTANCE_INFORMATION_REGEX = "/TXT/(.*)";

private static final List<String> listOrigin;
private static final List<String> 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(
Expand Down Expand Up @@ -327,7 +329,11 @@ public static void tokenizeReceipt(BizEventToReceiptService service, List<BizEve
* @return the remittance information
*/
public static String getItemSubject(BizEvent bizEvent) {
if (bizEvent.getPaymentInfo() != null && bizEvent.getPaymentInfo().getRemittanceInformation() != null) {
if (
bizEvent.getPaymentInfo() != null &&
bizEvent.getPaymentInfo().getRemittanceInformation() != null &&
!listUnwantedRemittanceInfo.contains(bizEvent.getPaymentInfo().getRemittanceInformation())
) {
return bizEvent.getPaymentInfo().getRemittanceInformation();
}
List<Transfer> transferList = bizEvent.getTransferList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -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";
Expand All @@ -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());
}

Expand All @@ -94,17 +132,65 @@ 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());
assertNull(receipt.getEventData());
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();
Expand All @@ -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<Transfer> transferList = List.of(
Transfer.builder()
.amount(TRANSFER_AMOUNT_LOWEST)
Expand Down Expand Up @@ -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<Transfer> 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<Transfer> 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;
}

}
Loading