Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo-catalano committed Apr 11, 2024
1 parent e507c9d commit dbe8562
Show file tree
Hide file tree
Showing 10 changed files with 264 additions and 144 deletions.
326 changes: 199 additions & 127 deletions openapi/openapi.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package it.gov.pagopa.wispconverter.config;

import it.gov.pagopa.wispconverter.service.ConfigCacheService;
import java.time.ZonedDateTime;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.ZonedDateTime;

@Configuration
@Slf4j
@EnableScheduling
@ConditionalOnExpression("'${info.properties.environment}'!='test'")
public class ScheduledJobsConfig {

private final ConfigCacheService configCacheService;
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/it/gov/pagopa/wispconverter/CarrelloTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import it.gov.pagopa.gen.wispconverter.client.iuvgenerator.model.IuvGenerationModelResponseDto;
import it.gov.pagopa.wispconverter.repository.RPTRequestRepository;
import it.gov.pagopa.wispconverter.repository.RTRequestRepository;
import it.gov.pagopa.wispconverter.repository.ReEventRepository;
import it.gov.pagopa.wispconverter.repository.model.RPTRequestEntity;
import it.gov.pagopa.wispconverter.service.ConfigCacheService;
import it.gov.pagopa.wispconverter.utils.TestUtils;
Expand Down Expand Up @@ -44,13 +46,17 @@ class CarrelloTest {

@MockBean
private RPTRequestRepository rptRequestRepository;
@MockBean
private RTRequestRepository rtRequestRepository;
@MockBean private it.gov.pagopa.gen.wispconverter.client.iuvgenerator.invoker.ApiClient iuveneratorClient;
@MockBean private it.gov.pagopa.gen.wispconverter.client.gpd.invoker.ApiClient gpdClient;
@MockBean private it.gov.pagopa.gen.wispconverter.client.checkout.invoker.ApiClient checkoutClient;
@MockBean private it.gov.pagopa.gen.wispconverter.client.cache.invoker.ApiClient cacheClient;
@MockBean private it.gov.pagopa.gen.wispconverter.client.decouplercaching.invoker.ApiClient decouplerCachingClient;
@Qualifier("redisSimpleTemplate")
@MockBean private RedisTemplate<String, Object> redisSimpleTemplate;
@MockBean
private ReEventRepository reEventRepository;

@Test
void success() throws Exception {
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/it/gov/pagopa/wispconverter/HomeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import it.gov.pagopa.wispconverter.controller.model.AppInfoResponse;
import it.gov.pagopa.wispconverter.repository.CacheRepository;
import it.gov.pagopa.wispconverter.repository.RPTRequestRepository;
import it.gov.pagopa.wispconverter.repository.RTRequestRepository;
import it.gov.pagopa.wispconverter.repository.ReEventRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
Expand Down Expand Up @@ -39,7 +41,11 @@ class HomeTest {
@MockBean
private RPTRequestRepository rptRequestRepository;
@MockBean
private RTRequestRepository rtRequestRepository;
@MockBean
private CacheRepository cacheRepository;
@MockBean
private ReEventRepository reEventRepository;

@Test
void slash() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import it.gov.pagopa.wispconverter.controller.RedirectController;
import it.gov.pagopa.wispconverter.repository.RPTRequestRepository;
import it.gov.pagopa.wispconverter.repository.RTRequestRepository;
import it.gov.pagopa.wispconverter.repository.ReEventRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
Expand Down Expand Up @@ -39,9 +41,15 @@ class OpenApiGenerationTest {
@MockBean
private RPTRequestRepository rptRequestRepository;

@MockBean
private RTRequestRepository rtRequestRepository;

@MockBean
private CosmosAsyncClient cosmosAsyncClient;

@MockBean
private ReEventRepository reEventRepository;

@Test
void swaggerSpringPlugin() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/v3/api-docs").accept(MediaType.APPLICATION_JSON))
Expand Down
26 changes: 19 additions & 7 deletions src/test/java/it/gov/pagopa/wispconverter/ReceiptTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import it.gov.pagopa.wispconverter.controller.model.ReceiptRequest;
import it.gov.pagopa.wispconverter.repository.RPTRequestRepository;
import it.gov.pagopa.wispconverter.repository.RTRequestRepository;
import it.gov.pagopa.wispconverter.repository.ReEventRepository;
import it.gov.pagopa.wispconverter.repository.model.RPTRequestEntity;
import it.gov.pagopa.wispconverter.service.ConfigCacheService;
import it.gov.pagopa.wispconverter.service.ReService;
Expand Down Expand Up @@ -48,9 +50,10 @@ class ReceiptTest {

@MockBean
private ApplicationStartup applicationStartup;

@MockBean
private RPTRequestRepository rptRequestRepository;
@MockBean
private RTRequestRepository rtRequestRepository;
@MockBean private it.gov.pagopa.gen.wispconverter.client.iuvgenerator.invoker.ApiClient iuveneratorClient;
@MockBean private it.gov.pagopa.gen.wispconverter.client.gpd.invoker.ApiClient gpdClient;
@MockBean private it.gov.pagopa.gen.wispconverter.client.checkout.invoker.ApiClient checkoutClient;
Expand All @@ -59,8 +62,8 @@ class ReceiptTest {
@MockBean private CosmosClientBuilder cosmosClientBuilder;
@Qualifier("redisSimpleTemplate")
@MockBean private RedisTemplate<String, Object> redisSimpleTemplate;
@SpyBean
private ReService reService;
@MockBean
private ReEventRepository reEventRepository;

private String getPaSendRTPayload(){
String pasendrtv2 = TestUtils.loadFileContent("/requests/paSendRTV2.xml");
Expand All @@ -79,8 +82,17 @@ private byte[] zip(byte[] uncompressed) throws IOException {
@Test
void success_positive() throws Exception {
String station = "mystation";
TestUtils.setMock(cacheClient,ResponseEntity.ok().body(TestUtils.configData(station)));
org.springframework.test.util.ReflectionTestUtils.setField(configCacheService, "configCacheClient",cacheClient);
org.springframework.test.util.ReflectionTestUtils.setField(configCacheService, "configData",TestUtils.configData(station));

when(rptRequestRepository.findById(any())).thenReturn(
Optional.of(
RPTRequestEntity.builder().primitive("nodoInviaRPT")
.payload(
TestUtils.zipAndEncode(TestUtils.getRptPayload(false,station,"100.00","datispec"))
).build()
)
);

mvc.perform(MockMvcRequestBuilders.post("/receipt/ok")
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
Expand All @@ -93,7 +105,7 @@ void success_positive() throws Exception {
});


verify(reService,times(1)).addRe(any());
verify(reEventRepository,times(3)).save(any());
}

@Test
Expand Down Expand Up @@ -123,7 +135,7 @@ void success_negative() throws Exception {
assertNotNull(result.getResponse());
});

verify(reService,times(1)).addRe(any());
verify(reEventRepository,times(3)).save(any());
}

}
19 changes: 12 additions & 7 deletions src/test/java/it/gov/pagopa/wispconverter/RptTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import it.gov.pagopa.gen.wispconverter.client.iuvgenerator.model.IuvGenerationModelResponseDto;
import it.gov.pagopa.wispconverter.repository.RPTRequestRepository;
import it.gov.pagopa.wispconverter.repository.RTRequestRepository;
import it.gov.pagopa.wispconverter.repository.ReEventRepository;
import it.gov.pagopa.wispconverter.repository.model.RPTRequestEntity;
import it.gov.pagopa.wispconverter.service.ConfigCacheService;
import it.gov.pagopa.wispconverter.service.ReService;
Expand Down Expand Up @@ -58,6 +59,8 @@ class RptTest {
@MockBean private it.gov.pagopa.gen.wispconverter.client.decouplercaching.invoker.ApiClient decouplerCachingClient;
@Qualifier("redisSimpleTemplate")
@MockBean private RedisTemplate<String, Object> redisSimpleTemplate;
@MockBean
private ReEventRepository reEventRepository;
@SpyBean
private ReService reService;

Expand Down Expand Up @@ -206,7 +209,7 @@ void fail_rpt_not_exists() throws Exception {

IuvGenerationModelResponseDto iuvGenerationModelResponseDto = new IuvGenerationModelResponseDto();
iuvGenerationModelResponseDto.setIuv("00000000");
doThrow(new RestClientException("fail"))
doThrow(new RestClientException("fail", new RuntimeException("this test must fail")))
.when(iuveneratorClient).invokeAPI(any(),any(),any(),any(),any(),any(),any(),any(),any(),any(),any(),any());
TestUtils.setMock(gpdClient,ResponseEntity.ok().build());
TestUtils.setMock(decouplerCachingClient,ResponseEntity.ok().build());
Expand Down Expand Up @@ -240,7 +243,7 @@ void fail_generic() throws Exception {

IuvGenerationModelResponseDto iuvGenerationModelResponseDto = new IuvGenerationModelResponseDto();
iuvGenerationModelResponseDto.setIuv("00000000");
doThrow(new RestClientException("fail"))
doThrow(new RestClientException("fail", new RuntimeException("this test must fail")))
.when(iuveneratorClient).invokeAPI(any(),any(),any(),any(),any(),any(),any(),any(),any(),any(),any(),any());
TestUtils.setMock(gpdClient,ResponseEntity.ok().build());
TestUtils.setMock(decouplerCachingClient,ResponseEntity.ok().build());
Expand Down Expand Up @@ -274,7 +277,7 @@ void fail_getNAVCodeFromIUVGenerator() throws Exception {

IuvGenerationModelResponseDto iuvGenerationModelResponseDto = new IuvGenerationModelResponseDto();
iuvGenerationModelResponseDto.setIuv("00000000");
doThrow(new RestClientException("fail"))
doThrow(new RestClientException("fail", new RuntimeException("this test must fail")))
.when(iuveneratorClient).invokeAPI(any(),any(),any(),any(),any(),any(),any(),any(),any(),any(),any(),any());
TestUtils.setMock(gpdClient,ResponseEntity.ok().build());
TestUtils.setMock(decouplerCachingClient,ResponseEntity.ok().build());
Expand Down Expand Up @@ -310,7 +313,7 @@ void fail_createDebtPositions() throws Exception {
IuvGenerationModelResponseDto iuvGenerationModelResponseDto = new IuvGenerationModelResponseDto();
iuvGenerationModelResponseDto.setIuv("00000000");
TestUtils.setMock(iuveneratorClient,ResponseEntity.ok().body(iuvGenerationModelResponseDto));
doThrow(new RestClientException("fail"))
doThrow(new RestClientException("fail", new RuntimeException("this test must fail")))
.when(gpdClient).parameterToMultiValueMap(any(),any(),any());
TestUtils.setMock(decouplerCachingClient,ResponseEntity.ok().build());
when(rptRequestRepository.findById(any())).thenReturn(
Expand Down Expand Up @@ -348,8 +351,10 @@ void fail_storeRequestMappingInCache() throws Exception {
iuvGenerationModelResponseDto.setIuv("00000000");
TestUtils.setMock(iuveneratorClient,ResponseEntity.ok().body(iuvGenerationModelResponseDto));
TestUtils.setMock(gpdClient,ResponseEntity.ok().build());
doThrow(new RestClientException("fail"))
.when(decouplerCachingClient).invokeAPI(any(),any(),any(),any(),any(),any(),any(),any(),any(),any(),any(),any());
doThrow(new RestClientException("fail", new RuntimeException("this test must fail")))
.when(decouplerCachingClient)
.invokeAPI(
any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), any());
when(rptRequestRepository.findById(any())).thenReturn(
Optional.of(
RPTRequestEntity.builder().primitive("nodoInviaRPT")
Expand Down Expand Up @@ -378,7 +383,7 @@ void fail_checkout() throws Exception {
org.springframework.test.util.ReflectionTestUtils.setField(configCacheService, "configCacheClient",cacheClient);
HttpHeaders headers = new HttpHeaders();
headers.add("location","locationheader");
doThrow(new RestClientException("fail"))
doThrow(new RestClientException("fail", new RuntimeException("this test must fail")))
.when(checkoutClient).invokeAPI(any(),any(),any(),any(),any(),any(),any(),any(),any(),any(),any(),any());

IuvGenerationModelResponseDto iuvGenerationModelResponseDto = new IuvGenerationModelResponseDto();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import java.util.Base64;
import java.util.HashMap;
import java.util.zip.GZIPOutputStream;

import it.gov.pagopa.gen.wispconverter.client.cache.model.ConnectionDto;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
Expand All @@ -35,6 +37,10 @@ public static it.gov.pagopa.gen.wispconverter.client.cache.model.ConfigDataV1Dto
configDataV1.setStations(new HashMap<>());
it.gov.pagopa.gen.wispconverter.client.cache.model.StationDto station = new it.gov.pagopa.gen.wispconverter.client.cache.model.StationDto();
station.setStationCode(stationCode);
station.setConnection(new ConnectionDto());
station.getConnection().setIp("127.0.0.1");
station.getConnection().setPort(8888l);
station.getConnection().setProtocol(ConnectionDto.ProtocolEnum.HTTP);
station.setRedirect(new it.gov.pagopa.gen.wispconverter.client.cache.model.RedirectDto());
station.getRedirect().setIp("127.0.0.1");
station.getRedirect().setPath("/redirect");
Expand Down
4 changes: 4 additions & 0 deletions src/test/resources/application-test.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
info.properties.environment=test

client.gpd.base-path=${CLIENT_GPD_HOST:http://localhost}
client.checkout.base-path=${CLIENT_CHECKOUT_HOST:http://localhost}
client.decoupler-caching.base-path=${CLIENT_DECOUPLERCACHING_HOST:http://localhost}
client.cache.base-path=${CLIENT_CACHE_HOST:http://localhost}

# Logging
logging.level.it.gov.pagopa=DEBUG
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/requests/paSendRTV2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<pafn:paSendRTReq>
<idPA>77777777777</idPA>
<idBrokerPA>77777777777</idBrokerPA>
<idStation>77777777777_01</idStation>
<idStation>mystation</idStation>
<receipt>
<receiptId>c110729d258c4ab1b765fe902aae41d6</receiptId>
<noticeNumber>311111111112222222</noticeNumber>
Expand Down

0 comments on commit dbe8562

Please sign in to comment.