diff --git a/src/main/java/it/gov/pagopa/arc/controller/TransactionsControllerImpl.java b/src/main/java/it/gov/pagopa/arc/controller/TransactionsControllerImpl.java index 3823ba71..df8a509b 100644 --- a/src/main/java/it/gov/pagopa/arc/controller/TransactionsControllerImpl.java +++ b/src/main/java/it/gov/pagopa/arc/controller/TransactionsControllerImpl.java @@ -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; @@ -21,4 +22,11 @@ public ResponseEntity getTransactionsList(Integer page, Int TransactionsListDTO transactionsListDTO = transactionsService.retrieveTransactionsList(page,size,filter); return new ResponseEntity<>(transactionsListDTO, HttpStatus.OK); } + + @Override + public ResponseEntity getTransactionDetails(String transactionId) { + TransactionDetailsDTO transactionDetailsDTO = transactionsService.retrieveTransactionDetails(transactionId); + return new ResponseEntity<>(transactionDetailsDTO, HttpStatus.OK); + } + } diff --git a/src/main/java/it/gov/pagopa/arc/dto/mapper/BizEventsTransactionDetails2TransactionDetailsDTO.java b/src/main/java/it/gov/pagopa/arc/dto/mapper/BizEventsTransactionDetails2TransactionDetailsDTO.java index 2dcd4e1e..2eaefd28 100644 --- a/src/main/java/it/gov/pagopa/arc/dto/mapper/BizEventsTransactionDetails2TransactionDetailsDTO.java +++ b/src/main/java/it/gov/pagopa/arc/dto/mapper/BizEventsTransactionDetails2TransactionDetailsDTO.java @@ -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 cartsList){ + public TransactionDetailsDTO apply(BizEventsTransactionDetailsDTO bizEventsTransactionDetailsDTO){ + List carts = bizEventsTransactionDetailsDTO + .getBizEventsCartsDTO() + .stream() + .map(cartItemDTOMapper::mapCart) + .toList(); + return TransactionDetailsDTO.builder() .infoTransaction(transactionInfoMapper.mapInfoTransaction(bizEventsTransactionDetailsDTO.getBizEventsInfoTransactionDTO())) - .carts(cartsList) + .carts(carts) .build(); } } diff --git a/src/main/java/it/gov/pagopa/arc/service/TransactionsService.java b/src/main/java/it/gov/pagopa/arc/service/TransactionsService.java index 95c46b7f..a1758d31 100644 --- a/src/main/java/it/gov/pagopa/arc/service/TransactionsService.java +++ b/src/main/java/it/gov/pagopa/arc/service/TransactionsService.java @@ -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); } diff --git a/src/main/java/it/gov/pagopa/arc/service/TransactionsServiceImpl.java b/src/main/java/it/gov/pagopa/arc/service/TransactionsServiceImpl.java index 5ae6bb15..fecfd039 100644 --- a/src/main/java/it/gov/pagopa/arc/service/TransactionsServiceImpl.java +++ b/src/main/java/it/gov/pagopa/arc/service/TransactionsServiceImpl.java @@ -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; @@ -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); + } } diff --git a/src/main/java/it/gov/pagopa/arc/service/bizevents/BizEventsService.java b/src/main/java/it/gov/pagopa/arc/service/bizevents/BizEventsService.java index c6249c4b..62a76731 100644 --- a/src/main/java/it/gov/pagopa/arc/service/bizevents/BizEventsService.java +++ b/src/main/java/it/gov/pagopa/arc/service/bizevents/BizEventsService.java @@ -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); } diff --git a/src/main/java/it/gov/pagopa/arc/service/bizevents/BizEventsServiceImpl.java b/src/main/java/it/gov/pagopa/arc/service/bizevents/BizEventsServiceImpl.java index 1cdede49..ceb45eb9 100644 --- a/src/main/java/it/gov/pagopa/arc/service/bizevents/BizEventsServiceImpl.java +++ b/src/main/java/it/gov/pagopa/arc/service/bizevents/BizEventsServiceImpl.java @@ -1,11 +1,15 @@ 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; @@ -13,21 +17,15 @@ 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) { @@ -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); + } } diff --git a/src/test/java/it/gov/pagopa/arc/controller/TransactionsControllerTest.java b/src/test/java/it/gov/pagopa/arc/controller/TransactionsControllerTest.java index 589d1430..6c89c48f 100644 --- a/src/test/java/it/gov/pagopa/arc/controller/TransactionsControllerTest.java +++ b/src/test/java/it/gov/pagopa/arc/controller/TransactionsControllerTest.java @@ -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; @@ -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; @@ -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; @@ -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()); + } } \ No newline at end of file diff --git a/src/test/java/it/gov/pagopa/arc/dto/mapper/BizEventsTransactionDetails2TransactionDetailsDTOTest.java b/src/test/java/it/gov/pagopa/arc/dto/mapper/BizEventsTransactionDetails2TransactionDetailsDTOTest.java index 74f97fe5..52dbbde8 100644 --- a/src/test/java/it/gov/pagopa/arc/dto/mapper/BizEventsTransactionDetails2TransactionDetailsDTOTest.java +++ b/src/test/java/it/gov/pagopa/arc/dto/mapper/BizEventsTransactionDetails2TransactionDetailsDTOTest.java @@ -1,13 +1,9 @@ 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; @@ -15,9 +11,8 @@ 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) @@ -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 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 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()); } } \ No newline at end of file diff --git a/src/test/java/it/gov/pagopa/arc/fakers/TransactionDetailsDTOFaker.java b/src/test/java/it/gov/pagopa/arc/fakers/TransactionDetailsDTOFaker.java new file mode 100644 index 00000000..bd083e84 --- /dev/null +++ b/src/test/java/it/gov/pagopa/arc/fakers/TransactionDetailsDTOFaker.java @@ -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 cartsList = List.of(cartItem1, cartItem2); + + return TransactionDetailsDTO.builder() + .infoTransaction(infoTransaction) + .carts(cartsList); + } + +} diff --git a/src/test/java/it/gov/pagopa/arc/fakers/bizEvents/BizEventsTransactionDetailsDTOFaker.java b/src/test/java/it/gov/pagopa/arc/fakers/bizEvents/BizEventsTransactionDetailsDTOFaker.java new file mode 100644 index 00000000..3f48ea61 --- /dev/null +++ b/src/test/java/it/gov/pagopa/arc/fakers/bizEvents/BizEventsTransactionDetailsDTOFaker.java @@ -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 bizEventsCartsList = List.of(bizEventsCartItem1, bizEventsCartItem2); + + return BizEventsTransactionDetailsDTO.builder() + .bizEventsInfoTransactionDTO(bizEventsInfoTransaction) + .bizEventsCartsDTO(bizEventsCartsList); + } +} diff --git a/src/test/java/it/gov/pagopa/arc/service/TransactionsServiceTest.java b/src/test/java/it/gov/pagopa/arc/service/TransactionsServiceTest.java index 77919bd0..c3dc28c6 100644 --- a/src/test/java/it/gov/pagopa/arc/service/TransactionsServiceTest.java +++ b/src/test/java/it/gov/pagopa/arc/service/TransactionsServiceTest.java @@ -1,9 +1,13 @@ package it.gov.pagopa.arc.service; +import ch.qos.logback.classic.LoggerContext; import it.gov.pagopa.arc.fakers.TransactionDTOFaker; +import it.gov.pagopa.arc.fakers.TransactionDetailsDTOFaker; 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 it.gov.pagopa.arc.service.bizevents.BizEventsService; +import it.gov.pagopa.arc.utils.MemoryAppender; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -11,6 +15,7 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import java.util.ArrayList; @@ -24,6 +29,9 @@ class TransactionsServiceTest { 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"; + + private MemoryAppender memoryAppender; @Autowired private TransactionsService transactionsService; @@ -34,6 +42,12 @@ class TransactionsServiceTest { @BeforeEach void setUp() { transactionsService = new TransactionsServiceImpl(bizEventsServiceMock); + ch.qos.logback.classic.Logger logger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger("it.gov.pagopa.arc.service.TransactionsServiceImpl"); + memoryAppender = new MemoryAppender(); + memoryAppender.setContext((LoggerContext) LoggerFactory.getILoggerFactory()); + logger.setLevel(ch.qos.logback.classic.Level.INFO); + logger.addAppender(memoryAppender); + memoryAppender.start(); } @Test @@ -68,6 +82,7 @@ void givenPageSizeFilterWhenCallRetrieveTransactionsListThenReturnTransactionLis Assertions.assertEquals(2, result.getItemsForPage()); Assertions.assertEquals(1, result.getTotalPages()); Assertions.assertEquals(10, result.getTotalItems()); + Assertions.assertTrue(memoryAppender.getLoggedEvents().get(0).getFormattedMessage().contains("[GET_TRANSACTIONS_LIST] The current user has requested to retrieve his list of transactions, with the current parameters: page 1, size 2 and filter DUMMY_FILTER")); Mockito.verify(bizEventsServiceMock).retrieveTransactionsListFromBizEvents(anyInt(),anyInt(),anyString()); } @@ -97,4 +112,19 @@ void givenPageSizeFilterWhenCallRetrieveTransactionsListFromBizEventsThenReturnE Assertions.assertEquals(10, result.getTotalItems()); Mockito.verify(bizEventsServiceMock).retrieveTransactionsListFromBizEvents(anyInt(),anyInt(),anyString()); } + + @Test + void givenTransactionIdWhenCallRetrieveTransactionDetailsThenReturnTransactionDetails() { + TransactionDetailsDTO transactionDetails = TransactionDetailsDTOFaker.mockInstance(); + //given + Mockito.when(bizEventsServiceMock.retrieveTransactionDetailsFromBizEvents(TRANSACTION_ID)).thenReturn(transactionDetails); + //when + TransactionDetailsDTO result = transactionsService.retrieveTransactionDetails(TRANSACTION_ID); + //then + Assertions.assertNotNull(result); + Assertions.assertEquals(2, result.getCarts().size()); + Assertions.assertEquals(transactionDetails, result); + Assertions.assertTrue(memoryAppender.getLoggedEvents().get(0).getFormattedMessage().contains("[GET_TRANSACTION_DETAILS] The current user has requested to retrieve transaction details for transaction with ID TRANSACTION_ID")); + Mockito.verify(bizEventsServiceMock).retrieveTransactionDetailsFromBizEvents(anyString()); + } } \ No newline at end of file diff --git a/src/test/java/it/gov/pagopa/arc/service/bizevents/BizEventsServiceImplTest.java b/src/test/java/it/gov/pagopa/arc/service/bizevents/BizEventsServiceImplTest.java index 95717558..f3ded0e5 100644 --- a/src/test/java/it/gov/pagopa/arc/service/bizevents/BizEventsServiceImplTest.java +++ b/src/test/java/it/gov/pagopa/arc/service/bizevents/BizEventsServiceImplTest.java @@ -2,12 +2,17 @@ import it.gov.pagopa.arc.connector.bizevents.BizEventsConnector; import it.gov.pagopa.arc.connector.bizevents.dto.BizEventsTransactionDTO; +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.fakers.BizEventsTransactionDTOFaker; import it.gov.pagopa.arc.fakers.TransactionDTOFaker; +import it.gov.pagopa.arc.fakers.TransactionDetailsDTOFaker; +import it.gov.pagopa.arc.fakers.bizEvents.BizEventsTransactionDTOFaker; +import it.gov.pagopa.arc.fakers.bizEvents.BizEventsTransactionDetailsDTOFaker; 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 org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; @@ -27,7 +32,7 @@ class BizEventsServiceImplTest { 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"; private static final String DUMMY_FISCAL_CODE = "DUMMY_FISCAL_CODE"; @Autowired @@ -39,10 +44,12 @@ class BizEventsServiceImplTest { private BizEventsTransactionDTO2TransactionDTO transactionDTOMapperMock; @Mock private BizEventsTransactionsListDTO2TransactionsListDTO transactionsListDTOMapperMock; + @Mock + private BizEventsTransactionDetails2TransactionDetailsDTO transactionDetailsDTOMapperMock; @BeforeEach void setUp() { - bizEventsService = new BizEventsServiceImpl(DUMMY_FISCAL_CODE,bizEventsConnectorMock , transactionDTOMapperMock, transactionsListDTOMapperMock); + bizEventsService = new BizEventsServiceImpl(DUMMY_FISCAL_CODE,bizEventsConnectorMock , transactionDTOMapperMock, transactionsListDTOMapperMock, transactionDetailsDTOMapperMock); } @Test @@ -135,4 +142,26 @@ void givenPageSizeFilterWhenCallRetrieveTransactionsListThenReturnEmptyTransacti Mockito.verify(transactionsListDTOMapperMock).apply(any(),anyInt()); } + + @Test + void givenTransactionIdWhenCallRetrieveTransactionDetailsFromBizEventsThenReturnTransactionDetails() { + //given + BizEventsTransactionDetailsDTO bizEventsTransactionDetails = BizEventsTransactionDetailsDTOFaker.mockInstance(); + TransactionDetailsDTO expectedResult = TransactionDetailsDTOFaker.mockInstance(); + + Mockito.when(bizEventsConnectorMock.getTransactionDetails(DUMMY_FISCAL_CODE,TRANSACTION_ID)).thenReturn(bizEventsTransactionDetails); + Mockito.when(transactionDetailsDTOMapperMock.apply(bizEventsTransactionDetails)).thenReturn(expectedResult); + //when + TransactionDetailsDTO result = bizEventsService.retrieveTransactionDetailsFromBizEvents(TRANSACTION_ID); + + //then + Assertions.assertNotNull(result); + Assertions.assertEquals(2, result.getCarts().size()); + Assertions.assertEquals(expectedResult.getInfoTransaction(), result.getInfoTransaction()); + Assertions.assertEquals(expectedResult.getCarts(), result.getCarts()); + Assertions.assertEquals(expectedResult, result); + + Mockito.verify(bizEventsConnectorMock).getTransactionDetails(anyString(),anyString()); + Mockito.verify(transactionDetailsDTOMapperMock).apply(any()); + } } \ No newline at end of file