Skip to content

Commit

Permalink
feat: P4PU-201 added business logic to retrieve transaction details (#17
Browse files Browse the repository at this point in the history
)

* P4PU-201 added business logic to retrieve transaction details

* P4PU-201 added fakers

* P4PU-201 added tests

* P4PU-201 updated service

* P4PU-201 updated mapper and tests
  • Loading branch information
Giuseppe-LaManna authored Jul 2, 2024
1 parent 674f278 commit 87e85d6
Show file tree
Hide file tree
Showing 12 changed files with 209 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import it.gov.pagopa.arc.controller.generated.ArcTransactionsApi;
import it.gov.pagopa.arc.model.generated.TransactionDetailsDTO;
import it.gov.pagopa.arc.model.generated.TransactionsListDTO;
import it.gov.pagopa.arc.service.TransactionsService;
import org.springframework.http.HttpStatus;
Expand All @@ -21,4 +22,11 @@ public ResponseEntity<TransactionsListDTO> getTransactionsList(Integer page, Int
TransactionsListDTO transactionsListDTO = transactionsService.retrieveTransactionsList(page,size,filter);
return new ResponseEntity<>(transactionsListDTO, HttpStatus.OK);
}

@Override
public ResponseEntity<TransactionDetailsDTO> getTransactionDetails(String transactionId) {
TransactionDetailsDTO transactionDetailsDTO = transactionsService.retrieveTransactionDetails(transactionId);
return new ResponseEntity<>(transactionDetailsDTO, HttpStatus.OK);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,23 @@
@Component
public class BizEventsTransactionDetails2TransactionDetailsDTO {
private final BizEventsInfoTransaction2InfoTransactionDTO transactionInfoMapper;
private final BizEventsCartItem2CartItemDTO cartItemDTOMapper;

public BizEventsTransactionDetails2TransactionDetailsDTO(BizEventsInfoTransaction2InfoTransactionDTO transactionInfoMapper) {
public BizEventsTransactionDetails2TransactionDetailsDTO(BizEventsInfoTransaction2InfoTransactionDTO transactionInfoMapper, BizEventsCartItem2CartItemDTO cartItemDTOMapper) {
this.transactionInfoMapper = transactionInfoMapper;
this.cartItemDTOMapper = cartItemDTOMapper;
}

public TransactionDetailsDTO apply(BizEventsTransactionDetailsDTO bizEventsTransactionDetailsDTO, List<CartItemDTO> cartsList){
public TransactionDetailsDTO apply(BizEventsTransactionDetailsDTO bizEventsTransactionDetailsDTO){
List<CartItemDTO> carts = bizEventsTransactionDetailsDTO
.getBizEventsCartsDTO()
.stream()
.map(cartItemDTOMapper::mapCart)
.toList();

return TransactionDetailsDTO.builder()
.infoTransaction(transactionInfoMapper.mapInfoTransaction(bizEventsTransactionDetailsDTO.getBizEventsInfoTransactionDTO()))
.carts(cartsList)
.carts(carts)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package it.gov.pagopa.arc.service;


import it.gov.pagopa.arc.model.generated.TransactionDetailsDTO;
import it.gov.pagopa.arc.model.generated.TransactionsListDTO;

public interface TransactionsService {
TransactionsListDTO retrieveTransactionsList(Integer page, Integer size, String filter);
TransactionDetailsDTO retrieveTransactionDetails(String transactionId);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package it.gov.pagopa.arc.service;

import it.gov.pagopa.arc.model.generated.TransactionDetailsDTO;
import it.gov.pagopa.arc.model.generated.TransactionsListDTO;
import it.gov.pagopa.arc.service.bizevents.BizEventsService;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -16,7 +17,14 @@ public TransactionsServiceImpl(BizEventsService bizEventsService) {

@Override
public TransactionsListDTO retrieveTransactionsList(Integer page, Integer size, String filter) {
log.info("[GET_TRANSACTIONS_LIST] The current user has requested to retrieve his list of transactions, with the current parameters: page {}, size {} and filter {}", page, size, filter);
return bizEventsService.retrieveTransactionsListFromBizEvents(page,size,filter);
}

@Override
public TransactionDetailsDTO retrieveTransactionDetails(String transactionId) {
log.info("[GET_TRANSACTION_DETAILS] The current user has requested to retrieve transaction details for transaction with ID {}", transactionId);
return bizEventsService.retrieveTransactionDetailsFromBizEvents(transactionId);
}
}

Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package it.gov.pagopa.arc.service.bizevents;

import it.gov.pagopa.arc.model.generated.TransactionDetailsDTO;
import it.gov.pagopa.arc.model.generated.TransactionsListDTO;

public interface BizEventsService {
TransactionsListDTO retrieveTransactionsListFromBizEvents(Integer page, Integer size, String filter);
TransactionDetailsDTO retrieveTransactionDetailsFromBizEvents(String transactionId);
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@
package it.gov.pagopa.arc.service.bizevents;

import it.gov.pagopa.arc.connector.bizevents.BizEventsConnector;
import it.gov.pagopa.arc.connector.bizevents.dto.BizEventsTransactionDetailsDTO;
import it.gov.pagopa.arc.connector.bizevents.dto.BizEventsTransactionsListDTO;
import it.gov.pagopa.arc.dto.mapper.BizEventsTransactionDTO2TransactionDTO;
import it.gov.pagopa.arc.dto.mapper.BizEventsTransactionDetails2TransactionDetailsDTO;
import it.gov.pagopa.arc.dto.mapper.BizEventsTransactionsListDTO2TransactionsListDTO;
import it.gov.pagopa.arc.model.generated.TransactionDTO;
import it.gov.pagopa.arc.model.generated.TransactionDetailsDTO;
import it.gov.pagopa.arc.model.generated.TransactionsListDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

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

@Service
@RequiredArgsConstructor
public class BizEventsServiceImpl implements BizEventsService{

@Value("${rest-client.biz-events.fake-fiscal-code}")
private final String fakeFiscalCode;
private final BizEventsConnector bizEventsConnector;
private final BizEventsTransactionDTO2TransactionDTO transactionDTOMapper;
private final BizEventsTransactionsListDTO2TransactionsListDTO transactionsListDTOMapper;

public BizEventsServiceImpl(@Value("${rest-client.biz-events.fake-fiscal-code}") String fakeFiscalCode,
BizEventsConnector bizEventsConnector,
BizEventsTransactionDTO2TransactionDTO transactionDTOMapper,
BizEventsTransactionsListDTO2TransactionsListDTO transactionsListDTOMapper) {
this.fakeFiscalCode = fakeFiscalCode;
this.bizEventsConnector = bizEventsConnector;
this.transactionDTOMapper = transactionDTOMapper;
this.transactionsListDTOMapper = transactionsListDTOMapper;
}
private final BizEventsTransactionDetails2TransactionDetailsDTO transactionDetailsDTOMapper;

@Override
public TransactionsListDTO retrieveTransactionsListFromBizEvents(Integer page, Integer size, String filter) {
Expand All @@ -44,4 +42,10 @@ public TransactionsListDTO retrieveTransactionsListFromBizEvents(Integer page, I
}
return transactionsListDTOMapper.apply(transactions, size);
}

@Override
public TransactionDetailsDTO retrieveTransactionDetailsFromBizEvents(String transactionId) {
BizEventsTransactionDetailsDTO bizEventsTransactionDetails = bizEventsConnector.getTransactionDetails(fakeFiscalCode, transactionId);
return transactionDetailsDTOMapper.apply(bizEventsTransactionDetails);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import it.gov.pagopa.arc.controller.generated.ArcTransactionsApi;
import it.gov.pagopa.arc.fakers.TransactionDetailsDTOFaker;
import it.gov.pagopa.arc.model.generated.TransactionDetailsDTO;
import it.gov.pagopa.arc.model.generated.TransactionsListDTO;
import it.gov.pagopa.arc.service.TransactionsService;
import it.gov.pagopa.arc.utils.TestUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
Expand All @@ -12,7 +15,6 @@
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import utils.TestUtils;

import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
Expand All @@ -26,6 +28,7 @@ class TransactionsControllerTest {
private static final int PAGE = 1;
private static final int SIZE = 2;
private static final String FILTER = "DUMMY_FILTER";
private static final String TRANSACTION_ID = "TRANSACTION_ID";

@Autowired
private ObjectMapper objectMapper;
Expand Down Expand Up @@ -58,4 +61,27 @@ void givenFiscalCodeWhenCallGetTransactionsListThenReturnTransactionList() throw
Assertions.assertEquals(transactionsListDTO,resultResponse);
Mockito.verify(transactionsServiceMock).retrieveTransactionsList(anyInt(),anyInt(),anyString());
}

@Test
void givenTransactionIdWhenCallGetTransactionDetailsThenReturnTransactionDetails() throws Exception {
//Given
TransactionDetailsDTO transactionDetailsDTO = TransactionDetailsDTOFaker.mockInstance();

Mockito.when(transactionsServiceMock.retrieveTransactionDetails(TRANSACTION_ID)).thenReturn(transactionDetailsDTO);

//When
MvcResult result = mockMvc.perform(
get("/arc/transactions/{transactionId}", TRANSACTION_ID)
).andExpect(status().is2xxSuccessful())
.andReturn();

TransactionDetailsDTO resultResponse = TestUtils.objectMapper.readValue(
result.getResponse().getContentAsString(),
TransactionDetailsDTO.class);

//Then
Assertions.assertNotNull(resultResponse);
Assertions.assertEquals(transactionDetailsDTO,resultResponse);
Mockito.verify(transactionsServiceMock).retrieveTransactionDetails(anyString());
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
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 it.gov.pagopa.arc.connector.bizevents.dto.BizEventsTransactionDetailsDTO;
import it.gov.pagopa.arc.fakers.TransactionDetailsDTOFaker;
import it.gov.pagopa.arc.fakers.bizEvents.BizEventsTransactionDetailsDTOFaker;
import it.gov.pagopa.arc.model.generated.TransactionDetailsDTO;
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.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.any;

@ExtendWith(MockitoExtension.class)
Expand All @@ -27,53 +22,34 @@ class BizEventsTransactionDetails2TransactionDetailsDTOTest {

@Mock
private BizEventsInfoTransaction2InfoTransactionDTO transactionInfoMapperMock;
@Mock
private BizEventsCartItem2CartItemDTO cartItemDTOMapperMock;

@BeforeEach
void setUp() {
transactionDetailsMapper = new BizEventsTransactionDetails2TransactionDetailsDTO(transactionInfoMapperMock);
transactionDetailsMapper = new BizEventsTransactionDetails2TransactionDetailsDTO(transactionInfoMapperMock, cartItemDTOMapperMock);
}

@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<BizEventsCartItemDTO> bizEventsCartsList = List.of(bizEventsCartItem1, bizEventsCartItem2);

BizEventsTransactionDetailsDTO bizEventsTransactionDetailsDTO = BizEventsTransactionDetailsDTO.builder()
.bizEventsInfoTransactionDTO(bizEventsInfoTransaction)
.bizEventsCartsDTO(bizEventsCartsList)
.build();

BizEventsTransactionDetailsDTO bizEventsTransactionDetailsDTO = BizEventsTransactionDetailsDTOFaker.mockInstance();

//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<CartItemDTO> cartsList = List.of(cartItem1, cartItem2);
TransactionDetailsDTO transactionDetailsDTO = TransactionDetailsDTOFaker.mockInstance();

Mockito.when(transactionInfoMapperMock.mapInfoTransaction(bizEventsInfoTransaction)).thenReturn(infoTransaction);
Mockito.when(transactionInfoMapperMock.mapInfoTransaction(bizEventsTransactionDetailsDTO.getBizEventsInfoTransactionDTO())).thenReturn(transactionDetailsDTO.getInfoTransaction());
Mockito.when(cartItemDTOMapperMock.mapCart(bizEventsTransactionDetailsDTO.getBizEventsCartsDTO().get(0))).thenReturn(transactionDetailsDTO.getCarts().get(0));
Mockito.when(cartItemDTOMapperMock.mapCart(bizEventsTransactionDetailsDTO.getBizEventsCartsDTO().get(1))).thenReturn(transactionDetailsDTO.getCarts().get(1));
//when
TransactionDetailsDTO result = transactionDetailsMapper.apply(bizEventsTransactionDetailsDTO, cartsList);
TransactionDetailsDTO result = transactionDetailsMapper.apply(bizEventsTransactionDetailsDTO);
//then
assertNotNull(result);
assertEquals(infoTransaction, result.getInfoTransaction());
System.out.println(infoTransaction.getWalletInfo());
System.out.println(result.getInfoTransaction().getWalletInfo());
assertEquals(transactionDetailsDTO.getInfoTransaction(), result.getInfoTransaction());
assertEquals(2, result.getCarts().size());
assertEquals( cartsList, result.getCarts());
assertEquals( transactionDetailsDTO.getCarts(), result.getCarts());
Mockito.verify(transactionInfoMapperMock).mapInfoTransaction(any());
Mockito.verify(cartItemDTOMapperMock,Mockito.times(2)).mapCart(any());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package it.gov.pagopa.arc.fakers;

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

import java.util.List;

public class TransactionDetailsDTOFaker {
public static TransactionDetailsDTO mockInstance(){
return mockInstanceBuilder().build();
}

public static TransactionDetailsDTO.TransactionDetailsDTOBuilder mockInstanceBuilder(){
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<CartItemDTO> cartsList = List.of(cartItem1, cartItem2);

return TransactionDetailsDTO.builder()
.infoTransaction(infoTransaction)
.carts(cartsList);
}

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

import it.gov.pagopa.arc.connector.bizevents.dto.*;
import it.gov.pagopa.arc.fakers.CommonUserDetailDTOFaker;
import it.gov.pagopa.arc.fakers.CommonWalletInfoDTOFaker;

import java.util.List;

public class BizEventsTransactionDetailsDTOFaker {
public static BizEventsTransactionDetailsDTO mockInstance(){
return mockInstanceBuilder().build();
}

public static BizEventsTransactionDetailsDTO.BizEventsTransactionDetailsDTOBuilder mockInstanceBuilder(){
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<BizEventsCartItemDTO> bizEventsCartsList = List.of(bizEventsCartItem1, bizEventsCartItem2);

return BizEventsTransactionDetailsDTO.builder()
.bizEventsInfoTransactionDTO(bizEventsInfoTransaction)
.bizEventsCartsDTO(bizEventsCartsList);
}
}
Loading

0 comments on commit 87e85d6

Please sign in to comment.