Skip to content

Commit

Permalink
[VAS-1167] feat: add checks on amount for unamanaged cart (#132)
Browse files Browse the repository at this point in the history
* [VAS-1167] feat: introducing checks on amount to discard legacy cart content

* [VAS-1167] feat: Updated BizEventToReceiptUtils

* [VAS-1167] feat: removed variable blocking int. test

* [VAS-1167] feat: updated method name for cartMod1 validation

* [VAS-1167] feat: updated method name for cartMod1 validation

* [VAS-1167] feat: updated method name for cartMod1 validation
  • Loading branch information
alessio-cialini authored Jul 10, 2024
1 parent 0178a26 commit 5725058
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 3 deletions.
1 change: 0 additions & 1 deletion helm/values-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ microservice-chart:
ECOMMERCE_FILTER_ENABLED: "true"
ENABLE_CART: "true"
AUTHENTICATED_CHANNELS: "IO"
UNWANTED_REMITTANCE_INFO: "pagamento multibeneficiario,pagamento bpay"
envFieldRef:
APP_NAME: "metadata.labels['app.kubernetes.io/instance']"
APP_VERSION: "metadata.labels['app.kubernetes.io/version']"
Expand Down
2 changes: 1 addition & 1 deletion integration-test/src/step_definitions/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ function createEvent(id, transactionId, totalNotice) {
"idTransaction": "123456",
"transactionId": transactionId ? transactionId : "123456",
"grandTotal": 0,
"amount": 0,
"amount": 1000,
"fee": 0
}
},
Expand Down
2 changes: 1 addition & 1 deletion performance-test/src/modules/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export function createEvent(id, customCF) {
"idTransaction": "123456",
"transactionId": "123456",
"grandTotal": 0,
"amount": 0,
"amount": 1000,
"fee": 0
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ public static boolean isBizEventInvalid(BizEvent bizEvent, ExecutionContext cont
return true;
}

if (!isCartMod1(bizEvent)) {
logger.debug("[{}] event with id {} contain either an invalid amount value," +
" or it is a legacy cart element",
context.getFunctionName(), bizEvent.getId());
return true;
}

try {
Receipt receipt = service.getReceipt(bizEvent.getId());
logger.debug("[{}] event with id {} discarded because already processed, receipt already exist with id {}",
Expand Down Expand Up @@ -298,6 +305,22 @@ public static boolean isValidFiscalCode(String fiscalCode) {
return false;
}

/**
* Method to check if the content comes from a legacy cart model (see https://pagopa.atlassian.net/browse/VAS-1167)
* @param bizEvent bizEvent to validate
* @return flag to determine if it is a manageable cart, or otherwise, will return false if
* it is considered a legacy cart content (not having a totalNotice field and having amount values != 0)
*/
public static boolean isCartMod1(BizEvent bizEvent) {
if (bizEvent.getPaymentInfo() != null && bizEvent.getPaymentInfo().getTotalNotice() == null) {
return bizEvent.getTransactionDetails() != null &&
new BigDecimal(bizEvent.getPaymentInfo().getAmount()).subtract(
formatEuroCentAmount(bizEvent.getTransactionDetails().getTransaction().getAmount()))
.intValue() == 0;
}
return true;
}


public static boolean isValidChannelOrigin(BizEvent bizEvent) {
if (bizEvent.getTransactionDetails() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,24 @@ void bizEventNotProcessedCartNotEnabled() throws CartNotFoundException {
verify(cartReceiptsCosmosClient, never()).saveCart(any());
}

@Test
void runDiscardedWithInvalidCartAmounts() {
List<BizEvent> bizEventItems = new ArrayList<>();
BizEvent bizEvent = generateValidBizEvent(null);
bizEvent.getTransactionDetails().getTransaction().setAmount(100);
bizEventItems.add(bizEvent);

@SuppressWarnings("unchecked")
OutputBinding<List<Receipt>> documentdb = (OutputBinding<List<Receipt>>) spy(OutputBinding.class);
BizEventToReceiptServiceImpl receiptService = new BizEventToReceiptServiceImpl(
pdvTokenizerServiceMock, receiptCosmosClient, cartReceiptsCosmosClient, bizEventCosmosClientMock, queueClient);
function = new BizEventToReceipt(receiptService);
// test execution
assertDoesNotThrow(() -> function.processBizEventToReceipt(bizEventItems, documentdb, context));

verify(documentdb, never()).setValue(any());
}

private BizEvent generateValidBizEvent(String totalNotice){
BizEvent item = new BizEvent();

Expand All @@ -520,10 +538,12 @@ private BizEvent generateValidBizEvent(String totalNotice){
Transaction transaction = new Transaction();
transaction.setCreationDate(String.valueOf(LocalDateTime.now()));
transaction.setOrigin(VALID_IO_CHANNEL);
transaction.setAmount(10000);
transactionDetails.setTransaction(transaction);

PaymentInfo paymentInfo = new PaymentInfo();
paymentInfo.setTotalNotice(totalNotice);
paymentInfo.setAmount("100.0");

item.setEventStatus(BizEventStatusType.DONE);
item.setId(EVENT_ID);
Expand All @@ -545,9 +565,11 @@ private BizEvent generateValidBizEventWithTDetails(String totalNotice){
Transaction transaction = new Transaction();
transaction.setCreationDate(String.valueOf(LocalDateTime.now()));
transaction.setOrigin(VALID_IO_CHANNEL);
transaction.setAmount(10000);
transactionDetails.setTransaction(transaction);
transactionDetails.setUser(User.builder().fiscalCode(PAYER_FISCAL_CODE).build());


PaymentInfo paymentInfo = new PaymentInfo();
paymentInfo.setTotalNotice(totalNotice);

Expand All @@ -572,10 +594,12 @@ private BizEvent generateAnonymDebtorBizEvent(String totalNotice){
Transaction transaction = new Transaction();
transaction.setCreationDate(String.valueOf(LocalDateTime.now()));
transaction.setOrigin(VALID_IO_CHANNEL);
transaction.setAmount(10000);
transactionDetails.setTransaction(transaction);

PaymentInfo paymentInfo = new PaymentInfo();
paymentInfo.setTotalNotice(totalNotice);
paymentInfo.setAmount("100");

item.setEventStatus(BizEventStatusType.DONE);
item.setId(EVENT_ID);
Expand Down

0 comments on commit 5725058

Please sign in to comment.