From 3873ff396bb52b638c0a1f187f3a25d4ba247e51 Mon Sep 17 00:00:00 2001 From: giomella Date: Thu, 7 Mar 2024 15:08:56 +0100 Subject: [PATCH] [VAS-800] Added unit tests and fixed test packages --- .../model/ProblemJson.java | 8 +- .../service/CreditorInstitutionsService.java | 2 +- .../ApplicationTest.java | 3 +- .../CreditorInstitutionsControllerTest.java | 77 ----- .../OpenApiGenerationTest.java | 2 +- .../controller/BrokerPspsControllerTest.java | 4 +- .../controller/BrokersControllerTest.java | 6 +- .../CreditorInstitutionsControllerTest.java | 93 ++++++ .../controller/HomeControllerTest.java | 2 +- .../controller/IbanControllerTest.java | 4 +- .../controller/PspControllerTest.java | 4 +- .../exception/ErrorHandlerTest.java | 4 +- .../ConvertCanaliToChannelDetailsTest.java | 7 +- .../service/BrokerPSPsServiceTest.java | 9 +- .../service/BrokersServiceTest.java | 7 +- .../CreditorInstitutionsServiceTest.java | 273 ++++++++++++++++++ .../service/HealthCheckServiceTest.java | 3 +- .../service/IbanServiceTest.java | 9 +- .../service/PspServiceTest.java | 17 +- .../util/TestUtil.java | 2 +- .../CreditorInstitutionsServiceTest.java | 222 -------------- 21 files changed, 404 insertions(+), 354 deletions(-) rename src/test/java/it/gov/pagopa/apiconfig/{selfcareintegration => }/ApplicationTest.java (80%) delete mode 100644 src/test/java/it/gov/pagopa/apiconfig/controller/CreditorInstitutionsControllerTest.java rename src/test/java/it/gov/pagopa/apiconfig/{ => selfcareintegration}/OpenApiGenerationTest.java (97%) rename src/test/java/it/gov/pagopa/apiconfig/{ => selfcareintegration}/controller/BrokerPspsControllerTest.java (93%) rename src/test/java/it/gov/pagopa/apiconfig/{ => selfcareintegration}/controller/BrokersControllerTest.java (90%) create mode 100644 src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/controller/CreditorInstitutionsControllerTest.java rename src/test/java/it/gov/pagopa/apiconfig/{ => selfcareintegration}/controller/HomeControllerTest.java (96%) rename src/test/java/it/gov/pagopa/apiconfig/{ => selfcareintegration}/controller/IbanControllerTest.java (93%) rename src/test/java/it/gov/pagopa/apiconfig/{ => selfcareintegration}/controller/PspControllerTest.java (91%) rename src/test/java/it/gov/pagopa/apiconfig/{ => selfcareintegration}/exception/ErrorHandlerTest.java (97%) rename src/test/java/it/gov/pagopa/apiconfig/{ => selfcareintegration}/mapper/ConvertCanaliToChannelDetailsTest.java (89%) rename src/test/java/it/gov/pagopa/apiconfig/{ => selfcareintegration}/service/BrokerPSPsServiceTest.java (94%) rename src/test/java/it/gov/pagopa/apiconfig/{ => selfcareintegration}/service/BrokersServiceTest.java (96%) create mode 100644 src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/service/CreditorInstitutionsServiceTest.java rename src/test/java/it/gov/pagopa/apiconfig/{ => selfcareintegration}/service/HealthCheckServiceTest.java (93%) rename src/test/java/it/gov/pagopa/apiconfig/{ => selfcareintegration}/service/IbanServiceTest.java (93%) rename src/test/java/it/gov/pagopa/apiconfig/{ => selfcareintegration}/service/PspServiceTest.java (70%) rename src/test/java/it/gov/pagopa/apiconfig/{ => selfcareintegration}/util/TestUtil.java (99%) delete mode 100644 src/test/java/it/gov/pagopa/apiconfig/service/CreditorInstitutionsServiceTest.java diff --git a/src/main/java/it/gov/pagopa/apiconfig/selfcareintegration/model/ProblemJson.java b/src/main/java/it/gov/pagopa/apiconfig/selfcareintegration/model/ProblemJson.java index ae17ab16..bc772403 100644 --- a/src/main/java/it/gov/pagopa/apiconfig/selfcareintegration/model/ProblemJson.java +++ b/src/main/java/it/gov/pagopa/apiconfig/selfcareintegration/model/ProblemJson.java @@ -3,18 +3,20 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; import io.swagger.v3.oas.annotations.media.Schema; -import javax.validation.constraints.Max; -import javax.validation.constraints.Min; +import it.gov.pagopa.apiconfig.selfcareintegration.exception.ErrorHandler; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; import lombok.ToString; +import javax.validation.constraints.Max; +import javax.validation.constraints.Min; + /** * Object returned as response in case of an error. * - *

See {@link it.gov.pagopa.apiconfig.exception.ErrorHandler} + *

See {@link ErrorHandler} */ @Data @Builder(toBuilder = true) diff --git a/src/main/java/it/gov/pagopa/apiconfig/selfcareintegration/service/CreditorInstitutionsService.java b/src/main/java/it/gov/pagopa/apiconfig/selfcareintegration/service/CreditorInstitutionsService.java index b17fea73..b02c2e64 100644 --- a/src/main/java/it/gov/pagopa/apiconfig/selfcareintegration/service/CreditorInstitutionsService.java +++ b/src/main/java/it/gov/pagopa/apiconfig/selfcareintegration/service/CreditorInstitutionsService.java @@ -131,8 +131,8 @@ public List getCreditorInstitutionInfoList(List if (optionalPaList.isEmpty()) { throw new AppException(AppError.MULTIPLE_CREDITOR_INSTITUTIONS_NOT_FOUND, taxCodes); } - List paList = optionalPaList.get(); + List paList = optionalPaList.get(); if (paList.size() != taxCodes.size()) { throw new AppException(AppError.INTERNAL_SERVER_ERROR, "Retrieved less creditor institutions than expected"); } diff --git a/src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/ApplicationTest.java b/src/test/java/it/gov/pagopa/apiconfig/ApplicationTest.java similarity index 80% rename from src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/ApplicationTest.java rename to src/test/java/it/gov/pagopa/apiconfig/ApplicationTest.java index 8505391a..46ec1b8b 100644 --- a/src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/ApplicationTest.java +++ b/src/test/java/it/gov/pagopa/apiconfig/ApplicationTest.java @@ -1,8 +1,7 @@ -package it.gov.pagopa.apiconfig.selfcareintegration; +package it.gov.pagopa.apiconfig; import static org.junit.jupiter.api.Assertions.assertTrue; -import it.gov.pagopa.apiconfig.Application; import org.junit.jupiter.api.Test; class ApplicationTest { diff --git a/src/test/java/it/gov/pagopa/apiconfig/controller/CreditorInstitutionsControllerTest.java b/src/test/java/it/gov/pagopa/apiconfig/controller/CreditorInstitutionsControllerTest.java deleted file mode 100644 index 16bf2a77..00000000 --- a/src/test/java/it/gov/pagopa/apiconfig/controller/CreditorInstitutionsControllerTest.java +++ /dev/null @@ -1,77 +0,0 @@ -package it.gov.pagopa.apiconfig.controller; - -import static it.gov.pagopa.apiconfig.util.TestUtil.getMockApplicationCodesList; -import static it.gov.pagopa.apiconfig.util.TestUtil.getMockSegregationCodesList; -import static it.gov.pagopa.apiconfig.util.TestUtil.getMockStationDetailsList; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyBoolean; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.when; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; - -import it.gov.pagopa.apiconfig.Application; -import it.gov.pagopa.apiconfig.selfcareintegration.service.CreditorInstitutionsService; -import java.io.IOException; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.CsvSource; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.data.domain.PageRequest; -import org.springframework.http.MediaType; -import org.springframework.test.web.servlet.MockMvc; - -@SpringBootTest(classes = Application.class) -@AutoConfigureMockMvc -class CreditorInstitutionsControllerTest { - - @Autowired private MockMvc mvc; - - @MockBean private CreditorInstitutionsService creditorInstitutionsService; - - @BeforeEach - void setUp() throws IOException {} - - @ParameterizedTest - @CsvSource({ - "/creditorinstitutions/1234/stations?limit=50&page=0", - }) - void testGetStations(String url) throws Exception { - when(creditorInstitutionsService.getStationsDetailsFromCreditorInstitution( - "1234", PageRequest.of(0, 50))) - .thenReturn(getMockStationDetailsList()); - mvc.perform(get(url).contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()) - .andExpect(content().contentType(MediaType.APPLICATION_JSON)); - } - - @ParameterizedTest - @CsvSource({ - "/creditorinstitutions/1234/applicationcodes", - }) - void testGetApplicationCodes(String url) throws Exception { - when(creditorInstitutionsService.getApplicationCodesFromCreditorInstitution( - anyString(), anyBoolean())) - .thenReturn(getMockApplicationCodesList()); - mvc.perform(get(url).contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()) - .andExpect(content().contentType(MediaType.APPLICATION_JSON)); - } - - @ParameterizedTest - @CsvSource({ - "/creditorinstitutions/1234/segregationcodes", - }) - void testGetSegregationCodes(String url) throws Exception { - when(creditorInstitutionsService.getSegregationCodesFromCreditorInstitution( - anyString(), anyBoolean(), any())) - .thenReturn(getMockSegregationCodesList()); - mvc.perform(get(url).contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()) - .andExpect(content().contentType(MediaType.APPLICATION_JSON)); - } -} diff --git a/src/test/java/it/gov/pagopa/apiconfig/OpenApiGenerationTest.java b/src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/OpenApiGenerationTest.java similarity index 97% rename from src/test/java/it/gov/pagopa/apiconfig/OpenApiGenerationTest.java rename to src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/OpenApiGenerationTest.java index 819b94c6..5f903aa4 100644 --- a/src/test/java/it/gov/pagopa/apiconfig/OpenApiGenerationTest.java +++ b/src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/OpenApiGenerationTest.java @@ -1,4 +1,4 @@ -package it.gov.pagopa.apiconfig; +package it.gov.pagopa.apiconfig.selfcareintegration; import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.jupiter.api.Test; diff --git a/src/test/java/it/gov/pagopa/apiconfig/controller/BrokerPspsControllerTest.java b/src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/controller/BrokerPspsControllerTest.java similarity index 93% rename from src/test/java/it/gov/pagopa/apiconfig/controller/BrokerPspsControllerTest.java rename to src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/controller/BrokerPspsControllerTest.java index 12f30893..aaffdd11 100644 --- a/src/test/java/it/gov/pagopa/apiconfig/controller/BrokerPspsControllerTest.java +++ b/src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/controller/BrokerPspsControllerTest.java @@ -1,6 +1,6 @@ -package it.gov.pagopa.apiconfig.controller; +package it.gov.pagopa.apiconfig.selfcareintegration.controller; -import static it.gov.pagopa.apiconfig.util.TestUtil.getMockChannelDetailsList; +import static it.gov.pagopa.apiconfig.selfcareintegration.util.TestUtil.getMockChannelDetailsList; import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; diff --git a/src/test/java/it/gov/pagopa/apiconfig/controller/BrokersControllerTest.java b/src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/controller/BrokersControllerTest.java similarity index 90% rename from src/test/java/it/gov/pagopa/apiconfig/controller/BrokersControllerTest.java rename to src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/controller/BrokersControllerTest.java index fb198895..0a62bc51 100644 --- a/src/test/java/it/gov/pagopa/apiconfig/controller/BrokersControllerTest.java +++ b/src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/controller/BrokersControllerTest.java @@ -1,4 +1,4 @@ -package it.gov.pagopa.apiconfig.controller; +package it.gov.pagopa.apiconfig.selfcareintegration.controller; import it.gov.pagopa.apiconfig.Application; import it.gov.pagopa.apiconfig.selfcareintegration.service.BrokersService; @@ -15,8 +15,8 @@ import java.io.IOException; -import static it.gov.pagopa.apiconfig.util.TestUtil.getMockCreditorInstitutionDetails; -import static it.gov.pagopa.apiconfig.util.TestUtil.getMockStationDetailsList; +import static it.gov.pagopa.apiconfig.selfcareintegration.util.TestUtil.getMockCreditorInstitutionDetails; +import static it.gov.pagopa.apiconfig.selfcareintegration.util.TestUtil.getMockStationDetailsList; import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; diff --git a/src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/controller/CreditorInstitutionsControllerTest.java b/src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/controller/CreditorInstitutionsControllerTest.java new file mode 100644 index 00000000..50b1c8a5 --- /dev/null +++ b/src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/controller/CreditorInstitutionsControllerTest.java @@ -0,0 +1,93 @@ +package it.gov.pagopa.apiconfig.selfcareintegration.controller; + +import com.fasterxml.jackson.databind.ObjectMapper; +import it.gov.pagopa.apiconfig.Application; +import it.gov.pagopa.apiconfig.selfcareintegration.service.CreditorInstitutionsService; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.data.domain.PageRequest; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +import java.util.Collections; + +import static it.gov.pagopa.apiconfig.selfcareintegration.util.TestUtil.getMockApplicationCodesList; +import static it.gov.pagopa.apiconfig.selfcareintegration.util.TestUtil.getMockSegregationCodesList; +import static it.gov.pagopa.apiconfig.selfcareintegration.util.TestUtil.getMockStationDetailsList; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.when; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@SpringBootTest(classes = Application.class) +@AutoConfigureMockMvc +class CreditorInstitutionsControllerTest { + + @Autowired + private MockMvc mvc; + + @MockBean + private CreditorInstitutionsService creditorInstitutionsService; + + private final ObjectMapper objectMapper = new ObjectMapper(); + + @ParameterizedTest + @CsvSource({ + "/creditorinstitutions/1234/stations?limit=50&page=0", + }) + void testGetStations(String url) throws Exception { + when(creditorInstitutionsService.getStationsDetailsFromCreditorInstitution( + "1234", PageRequest.of(0, 50))) + .thenReturn(getMockStationDetailsList()); + mvc.perform(get(url).contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)); + } + + @ParameterizedTest + @CsvSource({ + "/creditorinstitutions/1234/applicationcodes", + }) + void testGetApplicationCodes(String url) throws Exception { + when(creditorInstitutionsService.getApplicationCodesFromCreditorInstitution( + anyString(), anyBoolean())) + .thenReturn(getMockApplicationCodesList()); + mvc.perform(get(url).contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)); + } + + @ParameterizedTest + @CsvSource({ + "/creditorinstitutions/1234/segregationcodes", + }) + void testGetSegregationCodes(String url) throws Exception { + when(creditorInstitutionsService.getSegregationCodesFromCreditorInstitution( + anyString(), anyBoolean(), any())) + .thenReturn(getMockSegregationCodesList()); + mvc.perform(get(url).contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)); + } + + @Test + void getCreditorInstitutionsTest() throws Exception { + when(creditorInstitutionsService.getSegregationCodesFromCreditorInstitution( + anyString(), anyBoolean(), any())) + .thenReturn(getMockSegregationCodesList()); + mvc.perform(post("/creditorinstitutions/") + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(Collections.singletonList("00168480242")))) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)); + } +} diff --git a/src/test/java/it/gov/pagopa/apiconfig/controller/HomeControllerTest.java b/src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/controller/HomeControllerTest.java similarity index 96% rename from src/test/java/it/gov/pagopa/apiconfig/controller/HomeControllerTest.java rename to src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/controller/HomeControllerTest.java index 7b99caef..5722d082 100644 --- a/src/test/java/it/gov/pagopa/apiconfig/controller/HomeControllerTest.java +++ b/src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/controller/HomeControllerTest.java @@ -1,4 +1,4 @@ -package it.gov.pagopa.apiconfig.controller; +package it.gov.pagopa.apiconfig.selfcareintegration.controller; import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; diff --git a/src/test/java/it/gov/pagopa/apiconfig/controller/IbanControllerTest.java b/src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/controller/IbanControllerTest.java similarity index 93% rename from src/test/java/it/gov/pagopa/apiconfig/controller/IbanControllerTest.java rename to src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/controller/IbanControllerTest.java index d75376df..4ebe675a 100644 --- a/src/test/java/it/gov/pagopa/apiconfig/controller/IbanControllerTest.java +++ b/src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/controller/IbanControllerTest.java @@ -1,4 +1,4 @@ -package it.gov.pagopa.apiconfig.controller; +package it.gov.pagopa.apiconfig.selfcareintegration.controller; import it.gov.pagopa.apiconfig.Application; import it.gov.pagopa.apiconfig.selfcareintegration.service.IbansService; @@ -18,7 +18,7 @@ import java.util.List; import java.util.TimeZone; -import static it.gov.pagopa.apiconfig.util.TestUtil.getMockIbanList; +import static it.gov.pagopa.apiconfig.selfcareintegration.util.TestUtil.getMockIbanList; import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; diff --git a/src/test/java/it/gov/pagopa/apiconfig/controller/PspControllerTest.java b/src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/controller/PspControllerTest.java similarity index 91% rename from src/test/java/it/gov/pagopa/apiconfig/controller/PspControllerTest.java rename to src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/controller/PspControllerTest.java index b5edbb1c..73d67142 100644 --- a/src/test/java/it/gov/pagopa/apiconfig/controller/PspControllerTest.java +++ b/src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/controller/PspControllerTest.java @@ -1,4 +1,4 @@ -package it.gov.pagopa.apiconfig.controller; +package it.gov.pagopa.apiconfig.selfcareintegration.controller; import it.gov.pagopa.apiconfig.Application; import it.gov.pagopa.apiconfig.selfcareintegration.service.PspService; @@ -15,7 +15,7 @@ import java.io.IOException; -import static it.gov.pagopa.apiconfig.util.TestUtil.getMockChannelDetailsList; +import static it.gov.pagopa.apiconfig.selfcareintegration.util.TestUtil.getMockChannelDetailsList; import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; diff --git a/src/test/java/it/gov/pagopa/apiconfig/exception/ErrorHandlerTest.java b/src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/exception/ErrorHandlerTest.java similarity index 97% rename from src/test/java/it/gov/pagopa/apiconfig/exception/ErrorHandlerTest.java rename to src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/exception/ErrorHandlerTest.java index 2554226f..7fc1b9d1 100644 --- a/src/test/java/it/gov/pagopa/apiconfig/exception/ErrorHandlerTest.java +++ b/src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/exception/ErrorHandlerTest.java @@ -1,12 +1,10 @@ -package it.gov.pagopa.apiconfig.exception; +package it.gov.pagopa.apiconfig.selfcareintegration.exception; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.mockito.Mockito.when; import it.gov.pagopa.apiconfig.Application; -import it.gov.pagopa.apiconfig.selfcareintegration.exception.AppException; -import it.gov.pagopa.apiconfig.selfcareintegration.exception.ErrorHandler; import it.gov.pagopa.apiconfig.selfcareintegration.model.ProblemJson; import java.sql.SQLException; import org.hibernate.exception.ConstraintViolationException; diff --git a/src/test/java/it/gov/pagopa/apiconfig/mapper/ConvertCanaliToChannelDetailsTest.java b/src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/mapper/ConvertCanaliToChannelDetailsTest.java similarity index 89% rename from src/test/java/it/gov/pagopa/apiconfig/mapper/ConvertCanaliToChannelDetailsTest.java rename to src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/mapper/ConvertCanaliToChannelDetailsTest.java index 7b823993..9081da90 100644 --- a/src/test/java/it/gov/pagopa/apiconfig/mapper/ConvertCanaliToChannelDetailsTest.java +++ b/src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/mapper/ConvertCanaliToChannelDetailsTest.java @@ -1,13 +1,12 @@ -package it.gov.pagopa.apiconfig.mapper; +package it.gov.pagopa.apiconfig.selfcareintegration.mapper; -import static it.gov.pagopa.apiconfig.util.TestUtil.getMockChannelMapping; +import static it.gov.pagopa.apiconfig.selfcareintegration.util.TestUtil.getMockChannelMapping; import com.fasterxml.jackson.databind.ObjectMapper; import it.gov.pagopa.apiconfig.Application; -import it.gov.pagopa.apiconfig.selfcareintegration.mapper.ConvertCanaliToChannelDetails; import it.gov.pagopa.apiconfig.selfcareintegration.model.channel.ChannelDetails; import it.gov.pagopa.apiconfig.starter.entity.Canali; -import it.gov.pagopa.apiconfig.util.TestUtil; +import it.gov.pagopa.apiconfig.selfcareintegration.util.TestUtil; import java.io.IOException; import org.json.JSONException; import org.junit.jupiter.api.Test; diff --git a/src/test/java/it/gov/pagopa/apiconfig/service/BrokerPSPsServiceTest.java b/src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/service/BrokerPSPsServiceTest.java similarity index 94% rename from src/test/java/it/gov/pagopa/apiconfig/service/BrokerPSPsServiceTest.java rename to src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/service/BrokerPSPsServiceTest.java index e6622841..6162937d 100644 --- a/src/test/java/it/gov/pagopa/apiconfig/service/BrokerPSPsServiceTest.java +++ b/src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/service/BrokerPSPsServiceTest.java @@ -1,7 +1,7 @@ -package it.gov.pagopa.apiconfig.service; +package it.gov.pagopa.apiconfig.selfcareintegration.service; -import static it.gov.pagopa.apiconfig.util.TestUtil.getMockChannel; -import static it.gov.pagopa.apiconfig.util.TestUtil.getMockPSPBroker; +import static it.gov.pagopa.apiconfig.selfcareintegration.util.TestUtil.getMockChannel; +import static it.gov.pagopa.apiconfig.selfcareintegration.util.TestUtil.getMockPSPBroker; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.fail; import static org.mockito.ArgumentMatchers.any; @@ -13,11 +13,10 @@ import it.gov.pagopa.apiconfig.selfcareintegration.exception.AppException; import it.gov.pagopa.apiconfig.selfcareintegration.model.channel.ChannelDetailsList; import it.gov.pagopa.apiconfig.selfcareintegration.repository.ExtendedChannelRepository; -import it.gov.pagopa.apiconfig.selfcareintegration.service.BrokerPSPsService; import it.gov.pagopa.apiconfig.starter.entity.Canali; import it.gov.pagopa.apiconfig.starter.entity.IntermediariPsp; import it.gov.pagopa.apiconfig.starter.repository.IntermediariPspRepository; -import it.gov.pagopa.apiconfig.util.TestUtil; +import it.gov.pagopa.apiconfig.selfcareintegration.util.TestUtil; import java.io.IOException; import java.util.Optional; import java.util.TimeZone; diff --git a/src/test/java/it/gov/pagopa/apiconfig/service/BrokersServiceTest.java b/src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/service/BrokersServiceTest.java similarity index 96% rename from src/test/java/it/gov/pagopa/apiconfig/service/BrokersServiceTest.java rename to src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/service/BrokersServiceTest.java index 74d0f98b..989cd8c5 100644 --- a/src/test/java/it/gov/pagopa/apiconfig/service/BrokersServiceTest.java +++ b/src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/service/BrokersServiceTest.java @@ -1,17 +1,16 @@ -package it.gov.pagopa.apiconfig.service; +package it.gov.pagopa.apiconfig.selfcareintegration.service; import it.gov.pagopa.apiconfig.Application; import it.gov.pagopa.apiconfig.selfcareintegration.exception.AppException; import it.gov.pagopa.apiconfig.selfcareintegration.model.creditorinstitution.CreditorInstitutionDetails; import it.gov.pagopa.apiconfig.selfcareintegration.model.station.StationDetailsList; -import it.gov.pagopa.apiconfig.selfcareintegration.service.BrokersService; import it.gov.pagopa.apiconfig.starter.entity.IntermediariPa; import it.gov.pagopa.apiconfig.starter.entity.PaStazionePa; import it.gov.pagopa.apiconfig.starter.entity.Stazioni; import it.gov.pagopa.apiconfig.starter.repository.IntermediariPaRepository; import it.gov.pagopa.apiconfig.starter.repository.PaStazionePaRepository; import it.gov.pagopa.apiconfig.starter.repository.StazioniRepository; -import it.gov.pagopa.apiconfig.util.TestUtil; +import it.gov.pagopa.apiconfig.selfcareintegration.util.TestUtil; import org.assertj.core.util.Lists; import org.json.JSONException; import org.junit.jupiter.api.BeforeEach; @@ -35,7 +34,7 @@ import java.util.Optional; import java.util.TimeZone; -import static it.gov.pagopa.apiconfig.util.TestUtil.*; +import static it.gov.pagopa.apiconfig.selfcareintegration.util.TestUtil.*; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.fail; import static org.mockito.ArgumentMatchers.*; diff --git a/src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/service/CreditorInstitutionsServiceTest.java b/src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/service/CreditorInstitutionsServiceTest.java new file mode 100644 index 00000000..3e514d0f --- /dev/null +++ b/src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/service/CreditorInstitutionsServiceTest.java @@ -0,0 +1,273 @@ +package it.gov.pagopa.apiconfig.selfcareintegration.service; + +import it.gov.pagopa.apiconfig.selfcareintegration.config.MappingsConfiguration; +import it.gov.pagopa.apiconfig.selfcareintegration.exception.AppError; +import it.gov.pagopa.apiconfig.selfcareintegration.exception.AppException; +import it.gov.pagopa.apiconfig.selfcareintegration.model.code.CIAssociatedCodeList; +import it.gov.pagopa.apiconfig.selfcareintegration.model.creditorinstitution.CreditorInstitutionInfo; +import it.gov.pagopa.apiconfig.selfcareintegration.model.station.StationDetailsList; +import it.gov.pagopa.apiconfig.selfcareintegration.repository.ExtendedCreditorInstitutionStationRepository; +import it.gov.pagopa.apiconfig.starter.entity.Pa; +import it.gov.pagopa.apiconfig.starter.entity.PaStazionePa; +import it.gov.pagopa.apiconfig.starter.repository.PaRepository; +import it.gov.pagopa.apiconfig.selfcareintegration.util.TestUtil; +import org.assertj.core.util.Lists; +import org.json.JSONException; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.skyscreamer.jsonassert.JSONAssert; +import org.skyscreamer.jsonassert.JSONCompareMode; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.http.HttpStatus; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Optional; +import java.util.TimeZone; + +import static it.gov.pagopa.apiconfig.selfcareintegration.util.TestUtil.getMockPa; +import static it.gov.pagopa.apiconfig.selfcareintegration.util.TestUtil.getMockPaStazionePa; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.fail; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.Mockito.when; + +@SpringBootTest(classes = {CreditorInstitutionsService.class, MappingsConfiguration.class}) +class CreditorInstitutionsServiceTest { + + @MockBean + private PaRepository paRepository; + + @MockBean + private ExtendedCreditorInstitutionStationRepository ciStationRepository; + + @Autowired + private CreditorInstitutionsService creditorInstitutionsService; + + private final Pageable pageable = PageRequest.of(0, 10); + + @BeforeEach + void setup() { + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + } + + @Test + void getStationsDetailsCI_200() throws IOException, JSONException { + Page page = TestUtil.mockPage(Lists.newArrayList(getMockPaStazionePa()), 10, 0); + + when(paRepository.findByIdDominio("1234")).thenReturn(Optional.of(getMockPa())); + when(ciStationRepository.findByFkPa(anyLong(), any(Pageable.class))).thenReturn(page); + + StationDetailsList result = + creditorInstitutionsService.getStationsDetailsFromCreditorInstitution("1234", pageable); + String actual = TestUtil.toJson(result); + String expected = + TestUtil.readJsonFromFile("response/get_creditorinstitution_stations_details_ok1.json"); + JSONAssert.assertEquals(expected, actual, JSONCompareMode.STRICT); + } + + @Test + void getStationsDetailsCI_404() { + when(paRepository.findByIdDominio("12345")).thenReturn(Optional.empty()); + try { + creditorInstitutionsService.getStationsDetailsFromCreditorInstitution("12345", pageable); + } catch (AppException e) { + assertEquals(HttpStatus.NOT_FOUND, e.getHttpStatus()); + } catch (Exception e) { + fail(); + } + } + + @Test + void getApplicationCodes_noUsedIncluded_200() throws IOException, JSONException { + PaStazionePa stationWithoutApplicationCode = getMockPaStazionePa(); + stationWithoutApplicationCode.setProgressivo(null); + stationWithoutApplicationCode.getFkStazione().setIdStazione("noappcodestation"); + List stations = List.of(getMockPaStazionePa(), stationWithoutApplicationCode); + + when(paRepository.findByIdDominio("1234")).thenReturn(Optional.of(getMockPa())); + when(ciStationRepository.findByFkPa(anyLong())).thenReturn(stations); + + CIAssociatedCodeList result = + creditorInstitutionsService.getApplicationCodesFromCreditorInstitution("1234", false); + String actual = TestUtil.toJson(result); + String expected = + TestUtil.readJsonFromFile("response/get_creditorinstitution_applicationcodes_ok1.json"); + JSONAssert.assertEquals(expected, actual, JSONCompareMode.STRICT); + } + + @Test + void getApplicationCodes_usedIncluded_200() throws IOException, JSONException { + PaStazionePa stationWithoutApplicationCode = getMockPaStazionePa(); + stationWithoutApplicationCode.setProgressivo(null); + stationWithoutApplicationCode.getFkStazione().setIdStazione("noappcodestation"); + List stations = List.of(getMockPaStazionePa(), stationWithoutApplicationCode); + + when(paRepository.findByIdDominio("1234")).thenReturn(Optional.of(getMockPa())); + when(ciStationRepository.findByFkPa(anyLong())).thenReturn(stations); + + CIAssociatedCodeList result = + creditorInstitutionsService.getApplicationCodesFromCreditorInstitution("1234", true); + String actual = TestUtil.toJson(result); + String expected = + TestUtil.readJsonFromFile("response/get_creditorinstitution_applicationcodes_ok2.json"); + JSONAssert.assertEquals(expected, actual, JSONCompareMode.STRICT); + } + + @Test + void getApplicationCodes_404() { + when(paRepository.findByIdDominio("12345")).thenReturn(Optional.empty()); + try { + creditorInstitutionsService.getApplicationCodesFromCreditorInstitution("12345", false); + } catch (AppException e) { + assertEquals(HttpStatus.NOT_FOUND, e.getHttpStatus()); + } catch (Exception e) { + fail(); + } + } + + @Test + void getSegregationCodes_noUsedIncluded_200() throws IOException, JSONException { + PaStazionePa stationWithoutSegregationCode = getMockPaStazionePa(); + stationWithoutSegregationCode.setSegregazione(null); + stationWithoutSegregationCode.getFkStazione().setIdStazione("nosegcodestation"); + List stations = List.of(getMockPaStazionePa(), stationWithoutSegregationCode); + + when(paRepository.findByIdDominio("1234")).thenReturn(Optional.of(getMockPa())); + when(ciStationRepository.findByFkPa(anyLong())).thenReturn(stations); + + CIAssociatedCodeList result = + creditorInstitutionsService.getSegregationCodesFromCreditorInstitution("1234", false, null); + String actual = TestUtil.toJson(result); + String expected = + TestUtil.readJsonFromFile("response/get_creditorinstitution_segregationcodes_ok1.json"); + JSONAssert.assertEquals(expected, actual, JSONCompareMode.STRICT); + } + + @Test + void getSegregationCodes_usedIncluded_200() throws IOException, JSONException { + PaStazionePa stationWithoutSegregationCode = getMockPaStazionePa(); + stationWithoutSegregationCode.setSegregazione(null); + stationWithoutSegregationCode.getFkStazione().setIdStazione("nosegcodestation"); + List stations = List.of(getMockPaStazionePa(), stationWithoutSegregationCode); + + when(paRepository.findByIdDominio("1234")).thenReturn(Optional.of(getMockPa())); + when(ciStationRepository.findByFkPa(anyLong())).thenReturn(stations); + + CIAssociatedCodeList result = + creditorInstitutionsService.getSegregationCodesFromCreditorInstitution("1234", true, null); + String actual = TestUtil.toJson(result); + String expected = + TestUtil.readJsonFromFile("response/get_creditorinstitution_segregationcodes_ok2.json"); + JSONAssert.assertEquals(expected, actual, JSONCompareMode.STRICT); + } + + @Test + void getSegregationCodes_filterByService_200() throws IOException, JSONException { + PaStazionePa stationWithoutSegregationCode = getMockPaStazionePa(); + stationWithoutSegregationCode.setSegregazione(null); + stationWithoutSegregationCode.getFkStazione().setIdStazione("nosegcodestation"); + + PaStazionePa stationMock = getMockPaStazionePa(); + stationMock.getFkStazione().setServizio("mockedService"); + stationMock.getFkStazione().setIdStazione("fakestation"); + stationMock.setSegregazione(15L); + + List stations = List.of(stationMock, stationWithoutSegregationCode); + when(paRepository.findByIdDominio("1234")).thenReturn(Optional.of(getMockPa())); + when(ciStationRepository.findByFkPa(anyLong())).thenReturn(stations); + + CIAssociatedCodeList result = + creditorInstitutionsService.getSegregationCodesFromCreditorInstitution( + "1234", true, "mockedser"); + String actual = TestUtil.toJson(result); + String expected = + TestUtil.readJsonFromFile("response/get_creditorinstitution_segregationcodes_ok3.json"); + JSONAssert.assertEquals(expected, actual, JSONCompareMode.STRICT); + + // check if lower case check works + result = + creditorInstitutionsService.getSegregationCodesFromCreditorInstitution( + "1234", true, "MOCKEDSERVICE"); + actual = TestUtil.toJson(result); + expected = + TestUtil.readJsonFromFile("response/get_creditorinstitution_segregationcodes_ok3.json"); + JSONAssert.assertEquals(expected, actual, JSONCompareMode.STRICT); + + // check if null service are ignored + stationMock.getFkStazione().setServizio(null); + stations = List.of(stationMock, stationWithoutSegregationCode); + when(ciStationRepository.findByFkPa(anyLong())).thenReturn(stations); + result = + creditorInstitutionsService.getSegregationCodesFromCreditorInstitution( + "1234", true, "MOCKEDSERVICE"); + actual = TestUtil.toJson(result); + expected = + TestUtil.readJsonFromFile("response/get_creditorinstitution_segregationcodes_ok4.json"); + JSONAssert.assertEquals(expected, actual, JSONCompareMode.STRICT); + } + + @Test + void getSegregationCodes_404() { + when(paRepository.findByIdDominio("12345")).thenReturn(Optional.empty()); + try { + creditorInstitutionsService.getSegregationCodesFromCreditorInstitution("12345", false, null); + } catch (AppException e) { + assertEquals(HttpStatus.NOT_FOUND, e.getHttpStatus()); + } catch (Exception e) { + fail(); + } + } + + @Test + void getCreditorInstitutionInfoListSuccess() throws IOException { + Pa pa = getMockPa(); + List taxCodes = Collections.singletonList("168480242"); + when(paRepository.findByIdDominioIn(taxCodes)).thenReturn(Optional.of(Collections.singletonList(pa))); + + List result = creditorInstitutionsService.getCreditorInstitutionInfoList(taxCodes); + + assertNotNull(result); + assertEquals(1, result.size()); + CreditorInstitutionInfo ci = result.get(0); + assertEquals(pa.getIdDominio(), ci.getCreditorInstitutionCode()); + assertEquals(pa.getRagioneSociale(), ci.getBusinessName()); + } + + @Test + void getCreditorInstitutionInfoListFailNotFound() { + List taxCodes = Collections.singletonList("168480242"); + when(paRepository.findByIdDominioIn(taxCodes)).thenReturn(Optional.empty()); + + AppException e = assertThrows(AppException.class, () -> creditorInstitutionsService.getCreditorInstitutionInfoList(taxCodes)); + + assertNotNull(e); + assertEquals(AppError.MULTIPLE_CREDITOR_INSTITUTIONS_NOT_FOUND.title, e.getTitle()); + assertEquals(AppError.MULTIPLE_CREDITOR_INSTITUTIONS_NOT_FOUND.httpStatus, e.getHttpStatus()); + } + + @Test + void getCreditorInstitutionInfoListFailInternalServerError() throws IOException { + Pa pa = getMockPa(); + List taxCodes = new ArrayList<>(); + taxCodes.add("168480242"); + taxCodes.add("168480243"); + when(paRepository.findByIdDominioIn(taxCodes)).thenReturn(Optional.of(Collections.singletonList(pa))); + + AppException e = assertThrows(AppException.class, () -> creditorInstitutionsService.getCreditorInstitutionInfoList(taxCodes)); + + assertNotNull(e); + assertEquals(AppError.INTERNAL_SERVER_ERROR.title, e.getTitle()); + assertEquals(AppError.INTERNAL_SERVER_ERROR.httpStatus, e.getHttpStatus()); + } +} diff --git a/src/test/java/it/gov/pagopa/apiconfig/service/HealthCheckServiceTest.java b/src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/service/HealthCheckServiceTest.java similarity index 93% rename from src/test/java/it/gov/pagopa/apiconfig/service/HealthCheckServiceTest.java rename to src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/service/HealthCheckServiceTest.java index c0102d43..228fe5ab 100644 --- a/src/test/java/it/gov/pagopa/apiconfig/service/HealthCheckServiceTest.java +++ b/src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/service/HealthCheckServiceTest.java @@ -1,4 +1,4 @@ -package it.gov.pagopa.apiconfig.service; +package it.gov.pagopa.apiconfig.selfcareintegration.service; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -6,7 +6,6 @@ import static org.mockito.Mockito.when; import it.gov.pagopa.apiconfig.Application; -import it.gov.pagopa.apiconfig.selfcareintegration.service.HealthCheckService; import it.gov.pagopa.apiconfig.starter.repository.HealthCheckRepository; import java.util.Optional; import org.junit.jupiter.api.Test; diff --git a/src/test/java/it/gov/pagopa/apiconfig/service/IbanServiceTest.java b/src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/service/IbanServiceTest.java similarity index 93% rename from src/test/java/it/gov/pagopa/apiconfig/service/IbanServiceTest.java rename to src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/service/IbanServiceTest.java index a70dd157..77cf3110 100644 --- a/src/test/java/it/gov/pagopa/apiconfig/service/IbanServiceTest.java +++ b/src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/service/IbanServiceTest.java @@ -1,12 +1,11 @@ -package it.gov.pagopa.apiconfig.service; +package it.gov.pagopa.apiconfig.selfcareintegration.service; import it.gov.pagopa.apiconfig.Application; import it.gov.pagopa.apiconfig.selfcareintegration.model.PageInfo; import it.gov.pagopa.apiconfig.selfcareintegration.model.iban.IbansList; import it.gov.pagopa.apiconfig.selfcareintegration.repository.ExtendedIbanMasterRepository; -import it.gov.pagopa.apiconfig.selfcareintegration.service.IbansService; import it.gov.pagopa.apiconfig.starter.entity.IbanMaster; -import it.gov.pagopa.apiconfig.util.TestUtil; +import it.gov.pagopa.apiconfig.selfcareintegration.util.TestUtil; import org.assertj.core.util.Lists; import org.json.JSONException; import org.junit.jupiter.api.BeforeEach; @@ -27,8 +26,8 @@ import java.util.List; import java.util.TimeZone; -import static it.gov.pagopa.apiconfig.util.TestUtil.getMockIbanMaster; -import static it.gov.pagopa.apiconfig.util.TestUtil.getMockIbanMaster2; +import static it.gov.pagopa.apiconfig.selfcareintegration.util.TestUtil.getMockIbanMaster; +import static it.gov.pagopa.apiconfig.selfcareintegration.util.TestUtil.getMockIbanMaster2; import static org.junit.Assert.assertThrows; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyList; diff --git a/src/test/java/it/gov/pagopa/apiconfig/service/PspServiceTest.java b/src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/service/PspServiceTest.java similarity index 70% rename from src/test/java/it/gov/pagopa/apiconfig/service/PspServiceTest.java rename to src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/service/PspServiceTest.java index dcce40cf..92145fcf 100644 --- a/src/test/java/it/gov/pagopa/apiconfig/service/PspServiceTest.java +++ b/src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/service/PspServiceTest.java @@ -1,15 +1,10 @@ -package it.gov.pagopa.apiconfig.service; +package it.gov.pagopa.apiconfig.selfcareintegration.service; import it.gov.pagopa.apiconfig.Application; -import it.gov.pagopa.apiconfig.selfcareintegration.exception.AppException; -import it.gov.pagopa.apiconfig.selfcareintegration.model.channel.ChannelDetailsList; import it.gov.pagopa.apiconfig.selfcareintegration.repository.ExtendedChannelRepository; -import it.gov.pagopa.apiconfig.selfcareintegration.service.BrokerPSPsService; -import it.gov.pagopa.apiconfig.selfcareintegration.service.PspService; import it.gov.pagopa.apiconfig.starter.entity.Canali; -import it.gov.pagopa.apiconfig.starter.entity.IntermediariPsp; import it.gov.pagopa.apiconfig.starter.repository.IntermediariPspRepository; -import it.gov.pagopa.apiconfig.util.TestUtil; +import it.gov.pagopa.apiconfig.selfcareintegration.util.TestUtil; import org.assertj.core.util.Lists; import org.json.JSONException; import org.junit.jupiter.api.BeforeEach; @@ -22,18 +17,12 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.Pageable; -import org.springframework.http.HttpStatus; import java.io.IOException; -import java.util.Optional; import java.util.TimeZone; -import static it.gov.pagopa.apiconfig.util.TestUtil.getMockChannel; -import static it.gov.pagopa.apiconfig.util.TestUtil.getMockPSPBroker; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; +import static it.gov.pagopa.apiconfig.selfcareintegration.util.TestUtil.getMockChannel; import static org.mockito.ArgumentMatchers.*; import static org.mockito.Mockito.when; diff --git a/src/test/java/it/gov/pagopa/apiconfig/util/TestUtil.java b/src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/util/TestUtil.java similarity index 99% rename from src/test/java/it/gov/pagopa/apiconfig/util/TestUtil.java rename to src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/util/TestUtil.java index bb6554ae..72360d66 100644 --- a/src/test/java/it/gov/pagopa/apiconfig/util/TestUtil.java +++ b/src/test/java/it/gov/pagopa/apiconfig/selfcareintegration/util/TestUtil.java @@ -1,4 +1,4 @@ -package it.gov.pagopa.apiconfig.util; +package it.gov.pagopa.apiconfig.selfcareintegration.util; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; diff --git a/src/test/java/it/gov/pagopa/apiconfig/service/CreditorInstitutionsServiceTest.java b/src/test/java/it/gov/pagopa/apiconfig/service/CreditorInstitutionsServiceTest.java deleted file mode 100644 index 7d8f997c..00000000 --- a/src/test/java/it/gov/pagopa/apiconfig/service/CreditorInstitutionsServiceTest.java +++ /dev/null @@ -1,222 +0,0 @@ -package it.gov.pagopa.apiconfig.service; - -import static it.gov.pagopa.apiconfig.util.TestUtil.getMockPa; -import static it.gov.pagopa.apiconfig.util.TestUtil.getMockPaStazionePa; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; -import static org.mockito.ArgumentMatchers.*; -import static org.mockito.Mockito.*; - -import it.gov.pagopa.apiconfig.Application; -import it.gov.pagopa.apiconfig.selfcareintegration.exception.AppException; -import it.gov.pagopa.apiconfig.selfcareintegration.model.code.CIAssociatedCodeList; -import it.gov.pagopa.apiconfig.selfcareintegration.model.station.StationDetailsList; -import it.gov.pagopa.apiconfig.selfcareintegration.repository.ExtendedCreditorInstitutionStationRepository; -import it.gov.pagopa.apiconfig.selfcareintegration.service.CreditorInstitutionsService; -import it.gov.pagopa.apiconfig.starter.entity.PaStazionePa; -import it.gov.pagopa.apiconfig.starter.repository.PaRepository; -import it.gov.pagopa.apiconfig.util.TestUtil; -import java.io.IOException; -import java.util.List; -import java.util.Optional; -import java.util.TimeZone; - -import org.assertj.core.util.Lists; -import org.json.JSONException; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.mockito.InjectMocks; -import org.skyscreamer.jsonassert.JSONAssert; -import org.skyscreamer.jsonassert.JSONCompareMode; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageRequest; -import org.springframework.data.domain.Pageable; -import org.springframework.http.HttpStatus; - -@SpringBootTest(classes = Application.class) -class CreditorInstitutionsServiceTest { - - @MockBean private PaRepository paRepository; - - @MockBean private ExtendedCreditorInstitutionStationRepository ciStationRepository; - - @Autowired @InjectMocks private CreditorInstitutionsService creditorInstitutionsService; - - private Pageable pageable = PageRequest.of(0, 10); - - @BeforeEach - void setup() { - TimeZone.setDefault(TimeZone.getTimeZone("UTC")); - } - - @Test - void getStationsDetailsCI_200() throws IOException, JSONException { - Page page = TestUtil.mockPage(Lists.newArrayList(getMockPaStazionePa()), 10, 0); - - when(paRepository.findByIdDominio("1234")).thenReturn(Optional.of(getMockPa())); - when(ciStationRepository.findByFkPa(anyLong(), any(Pageable.class))).thenReturn(page); - - StationDetailsList result = - creditorInstitutionsService.getStationsDetailsFromCreditorInstitution("1234", pageable); - String actual = TestUtil.toJson(result); - String expected = - TestUtil.readJsonFromFile("response/get_creditorinstitution_stations_details_ok1.json"); - JSONAssert.assertEquals(expected, actual, JSONCompareMode.STRICT); - } - - @Test - void getStationsDetailsCI_404() throws IOException, JSONException { - when(paRepository.findByIdDominio("12345")).thenReturn(Optional.empty()); - try { - creditorInstitutionsService.getStationsDetailsFromCreditorInstitution("12345", pageable); - } catch (AppException e) { - assertEquals(HttpStatus.NOT_FOUND, e.getHttpStatus()); - } catch (Exception e) { - fail(); - } - } - - @Test - void getApplicationCodes_noUsedIncluded_200() throws IOException, JSONException { - PaStazionePa stationWithoutApplicationCode = getMockPaStazionePa(); - stationWithoutApplicationCode.setProgressivo(null); - stationWithoutApplicationCode.getFkStazione().setIdStazione("noappcodestation"); - List stations = List.of(getMockPaStazionePa(), stationWithoutApplicationCode); - - when(paRepository.findByIdDominio("1234")).thenReturn(Optional.of(getMockPa())); - when(ciStationRepository.findByFkPa(anyLong())).thenReturn(stations); - - CIAssociatedCodeList result = - creditorInstitutionsService.getApplicationCodesFromCreditorInstitution("1234", false); - String actual = TestUtil.toJson(result); - String expected = - TestUtil.readJsonFromFile("response/get_creditorinstitution_applicationcodes_ok1.json"); - JSONAssert.assertEquals(expected, actual, JSONCompareMode.STRICT); - } - - @Test - void getApplicationCodes_usedIncluded_200() throws IOException, JSONException { - PaStazionePa stationWithoutApplicationCode = getMockPaStazionePa(); - stationWithoutApplicationCode.setProgressivo(null); - stationWithoutApplicationCode.getFkStazione().setIdStazione("noappcodestation"); - List stations = List.of(getMockPaStazionePa(), stationWithoutApplicationCode); - - when(paRepository.findByIdDominio("1234")).thenReturn(Optional.of(getMockPa())); - when(ciStationRepository.findByFkPa(anyLong())).thenReturn(stations); - - CIAssociatedCodeList result = - creditorInstitutionsService.getApplicationCodesFromCreditorInstitution("1234", true); - String actual = TestUtil.toJson(result); - String expected = - TestUtil.readJsonFromFile("response/get_creditorinstitution_applicationcodes_ok2.json"); - JSONAssert.assertEquals(expected, actual, JSONCompareMode.STRICT); - } - - @Test - void getApplicationCodes_404() throws IOException, JSONException { - when(paRepository.findByIdDominio("12345")).thenReturn(Optional.empty()); - try { - creditorInstitutionsService.getApplicationCodesFromCreditorInstitution("12345", false); - } catch (AppException e) { - assertEquals(HttpStatus.NOT_FOUND, e.getHttpStatus()); - } catch (Exception e) { - fail(); - } - } - - @Test - void getSegregationCodes_noUsedIncluded_200() throws IOException, JSONException { - PaStazionePa stationWithoutSegregationCode = getMockPaStazionePa(); - stationWithoutSegregationCode.setSegregazione(null); - stationWithoutSegregationCode.getFkStazione().setIdStazione("nosegcodestation"); - List stations = List.of(getMockPaStazionePa(), stationWithoutSegregationCode); - - when(paRepository.findByIdDominio("1234")).thenReturn(Optional.of(getMockPa())); - when(ciStationRepository.findByFkPa(anyLong())).thenReturn(stations); - - CIAssociatedCodeList result = - creditorInstitutionsService.getSegregationCodesFromCreditorInstitution("1234", false, null); - String actual = TestUtil.toJson(result); - String expected = - TestUtil.readJsonFromFile("response/get_creditorinstitution_segregationcodes_ok1.json"); - JSONAssert.assertEquals(expected, actual, JSONCompareMode.STRICT); - } - - @Test - void getSegregationCodes_usedIncluded_200() throws IOException, JSONException { - PaStazionePa stationWithoutSegregationCode = getMockPaStazionePa(); - stationWithoutSegregationCode.setSegregazione(null); - stationWithoutSegregationCode.getFkStazione().setIdStazione("nosegcodestation"); - List stations = List.of(getMockPaStazionePa(), stationWithoutSegregationCode); - - when(paRepository.findByIdDominio("1234")).thenReturn(Optional.of(getMockPa())); - when(ciStationRepository.findByFkPa(anyLong())).thenReturn(stations); - - CIAssociatedCodeList result = - creditorInstitutionsService.getSegregationCodesFromCreditorInstitution("1234", true, null); - String actual = TestUtil.toJson(result); - String expected = - TestUtil.readJsonFromFile("response/get_creditorinstitution_segregationcodes_ok2.json"); - JSONAssert.assertEquals(expected, actual, JSONCompareMode.STRICT); - } - - @Test - void getSegregationCodes_filterByService_200() throws IOException, JSONException { - PaStazionePa stationWithoutSegregationCode = getMockPaStazionePa(); - stationWithoutSegregationCode.setSegregazione(null); - stationWithoutSegregationCode.getFkStazione().setIdStazione("nosegcodestation"); - - PaStazionePa stationMock = getMockPaStazionePa(); - stationMock.getFkStazione().setServizio("mockedService"); - stationMock.getFkStazione().setIdStazione("fakestation"); - stationMock.setSegregazione(15L); - - List stations = List.of(stationMock, stationWithoutSegregationCode); - when(paRepository.findByIdDominio("1234")).thenReturn(Optional.of(getMockPa())); - when(ciStationRepository.findByFkPa(anyLong())).thenReturn(stations); - - CIAssociatedCodeList result = - creditorInstitutionsService.getSegregationCodesFromCreditorInstitution( - "1234", true, "mockedser"); - String actual = TestUtil.toJson(result); - String expected = - TestUtil.readJsonFromFile("response/get_creditorinstitution_segregationcodes_ok3.json"); - JSONAssert.assertEquals(expected, actual, JSONCompareMode.STRICT); - - // check if lower case check works - result = - creditorInstitutionsService.getSegregationCodesFromCreditorInstitution( - "1234", true, "MOCKEDSERVICE"); - actual = TestUtil.toJson(result); - expected = - TestUtil.readJsonFromFile("response/get_creditorinstitution_segregationcodes_ok3.json"); - JSONAssert.assertEquals(expected, actual, JSONCompareMode.STRICT); - - // check if null service are ignored - stationMock.getFkStazione().setServizio(null); - stations = List.of(stationMock, stationWithoutSegregationCode); - when(ciStationRepository.findByFkPa(anyLong())).thenReturn(stations); - result = - creditorInstitutionsService.getSegregationCodesFromCreditorInstitution( - "1234", true, "MOCKEDSERVICE"); - actual = TestUtil.toJson(result); - expected = - TestUtil.readJsonFromFile("response/get_creditorinstitution_segregationcodes_ok4.json"); - JSONAssert.assertEquals(expected, actual, JSONCompareMode.STRICT); - } - - @Test - void getSegregationCodes_404() throws IOException, JSONException { - when(paRepository.findByIdDominio("12345")).thenReturn(Optional.empty()); - try { - creditorInstitutionsService.getSegregationCodesFromCreditorInstitution("12345", false, null); - } catch (AppException e) { - assertEquals(HttpStatus.NOT_FOUND, e.getHttpStatus()); - } catch (Exception e) { - fail(); - } - } -}