Skip to content

Commit

Permalink
chore: P4PU-200 added mapper (#16)
Browse files Browse the repository at this point in the history
* P4PU-200 added mapper

* P4PU-200 renamed mapper

* P4PU-200 fixed Utilities

* P4PU-200 added faker

* P4PU-200 added junit tests

* P4PU-200 updated annotation from @service to @component
  • Loading branch information
Giuseppe-LaManna authored Jun 28, 2024
1 parent d18e0b5 commit 674f278
Show file tree
Hide file tree
Showing 24 changed files with 673 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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<CartItemDTO> cartsList){
return TransactionDetailsDTO.builder()
.infoTransaction(transactionInfoMapper.mapInfoTransaction(bizEventsTransactionDetailsDTO.getBizEventsInfoTransactionDTO()))
.carts(cartsList)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<TransactionDTO> transactionsList, Integer size){
return TransactionsListDTO.builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
24 changes: 24 additions & 0 deletions src/main/java/it/gov/pagopa/arc/dto/mapper/OriginEnumMapper.java
Original file line number Diff line number Diff line change
@@ -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<Origin, InfoTransactionDTO.OriginEnum> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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<PaymentMethod, InfoTransactionDTO.PaymentMethodEnum> 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);
}
}
2 changes: 1 addition & 1 deletion src/main/java/it/gov/pagopa/arc/utils/Utilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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());
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Loading

0 comments on commit 674f278

Please sign in to comment.