diff --git a/src/main/java/it/gov/pagopa/arc/dto/mapper/BizEventsCartItem2CartItemDTO.java b/src/main/java/it/gov/pagopa/arc/dto/mapper/BizEventsCartItem2CartItemDTO.java new file mode 100644 index 00000000..f11f5a69 --- /dev/null +++ b/src/main/java/it/gov/pagopa/arc/dto/mapper/BizEventsCartItem2CartItemDTO.java @@ -0,0 +1,26 @@ +package it.gov.pagopa.arc.dto.mapper; + +import it.gov.pagopa.arc.connector.bizevents.dto.BizEventsCartItemDTO; +import it.gov.pagopa.arc.model.generated.CartItemDTO; +import it.gov.pagopa.arc.utils.Utilities; +import org.springframework.stereotype.Component; + +@Component +public class BizEventsCartItem2CartItemDTO { + private final BizEventsUserDetail2UserDetailDTO userDetailsMapper; + + public BizEventsCartItem2CartItemDTO(BizEventsUserDetail2UserDetailDTO userDetailsMapper) { + this.userDetailsMapper = userDetailsMapper; + } + + public CartItemDTO mapCart(BizEventsCartItemDTO bizEventsCartItemDTO){ + return CartItemDTO.builder() + .subject(bizEventsCartItemDTO.getSubject()) + .amount(bizEventsCartItemDTO.getAmount() != null ? Utilities.euroToCents(bizEventsCartItemDTO.getAmount()) : null) + .payee(userDetailsMapper.mapUserDetail(bizEventsCartItemDTO.getPayee())) + .debtor(userDetailsMapper.mapUserDetail(bizEventsCartItemDTO.getDebtor())) + .refNumberType(bizEventsCartItemDTO.getRefNumberType()) + .refNumberValue(bizEventsCartItemDTO.getRefNumberValue()) + .build(); + } +} diff --git a/src/main/java/it/gov/pagopa/arc/dto/mapper/BizEventsInfoTransaction2InfoTransactionDTO.java b/src/main/java/it/gov/pagopa/arc/dto/mapper/BizEventsInfoTransaction2InfoTransactionDTO.java new file mode 100644 index 00000000..f3ceecb0 --- /dev/null +++ b/src/main/java/it/gov/pagopa/arc/dto/mapper/BizEventsInfoTransaction2InfoTransactionDTO.java @@ -0,0 +1,38 @@ +package it.gov.pagopa.arc.dto.mapper; + +import it.gov.pagopa.arc.connector.bizevents.dto.BizEventsInfoTransactionDTO; +import it.gov.pagopa.arc.connector.bizevents.enums.PaymentMethod; +import it.gov.pagopa.arc.model.generated.InfoTransactionDTO; +import it.gov.pagopa.arc.utils.Utilities; +import org.springframework.stereotype.Component; + +@Component +public class BizEventsInfoTransaction2InfoTransactionDTO { + private final BizEventsWalletInfo2WalletInfoDTO walletInfoMapper; + private final BizEventsUserDetail2UserDetailDTO userDetailsMapper; + public BizEventsInfoTransaction2InfoTransactionDTO(BizEventsWalletInfo2WalletInfoDTO walletInfoMapper, BizEventsUserDetail2UserDetailDTO userDetailsMapper) { + this.walletInfoMapper = walletInfoMapper; + this.userDetailsMapper = userDetailsMapper; + } + + public InfoTransactionDTO mapInfoTransaction(BizEventsInfoTransactionDTO bizEventsInfoTransactionDTO){ + PaymentMethod paymentMethod = null; + if(bizEventsInfoTransactionDTO.getPaymentMethod() != null) { + paymentMethod = bizEventsInfoTransactionDTO.getPaymentMethod(); + } + return InfoTransactionDTO.builder() + .transactionId(bizEventsInfoTransactionDTO.getTransactionId()) + .authCode(bizEventsInfoTransactionDTO.getAuthCode()) + .rrn(bizEventsInfoTransactionDTO.getRrn()) + .transactionDate(bizEventsInfoTransactionDTO.getTransactionDate() != null ? Utilities.dateStringToZonedDateTime(bizEventsInfoTransactionDTO.getTransactionDate()) : null) + .pspName(bizEventsInfoTransactionDTO.getPspName()) + .walletInfo(bizEventsInfoTransactionDTO.getBizEventsWalletInfoDTO() != null ? walletInfoMapper.mapWalletInfo(bizEventsInfoTransactionDTO.getBizEventsWalletInfoDTO()) : null ) + .paymentMethod(paymentMethod != null ? PaymentMethodEnumMapper.paymentMethodEnumMap.get(paymentMethod) : null) + .payer(bizEventsInfoTransactionDTO.getPayer() != null ? userDetailsMapper.mapUserDetail(bizEventsInfoTransactionDTO.getPayer()) : null) + .amount(bizEventsInfoTransactionDTO.getAmount() != null ? Utilities.euroToCents(bizEventsInfoTransactionDTO.getAmount()) : null) + .fee(bizEventsInfoTransactionDTO.getFee() != null ? Utilities.euroToCents(bizEventsInfoTransactionDTO.getFee()) : null) + .origin(OriginEnumMapper.originEnumMap.get(bizEventsInfoTransactionDTO.getOrigin())) + .build(); + } + +} diff --git a/src/main/java/it/gov/pagopa/arc/dto/mapper/BizEventsTransactionDTO2TransactionDTO.java b/src/main/java/it/gov/pagopa/arc/dto/mapper/BizEventsTransactionDTO2TransactionDTO.java index 512a07a4..23b68061 100644 --- a/src/main/java/it/gov/pagopa/arc/dto/mapper/BizEventsTransactionDTO2TransactionDTO.java +++ b/src/main/java/it/gov/pagopa/arc/dto/mapper/BizEventsTransactionDTO2TransactionDTO.java @@ -3,8 +3,9 @@ import it.gov.pagopa.arc.connector.bizevents.dto.BizEventsTransactionDTO; import it.gov.pagopa.arc.model.generated.TransactionDTO; import it.gov.pagopa.arc.utils.Utilities; -import org.springframework.stereotype.Service; -@Service +import org.springframework.stereotype.Component; + +@Component public class BizEventsTransactionDTO2TransactionDTO { public TransactionDTO apply(BizEventsTransactionDTO transaction){ return TransactionDTO.builder() diff --git a/src/main/java/it/gov/pagopa/arc/dto/mapper/BizEventsTransactionDetails2TransactionDetailsDTO.java b/src/main/java/it/gov/pagopa/arc/dto/mapper/BizEventsTransactionDetails2TransactionDetailsDTO.java new file mode 100644 index 00000000..2dcd4e1e --- /dev/null +++ b/src/main/java/it/gov/pagopa/arc/dto/mapper/BizEventsTransactionDetails2TransactionDetailsDTO.java @@ -0,0 +1,24 @@ +package it.gov.pagopa.arc.dto.mapper; + +import it.gov.pagopa.arc.connector.bizevents.dto.BizEventsTransactionDetailsDTO; +import it.gov.pagopa.arc.model.generated.CartItemDTO; +import it.gov.pagopa.arc.model.generated.TransactionDetailsDTO; +import org.springframework.stereotype.Component; + +import java.util.List; + +@Component +public class BizEventsTransactionDetails2TransactionDetailsDTO { + private final BizEventsInfoTransaction2InfoTransactionDTO transactionInfoMapper; + + public BizEventsTransactionDetails2TransactionDetailsDTO(BizEventsInfoTransaction2InfoTransactionDTO transactionInfoMapper) { + this.transactionInfoMapper = transactionInfoMapper; + } + + public TransactionDetailsDTO apply(BizEventsTransactionDetailsDTO bizEventsTransactionDetailsDTO, List cartsList){ + return TransactionDetailsDTO.builder() + .infoTransaction(transactionInfoMapper.mapInfoTransaction(bizEventsTransactionDetailsDTO.getBizEventsInfoTransactionDTO())) + .carts(cartsList) + .build(); + } +} diff --git a/src/main/java/it/gov/pagopa/arc/dto/mapper/BizEventsTransactionsListDTO2TransactionsListDTO.java b/src/main/java/it/gov/pagopa/arc/dto/mapper/BizEventsTransactionsListDTO2TransactionsListDTO.java index 54dcd8fe..ea8ecd32 100644 --- a/src/main/java/it/gov/pagopa/arc/dto/mapper/BizEventsTransactionsListDTO2TransactionsListDTO.java +++ b/src/main/java/it/gov/pagopa/arc/dto/mapper/BizEventsTransactionsListDTO2TransactionsListDTO.java @@ -2,11 +2,11 @@ import it.gov.pagopa.arc.model.generated.TransactionDTO; import it.gov.pagopa.arc.model.generated.TransactionsListDTO; -import org.springframework.stereotype.Service; +import org.springframework.stereotype.Component; import java.util.List; -@Service +@Component public class BizEventsTransactionsListDTO2TransactionsListDTO { public TransactionsListDTO apply(List transactionsList, Integer size){ return TransactionsListDTO.builder() diff --git a/src/main/java/it/gov/pagopa/arc/dto/mapper/BizEventsUserDetail2UserDetailDTO.java b/src/main/java/it/gov/pagopa/arc/dto/mapper/BizEventsUserDetail2UserDetailDTO.java new file mode 100644 index 00000000..effa37b7 --- /dev/null +++ b/src/main/java/it/gov/pagopa/arc/dto/mapper/BizEventsUserDetail2UserDetailDTO.java @@ -0,0 +1,16 @@ +package it.gov.pagopa.arc.dto.mapper; + +import it.gov.pagopa.arc.connector.bizevents.dto.BizEventsUserDetailDTO; +import it.gov.pagopa.arc.model.generated.UserDetailDTO; +import org.springframework.stereotype.Component; + +@Component +public class BizEventsUserDetail2UserDetailDTO { + + public UserDetailDTO mapUserDetail(BizEventsUserDetailDTO bizEventsUserDetailDTO){ + return UserDetailDTO.builder() + .name(bizEventsUserDetailDTO.getName()) + .taxCode(bizEventsUserDetailDTO.getTaxCode()) + .build(); + } +} diff --git a/src/main/java/it/gov/pagopa/arc/dto/mapper/BizEventsWalletInfo2WalletInfoDTO.java b/src/main/java/it/gov/pagopa/arc/dto/mapper/BizEventsWalletInfo2WalletInfoDTO.java new file mode 100644 index 00000000..5528d331 --- /dev/null +++ b/src/main/java/it/gov/pagopa/arc/dto/mapper/BizEventsWalletInfo2WalletInfoDTO.java @@ -0,0 +1,18 @@ +package it.gov.pagopa.arc.dto.mapper; + +import it.gov.pagopa.arc.connector.bizevents.dto.BizEventsWalletInfoDTO; +import it.gov.pagopa.arc.model.generated.WalletInfoDTO; +import org.springframework.stereotype.Component; + +@Component +public class BizEventsWalletInfo2WalletInfoDTO { + + public WalletInfoDTO mapWalletInfo(BizEventsWalletInfoDTO bizEventsWalletInfoDTO){ + return WalletInfoDTO.builder() + .accountHolder(bizEventsWalletInfoDTO.getAccountHolder()) + .brand(bizEventsWalletInfoDTO.getBrand()) + .blurredNumber(bizEventsWalletInfoDTO.getBlurredNumber()) + .maskedEmail(bizEventsWalletInfoDTO.getMaskedEmail()) + .build(); + } +} diff --git a/src/main/java/it/gov/pagopa/arc/dto/mapper/OriginEnumMapper.java b/src/main/java/it/gov/pagopa/arc/dto/mapper/OriginEnumMapper.java new file mode 100644 index 00000000..f1b1a19b --- /dev/null +++ b/src/main/java/it/gov/pagopa/arc/dto/mapper/OriginEnumMapper.java @@ -0,0 +1,24 @@ +package it.gov.pagopa.arc.dto.mapper; + +import it.gov.pagopa.arc.connector.bizevents.enums.Origin; +import it.gov.pagopa.arc.model.generated.InfoTransactionDTO; +import org.springframework.stereotype.Component; + +import java.util.EnumMap; +import java.util.Map; + +@Component +public class OriginEnumMapper { + private OriginEnumMapper(){} + protected static final Map originEnumMap; + + static { + originEnumMap = new EnumMap<>(Origin.class); + originEnumMap.put(Origin.INTERNAL, InfoTransactionDTO.OriginEnum.INTERNAL); + originEnumMap.put(Origin.PM, InfoTransactionDTO.OriginEnum.PM); + originEnumMap.put(Origin.NDP001PROD, InfoTransactionDTO.OriginEnum.NDP001PROD); + originEnumMap.put(Origin.NDP002PROD, InfoTransactionDTO.OriginEnum.NDP002PROD); + originEnumMap.put(Origin.NDP003PROD, InfoTransactionDTO.OriginEnum.NDP003PROD); + originEnumMap.put(Origin.UNKNOWN, InfoTransactionDTO.OriginEnum.UNKNOWN); + } +} diff --git a/src/main/java/it/gov/pagopa/arc/dto/mapper/PaymentMethodEnumMapper.java b/src/main/java/it/gov/pagopa/arc/dto/mapper/PaymentMethodEnumMapper.java new file mode 100644 index 00000000..5990cc67 --- /dev/null +++ b/src/main/java/it/gov/pagopa/arc/dto/mapper/PaymentMethodEnumMapper.java @@ -0,0 +1,28 @@ +package it.gov.pagopa.arc.dto.mapper; + +import it.gov.pagopa.arc.connector.bizevents.enums.PaymentMethod; +import it.gov.pagopa.arc.model.generated.InfoTransactionDTO; +import org.springframework.stereotype.Component; + +import java.util.EnumMap; +import java.util.Map; + +@Component +public class PaymentMethodEnumMapper { + private PaymentMethodEnumMapper(){} + protected static final Map paymentMethodEnumMap; + + static { + paymentMethodEnumMap = new EnumMap<>(PaymentMethod.class); + paymentMethodEnumMap.put(PaymentMethod.BBT, InfoTransactionDTO.PaymentMethodEnum.BBT); + paymentMethodEnumMap.put(PaymentMethod.BP, InfoTransactionDTO.PaymentMethodEnum.BP); + paymentMethodEnumMap.put(PaymentMethod.AD, InfoTransactionDTO.PaymentMethodEnum.AD); + paymentMethodEnumMap.put(PaymentMethod.CP, InfoTransactionDTO.PaymentMethodEnum.CP); + paymentMethodEnumMap.put(PaymentMethod.PO, InfoTransactionDTO.PaymentMethodEnum.PO); + paymentMethodEnumMap.put(PaymentMethod.OBEP, InfoTransactionDTO.PaymentMethodEnum.OBEP); + paymentMethodEnumMap.put(PaymentMethod.JIF, InfoTransactionDTO.PaymentMethodEnum.JIF); + paymentMethodEnumMap.put(PaymentMethod.MYBK, InfoTransactionDTO.PaymentMethodEnum.MYBK); + paymentMethodEnumMap.put(PaymentMethod.PPAL, InfoTransactionDTO.PaymentMethodEnum.PPAL); + paymentMethodEnumMap.put(PaymentMethod.UNKNOWN, InfoTransactionDTO.PaymentMethodEnum.UNKNOWN); + } +} diff --git a/src/main/java/it/gov/pagopa/arc/utils/Utilities.java b/src/main/java/it/gov/pagopa/arc/utils/Utilities.java index dfcf0b51..33a77cdc 100644 --- a/src/main/java/it/gov/pagopa/arc/utils/Utilities.java +++ b/src/main/java/it/gov/pagopa/arc/utils/Utilities.java @@ -16,7 +16,7 @@ private Utilities(){} /** To convert euro into cents */ public static Long euroToCents(String euroString){ Double euroDouble = euroStringToDouble(euroString); - return (long) (euroDouble * 100); + return Math.round(euroDouble * 100); } /** To convert euro from String to double */ diff --git a/src/test/java/it/gov/pagopa/arc/dto/mapper/BizEventsCartItem2CartItemDTOTest.java b/src/test/java/it/gov/pagopa/arc/dto/mapper/BizEventsCartItem2CartItemDTOTest.java new file mode 100644 index 00000000..d5f064d8 --- /dev/null +++ b/src/test/java/it/gov/pagopa/arc/dto/mapper/BizEventsCartItem2CartItemDTOTest.java @@ -0,0 +1,66 @@ +package it.gov.pagopa.arc.dto.mapper; + +import it.gov.pagopa.arc.connector.bizevents.dto.BizEventsCartItemDTO; +import it.gov.pagopa.arc.connector.bizevents.dto.BizEventsUserDetailDTO; +import it.gov.pagopa.arc.fakers.CommonUserDetailDTOFaker; +import it.gov.pagopa.arc.fakers.bizEvents.BizEventsCartItemDTOFaker; +import it.gov.pagopa.arc.model.generated.CartItemDTO; +import it.gov.pagopa.arc.model.generated.UserDetailDTO; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.ArgumentMatchers.any; + +@ExtendWith(MockitoExtension.class) +class BizEventsCartItem2CartItemDTOTest { + + private BizEventsCartItem2CartItemDTO cartItemMapper; + + @Mock + private BizEventsUserDetail2UserDetailDTO userDetailsMapperMock; + + @BeforeEach + void setUp() { + cartItemMapper = new BizEventsCartItem2CartItemDTO(userDetailsMapperMock); + } + + @Test + void givenBizEventsCartItemDTOWhenMapCartThenReturnCartItemDTO() { + //given + BizEventsUserDetailDTO payee = CommonUserDetailDTOFaker.mockBizEventsUserDetailDTO(CommonUserDetailDTOFaker.USER_DETAIL_PAYEE); + + BizEventsUserDetailDTO debtor = CommonUserDetailDTOFaker.mockBizEventsUserDetailDTO(CommonUserDetailDTOFaker.USER_DETAIL_DEBTOR); + + UserDetailDTO payeeResponse = UserDetailDTO.builder() + .name("ACI Automobile Club Italia") + .taxCode("00493410583") + .build(); + + UserDetailDTO debtorResponse = UserDetailDTO.builder() + .name("DEBTOR") + .taxCode("TAX_CODE") + .build(); + + Mockito.when(userDetailsMapperMock.mapUserDetail(payee)).thenReturn(payeeResponse); + Mockito.when(userDetailsMapperMock.mapUserDetail(debtor)).thenReturn(debtorResponse); + + BizEventsCartItemDTO cartItemDTO = BizEventsCartItemDTOFaker.mockInstance(payee,debtor); + //when + CartItemDTO result = cartItemMapper.mapCart(cartItemDTO); + //then + assertNotNull(result); + assertEquals("pagamento", result.getSubject()); + assertEquals(545230L, result.getAmount()); + assertEquals(payeeResponse, result.getPayee()); + assertEquals(debtorResponse, result.getDebtor()); + assertEquals("960000000094659945", result.getRefNumberValue()); + assertEquals("IUV", result.getRefNumberType()); + Mockito.verify(userDetailsMapperMock, Mockito.times(2)).mapUserDetail(any()); + } +} \ No newline at end of file diff --git a/src/test/java/it/gov/pagopa/arc/dto/mapper/BizEventsInfoTransaction2InfoTransactionDTOTest.java b/src/test/java/it/gov/pagopa/arc/dto/mapper/BizEventsInfoTransaction2InfoTransactionDTOTest.java new file mode 100644 index 00000000..83d9fc4c --- /dev/null +++ b/src/test/java/it/gov/pagopa/arc/dto/mapper/BizEventsInfoTransaction2InfoTransactionDTOTest.java @@ -0,0 +1,94 @@ +package it.gov.pagopa.arc.dto.mapper; + +import it.gov.pagopa.arc.connector.bizevents.dto.BizEventsInfoTransactionDTO; +import it.gov.pagopa.arc.connector.bizevents.dto.BizEventsUserDetailDTO; +import it.gov.pagopa.arc.connector.bizevents.dto.BizEventsWalletInfoDTO; +import it.gov.pagopa.arc.connector.bizevents.enums.Origin; +import it.gov.pagopa.arc.fakers.CommonUserDetailDTOFaker; +import it.gov.pagopa.arc.fakers.CommonWalletInfoDTOFaker; +import it.gov.pagopa.arc.fakers.bizEvents.BizEventsInfoTransactionDTOFaker; +import it.gov.pagopa.arc.model.generated.InfoTransactionDTO; +import it.gov.pagopa.arc.model.generated.UserDetailDTO; +import it.gov.pagopa.arc.model.generated.WalletInfoDTO; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.time.ZonedDateTime; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.any; +@ExtendWith(MockitoExtension.class) +class BizEventsInfoTransaction2InfoTransactionDTOTest { + private BizEventsInfoTransaction2InfoTransactionDTO transactionInfoMapper; + @Mock + private BizEventsWalletInfo2WalletInfoDTO walletInfoMapperMock; + @Mock + private BizEventsUserDetail2UserDetailDTO userDetailsMapperMock; + + @BeforeEach + void setUp() { + transactionInfoMapper = new BizEventsInfoTransaction2InfoTransactionDTO(walletInfoMapperMock, userDetailsMapperMock); + } + + @Test + void givenBizEventsInfoTransactionDTOWhenMapInfoTransactionThenReturnInfoTransactionDTO() { + //given + BizEventsWalletInfoDTO bizEventsWalletInfo = CommonWalletInfoDTOFaker.mockBizEventsWalletInfoDTO(false); + BizEventsUserDetailDTO payer = CommonUserDetailDTOFaker.mockBizEventsUserDetailDTO(CommonUserDetailDTOFaker.USER_DETAIL_PAYER); + BizEventsInfoTransactionDTO bizEventsInfoTransaction = BizEventsInfoTransactionDTOFaker.mockInstance(bizEventsWalletInfo, payer); + WalletInfoDTO walletInfo = CommonWalletInfoDTOFaker.mockWalletInfoDTO(false); + UserDetailDTO payerMapped = CommonUserDetailDTOFaker.mockUserDetailDTO(CommonUserDetailDTOFaker.USER_DETAIL_PAYER); + + Mockito.when(walletInfoMapperMock.mapWalletInfo(bizEventsWalletInfo)).thenReturn(walletInfo); + Mockito.when(userDetailsMapperMock.mapUserDetail(any())).thenReturn(payerMapped); + + //when + InfoTransactionDTO result = transactionInfoMapper.mapInfoTransaction(bizEventsInfoTransaction); + //then + commonAssert(result); + + assertEquals(walletInfo, result.getWalletInfo()); + assertEquals(InfoTransactionDTO.PaymentMethodEnum.PO, result.getPaymentMethod()); + assertEquals(payerMapped, result.getPayer()); + Mockito.verify(walletInfoMapperMock).mapWalletInfo(any()); + Mockito.verify(userDetailsMapperMock).mapUserDetail(any()); + } + + @Test + void givenBizEventsInfoTransactionDTOWithoutPayerWhenMapInfoTransactionThenReturnInfoTransactionDTO() { + //given + BizEventsInfoTransactionDTO bizEventsInfoTransaction = BizEventsInfoTransactionDTO.builder() + .transactionId("TRANSACTION_ID") + .authCode("250863") + .rrn("51561651") + .transactionDate("2024-06-27T13:07:25Z") + .pspName("Worldline Merchant Services Italia S.p.A.") + .amount("5.654,3") + .fee("0,29") + .origin(Origin.PM) + .build(); + + //when + InfoTransactionDTO result = transactionInfoMapper.mapInfoTransaction(bizEventsInfoTransaction); + + //then + commonAssert(result); + } + + private static void commonAssert(InfoTransactionDTO result) { + assertNotNull(result); + assertEquals("TRANSACTION_ID", result.getTransactionId()); + assertEquals("250863", result.getAuthCode()); + assertEquals("51561651", result.getRrn()); + assertEquals(ZonedDateTime.parse("2024-06-27T13:07:25Z"), result.getTransactionDate()); + assertEquals("Worldline Merchant Services Italia S.p.A.", result.getPspName()); + assertEquals( 565430L, result.getAmount()); + assertEquals( 29L, result.getFee()); + assertEquals(InfoTransactionDTO.OriginEnum.PM, result.getOrigin()); + } + +} \ No newline at end of file diff --git a/src/test/java/it/gov/pagopa/arc/dto/mapper/BizEventsTransactionDTO2TransactionDTOTest.java b/src/test/java/it/gov/pagopa/arc/dto/mapper/BizEventsTransactionDTO2TransactionDTOTest.java index b9fb4077..ada98676 100644 --- a/src/test/java/it/gov/pagopa/arc/dto/mapper/BizEventsTransactionDTO2TransactionDTOTest.java +++ b/src/test/java/it/gov/pagopa/arc/dto/mapper/BizEventsTransactionDTO2TransactionDTOTest.java @@ -1,7 +1,7 @@ package it.gov.pagopa.arc.dto.mapper; import it.gov.pagopa.arc.connector.bizevents.dto.BizEventsTransactionDTO; -import it.gov.pagopa.arc.fakers.BizEventsTransactionDTOFaker; +import it.gov.pagopa.arc.fakers.bizEvents.BizEventsTransactionDTOFaker; import it.gov.pagopa.arc.model.generated.TransactionDTO; import it.gov.pagopa.arc.utils.Utilities; import org.junit.jupiter.api.BeforeEach; diff --git a/src/test/java/it/gov/pagopa/arc/dto/mapper/BizEventsTransactionDetails2TransactionDetailsDTOTest.java b/src/test/java/it/gov/pagopa/arc/dto/mapper/BizEventsTransactionDetails2TransactionDetailsDTOTest.java new file mode 100644 index 00000000..74f97fe5 --- /dev/null +++ b/src/test/java/it/gov/pagopa/arc/dto/mapper/BizEventsTransactionDetails2TransactionDetailsDTOTest.java @@ -0,0 +1,79 @@ +package it.gov.pagopa.arc.dto.mapper; + +import it.gov.pagopa.arc.connector.bizevents.dto.*; +import it.gov.pagopa.arc.fakers.CartItemDTOFaker; +import it.gov.pagopa.arc.fakers.CommonUserDetailDTOFaker; +import it.gov.pagopa.arc.fakers.CommonWalletInfoDTOFaker; +import it.gov.pagopa.arc.fakers.InfoTransactionDTOFaker; +import it.gov.pagopa.arc.fakers.bizEvents.BizEventsCartItemDTOFaker; +import it.gov.pagopa.arc.fakers.bizEvents.BizEventsInfoTransactionDTOFaker; +import it.gov.pagopa.arc.model.generated.*; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.any; + +@ExtendWith(MockitoExtension.class) +class BizEventsTransactionDetails2TransactionDetailsDTOTest { + + private BizEventsTransactionDetails2TransactionDetailsDTO transactionDetailsMapper; + + @Mock + private BizEventsInfoTransaction2InfoTransactionDTO transactionInfoMapperMock; + + @BeforeEach + void setUp() { + transactionDetailsMapper = new BizEventsTransactionDetails2TransactionDetailsDTO(transactionInfoMapperMock); + } + + @Test + void givenBizEventsTransactionDetailsWhenApplyThenReturnTransactionDetails() { + //given + //create BizEventsTransactionDetailsDTO + BizEventsWalletInfoDTO bizEventsWalletInfo = CommonWalletInfoDTOFaker.mockBizEventsWalletInfoDTO(false); + BizEventsUserDetailDTO payer = CommonUserDetailDTOFaker.mockBizEventsUserDetailDTO(CommonUserDetailDTOFaker.USER_DETAIL_PAYER); + BizEventsInfoTransactionDTO bizEventsInfoTransaction = BizEventsInfoTransactionDTOFaker.mockInstance(bizEventsWalletInfo, payer); + + BizEventsUserDetailDTO payee = CommonUserDetailDTOFaker.mockBizEventsUserDetailDTO(CommonUserDetailDTOFaker.USER_DETAIL_PAYEE); + BizEventsUserDetailDTO debtor = CommonUserDetailDTOFaker.mockBizEventsUserDetailDTO(CommonUserDetailDTOFaker.USER_DETAIL_DEBTOR); + BizEventsCartItemDTO bizEventsCartItem1 = BizEventsCartItemDTOFaker.mockInstance(payee,debtor); + BizEventsCartItemDTO bizEventsCartItem2 = BizEventsCartItemDTOFaker.mockInstance(payee,debtor); + List bizEventsCartsList = List.of(bizEventsCartItem1, bizEventsCartItem2); + + BizEventsTransactionDetailsDTO bizEventsTransactionDetailsDTO = BizEventsTransactionDetailsDTO.builder() + .bizEventsInfoTransactionDTO(bizEventsInfoTransaction) + .bizEventsCartsDTO(bizEventsCartsList) + .build(); + + + //create TransactionDetailsDTO + WalletInfoDTO walletInfo = CommonWalletInfoDTOFaker.mockWalletInfoDTO(false); + UserDetailDTO payerMapped = CommonUserDetailDTOFaker.mockUserDetailDTO(CommonUserDetailDTOFaker.USER_DETAIL_PAYER); + InfoTransactionDTO infoTransaction = InfoTransactionDTOFaker.mockInstance(walletInfo, payerMapped); + + UserDetailDTO payeeResponse = CommonUserDetailDTOFaker.mockUserDetailDTO(CommonUserDetailDTOFaker.USER_DETAIL_PAYEE); + UserDetailDTO debtorResponse = CommonUserDetailDTOFaker.mockUserDetailDTO(CommonUserDetailDTOFaker.USER_DETAIL_DEBTOR); + CartItemDTO cartItem1 = CartItemDTOFaker.mockInstance(payeeResponse, debtorResponse); + CartItemDTO cartItem2 = CartItemDTOFaker.mockInstance(payeeResponse, debtorResponse); + List cartsList = List.of(cartItem1, cartItem2); + + Mockito.when(transactionInfoMapperMock.mapInfoTransaction(bizEventsInfoTransaction)).thenReturn(infoTransaction); + //when + TransactionDetailsDTO result = transactionDetailsMapper.apply(bizEventsTransactionDetailsDTO, cartsList); + //then + assertNotNull(result); + assertEquals(infoTransaction, result.getInfoTransaction()); + System.out.println(infoTransaction.getWalletInfo()); + System.out.println(result.getInfoTransaction().getWalletInfo()); + assertEquals(2, result.getCarts().size()); + assertEquals( cartsList, result.getCarts()); + Mockito.verify(transactionInfoMapperMock).mapInfoTransaction(any()); + } +} \ No newline at end of file diff --git a/src/test/java/it/gov/pagopa/arc/dto/mapper/BizEventsUserDetail2UserDetailDTOTest.java b/src/test/java/it/gov/pagopa/arc/dto/mapper/BizEventsUserDetail2UserDetailDTOTest.java new file mode 100644 index 00000000..4e767389 --- /dev/null +++ b/src/test/java/it/gov/pagopa/arc/dto/mapper/BizEventsUserDetail2UserDetailDTOTest.java @@ -0,0 +1,33 @@ +package it.gov.pagopa.arc.dto.mapper; + +import it.gov.pagopa.arc.connector.bizevents.dto.BizEventsUserDetailDTO; +import it.gov.pagopa.arc.fakers.CommonUserDetailDTOFaker; +import it.gov.pagopa.arc.model.generated.UserDetailDTO; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +class BizEventsUserDetail2UserDetailDTOTest { + + private BizEventsUserDetail2UserDetailDTO userDetailMapper; + + @BeforeEach + void setUp() { + userDetailMapper = new BizEventsUserDetail2UserDetailDTO(); + } + + @Test + void givenBizEventsUserDetailDTOWhenMapUserDetailThenReturnUserDetailDTO() { + //given + BizEventsUserDetailDTO payee = CommonUserDetailDTOFaker.mockBizEventsUserDetailDTO(CommonUserDetailDTOFaker.USER_DETAIL_PAYEE); + //when + UserDetailDTO result = userDetailMapper.mapUserDetail(payee); + //then + assertNotNull(result); + assertEquals("CREDITOR_NAME", result.getName()); + assertEquals("CREDITOR_TAX_CODE", result.getTaxCode()); + + } +} \ No newline at end of file diff --git a/src/test/java/it/gov/pagopa/arc/dto/mapper/BizEventsWalletInfo2WalletInfoDTOTest.java b/src/test/java/it/gov/pagopa/arc/dto/mapper/BizEventsWalletInfo2WalletInfoDTOTest.java new file mode 100644 index 00000000..f4577801 --- /dev/null +++ b/src/test/java/it/gov/pagopa/arc/dto/mapper/BizEventsWalletInfo2WalletInfoDTOTest.java @@ -0,0 +1,33 @@ +package it.gov.pagopa.arc.dto.mapper; + +import it.gov.pagopa.arc.connector.bizevents.dto.BizEventsWalletInfoDTO; +import it.gov.pagopa.arc.fakers.CommonWalletInfoDTOFaker; +import it.gov.pagopa.arc.model.generated.WalletInfoDTO; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +class BizEventsWalletInfo2WalletInfoDTOTest { + private BizEventsWalletInfo2WalletInfoDTO walletInfoMapper; + + @BeforeEach + void setUp() { + walletInfoMapper = new BizEventsWalletInfo2WalletInfoDTO(); + } + + @Test + void givenBizEventsWalletInfoDTOWhenMapWalletInfoThenReturnWalletInfoDTO() { + //given + BizEventsWalletInfoDTO bizEventsWalletInfo = CommonWalletInfoDTOFaker.mockBizEventsWalletInfoDTO(true); + //when + WalletInfoDTO result = walletInfoMapper.mapWalletInfo(bizEventsWalletInfo); + //then + assertNotNull(result); + assertEquals("USER_HOLDER", result.getAccountHolder()); + assertEquals("VISA", result.getBrand()); + assertEquals("0932", result.getBlurredNumber()); + assertEquals("user@paypal.com", result.getMaskedEmail()); + } +} \ No newline at end of file diff --git a/src/test/java/it/gov/pagopa/arc/fakers/CartItemDTOFaker.java b/src/test/java/it/gov/pagopa/arc/fakers/CartItemDTOFaker.java new file mode 100644 index 00000000..d28070b0 --- /dev/null +++ b/src/test/java/it/gov/pagopa/arc/fakers/CartItemDTOFaker.java @@ -0,0 +1,20 @@ +package it.gov.pagopa.arc.fakers; + + +import it.gov.pagopa.arc.model.generated.CartItemDTO; +import it.gov.pagopa.arc.model.generated.UserDetailDTO; + +public class CartItemDTOFaker { + public static CartItemDTO mockInstance(UserDetailDTO payee, UserDetailDTO debtor){ + return mockInstanceBuilder(payee, debtor).build(); + } + private static CartItemDTO.CartItemDTOBuilder mockInstanceBuilder(UserDetailDTO payee, UserDetailDTO debtor){ + return CartItemDTO.builder() + .subject("pagamento") + .amount(545230L) + .payee(payee) + .debtor(debtor) + .refNumberValue("960000000094659945") + .refNumberType("IUV"); + } +} diff --git a/src/test/java/it/gov/pagopa/arc/fakers/CommonUserDetailDTOFaker.java b/src/test/java/it/gov/pagopa/arc/fakers/CommonUserDetailDTOFaker.java new file mode 100644 index 00000000..a18889f8 --- /dev/null +++ b/src/test/java/it/gov/pagopa/arc/fakers/CommonUserDetailDTOFaker.java @@ -0,0 +1,48 @@ +package it.gov.pagopa.arc.fakers; + +import it.gov.pagopa.arc.connector.bizevents.dto.BizEventsUserDetailDTO; +import it.gov.pagopa.arc.model.generated.UserDetailDTO; + +import java.lang.reflect.Method; + +public class CommonUserDetailDTOFaker { + public static final String USER_DETAIL_PAYER = "payer"; + public static final String USER_DETAIL_DEBTOR = "debtor"; + public static final String USER_DETAIL_PAYEE = "payee"; + + public static UserDetailDTO mockUserDetailDTO(String role) { + return (UserDetailDTO) buildUserDetailDTO(UserDetailDTO.builder(), role); + } + public static BizEventsUserDetailDTO mockBizEventsUserDetailDTO(String role) { + return (BizEventsUserDetailDTO) buildUserDetailDTO(BizEventsUserDetailDTO.builder(), role); + } + + private static Object buildUserDetailDTO(Object builder, String role) { + try { + Method nameMethod = builder.getClass().getMethod("name", String.class); + Method taxCodeMethod = builder.getClass().getMethod("taxCode", String.class); + Method buildMethod = builder.getClass().getMethod("build"); + + switch (role) { + case "payee" -> { + nameMethod.invoke(builder, "CREDITOR_NAME"); + taxCodeMethod.invoke(builder, "CREDITOR_TAX_CODE"); + } + case "debtor" -> { + nameMethod.invoke(builder, "USER_DEBTOR"); + taxCodeMethod.invoke(builder, "FAKE_CF_DEBTOR"); + } + case "payer" -> { + nameMethod.invoke(builder, "USER_PAYER"); + taxCodeMethod.invoke(builder, "FAKE_CF_PAYER"); + } + default -> throw new IllegalArgumentException("Unsupported role: " + role); + } + + return buildMethod.invoke(builder); + + } catch (Exception e) { + throw new RuntimeException("Error building DTO", e); + } + } +} diff --git a/src/test/java/it/gov/pagopa/arc/fakers/CommonWalletInfoDTOFaker.java b/src/test/java/it/gov/pagopa/arc/fakers/CommonWalletInfoDTOFaker.java new file mode 100644 index 00000000..1eceb059 --- /dev/null +++ b/src/test/java/it/gov/pagopa/arc/fakers/CommonWalletInfoDTOFaker.java @@ -0,0 +1,41 @@ +package it.gov.pagopa.arc.fakers; + +import it.gov.pagopa.arc.connector.bizevents.dto.BizEventsWalletInfoDTO; +import it.gov.pagopa.arc.model.generated.WalletInfoDTO; + +import java.lang.reflect.Method; + +public class CommonWalletInfoDTOFaker { + + public static WalletInfoDTO mockWalletInfoDTO(boolean paypal) { + return (WalletInfoDTO) buildWalletInfoDTO(WalletInfoDTO.builder(), paypal); + } + + public static BizEventsWalletInfoDTO mockBizEventsWalletInfoDTO(boolean paypal) { + return (BizEventsWalletInfoDTO) buildWalletInfoDTO(BizEventsWalletInfoDTO.builder(), paypal); + } + + private static Object buildWalletInfoDTO(Object builder, boolean paypal) { + try { + Method accountHolderMethod = builder.getClass().getMethod("accountHolder", String.class); + Method brandMethod = builder.getClass().getMethod("brand", String.class); + Method blurredNumberMethod = builder.getClass().getMethod("blurredNumber", String.class); + Method maskedEmailMethod = builder.getClass().getMethod("maskedEmail", String.class); + Method buildMethod = builder.getClass().getMethod("build"); + + accountHolderMethod.invoke(builder, "USER_HOLDER"); + brandMethod.invoke(builder, "VISA"); + blurredNumberMethod.invoke(builder, "0932"); + + if (paypal) { + maskedEmailMethod.invoke(builder, "user@paypal.com"); + } + + return buildMethod.invoke(builder); + + } catch (Exception e) { + throw new RuntimeException("Error building DTO", e); + } + } + +} diff --git a/src/test/java/it/gov/pagopa/arc/fakers/InfoTransactionDTOFaker.java b/src/test/java/it/gov/pagopa/arc/fakers/InfoTransactionDTOFaker.java new file mode 100644 index 00000000..3466bdea --- /dev/null +++ b/src/test/java/it/gov/pagopa/arc/fakers/InfoTransactionDTOFaker.java @@ -0,0 +1,28 @@ +package it.gov.pagopa.arc.fakers; + +import it.gov.pagopa.arc.model.generated.InfoTransactionDTO; +import it.gov.pagopa.arc.model.generated.UserDetailDTO; +import it.gov.pagopa.arc.model.generated.WalletInfoDTO; + +import java.time.ZonedDateTime; + +public class InfoTransactionDTOFaker { + + public static InfoTransactionDTO mockInstance(WalletInfoDTO walletInfo, UserDetailDTO payer){ + return mockInstanceBuilder(walletInfo, payer).build(); + } + private static InfoTransactionDTO.InfoTransactionDTOBuilder mockInstanceBuilder(WalletInfoDTO walletInfo, UserDetailDTO payer){ + return InfoTransactionDTO.builder() + .transactionId("TRANSACTION_ID") + .authCode("250863") + .rrn("51561651") + .transactionDate(ZonedDateTime.parse("2024-06-27T13:07:25Z")) + .pspName("Worldline Merchant Services Italia S.p.A.") + .walletInfo(walletInfo) + .paymentMethod(InfoTransactionDTO.PaymentMethodEnum.PO) + .payer(payer) + .amount(565430L) + .fee(29L) + .origin(InfoTransactionDTO.OriginEnum.UNKNOWN); + } +} diff --git a/src/test/java/it/gov/pagopa/arc/fakers/bizEvents/BizEventsCartItemDTOFaker.java b/src/test/java/it/gov/pagopa/arc/fakers/bizEvents/BizEventsCartItemDTOFaker.java new file mode 100644 index 00000000..5f2b4430 --- /dev/null +++ b/src/test/java/it/gov/pagopa/arc/fakers/bizEvents/BizEventsCartItemDTOFaker.java @@ -0,0 +1,20 @@ +package it.gov.pagopa.arc.fakers.bizEvents; + +import it.gov.pagopa.arc.connector.bizevents.dto.BizEventsCartItemDTO; +import it.gov.pagopa.arc.connector.bizevents.dto.BizEventsUserDetailDTO; + +public class BizEventsCartItemDTOFaker { + + public static BizEventsCartItemDTO mockInstance(BizEventsUserDetailDTO payee, BizEventsUserDetailDTO debtor){ + return mockInstanceBuilder(payee, debtor).build(); + } + private static BizEventsCartItemDTO.BizEventsCartItemDTOBuilder mockInstanceBuilder(BizEventsUserDetailDTO payee, BizEventsUserDetailDTO debtor){ + return BizEventsCartItemDTO.builder() + .subject("pagamento") + .amount("5.452,3") + .payee(payee) + .debtor(debtor) + .refNumberValue("960000000094659945") + .refNumberType("IUV"); + } +} diff --git a/src/test/java/it/gov/pagopa/arc/fakers/bizEvents/BizEventsInfoTransactionDTOFaker.java b/src/test/java/it/gov/pagopa/arc/fakers/bizEvents/BizEventsInfoTransactionDTOFaker.java new file mode 100644 index 00000000..84a1fa11 --- /dev/null +++ b/src/test/java/it/gov/pagopa/arc/fakers/bizEvents/BizEventsInfoTransactionDTOFaker.java @@ -0,0 +1,27 @@ +package it.gov.pagopa.arc.fakers.bizEvents; + +import it.gov.pagopa.arc.connector.bizevents.dto.BizEventsInfoTransactionDTO; +import it.gov.pagopa.arc.connector.bizevents.dto.BizEventsUserDetailDTO; +import it.gov.pagopa.arc.connector.bizevents.dto.BizEventsWalletInfoDTO; +import it.gov.pagopa.arc.connector.bizevents.enums.Origin; +import it.gov.pagopa.arc.connector.bizevents.enums.PaymentMethod; + +public class BizEventsInfoTransactionDTOFaker { + public static BizEventsInfoTransactionDTO mockInstance(BizEventsWalletInfoDTO walletInfo, BizEventsUserDetailDTO payer){ + return mockInstanceBuilder(walletInfo, payer).build(); + } + private static BizEventsInfoTransactionDTO.BizEventsInfoTransactionDTOBuilder mockInstanceBuilder(BizEventsWalletInfoDTO walletInfo, BizEventsUserDetailDTO payer){ + return BizEventsInfoTransactionDTO.builder() + .transactionId("TRANSACTION_ID") + .authCode("250863") + .rrn("51561651") + .transactionDate("2024-06-27T13:07:25Z") + .pspName("Worldline Merchant Services Italia S.p.A.") + .bizEventsWalletInfoDTO(walletInfo) + .paymentMethod(PaymentMethod.PO) + .payer(payer) + .amount("5.654,3") + .fee("0,29") + .origin(Origin.PM); + } +} diff --git a/src/test/java/it/gov/pagopa/arc/fakers/BizEventsTransactionDTOFaker.java b/src/test/java/it/gov/pagopa/arc/fakers/bizEvents/BizEventsTransactionDTOFaker.java similarity index 96% rename from src/test/java/it/gov/pagopa/arc/fakers/BizEventsTransactionDTOFaker.java rename to src/test/java/it/gov/pagopa/arc/fakers/bizEvents/BizEventsTransactionDTOFaker.java index 94252992..05219d11 100644 --- a/src/test/java/it/gov/pagopa/arc/fakers/BizEventsTransactionDTOFaker.java +++ b/src/test/java/it/gov/pagopa/arc/fakers/bizEvents/BizEventsTransactionDTOFaker.java @@ -1,4 +1,4 @@ -package it.gov.pagopa.arc.fakers; +package it.gov.pagopa.arc.fakers.bizEvents; import it.gov.pagopa.arc.connector.bizevents.dto.BizEventsTransactionDTO; diff --git a/src/test/java/it/gov/pagopa/arc/utils/UtilitiesTest.java b/src/test/java/it/gov/pagopa/arc/utils/UtilitiesTest.java index e73c2c84..0fcbc4ac 100644 --- a/src/test/java/it/gov/pagopa/arc/utils/UtilitiesTest.java +++ b/src/test/java/it/gov/pagopa/arc/utils/UtilitiesTest.java @@ -23,7 +23,8 @@ class UtilitiesTest { "2.681,52; 268152", "200,35; 20035", "54,1; 5410", - "2; 200" + "2; 200", + "0,24; 24" }) void givenCorrectEuroStringWhenCallEuroToCentsThenReturnAmountCents(String euroString, Long expected) { //given