Skip to content

Commit

Permalink
Merge pull request #75 from pagopa/PRDP-211-regex-full-name
Browse files Browse the repository at this point in the history
[PRDP-211] Implement Regex for invalid names
  • Loading branch information
pasqualespica authored Nov 20, 2023
2 parents e4eb467 + 4de69d2 commit f114a94
Show file tree
Hide file tree
Showing 2 changed files with 298 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class BuildTemplateServiceImpl implements BuildTemplateService {

Expand Down Expand Up @@ -247,7 +252,7 @@ private String getRefNumberValue(BizEvent event) throws TemplateDataMappingExcep
}

private String getDebtorFullName(BizEvent event) {
return event.getDebtor() != null ? event.getDebtor().getFullName() : null;
return event.getDebtor() != null ? formatFullName(event.getDebtor().getFullName(), event.getDebtor().getEntityUniqueIdentifierValue()) : null;
}

private String getDebtorTaxCode(BizEvent event) throws TemplateDataMappingException {
Expand All @@ -268,11 +273,11 @@ private String getPayeeTaxCode(BizEvent event) throws TemplateDataMappingExcepti
throw new TemplateDataMappingException(formatErrorMessage(TemplateDataField.CART_ITEM_PAYEE_TAX_CODE), ReasonErrorCode.ERROR_TEMPLATE_PDF.getCode());
}

private String getItemSubject(BizEvent event) {
private String getItemSubject(BizEvent event) {
if (event.getPaymentInfo() != null && event.getPaymentInfo().getRemittanceInformation() != null) {
return event.getPaymentInfo().getRemittanceInformation();
}

return null;
}

Expand Down Expand Up @@ -325,8 +330,8 @@ private String getOrThrow(LinkedHashMap<String, String> map, String key, String
return value;
}

public boolean getProcessedByPagoPA(BizEvent event){
if(event.getTransactionDetails() != null && event.getTransactionDetails().getWallet() != null){
public boolean getProcessedByPagoPA(BizEvent event) {
if (event.getTransactionDetails() != null && event.getTransactionDetails().getWallet() != null) {
String onboardingChannel = event.getTransactionDetails().getWallet().getOnboardingChannel();
return onboardingChannel != null &&
(onboardingChannel.equals(PAGO_PA_CHANNEL_IO) ||
Expand Down Expand Up @@ -356,4 +361,18 @@ private String dateFormat(String date, boolean withTimeZone) {
private String formatErrorMessage(String missingProperty) {
return String.format(TemplateDataField.ERROR_MAPPING_MESSAGE, missingProperty);
}

private String formatFullName(String fullName, String fiscalCode) {
if (fullName == null || fullName.equals(fiscalCode)) {
return null;
}

Pattern pattern = Pattern.compile("^[\\d\\s\\W_]+$", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(fullName);
if (matcher.find()) {
return null;
}

return fullName.replaceAll("[,;:]+", " ");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class BuildTemplateServiceImplTest {
public static final String COMPANY_NAME = "PA paolo";
public static final String ID_PSP = "ID_PSP";
public static final String DEBTOR_FULL_NAME = "John Doe";
public static final String DEBTOR_FULL_NAME_INVALID = "-- --";
public static final String DEBTOR_FULL_NAME_SPECIAL_CHAR = "John,Doe:Megacorp;SRL::Avenue;";
public static final String DEBTOR_FULL_NAME_SPECIAL_CHAR_FORMATTED = "John Doe Megacorp SRL Avenue ";
public static final String DEBTOR_VALID_CF = "CF_DEBTOR";
public static final String PAYER_FULL_NAME = "John Doe PAYER";
public static final String PAYER_VALID_CF = "CF_PAYER";
Expand Down Expand Up @@ -483,6 +486,276 @@ void mapTemplateWithoutTransactionDetailsAndPaymentTokenSuccess() throws Excepti
assertEquals(IUV, cart.getItems().get(0).getRefNumber().getValue());
}

@Test
void mapTemplateAllFieldsSuccessDebtorFullNameEmpty() throws Exception {
BizEvent event = BizEvent.builder()
.id(BIZ_EVENT_ID)
.debtorPosition(DebtorPosition.builder()
.iuv(IUV)
.modelType(MODEL_TYPE_IUV_CODE)
.build())
.creditor(Creditor.builder()
.companyName(COMPANY_NAME)
.idPA(ID_PA)
.build())
.psp(Psp.builder()
.idPsp(ID_PSP)
.psp(PSP_NAME)
.build())
.debtor(Debtor.builder()
.fullName(DEBTOR_FULL_NAME_INVALID)
.entityUniqueIdentifierValue(DEBTOR_VALID_CF)
.build())
.payer(Payer.builder().fullName(PAYER_FULL_NAME).entityUniqueIdentifierValue(PAYER_VALID_CF).build())
.paymentInfo(PaymentInfo.builder()
.paymentDateTime(DATE_TIME_TIMESTAMP_MILLISECONDS)
.paymentToken(PAYMENT_TOKEN)
.amount(AMOUNT_WITHOUT_CENTS)
.fee(FEE_WITH_SINGLE_DIGIT_CENTS)
.remittanceInformation(REMITTANCE_INFORMATION)
.IUR(IUR)
.build())
.transactionDetails(TransactionDetails.builder()
.wallet(WalletItem.builder()
.info(Info.builder().brand(BRAND).holder(HOLDER_FULL_NAME).build())
.onboardingChannel(PAGO_PA_CHANNEL_IO)
.build())
.transaction(Transaction.builder()
.idTransaction(ID_TRANSACTION)
.grandTotal(GRAND_TOTAL_LONG)
.amount(AMOUNT_LONG)
.fee(FEE_LONG)
.rrn(RRN)
.authorizationCode(AUTH_CODE)
.creationDate(DATE_TIME_TIMESTAMP_ZONED)
.psp(TransactionPsp.builder()
.businessName(PSP_NAME)
.build())
.build())
.build())
.eventStatus(BizEventStatusType.DONE)
.build();
ReceiptPDFTemplate receiptPdfTemplate = buildTemplateService.buildTemplate(event, COMPLETE_TEMPLATE);

assertNotNull(receiptPdfTemplate);

it.gov.pagopa.receipt.pdf.generator.model.template.Transaction transaction = receiptPdfTemplate.getTransaction();
assertEquals(String.valueOf(ID_TRANSACTION), transaction.getId());
assertEquals(DATE_TIME_TIMESTAMP_FORMATTED, transaction.getTimestamp());
assertEquals(FORMATTED_GRAND_TOTAL, transaction.getAmount());
assertEquals(PSP_LOGO, transaction.getPsp().getLogo());
assertEquals(FORMATTED_FEE, transaction.getPsp().getFee().getAmount());
assertEquals(PSP_NAME, transaction.getPsp().getName());
assertEquals(PSP_CITY, transaction.getPsp().getCity());
assertEquals(PSP_COMPANY, transaction.getPsp().getCompanyName());
assertEquals(PSP_POSTAL_CODE, transaction.getPsp().getPostalCode());
assertEquals(PSP_ADDRESS, transaction.getPsp().getAddress());
assertEquals(PSP_BUILDING_NUMBER, transaction.getPsp().getBuildingNumber());
assertEquals(PSP_PROVINCE, transaction.getPsp().getProvince());
assertEquals(RRN, transaction.getRrn());
assertEquals(BRAND, transaction.getPaymentMethod().getName());
assertEquals(BRAND_ASSET_URL, transaction.getPaymentMethod().getLogo());
assertEquals(HOLDER_FULL_NAME, transaction.getPaymentMethod().getAccountHolder());
assertEquals(AUTH_CODE, transaction.getAuthCode());
assertEquals(COMPLETE_TEMPLATE, transaction.isRequestedByDebtor());
assertTrue(transaction.isProcessedByPagoPA());

it.gov.pagopa.receipt.pdf.generator.model.template.UserData userData = receiptPdfTemplate.getUser().getData();
assertEquals(PAYER_VALID_CF, userData.getTaxCode());
assertEquals(PAYER_FULL_NAME, userData.getFullName());

it.gov.pagopa.receipt.pdf.generator.model.template.Cart cart = receiptPdfTemplate.getCart();
assertEquals(FORMATTED_AMOUNT, cart.getAmountPartial());
assertEquals(FORMATTED_AMOUNT, cart.getItems().get(0).getAmount());
assertNull(cart.getItems().get(0).getDebtor().getFullName());
assertEquals(DEBTOR_VALID_CF, cart.getItems().get(0).getDebtor().getTaxCode());
assertEquals(REMITTANCE_INFORMATION, cart.getItems().get(0).getSubject());
assertEquals(COMPANY_NAME, cart.getItems().get(0).getPayee().getName());
assertEquals(ID_PA, cart.getItems().get(0).getPayee().getTaxCode());
assertEquals(MODEL_TYPE_IUV_TEXT, cart.getItems().get(0).getRefNumber().getType());
assertEquals(IUV, cart.getItems().get(0).getRefNumber().getValue());
}

@Test
void mapTemplateAllFieldsSuccessDebtorFullNameWithSpecialChar() throws Exception {
BizEvent event = BizEvent.builder()
.id(BIZ_EVENT_ID)
.debtorPosition(DebtorPosition.builder()
.iuv(IUV)
.modelType(MODEL_TYPE_IUV_CODE)
.build())
.creditor(Creditor.builder()
.companyName(COMPANY_NAME)
.idPA(ID_PA)
.build())
.psp(Psp.builder()
.idPsp(ID_PSP)
.psp(PSP_NAME)
.build())
.debtor(Debtor.builder()
.fullName(DEBTOR_FULL_NAME_SPECIAL_CHAR)
.entityUniqueIdentifierValue(DEBTOR_VALID_CF)
.build())
.payer(Payer.builder().fullName(PAYER_FULL_NAME).entityUniqueIdentifierValue(PAYER_VALID_CF).build())
.paymentInfo(PaymentInfo.builder()
.paymentDateTime(DATE_TIME_TIMESTAMP_MILLISECONDS)
.paymentToken(PAYMENT_TOKEN)
.amount(AMOUNT_WITHOUT_CENTS)
.fee(FEE_WITH_SINGLE_DIGIT_CENTS)
.remittanceInformation(REMITTANCE_INFORMATION)
.IUR(IUR)
.build())
.transactionDetails(TransactionDetails.builder()
.wallet(WalletItem.builder()
.info(Info.builder().brand(BRAND).holder(HOLDER_FULL_NAME).build())
.onboardingChannel(PAGO_PA_CHANNEL_IO)
.build())
.transaction(Transaction.builder()
.idTransaction(ID_TRANSACTION)
.grandTotal(GRAND_TOTAL_LONG)
.amount(AMOUNT_LONG)
.fee(FEE_LONG)
.rrn(RRN)
.authorizationCode(AUTH_CODE)
.creationDate(DATE_TIME_TIMESTAMP_ZONED)
.psp(TransactionPsp.builder()
.businessName(PSP_NAME)
.build())
.build())
.build())
.eventStatus(BizEventStatusType.DONE)
.build();
ReceiptPDFTemplate receiptPdfTemplate = buildTemplateService.buildTemplate(event, COMPLETE_TEMPLATE);

assertNotNull(receiptPdfTemplate);

it.gov.pagopa.receipt.pdf.generator.model.template.Transaction transaction = receiptPdfTemplate.getTransaction();
assertEquals(String.valueOf(ID_TRANSACTION), transaction.getId());
assertEquals(DATE_TIME_TIMESTAMP_FORMATTED, transaction.getTimestamp());
assertEquals(FORMATTED_GRAND_TOTAL, transaction.getAmount());
assertEquals(PSP_LOGO, transaction.getPsp().getLogo());
assertEquals(FORMATTED_FEE, transaction.getPsp().getFee().getAmount());
assertEquals(PSP_NAME, transaction.getPsp().getName());
assertEquals(PSP_CITY, transaction.getPsp().getCity());
assertEquals(PSP_COMPANY, transaction.getPsp().getCompanyName());
assertEquals(PSP_POSTAL_CODE, transaction.getPsp().getPostalCode());
assertEquals(PSP_ADDRESS, transaction.getPsp().getAddress());
assertEquals(PSP_BUILDING_NUMBER, transaction.getPsp().getBuildingNumber());
assertEquals(PSP_PROVINCE, transaction.getPsp().getProvince());
assertEquals(RRN, transaction.getRrn());
assertEquals(BRAND, transaction.getPaymentMethod().getName());
assertEquals(BRAND_ASSET_URL, transaction.getPaymentMethod().getLogo());
assertEquals(HOLDER_FULL_NAME, transaction.getPaymentMethod().getAccountHolder());
assertEquals(AUTH_CODE, transaction.getAuthCode());
assertEquals(COMPLETE_TEMPLATE, transaction.isRequestedByDebtor());
assertTrue(transaction.isProcessedByPagoPA());

it.gov.pagopa.receipt.pdf.generator.model.template.UserData userData = receiptPdfTemplate.getUser().getData();
assertEquals(PAYER_VALID_CF, userData.getTaxCode());
assertEquals(PAYER_FULL_NAME, userData.getFullName());

it.gov.pagopa.receipt.pdf.generator.model.template.Cart cart = receiptPdfTemplate.getCart();
assertEquals(FORMATTED_AMOUNT, cart.getAmountPartial());
assertEquals(FORMATTED_AMOUNT, cart.getItems().get(0).getAmount());
assertEquals(DEBTOR_FULL_NAME_SPECIAL_CHAR_FORMATTED, cart.getItems().get(0).getDebtor().getFullName());
assertEquals(DEBTOR_VALID_CF, cart.getItems().get(0).getDebtor().getTaxCode());
assertEquals(REMITTANCE_INFORMATION, cart.getItems().get(0).getSubject());
assertEquals(COMPANY_NAME, cart.getItems().get(0).getPayee().getName());
assertEquals(ID_PA, cart.getItems().get(0).getPayee().getTaxCode());
assertEquals(MODEL_TYPE_IUV_TEXT, cart.getItems().get(0).getRefNumber().getType());
assertEquals(IUV, cart.getItems().get(0).getRefNumber().getValue());
}

@Test
void mapTemplateAllFieldsSuccessDebtorFullNameEqualsFiscalCode() throws Exception {
BizEvent event = BizEvent.builder()
.id(BIZ_EVENT_ID)
.debtorPosition(DebtorPosition.builder()
.iuv(IUV)
.modelType(MODEL_TYPE_IUV_CODE)
.build())
.creditor(Creditor.builder()
.companyName(COMPANY_NAME)
.idPA(ID_PA)
.build())
.psp(Psp.builder()
.idPsp(ID_PSP)
.psp(PSP_NAME)
.build())
.debtor(Debtor.builder()
.fullName(DEBTOR_VALID_CF)
.entityUniqueIdentifierValue(DEBTOR_VALID_CF)
.build())
.payer(Payer.builder().fullName(PAYER_FULL_NAME).entityUniqueIdentifierValue(PAYER_VALID_CF).build())
.paymentInfo(PaymentInfo.builder()
.paymentDateTime(DATE_TIME_TIMESTAMP_MILLISECONDS)
.paymentToken(PAYMENT_TOKEN)
.amount(AMOUNT_WITHOUT_CENTS)
.fee(FEE_WITH_SINGLE_DIGIT_CENTS)
.remittanceInformation(REMITTANCE_INFORMATION)
.IUR(IUR)
.build())
.transactionDetails(TransactionDetails.builder()
.wallet(WalletItem.builder()
.info(Info.builder().brand(BRAND).holder(HOLDER_FULL_NAME).build())
.onboardingChannel(PAGO_PA_CHANNEL_IO)
.build())
.transaction(Transaction.builder()
.idTransaction(ID_TRANSACTION)
.grandTotal(GRAND_TOTAL_LONG)
.amount(AMOUNT_LONG)
.fee(FEE_LONG)
.rrn(RRN)
.authorizationCode(AUTH_CODE)
.creationDate(DATE_TIME_TIMESTAMP_ZONED)
.psp(TransactionPsp.builder()
.businessName(PSP_NAME)
.build())
.build())
.build())
.eventStatus(BizEventStatusType.DONE)
.build();
ReceiptPDFTemplate receiptPdfTemplate = buildTemplateService.buildTemplate(event, COMPLETE_TEMPLATE);

assertNotNull(receiptPdfTemplate);

it.gov.pagopa.receipt.pdf.generator.model.template.Transaction transaction = receiptPdfTemplate.getTransaction();
assertEquals(String.valueOf(ID_TRANSACTION), transaction.getId());
assertEquals(DATE_TIME_TIMESTAMP_FORMATTED, transaction.getTimestamp());
assertEquals(FORMATTED_GRAND_TOTAL, transaction.getAmount());
assertEquals(PSP_LOGO, transaction.getPsp().getLogo());
assertEquals(FORMATTED_FEE, transaction.getPsp().getFee().getAmount());
assertEquals(PSP_NAME, transaction.getPsp().getName());
assertEquals(PSP_CITY, transaction.getPsp().getCity());
assertEquals(PSP_COMPANY, transaction.getPsp().getCompanyName());
assertEquals(PSP_POSTAL_CODE, transaction.getPsp().getPostalCode());
assertEquals(PSP_ADDRESS, transaction.getPsp().getAddress());
assertEquals(PSP_BUILDING_NUMBER, transaction.getPsp().getBuildingNumber());
assertEquals(PSP_PROVINCE, transaction.getPsp().getProvince());
assertEquals(RRN, transaction.getRrn());
assertEquals(BRAND, transaction.getPaymentMethod().getName());
assertEquals(BRAND_ASSET_URL, transaction.getPaymentMethod().getLogo());
assertEquals(HOLDER_FULL_NAME, transaction.getPaymentMethod().getAccountHolder());
assertEquals(AUTH_CODE, transaction.getAuthCode());
assertEquals(COMPLETE_TEMPLATE, transaction.isRequestedByDebtor());
assertTrue(transaction.isProcessedByPagoPA());

it.gov.pagopa.receipt.pdf.generator.model.template.UserData userData = receiptPdfTemplate.getUser().getData();
assertEquals(PAYER_VALID_CF, userData.getTaxCode());
assertEquals(PAYER_FULL_NAME, userData.getFullName());

it.gov.pagopa.receipt.pdf.generator.model.template.Cart cart = receiptPdfTemplate.getCart();
assertEquals(FORMATTED_AMOUNT, cart.getAmountPartial());
assertEquals(FORMATTED_AMOUNT, cart.getItems().get(0).getAmount());
assertNull(cart.getItems().get(0).getDebtor().getFullName());
assertEquals(DEBTOR_VALID_CF, cart.getItems().get(0).getDebtor().getTaxCode());
assertEquals(REMITTANCE_INFORMATION, cart.getItems().get(0).getSubject());
assertEquals(COMPANY_NAME, cart.getItems().get(0).getPayee().getName());
assertEquals(ID_PA, cart.getItems().get(0).getPayee().getTaxCode());
assertEquals(MODEL_TYPE_IUV_TEXT, cart.getItems().get(0).getRefNumber().getType());
assertEquals(IUV, cart.getItems().get(0).getRefNumber().getValue());
}

@Test
void mapTemplateLeastAmountOfInfoSuccess() {
BizEvent event = BizEvent.builder()
Expand Down

0 comments on commit f114a94

Please sign in to comment.