Skip to content

Commit

Permalink
P4PU-146 added faker and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Giuseppe-LaManna committed Jun 5, 2024
1 parent c5c8a6b commit 58777de
Show file tree
Hide file tree
Showing 9 changed files with 386 additions and 39 deletions.
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 @@ -28,7 +28,7 @@ public static Long euroToCents(String euroString){
}

/** To convert euro from String to double */
private static Double euroStringToLong(String euroString){
public static Double euroStringToLong(String euroString){
try {
NumberFormat nf = NumberFormat.getInstance(Locale.ITALIAN);
Number parse = nf.parse(euroString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void setUp() {
void givenHeaderAndParameterWhenCallBizEventsConnectorThenReturnTransactionList() {
//given
//when
BizEventsTransactionsListDTO bizEventsTransactionsListDTO = bizEventsConnector.getTransactionsList("DUMMY_FISCAL_CODE", "TOKEN", 1);
BizEventsTransactionsListDTO bizEventsTransactionsListDTO = bizEventsConnector.getTransactionsList("DUMMY_FISCAL_CODE", 1);

//then
assertEquals(1, bizEventsTransactionsListDTO.getTransactions().size());
Expand All @@ -77,7 +77,7 @@ void givenHeaderAndParameterWhenCallBizEventsConnectorThenReturnTransactionList(
void givenHeaderAndParameterWhenNotFoundThenReturnEmptyTransactionList() {
//given
//when
BizEventsTransactionsListDTO bizEventsTransactionsListDTO = bizEventsConnector.getTransactionsList("DUMMY_FISCAL_CODE_NOT_FOUND", "TOKEN", 2);
BizEventsTransactionsListDTO bizEventsTransactionsListDTO = bizEventsConnector.getTransactionsList("DUMMY_FISCAL_CODE_NOT_FOUND", 2);
//then
Assertions.assertEquals(0,bizEventsTransactionsListDTO.getTransactions().size());
Assertions.assertTrue(memoryAppender.getLoggedEvents().get(0).getFormattedMessage()
Expand All @@ -89,7 +89,7 @@ void givenHeaderAndParameterWhenErrorThenThrowBizEventsInvocationException() {
//When
//Then
Assertions.assertThrows(BizEventsInvocationException.class,
()-> bizEventsConnector.getTransactionsList("DUMMY_FISCAL_CODE_ERROR", "TOKEN", 2));
()-> bizEventsConnector.getTransactionsList("DUMMY_FISCAL_CODE_ERROR", 2));

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package it.gov.pagopa.arc.controller;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import it.gov.pagopa.arc.dto.TransactionDTO;
import it.gov.pagopa.arc.controller.generated.ArcTransactionsApi;
import it.gov.pagopa.arc.model.generated.TransactionsListDTO;
import it.gov.pagopa.arc.service.TransactionsService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand All @@ -14,17 +14,19 @@
import org.springframework.test.web.servlet.MvcResult;
import utils.TestUtils;

import java.time.ZonedDateTime;
import java.util.List;

import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@WebMvcTest(value = {
TransactionsController.class
ArcTransactionsApi.class
})
class TransactionsControllerTest {
private static final int PAGE = 1;
private static final int SIZE = 2;
private static final String FILTER = "DUMMY_FILTER";

@Autowired
private ObjectMapper objectMapper;
@Autowired
Expand All @@ -35,26 +37,25 @@ class TransactionsControllerTest {
@Test
void givenFiscalCodeWhenCallGetTransactionsListThenReturnTransactionList() throws Exception {
//Given
ZonedDateTime transactionDate = ZonedDateTime.parse("2024-05-09T14:44:22.854Z");
List<TransactionDTO> transactionDTOList = List.of(
new TransactionDTO("TRX_ID","PAYEE_TAX_CODE","20.80",transactionDate,false,true,true));
TransactionsListDTO transactionsListDTO = TransactionsListDTO.builder().build();
Mockito.when(transactionsServiceMock.retrieveTransactionsList(PAGE,SIZE,FILTER)).thenReturn(transactionsListDTO);

Mockito.when(transactionsServiceMock.retrieveTransactionsList("DUMMY_FISCAL_CODE")).thenReturn(transactionDTOList);
System.out.println(transactionDTOList);
//When
MvcResult result = mockMvc.perform(get("/arc/transactions")
.header("x-fiscal-code", "DUMMY_FISCAL_CODE")
MvcResult result = mockMvc.perform(
get("/arc/transactions")
.param("page", String.valueOf(PAGE))
.param("size", String.valueOf(SIZE))
.param("filter", FILTER)
).andExpect(status().is2xxSuccessful())
.andExpect(jsonPath("$[0].transactionDate").value("2024-05-09T14:44:22.854Z"))
.andReturn();

List<TransactionDTO> resultResponse = TestUtils.objectMapper.readValue(
TransactionsListDTO resultResponse = TestUtils.objectMapper.readValue(
result.getResponse().getContentAsString(),
new TypeReference<>() {});
TransactionsListDTO.class);

//Then
Assertions.assertNotNull(resultResponse);
Assertions.assertEquals(transactionDTOList,resultResponse);
Mockito.verify(transactionsServiceMock).retrieveTransactionsList(anyString());
Assertions.assertEquals(transactionsListDTO,resultResponse);
Mockito.verify(transactionsServiceMock).retrieveTransactionsList(anyInt(),anyInt(),anyString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
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.model.generated.TransactionDTO;
import it.gov.pagopa.arc.utils.Utilities;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class BizEventsTransactionDTO2TransactionDTOTest {

private BizEventsTransactionDTO2TransactionDTO transactionDTOMapper;

@BeforeEach
void setUp() {
transactionDTOMapper = new BizEventsTransactionDTO2TransactionDTO();
}

@Test
void givenApplyWhenBizEventsTransactionDTOIsCartFalseThenReturnMappedDTO() {
//given
BizEventsTransactionDTO bizEventsTransaction = BizEventsTransactionDTOFaker.mockInstance(1,false);
//when
TransactionDTO dtoMapped = transactionDTOMapper.apply(bizEventsTransaction);

//then
commonAssert(bizEventsTransaction, dtoMapped);
}

@Test
void givenApplyWhenBizEventsTransactionDTOIsCartTrueThenReturnMappedDTO() {
//given
BizEventsTransactionDTO bizEventsTransaction = BizEventsTransactionDTOFaker.mockInstance(1,true);
//when
TransactionDTO dtoMapped = transactionDTOMapper.apply(bizEventsTransaction);

//then
commonAssert(bizEventsTransaction, dtoMapped);
}

private static void commonAssert(BizEventsTransactionDTO bizEventsTransaction, TransactionDTO dtoMapped) {
assertAll( () -> {
assertEquals(bizEventsTransaction.getTransactionId(), dtoMapped.getTransactionId());
assertEquals(bizEventsTransaction.getPayeeName(), dtoMapped.getPayeeName());
assertEquals(bizEventsTransaction.getPayeeTaxCode(), dtoMapped.getPayeeTaxCode());
assertEquals(Utilities.euroToCents(bizEventsTransaction.getAmount()), dtoMapped.getAmount());
assertEquals(Utilities.dateStringToZonedDateTime(bizEventsTransaction.getTransactionDate()), dtoMapped.getTransactionDate());
assertEquals(bizEventsTransaction.getIsCart(), dtoMapped.getIsCart());
assertEquals(bizEventsTransaction.getIsPayer(), dtoMapped.getPayedByMe());
assertEquals(bizEventsTransaction.getIsDebtor(), dtoMapped.getRegisteredToMe());
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package it.gov.pagopa.arc.dto.mapper;

import it.gov.pagopa.arc.fakers.TransactionDTOFaker;
import it.gov.pagopa.arc.model.generated.TransactionDTO;
import it.gov.pagopa.arc.model.generated.TransactionsListDTO;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertAll;

class BizEventsTransactionsListDTO2TransactionsListDTOTest {
private BizEventsTransactionsListDTO2TransactionsListDTO transactionsListDTOMapper;


@BeforeEach
void setUp() {
transactionsListDTOMapper = new BizEventsTransactionsListDTO2TransactionsListDTO();
}
@Test
void givenApplyWhenBizEventsTransactionsListDTOThenReturnMappedDTO() {
//given
List<TransactionDTO> transactionsList = List.of(
TransactionDTOFaker.mockInstance(1,false),
TransactionDTOFaker.mockInstance(2,true));
//when
TransactionsListDTO mappedTransactionsListDTO = transactionsListDTOMapper.apply(transactionsList, 2);
//then
assertAll( () -> {
Assertions.assertEquals(transactionsList, mappedTransactionsListDTO.getTransactions());
Assertions.assertEquals(1, mappedTransactionsListDTO.getCurrentPage());
Assertions.assertEquals(2, mappedTransactionsListDTO.getItemsForPage());
Assertions.assertEquals(1, mappedTransactionsListDTO.getTotalPages());
Assertions.assertEquals(10, mappedTransactionsListDTO.getTotalItems());
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package it.gov.pagopa.arc.fakers;

import it.gov.pagopa.arc.connector.bizevents.dto.BizEventsTransactionDTO;

public class BizEventsTransactionDTOFaker {
public static BizEventsTransactionDTO mockInstance(Integer bias, boolean isCart){
return mockInstanceBuilder(bias, isCart).build();
}
public static BizEventsTransactionDTO.BizEventsTransactionDTOBuilder mockInstanceBuilder(Integer bias, boolean isCart) {
if (!isCart) {
return BizEventsTransactionDTO
.builder()
.transactionId("TRANSACTION_ID%d" .formatted(bias))
.payeeName("PAYEE_NAME%d" .formatted(bias))
.payeeTaxCode("PAYEE_TAX_CODE%d" .formatted(bias))
.amount("2.681,52")
.transactionDate("2024-05-31T13:07:25Z")
.isCart(false)
.isPayer(true)
.isDebtor(true);
} else {
return BizEventsTransactionDTO
.builder()
.transactionId("TRANSACTION_ID%d" .formatted(bias))
.payeeName("Pagamento Multiplo")
.payeeTaxCode("")
.transactionDate("2024-05-31T13:07:25Z")
.isCart(true)
.isPayer(false)
.isDebtor(true);
}
}
}
36 changes: 36 additions & 0 deletions src/test/java/it/gov/pagopa/arc/fakers/TransactionDTOFaker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package it.gov.pagopa.arc.fakers;

import it.gov.pagopa.arc.model.generated.TransactionDTO;

import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;

public class TransactionDTOFaker {
public static TransactionDTO mockInstance(Integer bias, boolean isCart){
return mockInstanceBuilder(bias, isCart).build();
}
public static TransactionDTO.TransactionDTOBuilder mockInstanceBuilder(Integer bias, boolean isCart) {
if (!isCart) {
return TransactionDTO
.builder()
.transactionId("TRANSACTION_ID%d" .formatted(bias))
.payeeName("PAYEE_NAME%d" .formatted(bias))
.payeeTaxCode("PAYEE_TAX_CODE%d" .formatted(bias))
.amount(268152L)
.transactionDate(ZonedDateTime.now().truncatedTo(ChronoUnit.MILLIS))
.isCart(false)
.payedByMe(true)
.registeredToMe(true);
} else {
return TransactionDTO
.builder()
.transactionId("TRANSACTION_ID%d" .formatted(bias))
.payeeName("Pagamento Multiplo")
.payeeTaxCode("")
.transactionDate(ZonedDateTime.now().truncatedTo(ChronoUnit.MILLIS))
.isCart(true)
.payedByMe(false)
.registeredToMe(true);
}
}
}
Loading

0 comments on commit 58777de

Please sign in to comment.