Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: [P4PU-533] create mapper #117

Merged
merged 8 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,14 @@

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;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.NullValueCheckStrategy;

@Component
public class BizEventsCartItem2CartItemDTOMapper {
private final BizEventsUserDetail2UserDetailDTOMapper userDetailsMapper;
@Mapper(componentModel = "spring", uses = {MapperUtilities.class, BizEventsUserDetail2UserDetailDTOMapper.class})
public interface BizEventsCartItem2CartItemDTOMapper {

public BizEventsCartItem2CartItemDTOMapper(BizEventsUserDetail2UserDetailDTOMapper userDetailsMapper) {
this.userDetailsMapper = userDetailsMapper;
}
@Mapping(source = "amount", target = "amount", nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS, qualifiedByName = "euroToCents")
CartItemDTO mapCart(BizEventsCartItemDTO bizEventsCartItemDTO);

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,19 @@
package it.gov.pagopa.arc.dto.mapper.bizevents.paidnotice;

import it.gov.pagopa.arc.connector.bizevents.dto.paidnotice.BizEventsInfoPaidNoticeDTO;
import it.gov.pagopa.arc.dto.mapper.BizEventsUserDetail2UserDetailDTOMapper;
import it.gov.pagopa.arc.dto.mapper.BizEventsWalletInfo2WalletInfoDTOMapper;
import it.gov.pagopa.arc.dto.mapper.MapperUtilities;
import it.gov.pagopa.arc.model.generated.InfoNoticeDTO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.NullValueCheckStrategy;

@Mapper(componentModel = "spring", uses = {MapperUtilities.class, BizEventsWalletInfo2WalletInfoDTOMapper.class, BizEventsUserDetail2UserDetailDTOMapper.class})
public interface BizEventsInfoPaidNoticeDTO2InfoNoticeDTOMapper {

@Mapping(source = "amount", target = "amount", nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS, qualifiedByName = "euroToCents")
@Mapping(source = "fee", target = "fee", nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS, qualifiedByName = "euroToCents")
@Mapping(source = "noticeDate", target = "noticeDate", nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS, qualifiedByName = "dateStringToZonedDateTime")
InfoNoticeDTO toInfoNoticeDTO(BizEventsInfoPaidNoticeDTO bizEventsInfoPaidNoticeDTO);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package it.gov.pagopa.arc.dto.mapper.bizevents.paidnotice;

import it.gov.pagopa.arc.connector.bizevents.dto.paidnotice.BizEventsPaidNoticeDetailsDTO;
import it.gov.pagopa.arc.dto.mapper.BizEventsCartItem2CartItemDTOMapper;
import it.gov.pagopa.arc.model.generated.NoticeDetailsDTO;
import org.mapstruct.Mapper;

@Mapper(componentModel = "spring", uses = {BizEventsInfoPaidNoticeDTO2InfoNoticeDTOMapper.class, BizEventsCartItem2CartItemDTOMapper.class})
public interface BizEventsPaidNoticeDetailsDTO2NoticeDetailsDTOMapper {
NoticeDetailsDTO toNoticeDetailsDTO(BizEventsPaidNoticeDetailsDTO bizEventsPaidNoticeDetailsDTO);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
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.mapstruct.factory.Mappers;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
Expand All @@ -20,15 +21,11 @@
@ExtendWith(MockitoExtension.class)
class BizEventsCartItem2CartItemDTOMapperTest {

private BizEventsCartItem2CartItemDTOMapper cartItemMapper;

@Mock
private BizEventsUserDetail2UserDetailDTOMapper userDetailsMapperMock;

@BeforeEach
void setUp() {
cartItemMapper = new BizEventsCartItem2CartItemDTOMapper(userDetailsMapperMock);
}
@InjectMocks
private final BizEventsCartItem2CartItemDTOMapper cartItemMapper = Mappers.getMapper(BizEventsCartItem2CartItemDTOMapper.class);

@Test
void givenBizEventsCartItemDTOWhenMapCartThenReturnCartItemDTO() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package it.gov.pagopa.arc.dto.mapper.bizevents.paidnotice;

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.dto.paidnotice.BizEventsInfoPaidNoticeDTO;
import it.gov.pagopa.arc.dto.mapper.BizEventsUserDetail2UserDetailDTOMapper;
import it.gov.pagopa.arc.dto.mapper.BizEventsWalletInfo2WalletInfoDTOMapper;
import it.gov.pagopa.arc.fakers.CommonUserDetailDTOFaker;
import it.gov.pagopa.arc.fakers.CommonWalletInfoDTOFaker;
import it.gov.pagopa.arc.fakers.bizEvents.paidnotice.BizEventsInfoPaidNoticeDTOFaker;
import it.gov.pagopa.arc.model.generated.InfoNoticeDTO;
import it.gov.pagopa.arc.model.generated.UserDetailDTO;
import it.gov.pagopa.arc.model.generated.WalletInfoDTO;
import it.gov.pagopa.arc.utils.TestUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mapstruct.factory.Mappers;
import org.mockito.InjectMocks;
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.assertEquals;
@ExtendWith(MockitoExtension.class)
class BizEventsInfoPaidNoticeDTO2InfoNoticeDTOMapperTest {
@Mock
private BizEventsUserDetail2UserDetailDTOMapper bizEventsUserDetail2UserDetailDTOMapperMock;
@Mock
private BizEventsWalletInfo2WalletInfoDTOMapper bizEventsWalletInfo2WalletInfoDTOMapperMock;

@InjectMocks
private final BizEventsInfoPaidNoticeDTO2InfoNoticeDTOMapper mapper = Mappers.getMapper(BizEventsInfoPaidNoticeDTO2InfoNoticeDTOMapper.class);

@AfterEach
void afterMethod() {
Mockito.verifyNoMoreInteractions( bizEventsUserDetail2UserDetailDTOMapperMock, bizEventsWalletInfo2WalletInfoDTOMapperMock);
}
@Test
void givenBizEventsInfoPaidNoticeDTOWhenCallMapperThenReturnInfoNoticeDTO() {
//given
BizEventsWalletInfoDTO bizEventsWalletInfo = CommonWalletInfoDTOFaker.mockBizEventsWalletInfoDTO(false);
BizEventsUserDetailDTO payer = CommonUserDetailDTOFaker.mockBizEventsUserDetailDTO(CommonUserDetailDTOFaker.USER_DETAIL_PAYER);
BizEventsInfoPaidNoticeDTO bizEventsInfoPaidNoticeDTO = BizEventsInfoPaidNoticeDTOFaker.mockInstance(bizEventsWalletInfo, payer);

WalletInfoDTO walletInfoDTO = CommonWalletInfoDTOFaker.mockWalletInfoDTO(false);
UserDetailDTO userDetailDTO = CommonUserDetailDTOFaker.mockUserDetailDTO(CommonUserDetailDTOFaker.USER_DETAIL_PAYER);

Mockito.when(bizEventsWalletInfo2WalletInfoDTOMapperMock.mapWalletInfo(bizEventsWalletInfo)).thenReturn(walletInfoDTO);
Mockito.when(bizEventsUserDetail2UserDetailDTOMapperMock.mapUserDetail(payer)).thenReturn(userDetailDTO);
//when
InfoNoticeDTO infoNoticeDTO = mapper.toInfoNoticeDTO(bizEventsInfoPaidNoticeDTO);
//then
Assertions.assertEquals("EVENT_ID", infoNoticeDTO.getEventId());
Assertions.assertEquals("250863", infoNoticeDTO.getAuthCode());
Assertions.assertEquals("51561651", infoNoticeDTO.getRrn());
Assertions.assertEquals("51561651", infoNoticeDTO.getRrn());
Assertions.assertEquals(ZonedDateTime.parse("2024-06-27T13:07:25Z"), infoNoticeDTO.getNoticeDate());
Assertions.assertEquals("Worldline Merchant Services Italia S.p.A.", infoNoticeDTO.getPspName());
assertEquals( 565430L, infoNoticeDTO.getAmount());
assertEquals( 29L, infoNoticeDTO.getFee());
assertEquals(InfoNoticeDTO.OriginEnum.PM, infoNoticeDTO.getOrigin());
antonioT90 marked this conversation as resolved.
Show resolved Hide resolved

TestUtils.assertNotNullFields(infoNoticeDTO);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package it.gov.pagopa.arc.dto.mapper.bizevents.paidnotice;

import it.gov.pagopa.arc.connector.bizevents.dto.paidnotice.BizEventsPaidNoticeDetailsDTO;
import it.gov.pagopa.arc.dto.mapper.BizEventsCartItem2CartItemDTOMapper;
import it.gov.pagopa.arc.fakers.NoticeDetailsDTOFaker;
import it.gov.pagopa.arc.fakers.bizEvents.paidnotice.BizEventsPaidNoticeDetailsDTOFaker;
import it.gov.pagopa.arc.model.generated.NoticeDetailsDTO;
import it.gov.pagopa.arc.utils.TestUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mapstruct.factory.Mappers;
import org.mockito.InjectMocks;
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;
@ExtendWith(MockitoExtension.class)
class BizEventsPaidNoticeDetailsDTO2NoticeDetailsDTOMapperTest {
@Mock
private BizEventsInfoPaidNoticeDTO2InfoNoticeDTOMapper bizEventsInfoPaidNoticeDTO2InfoNoticeDTOMapperMock;
@Mock
private BizEventsCartItem2CartItemDTOMapper bizEventsCartItem2CartItemDTOMapperMock;
@InjectMocks
private final BizEventsPaidNoticeDetailsDTO2NoticeDetailsDTOMapper mapper = Mappers.getMapper(BizEventsPaidNoticeDetailsDTO2NoticeDetailsDTOMapper.class);

@AfterEach
void afterMethod() {
Mockito.verifyNoMoreInteractions( bizEventsInfoPaidNoticeDTO2InfoNoticeDTOMapperMock,bizEventsCartItem2CartItemDTOMapperMock);
}

@Test
void givenBizEventsPaidNoticeDetailsDTOWhenCallToNoticeDetailsDTOThenReturnNoticeDetailsDTO() {
//given
BizEventsPaidNoticeDetailsDTO bizEventsPaidNoticeDetailsDTO = BizEventsPaidNoticeDetailsDTOFaker.mockInstance();

NoticeDetailsDTO noticeDetailsDTO = NoticeDetailsDTOFaker.mockInstance();

Mockito.when(bizEventsInfoPaidNoticeDTO2InfoNoticeDTOMapperMock.toInfoNoticeDTO(bizEventsPaidNoticeDetailsDTO.getInfoNotice())).thenReturn(noticeDetailsDTO.getInfoNotice());
Mockito.when(bizEventsCartItem2CartItemDTOMapperMock.mapCart( bizEventsPaidNoticeDetailsDTO.getCarts().get(0))).thenReturn(noticeDetailsDTO.getCarts().get(0));
Mockito.when(bizEventsCartItem2CartItemDTOMapperMock.mapCart( bizEventsPaidNoticeDetailsDTO.getCarts().get(1))).thenReturn(noticeDetailsDTO.getCarts().get(1));
//when
NoticeDetailsDTO result = mapper.toNoticeDetailsDTO(bizEventsPaidNoticeDetailsDTO);
//then
assertNotNull(result);
assertEquals(noticeDetailsDTO.getInfoNotice(), result.getInfoNotice());
assertEquals(2, result.getCarts().size());
assertEquals(noticeDetailsDTO.getCarts(), result.getCarts());
antonioT90 marked this conversation as resolved.
Show resolved Hide resolved

TestUtils.assertNotNullFields(result);
}
}
31 changes: 31 additions & 0 deletions src/test/java/it/gov/pagopa/arc/fakers/InfoNoticeDTOFaker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package it.gov.pagopa.arc.fakers;

import it.gov.pagopa.arc.model.generated.InfoNoticeDTO;
import it.gov.pagopa.arc.model.generated.UserDetailDTO;
import it.gov.pagopa.arc.model.generated.WalletInfoDTO;
import it.gov.pagopa.arc.utils.TestUtils;

import java.time.ZonedDateTime;

public class InfoNoticeDTOFaker {
public static InfoNoticeDTO mockInstance(WalletInfoDTO walletInfo, UserDetailDTO payer){
return mockInstanceBuilder(walletInfo, payer).build();
}
private static InfoNoticeDTO.InfoNoticeDTOBuilder mockInstanceBuilder(WalletInfoDTO walletInfo, UserDetailDTO payer){
InfoNoticeDTO.InfoNoticeDTOBuilder infoNoticeDTOBuilder = InfoNoticeDTO.builder()
.eventId("EVENT_ID")
.authCode("250863")
.rrn("51561651")
.noticeDate(ZonedDateTime.parse("2024-06-27T13:07:25Z"))
.pspName("Worldline Merchant Services Italia S.p.A.")
.walletInfo(walletInfo)
.paymentMethod(InfoNoticeDTO.PaymentMethodEnum.PO)
.payer(payer)
.amount(565430L)
.fee(29L)
.origin(InfoNoticeDTO.OriginEnum.UNKNOWN);

TestUtils.assertNotNullFields(infoNoticeDTOBuilder);
return infoNoticeDTOBuilder;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package it.gov.pagopa.arc.fakers;

import it.gov.pagopa.arc.model.generated.*;
import it.gov.pagopa.arc.utils.TestUtils;

import java.util.List;

public class NoticeDetailsDTOFaker {
public static NoticeDetailsDTO mockInstance(){
return mockInstanceBuilder().build();
}

public static NoticeDetailsDTO.NoticeDetailsDTOBuilder mockInstanceBuilder(){
WalletInfoDTO walletInfo = CommonWalletInfoDTOFaker.mockWalletInfoDTO(false);
UserDetailDTO payerMapped = CommonUserDetailDTOFaker.mockUserDetailDTO(CommonUserDetailDTOFaker.USER_DETAIL_PAYER);
InfoNoticeDTO infoNotice = InfoNoticeDTOFaker.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<CartItemDTO> cartsList = List.of(cartItem1, cartItem2);

NoticeDetailsDTO.NoticeDetailsDTOBuilder noticeDetailsDTOBuilder = NoticeDetailsDTO.builder()
.infoNotice(infoNotice)
.carts(cartsList);
TestUtils.assertNotNullFields(noticeDetailsDTOBuilder);

return noticeDetailsDTOBuilder;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package it.gov.pagopa.arc.fakers.bizEvents.paidnotice;

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.dto.paidnotice.BizEventsInfoPaidNoticeDTO;
import it.gov.pagopa.arc.connector.bizevents.enums.Origin;
import it.gov.pagopa.arc.connector.bizevents.enums.PaymentMethod;
import it.gov.pagopa.arc.utils.TestUtils;

public class BizEventsInfoPaidNoticeDTOFaker {
public static BizEventsInfoPaidNoticeDTO mockInstance(BizEventsWalletInfoDTO walletInfo, BizEventsUserDetailDTO payer){
return mockInstanceBuilder(walletInfo, payer).build();
}
private static BizEventsInfoPaidNoticeDTO.BizEventsInfoPaidNoticeDTOBuilder mockInstanceBuilder(BizEventsWalletInfoDTO walletInfo, BizEventsUserDetailDTO payer){
BizEventsInfoPaidNoticeDTO.BizEventsInfoPaidNoticeDTOBuilder bizEventsInfoPaidNoticeDTOBuilder = BizEventsInfoPaidNoticeDTO.builder()
.eventId("EVENT_ID")
.authCode("250863")
.rrn("51561651")
.noticeDate("2024-06-27T13:07:25Z")
.pspName("Worldline Merchant Services Italia S.p.A.")
.walletInfo(walletInfo)
.paymentMethod(PaymentMethod.PO)
.payer(payer)
.amount("5,654.3")
.fee("0.29")
.origin(Origin.PM);

TestUtils.assertNotNullFields(bizEventsInfoPaidNoticeDTOBuilder);
return bizEventsInfoPaidNoticeDTOBuilder;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package it.gov.pagopa.arc.fakers.bizEvents.paidnotice;

import it.gov.pagopa.arc.connector.bizevents.dto.BizEventsCartItemDTO;
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.dto.paidnotice.BizEventsInfoPaidNoticeDTO;
import it.gov.pagopa.arc.connector.bizevents.dto.paidnotice.BizEventsPaidNoticeDetailsDTO;
import it.gov.pagopa.arc.fakers.CommonUserDetailDTOFaker;
import it.gov.pagopa.arc.fakers.CommonWalletInfoDTOFaker;
import it.gov.pagopa.arc.fakers.bizEvents.BizEventsCartItemDTOFaker;
import it.gov.pagopa.arc.utils.TestUtils;

import java.util.List;

public class BizEventsPaidNoticeDetailsDTOFaker {
public static BizEventsPaidNoticeDetailsDTO mockInstance(){
return mockInstanceBuilder().build();
}

public static BizEventsPaidNoticeDetailsDTO.BizEventsPaidNoticeDetailsDTOBuilder mockInstanceBuilder(){
BizEventsWalletInfoDTO bizEventsWalletInfo = CommonWalletInfoDTOFaker.mockBizEventsWalletInfoDTO(false);
BizEventsUserDetailDTO payer = CommonUserDetailDTOFaker.mockBizEventsUserDetailDTO(CommonUserDetailDTOFaker.USER_DETAIL_PAYER);
BizEventsInfoPaidNoticeDTO bizEventsInfoPaidNoticeDTO = BizEventsInfoPaidNoticeDTOFaker.mockInstance(bizEventsWalletInfo, payer);

BizEventsUserDetailDTO payeeResponse = CommonUserDetailDTOFaker.mockBizEventsUserDetailDTO(CommonUserDetailDTOFaker.USER_DETAIL_PAYEE);
BizEventsUserDetailDTO debtorResponse = CommonUserDetailDTOFaker.mockBizEventsUserDetailDTO(CommonUserDetailDTOFaker.USER_DETAIL_DEBTOR);

BizEventsCartItemDTO bizEventsCartItem1 = BizEventsCartItemDTOFaker.mockInstance(payeeResponse,debtorResponse);
BizEventsCartItemDTO bizEventsCartItem2 = BizEventsCartItemDTOFaker.mockInstance(payeeResponse,debtorResponse);
List<BizEventsCartItemDTO> bizEventsCartsList = List.of(bizEventsCartItem1, bizEventsCartItem2);

BizEventsPaidNoticeDetailsDTO.BizEventsPaidNoticeDetailsDTOBuilder bizEventsPaidNoticeDetailsDTO = BizEventsPaidNoticeDetailsDTO.builder()
.infoNotice(bizEventsInfoPaidNoticeDTO)
.carts(bizEventsCartsList);

TestUtils.assertNotNullFields(bizEventsPaidNoticeDetailsDTO);

return bizEventsPaidNoticeDetailsDTO;
}
}
Loading