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-294 create mapper class #37

Merged
merged 5 commits into from
Jul 26, 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
13 changes: 11 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ val springdocOpenApiVersion = "2.5.0"
val janinoVersion = "3.1.12"
val openApiToolsVersion = "0.2.6"
val wiremockVersion = "3.5.4"
val mapstructVersion = "1.5.5.Final"

dependencies {
implementation("org.springframework.boot:spring-boot-starter")
Expand All @@ -42,10 +43,18 @@ dependencies {
// Spring Security
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-oauth2-client
implementation("org.springframework.boot:spring-boot-starter-oauth2-client")

//lombok
compileOnly("org.projectlombok:lombok")
annotationProcessor("org.projectlombok:lombok")
compileOnly("org.projectlombok:lombok")

/**
* Mapstruct
* https://mapstruct.org/
* mapstruct dependencies must always be placed after the lombok dependency
* or the generated mappers will return an empty object
**/
implementation("org.mapstruct:mapstruct:$mapstructVersion")
annotationProcessor("org.mapstruct:mapstruct-processor:$mapstructVersion")

// Testing
testImplementation("org.springframework.boot:spring-boot-starter-test")
Expand Down
2 changes: 2 additions & 0 deletions gradle.lockfile
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ org.aspectj:aspectjweaver:1.9.22=compileClasspath
org.bouncycastle:bcprov-jdk18on:1.77=compileClasspath
org.codehaus.janino:commons-compiler:3.1.12=compileClasspath
org.codehaus.janino:janino:3.1.12=compileClasspath
org.mapstruct:mapstruct-processor:1.5.5.Final=compileClasspath
org.mapstruct:mapstruct:1.5.5.Final=compileClasspath
org.openapitools:jackson-databind-nullable:0.2.6=compileClasspath
org.ow2.asm:asm:9.6=compileClasspath
org.projectlombok:lombok:1.18.32=compileClasspath
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package it.gov.pagopa.arc.dto.mapper.pullpayment;

import it.gov.pagopa.arc.model.generated.PaymentNoticeDTO;
import it.gov.pagopa.arc.model.generated.PaymentNoticesListDTO;
import org.mapstruct.Mapper;

import java.util.List;

@Mapper(componentModel = "spring", uses= PullPaymentNoticeDTO2PaymentNoticeDTO.class)
public interface PaymentNoticesListDTOMapper {

default PaymentNoticesListDTO toPaymentNoticesListDTO(List<PaymentNoticeDTO> paymentNotices) {
PaymentNoticesListDTO dto = new PaymentNoticesListDTO();
dto.setPaymentNotices(paymentNotices);
return dto;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package it.gov.pagopa.arc.dto.mapper.pullpayment;

import it.gov.pagopa.arc.connector.pullpayment.dto.PullPaymentInstallmentDTO;
import it.gov.pagopa.arc.model.generated.InstallmentDTO;
import org.mapstruct.Mapper;

@Mapper(componentModel = "spring", uses = PullPaymentOptionStatus2PaymentOptionStatus.class)
public interface PullPaymentInstallmentDTO2InstallmentDTO {
InstallmentDTO toInstallmentDTO(PullPaymentInstallmentDTO pullPaymentInstallmentDTOSource);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package it.gov.pagopa.arc.dto.mapper.pullpayment;

import it.gov.pagopa.arc.connector.pullpayment.dto.PullPaymentNoticeDTO;
import it.gov.pagopa.arc.model.generated.PaymentNoticeDTO;
import org.mapstruct.Mapper;

@Mapper(componentModel = "spring", uses = {PullPaymentNoticeStatus2PaymentNoticeStatus.class, PullPaymentOptionDTO2PaymentOptionDTO.class})
public interface PullPaymentNoticeDTO2PaymentNoticeDTO {

PaymentNoticeDTO toPaymentNoticeDTO(PullPaymentNoticeDTO source);

}


Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package it.gov.pagopa.arc.dto.mapper.pullpayment;

import it.gov.pagopa.arc.connector.pullpayment.enums.PullPaymentNoticeStatus;
import it.gov.pagopa.arc.model.generated.PaymentNoticeStatus;
import org.mapstruct.Mapper;

@Mapper(componentModel = "spring")
public interface PullPaymentNoticeStatus2PaymentNoticeStatus {

PaymentNoticeStatus toPaymentNoticeStatus(PullPaymentNoticeStatus paymentNoticeStatusSource);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package it.gov.pagopa.arc.dto.mapper.pullpayment;

import it.gov.pagopa.arc.connector.pullpayment.dto.PullPaymentOptionDTO;
import it.gov.pagopa.arc.model.generated.PaymentOptionDTO;
import org.mapstruct.Mapper;

@Mapper(componentModel = "spring", uses = PullPaymentInstallmentDTO2InstallmentDTO.class)
public interface PullPaymentOptionDTO2PaymentOptionDTO {
PaymentOptionDTO toPaymentOptionDTO(PullPaymentOptionDTO pullPaymentOptionDTOSource);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package it.gov.pagopa.arc.dto.mapper.pullpayment;

import it.gov.pagopa.arc.connector.pullpayment.enums.PullPaymentOptionStatus;
import it.gov.pagopa.arc.model.generated.PaymentOptionStatus;
import org.mapstruct.Mapper;
import org.mapstruct.ValueMapping;

@Mapper(componentModel = "spring")
public interface PullPaymentOptionStatus2PaymentOptionStatus {
@ValueMapping(target = "UNPAID", source = "PO_UNPAID")
@ValueMapping(target = "PAID", source = "PO_PAID")
@ValueMapping(target = "PARTIALLY_REPORTED", source = "PO_PARTIALLY_REPORTED")
@ValueMapping(target = "REPORTED", source = "PO_REPORTED")
PaymentOptionStatus toPaymentOptionStatus(PullPaymentOptionStatus paymentOptionStatusSource);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import it.gov.pagopa.arc.config.FeignConfig;
import it.gov.pagopa.arc.connector.pullpayment.dto.PullPaymentNoticeDTO;
import it.gov.pagopa.arc.fakers.pullPayment.PullPaymentNoticeDTOFaker;
import it.gov.pagopa.arc.fakers.connector.pullPayment.PullPaymentNoticeDTOFaker;
import it.gov.pagopa.arc.utils.MemoryAppender;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -59,7 +59,7 @@ void setUp() {
@Test
void givenHeaderAndParameterWhenCallPullPaymentConnectorThenReturnPaymentNoticesList() {
//given
PullPaymentNoticeDTO pullPaymentNoticeDTO = PullPaymentNoticeDTOFaker.mockInstance();
PullPaymentNoticeDTO pullPaymentNoticeDTO = PullPaymentNoticeDTOFaker.mockInstance(true);
//when
List<PullPaymentNoticeDTO> result = pullPaymentConnector.getPaymentNotices("DUMMY_FISCAL_CODE", LocalDate.now(), 10, 0);
//then
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package it.gov.pagopa.arc.dto.mapper.pullpayment;

import it.gov.pagopa.arc.fakers.paymentNotices.PaymentNoticeDTOFaker;
import it.gov.pagopa.arc.model.generated.PaymentNoticeDTO;
import it.gov.pagopa.arc.model.generated.PaymentNoticesListDTO;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mapstruct.factory.Mappers;

import java.util.ArrayList;
import java.util.List;

class PaymentNoticesListDTOMapperTest {
private final PaymentNoticesListDTOMapper mapper = Mappers.getMapper(PaymentNoticesListDTOMapper.class);

@Test
void givenListOfPullPaymentNoticesListDTOWhenCallMapperThenReturnPaymentNoticesListDTO() {
//given
PaymentNoticeDTO paymentNoticeDTO1 = PaymentNoticeDTOFaker.mockInstance(true);
PaymentNoticeDTO paymentNoticeDTO2 = PaymentNoticeDTOFaker.mockInstance(true);

List<PaymentNoticeDTO> listOfPaymentNoticeDto = List.of(paymentNoticeDTO1, paymentNoticeDTO2);
//when

PaymentNoticesListDTO result = mapper.toPaymentNoticesListDTO(listOfPaymentNoticeDto);
//then
Assertions.assertNotNull(result);
Assertions.assertEquals(2, result.getPaymentNotices().size());
Assertions.assertEquals(paymentNoticeDTO1, result.getPaymentNotices().get(0));
Assertions.assertEquals(paymentNoticeDTO2, result.getPaymentNotices().get(1));
}

@Test
void givenEmptyListWhenCallMapperThenReturnPaymentNoticesListDTOWithEmptyList() {
//given
List<PaymentNoticeDTO> listOfPaymentNoticeDto = new ArrayList<>();

//when
PaymentNoticesListDTO result = mapper.toPaymentNoticesListDTO(listOfPaymentNoticeDto);
//then
Assertions.assertNotNull(result);
Assertions.assertTrue(result.getPaymentNotices().isEmpty());

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package it.gov.pagopa.arc.dto.mapper.pullpayment;

import it.gov.pagopa.arc.connector.pullpayment.dto.PullPaymentInstallmentDTO;
import it.gov.pagopa.arc.connector.pullpayment.enums.PullPaymentOptionStatus;
import it.gov.pagopa.arc.fakers.connector.pullPayment.PullPaymentInstallmentDTOFaker;
import it.gov.pagopa.arc.model.generated.InstallmentDTO;
import it.gov.pagopa.arc.model.generated.PaymentOptionStatus;
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.LocalDateTime;

import static org.mockito.ArgumentMatchers.any;

@ExtendWith(MockitoExtension.class)
class PullPaymentInstallmentDTO2InstallmentDTOTest {

@Mock
private PullPaymentOptionStatus2PaymentOptionStatus pullPaymentOptionStatus2PaymentOptionStatusMock;

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

@Test
void givenPullPaymentInstallmentDTOWhenCallMapperThenReturnInstallmentDTO() {
//given
PullPaymentOptionStatus poUnpaid = PullPaymentOptionStatus.PO_UNPAID;
PaymentOptionStatus unpaid = PaymentOptionStatus.UNPAID;

Mockito.when(pullPaymentOptionStatus2PaymentOptionStatusMock.toPaymentOptionStatus(poUnpaid)).thenReturn(unpaid);

PullPaymentInstallmentDTO pullPaymentInstallmentDTO = PullPaymentInstallmentDTOFaker.mockInstance();

//when
InstallmentDTO result = mapper.toInstallmentDTO(pullPaymentInstallmentDTO);

//then
Assertions.assertAll(() -> {
Assertions.assertNotNull(result);
Assertions.assertEquals("347000000880099993", result.getNav());
Assertions.assertEquals("47000000880099993", result.getIuv());
Assertions.assertEquals("99999000013", result.getPaTaxCode());
Assertions.assertEquals("EC Demo Pagamenti Pull Test", result.getPaFullName());
Assertions.assertEquals(120L, result.getAmount());
Assertions.assertEquals("Test Pull - unica opzione", result.getDescription());
Assertions.assertEquals(LocalDateTime.parse("2024-10-30T23:59:59"), result.getDueDate());
Assertions.assertEquals(LocalDateTime.parse("2024-11-30T23:59:59"), result.getRetentionDate());
Assertions.assertEquals(LocalDateTime.parse("2024-04-11T06:56:14.845126"), result.getInsertedDate());
Assertions.assertEquals(0L, result.getNotificationFee());
Assertions.assertEquals(unpaid, result.getStatus());
Assertions.assertEquals(LocalDateTime.parse("2024-04-11T06:56:14.845126"), result.getLastUpdatedDate());

Mockito.verify(pullPaymentOptionStatus2PaymentOptionStatusMock).toPaymentOptionStatus(any());
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package it.gov.pagopa.arc.dto.mapper.pullpayment;

import it.gov.pagopa.arc.connector.pullpayment.dto.PullPaymentNoticeDTO;
import it.gov.pagopa.arc.fakers.paymentNotices.PaymentNoticeDTOFaker;
import it.gov.pagopa.arc.fakers.connector.pullPayment.PullPaymentNoticeDTOFaker;
import it.gov.pagopa.arc.model.generated.PaymentNoticeDTO;
import it.gov.pagopa.arc.model.generated.PaymentNoticeStatus;
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 static org.mockito.ArgumentMatchers.any;

@ExtendWith(MockitoExtension.class)
class PullPaymentNoticeDTO2PaymentNoticeDTOTest {

@Mock
PullPaymentNoticeStatus2PaymentNoticeStatus pullPaymentNoticeStatus2PaymentNoticeStatusMock;
@Mock
PullPaymentOptionDTO2PaymentOptionDTO pullPaymentOptionDTO2PaymentOptionDTOMock;

@InjectMocks
PullPaymentNoticeDTO2PaymentNoticeDTO mapper = Mappers.getMapper(PullPaymentNoticeDTO2PaymentNoticeDTO.class);

@Test
void givenPullPaymentNoticeDTOWhenCallMapperThenReturnPaymentNoticeDTO() {
//given
PullPaymentNoticeDTO pullPaymentNoticeDTO = PullPaymentNoticeDTOFaker.mockInstance(true);
PaymentNoticeDTO paymentNoticeDTO = PaymentNoticeDTOFaker.mockInstance(true);

Mockito.when(pullPaymentNoticeStatus2PaymentNoticeStatusMock.toPaymentNoticeStatus(pullPaymentNoticeDTO.getStatus())).thenReturn(PaymentNoticeStatus.VALID);
Mockito.when(pullPaymentOptionDTO2PaymentOptionDTOMock.toPaymentOptionDTO(pullPaymentNoticeDTO.getPaymentOptions().get(0))).thenReturn(paymentNoticeDTO.getPaymentOptions().get(0));
//when
PaymentNoticeDTO result = mapper.toPaymentNoticeDTO(pullPaymentNoticeDTO);
//then
Assertions.assertNotNull(result);
Assertions.assertEquals(paymentNoticeDTO, result);
Mockito.verify(pullPaymentNoticeStatus2PaymentNoticeStatusMock).toPaymentNoticeStatus(any());
Mockito.verify(pullPaymentOptionDTO2PaymentOptionDTOMock).toPaymentOptionDTO(any());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package it.gov.pagopa.arc.dto.mapper.pullpayment;

import it.gov.pagopa.arc.connector.pullpayment.enums.PullPaymentNoticeStatus;
import it.gov.pagopa.arc.model.generated.PaymentNoticeStatus;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mapstruct.factory.Mappers;

class PullPaymentNoticeStatus2PaymentNoticeStatusTest {
private final PullPaymentNoticeStatus2PaymentNoticeStatus mapper = Mappers.getMapper(PullPaymentNoticeStatus2PaymentNoticeStatus.class);

@Test
void givenPullPaymentNoticeStatusWhenCallMapperThenReturnPaymentNoticeStatus() {
//given
PullPaymentNoticeStatus valid = PullPaymentNoticeStatus.VALID;
//when
PaymentNoticeStatus result = mapper.toPaymentNoticeStatus(valid);
//then
Assertions.assertNotNull(result);
Assertions.assertEquals(PaymentNoticeStatus.VALID, result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package it.gov.pagopa.arc.dto.mapper.pullpayment;

import it.gov.pagopa.arc.connector.pullpayment.dto.PullPaymentInstallmentDTO;
import it.gov.pagopa.arc.connector.pullpayment.dto.PullPaymentOptionDTO;
import it.gov.pagopa.arc.fakers.connector.pullPayment.PullPaymentInstallmentDTOFaker;
import it.gov.pagopa.arc.fakers.connector.pullPayment.PullPaymentOptionDTOFaker;
import it.gov.pagopa.arc.fakers.paymentNotices.InstallmentDTOFaker;
import it.gov.pagopa.arc.model.generated.InstallmentDTO;
import it.gov.pagopa.arc.model.generated.PaymentOptionDTO;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

import java.time.LocalDateTime;
import java.util.List;

import static org.mockito.ArgumentMatchers.any;

@ExtendWith(MockitoExtension.class)
class PullPaymentOptionDTO2PaymentOptionDTOTest {
@Mock
private PullPaymentInstallmentDTO2InstallmentDTO pullPaymentInstallmentDTO2InstallmentDTOMock;

@InjectMocks
private PullPaymentOptionDTO2PaymentOptionDTOImpl mapper;
@Test
void givenPullPaymentOptionDTOWhenCallMapperThenReturnPaymentOptionDTO() {
//given
PullPaymentInstallmentDTO pullPaymentInstallmentDTO = PullPaymentInstallmentDTOFaker.mockInstance();
PullPaymentOptionDTO pullPaymentOptionDTO = PullPaymentOptionDTOFaker.mockInstance(pullPaymentInstallmentDTO, false);
InstallmentDTO installmentDTO = InstallmentDTOFaker.mockInstance();

Mockito.when(pullPaymentInstallmentDTO2InstallmentDTOMock.toInstallmentDTO(pullPaymentInstallmentDTO)).thenReturn(installmentDTO);
//when
PaymentOptionDTO result = mapper.toPaymentOptionDTO(pullPaymentOptionDTO);
//then
Assertions.assertAll(() -> {
Assertions.assertNotNull(result);
Assertions.assertEquals("Test Pull - unica opzione", result.getDescription());
Assertions.assertEquals(1, result.getNumberOfInstallments());
Assertions.assertEquals(120L, result.getAmount());
Assertions.assertEquals(LocalDateTime.parse("2024-10-30T23:59:59"), result.getDueDate());
Assertions.assertFalse(result.getIsPartialPayment());
Assertions.assertFalse(result.getSwitchToExpired());
Assertions.assertEquals(List.of(installmentDTO), result.getInstallments());

Mockito.verify(pullPaymentInstallmentDTO2InstallmentDTOMock).toInstallmentDTO(any());
});

}
}
Loading