Skip to content

Commit

Permalink
Merge pull request #105 from pagopa/PRDP-292-add-itemSubject-exception
Browse files Browse the repository at this point in the history
[PRDP-292] Added cart.item.subject exception
  • Loading branch information
pasqualespica authored Dec 18, 2023
2 parents ffb92df + 9c175d6 commit c564470
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,7 @@
import it.gov.pagopa.receipt.pdf.generator.entity.receipt.enumeration.ReasonErrorCode;
import it.gov.pagopa.receipt.pdf.generator.exception.PdfJsonMappingException;
import it.gov.pagopa.receipt.pdf.generator.exception.TemplateDataMappingException;
import it.gov.pagopa.receipt.pdf.generator.model.template.Cart;
import it.gov.pagopa.receipt.pdf.generator.model.template.Debtor;
import it.gov.pagopa.receipt.pdf.generator.model.template.Item;
import it.gov.pagopa.receipt.pdf.generator.model.template.PSP;
import it.gov.pagopa.receipt.pdf.generator.model.template.PSPFee;
import it.gov.pagopa.receipt.pdf.generator.model.template.Payee;
import it.gov.pagopa.receipt.pdf.generator.model.template.PaymentMethod;
import it.gov.pagopa.receipt.pdf.generator.model.template.ReceiptPDFTemplate;
import it.gov.pagopa.receipt.pdf.generator.model.template.RefNumber;
import it.gov.pagopa.receipt.pdf.generator.model.template.Transaction;
import it.gov.pagopa.receipt.pdf.generator.model.template.User;
import it.gov.pagopa.receipt.pdf.generator.model.template.UserData;
import it.gov.pagopa.receipt.pdf.generator.model.template.*;
import it.gov.pagopa.receipt.pdf.generator.service.BuildTemplateService;
import it.gov.pagopa.receipt.pdf.generator.utils.ObjectMapperUtils;
import it.gov.pagopa.receipt.pdf.generator.utils.TemplateDataField;
Expand All @@ -31,11 +20,7 @@
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -304,15 +289,15 @@ 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(Receipt receipt) {
private String getItemSubject(Receipt receipt) throws TemplateDataMappingException {
if (receipt.getEventData() != null &&
!receipt.getEventData().getCart().isEmpty() &&
receipt.getEventData().getCart().get(0) != null
receipt.getEventData().getCart().get(0) != null &&
receipt.getEventData().getCart().get(0).getSubject() != null
) {
return receipt.getEventData().getCart().get(0).getSubject();
}

return null;
throw new TemplateDataMappingException(formatErrorMessage(TemplateDataField.CART_ITEM_SUBJECT), ReasonErrorCode.ERROR_TEMPLATE_PDF.getCode());
}

private String getItemAmount(BizEvent event) throws TemplateDataMappingException {
Expand Down Expand Up @@ -367,11 +352,11 @@ private String getOrThrow(LinkedHashMap<String, String> map, String key, String
private boolean getProcessedByPagoPA(BizEvent event) {
if (event.getTransactionDetails() != null) {
if (event.getTransactionDetails().getTransaction() != null &&
event.getTransactionDetails().getTransaction().getOrigin() != null) {
event.getTransactionDetails().getTransaction().getOrigin() != null) {
return true;
}
if (event.getTransactionDetails().getInfo() != null &&
event.getTransactionDetails().getInfo().getClientId() != null) {
event.getTransactionDetails().getInfo().getClientId() != null) {
return true;
}
}
Expand Down Expand Up @@ -450,3 +435,4 @@ private String formatFullName(String fullName, String fiscalCode) {
return fullName.replaceAll("[,;:/]+", " ");
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -1293,7 +1293,8 @@ void mapTemplateLeastAmountOfInfoSuccessPayer() {
.idPA(ID_PA)
.build())
.build();
assertDoesNotThrow(() -> buildTemplateService.buildTemplate(event, GENERATED_BY_PAYER, Receipt.builder().build()));
Receipt receipt = Receipt.builder().eventData(EventData.builder().cart(List.of(CartItem.builder().subject(REMITTANCE_INFORMATION).build())).build()).build();
assertDoesNotThrow(() -> buildTemplateService.buildTemplate(event, GENERATED_BY_PAYER, receipt));
}

@Test
Expand Down Expand Up @@ -1321,7 +1322,8 @@ void mapTemplateLeastAmountOfInfoSuccessDebtor() {
.idPA(ID_PA)
.build())
.build();
assertDoesNotThrow(() -> buildTemplateService.buildTemplate(event, GENERATED_BY_DEBTOR, Receipt.builder().build()));
Receipt receipt = Receipt.builder().eventData(EventData.builder().cart(List.of(CartItem.builder().subject(REMITTANCE_INFORMATION).build())).build()).build();
assertDoesNotThrow(() -> buildTemplateService.buildTemplate(event, GENERATED_BY_DEBTOR, receipt));
}

@Test
Expand Down Expand Up @@ -1356,7 +1358,8 @@ void mapTemplateSuccessRequestByDebtorTrueWithoutPayerAndUserFiscalCode() {
)
.build();
AtomicReference<ReceiptPDFTemplate> atomicReference = new AtomicReference<>();
assertDoesNotThrow(() -> atomicReference.set(buildTemplateService.buildTemplate(event, GENERATED_BY_DEBTOR, Receipt.builder().build())));
Receipt receipt = Receipt.builder().eventData(EventData.builder().cart(List.of(CartItem.builder().subject(REMITTANCE_INFORMATION).build())).build()).build();
assertDoesNotThrow(() -> atomicReference.set(buildTemplateService.buildTemplate(event, GENERATED_BY_DEBTOR, receipt)));

ReceiptPDFTemplate receiptPDFTemplate = atomicReference.get();
assertTrue(receiptPDFTemplate.getTransaction().isRequestedByDebtor());
Expand Down Expand Up @@ -1397,7 +1400,8 @@ void mapTemplateSuccessRequestByDebtorTrueWithPayerFiscalCodeDifferent() {
)
.build();
AtomicReference<ReceiptPDFTemplate> atomicReference = new AtomicReference<>();
assertDoesNotThrow(() -> atomicReference.set(buildTemplateService.buildTemplate(event, GENERATED_BY_DEBTOR, Receipt.builder().build())));
Receipt receipt = Receipt.builder().eventData(EventData.builder().cart(List.of(CartItem.builder().subject(REMITTANCE_INFORMATION).build())).build()).build();
assertDoesNotThrow(() -> atomicReference.set(buildTemplateService.buildTemplate(event, GENERATED_BY_DEBTOR, receipt)));

ReceiptPDFTemplate receiptPDFTemplate = atomicReference.get();
assertTrue(receiptPDFTemplate.getTransaction().isRequestedByDebtor());
Expand Down Expand Up @@ -1436,7 +1440,8 @@ void mapTemplateSuccessRequestByDebtorTrueWithUserFiscalCodeDifferent() {
)
.build();
AtomicReference<ReceiptPDFTemplate> atomicReference = new AtomicReference<>();
assertDoesNotThrow(() -> atomicReference.set(buildTemplateService.buildTemplate(event, GENERATED_BY_DEBTOR, Receipt.builder().build())));
Receipt receipt = Receipt.builder().eventData(EventData.builder().cart(List.of(CartItem.builder().subject(REMITTANCE_INFORMATION).build())).build()).build();
assertDoesNotThrow(() -> atomicReference.set(buildTemplateService.buildTemplate(event, GENERATED_BY_DEBTOR, receipt)));

ReceiptPDFTemplate receiptPDFTemplate = atomicReference.get();
assertTrue(receiptPDFTemplate.getTransaction().isRequestedByDebtor());
Expand Down Expand Up @@ -1479,7 +1484,8 @@ void mapTemplateSuccessRequestByDebtorFalseWithPayerAndUserFiscalCodeEqual() {
)
.build();
AtomicReference<ReceiptPDFTemplate> atomicReference = new AtomicReference<>();
assertDoesNotThrow(() -> atomicReference.set(buildTemplateService.buildTemplate(event, GENERATED_BY_DEBTOR, Receipt.builder().build())));
Receipt receipt = Receipt.builder().eventData(EventData.builder().cart(List.of(CartItem.builder().subject(REMITTANCE_INFORMATION).build())).build()).build();
assertDoesNotThrow(() -> atomicReference.set(buildTemplateService.buildTemplate(event, GENERATED_BY_DEBTOR, receipt)));

ReceiptPDFTemplate receiptPDFTemplate = atomicReference.get();
assertFalse(receiptPDFTemplate.getTransaction().isRequestedByDebtor());
Expand Down Expand Up @@ -1521,7 +1527,8 @@ void mapTemplateSuccessRequestByDebtorFalseWhenGeneratedByPayer() {
)
.build();
AtomicReference<ReceiptPDFTemplate> atomicReference = new AtomicReference<>();
assertDoesNotThrow(() -> atomicReference.set(buildTemplateService.buildTemplate(event, GENERATED_BY_PAYER, Receipt.builder().build())));
Receipt receipt = Receipt.builder().eventData(EventData.builder().cart(List.of(CartItem.builder().subject(REMITTANCE_INFORMATION).build())).build()).build();
assertDoesNotThrow(() -> atomicReference.set(buildTemplateService.buildTemplate(event, GENERATED_BY_PAYER, receipt)));

ReceiptPDFTemplate receiptPDFTemplate = atomicReference.get();
assertFalse(receiptPDFTemplate.getTransaction().isRequestedByDebtor());
Expand Down Expand Up @@ -1566,7 +1573,8 @@ void mapTemplateSuccessWithUserNameAndSurname() {
)
.build();
AtomicReference<ReceiptPDFTemplate> atomicReference = new AtomicReference<>();
assertDoesNotThrow(() -> atomicReference.set(buildTemplateService.buildTemplate(event, GENERATED_BY_PAYER, Receipt.builder().build())));
Receipt receipt = Receipt.builder().eventData(EventData.builder().cart(List.of(CartItem.builder().subject(REMITTANCE_INFORMATION).build())).build()).build();
assertDoesNotThrow(() -> atomicReference.set(buildTemplateService.buildTemplate(event, GENERATED_BY_PAYER, receipt)));

ReceiptPDFTemplate receiptPDFTemplate = atomicReference.get();
assertEquals(USER_FORMATTED_FULL_NAME, receiptPDFTemplate.getUser().getData().getFullName());
Expand Down Expand Up @@ -1612,7 +1620,8 @@ void mapTemplateSuccessWithUserTaxCodeFromTransaction() {
)
.build();
AtomicReference<ReceiptPDFTemplate> atomicReference = new AtomicReference<>();
assertDoesNotThrow(() -> atomicReference.set(buildTemplateService.buildTemplate(event, GENERATED_BY_PAYER, Receipt.builder().build())));
Receipt receipt = Receipt.builder().eventData(EventData.builder().cart(List.of(CartItem.builder().subject(REMITTANCE_INFORMATION).build())).build()).build();
assertDoesNotThrow(() -> atomicReference.set(buildTemplateService.buildTemplate(event, GENERATED_BY_PAYER, receipt)));

ReceiptPDFTemplate receiptPDFTemplate = atomicReference.get();
assertEquals(USER_TAX_CODE, receiptPDFTemplate.getUser().getData().getTaxCode());
Expand Down Expand Up @@ -2071,9 +2080,51 @@ void mapTemplateNoCartItemAmountValueError() {
.build()
)
.build();
TemplateDataMappingException e = assertThrows(TemplateDataMappingException.class, () -> buildTemplateService.buildTemplate(event, GENERATED_BY_PAYER, Receipt.builder().build()));
Receipt receipt = Receipt.builder().eventData(EventData.builder().cart(List.of(CartItem.builder().subject(REMITTANCE_INFORMATION).build())).build()).build();
TemplateDataMappingException e = assertThrows(TemplateDataMappingException.class, () -> buildTemplateService.buildTemplate(event, GENERATED_BY_PAYER, receipt));

assertEquals(ReasonErrorCode.ERROR_TEMPLATE_PDF.getCode(), e.getStatusCode());
assertEquals(String.format(TemplateDataField.ERROR_MAPPING_MESSAGE, TemplateDataField.CART_ITEM_AMOUNT), e.getMessage());
}
}

@Test
void mapTemplateNoCartItemSubjectError() {
BizEvent event = BizEvent.builder()
.id(BIZ_EVENT_ID)
.paymentInfo(PaymentInfo.builder()
.IUR(IUR)
.paymentDateTime(DATE_TIME_TIMESTAMP_MILLISECONDS_DST_WINTER)
.remittanceInformation(REMITTANCE_INFORMATION)
.build())
.psp(Psp.builder()
.idPsp(ID_PSP)
.psp(PSP_NAME)
.build())
.payer(Payer.builder()
.fullName(PAYER_FULL_NAME)
.entityUniqueIdentifierValue(PAYER_VALID_CF)
.build())
.debtorPosition(DebtorPosition.builder()
.modelType(MODEL_TYPE_IUV_CODE)
.iuv(IUV)
.build())
.debtor(Debtor.builder()
.entityUniqueIdentifierValue(DEBTOR_VALID_CF)
.build())
.creditor(Creditor.builder()
.companyName(COMPANY_NAME)
.idPA(ID_PA)
.build())
.transactionDetails(TransactionDetails.builder()
.transaction(Transaction.builder()
.grandTotal(GRAND_TOTAL_LONG).build()
)
.build()
)
.build();
TemplateDataMappingException e = assertThrows(TemplateDataMappingException.class, () -> buildTemplateService.buildTemplate(event, GENERATED_BY_PAYER, Receipt.builder().build()));

assertEquals(ReasonErrorCode.ERROR_TEMPLATE_PDF.getCode(), e.getStatusCode());
assertEquals(String.format(TemplateDataField.ERROR_MAPPING_MESSAGE, TemplateDataField.CART_ITEM_SUBJECT), e.getMessage());
}
}

0 comments on commit c564470

Please sign in to comment.