Skip to content

Commit

Permalink
unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo-catalano committed Feb 15, 2024
1 parent 93fc9ac commit 809b2e0
Show file tree
Hide file tree
Showing 11 changed files with 172 additions and 75 deletions.
4 changes: 2 additions & 2 deletions openapi/openapi.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"openapi" : "3.0.1",
"info" : {
"description" : "Stand in Manager",
"description" : "@project.description@",
"termsOfService" : "https://www.pagopa.gov.it/",
"title" : "stand-in-manager",
"title" : "@project.name@",
"version" : "0.0.6"
},
"servers" : [ {
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<properties>
<java.version>11</java.version>
<sonar.exclusions>it.gov.pagopa.standinmanager.controller.TestController</sonar.exclusions>
<sonar.exclusions>it.gov.pagopa.standinmanager.config</sonar.exclusions>
</properties>

<repositories>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class NodoCalcService {
@Autowired private CosmosStationRepository cosmosStationRepository;
@Autowired private CosmosNodeDataRepository cosmosRepository;
@Autowired private CosmosEventsRepository cosmosEventsRepository;
@Autowired private MailService awsSesClient;
@Autowired private MailService mailService;
@Autowired private DatabaseStationsRepository dbStationsRepository;
@Autowired private EventHubService eventHubService;

Expand Down Expand Up @@ -165,7 +165,7 @@ public void runCalculations()
"adding station [%s] to standIn stations because [%s] of [%s] slots failed",
station, failedSlots, totalSlots));
String sendResult =
awsSesClient.sendEmail(
mailService.sendEmail(
String.format(
"[StandInManager][%s] Station [%s] added to standin", env, station),
String.format(
Expand Down
31 changes: 0 additions & 31 deletions src/main/java/it/gov/pagopa/standinmanager/util/Util.java

This file was deleted.

17 changes: 17 additions & 0 deletions src/test/java/it/gov/pagopa/standinmanager/ApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.azure.cosmos.CosmosClient;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.microsoft.azure.kusto.data.Client;
import it.gov.pagopa.standinmanager.model.AppInfo;
import it.gov.pagopa.standinmanager.model.GetResponse;
import it.gov.pagopa.standinmanager.repository.CosmosEventsRepository;
import it.gov.pagopa.standinmanager.repository.CosmosStationRepository;
Expand Down Expand Up @@ -44,7 +45,23 @@ class ApiTest {
@MockBean private EntityManager entityManager;
@MockBean private DataSource dataSource;
@MockBean private CosmosClient cosmosClient;
@Test
void info() throws Exception {
when(cosmosStationRepository.getStations()).thenReturn(Arrays.asList(new CosmosStandInStation("","station1", Instant.now()),new CosmosStandInStation("","station2", Instant.now())));
mvc.perform(MockMvcRequestBuilders.get("/info").accept(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().is2xxSuccessful())
.andDo(
(result) -> {
assertNotNull(result);
assertNotNull(result.getResponse());
final String content = result.getResponse().getContentAsString();
assertFalse(content.isBlank());
AppInfo res =
objectMapper.readValue(result.getResponse().getContentAsString(), AppInfo.class);
assertEquals(res.getEnvironment(), "test");
});

}
@Test
void stations() throws Exception {
when(cosmosStationRepository.getStations()).thenReturn(Arrays.asList(new CosmosStandInStation("","station1", Instant.now()),new CosmosStandInStation("","station2", Instant.now())));
Expand Down
51 changes: 51 additions & 0 deletions src/test/java/it/gov/pagopa/standinmanager/ConfigServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package it.gov.pagopa.standinmanager;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;

import com.microsoft.azure.kusto.data.exceptions.DataClientException;
import com.microsoft.azure.kusto.data.exceptions.DataServiceException;
import com.microsoft.azure.kusto.data.exceptions.KustoServiceQueryError;
import it.gov.pagopa.standinmanager.config.model.ConfigDataV1;
import it.gov.pagopa.standinmanager.config.model.Station;
import it.gov.pagopa.standinmanager.service.ConfigService;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.openapitools.client.api.CacheApi;

@ExtendWith(MockitoExtension.class)
class ConfigServiceTest {

@Mock
private CacheApi cacheApi;

@InjectMocks
private ConfigService configService;

@BeforeEach
void setUp() throws KustoServiceQueryError, DataServiceException, DataClientException {
ConfigDataV1 configDataV1 = new ConfigDataV1();
Map<String, Station> stations = new HashMap<>();
Station station1 = new Station();
Station station2 = new Station();
stations.put("station1",station1);
stations.put("station2",station2);
configDataV1.setStations(stations);
when(cacheApi.cache()).thenReturn(configDataV1);

}

@Test
void test1() throws Exception {
configService.loadCache();
ConfigDataV1 cache = configService.getCache();
verify(cacheApi, times(1)).cache();
assertEquals(cache.getStations().size(),2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
import static org.mockito.Mockito.*;

import com.azure.cosmos.CosmosClient;
import com.azure.cosmos.CosmosContainer;
import com.azure.cosmos.CosmosDatabase;
import com.azure.messaging.eventhubs.EventDataBatch;
import com.azure.messaging.eventhubs.EventHubProducerClient;
import com.microsoft.azure.kusto.data.Client;
import com.microsoft.azure.kusto.data.KustoOperationResult;
import com.microsoft.azure.kusto.data.exceptions.DataClientException;
import com.microsoft.azure.kusto.data.exceptions.DataServiceException;
import com.microsoft.azure.kusto.data.exceptions.KustoServiceQueryError;
Expand All @@ -25,7 +28,9 @@
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.testcontainers.shaded.com.fasterxml.jackson.databind.util.ArrayIterator;
import software.amazon.awssdk.services.ses.SesClient;
import software.amazon.awssdk.services.ses.model.SendEmailRequest;
import software.amazon.awssdk.services.ses.model.SendEmailResponse;

@ExtendWith(MockitoExtension.class)
class NodoCalcServiceTest {
Expand All @@ -37,22 +42,46 @@ class NodoCalcServiceTest {
@Mock private CosmosNodeDataRepository cosmosNodeDataRepository = mock(CosmosNodeDataRepository.class);
@Mock private CosmosStationRepository cosmosStationsRepository = mock(CosmosStationRepository.class);
@Mock private DatabaseStationsRepository databaseStationsRepository = mock(DatabaseStationsRepository.class);
@Mock private CosmosEventsRepository cosmosEventsRepository = mock(CosmosEventsRepository.class);
@Mock private EventHubService eventHubService;
@Mock private MailService awsSesClient;

private CosmosEventsRepository cosmosEventsRepository = spy(new CosmosEventsRepository());
private EventHubService eventHubService = new EventHubService();
private MailService mailService = new MailService();

@Mock private EventHubProducerClient producer;
@Mock private SesClient sesClient;
EventDataBatch testBatch = mock(EventDataBatch.class);
@Mock private SendEmailResponse testResponse;
@Mock private CosmosDatabase cosmosDatabase;
@Mock private CosmosContainer cosmosContainer;

@InjectMocks
private NodoCalcService nodoCalcService;

@BeforeEach
void setUp() throws KustoServiceQueryError, DataServiceException, DataClientException {
org.springframework.test.util.ReflectionTestUtils.setField(cosmosEventsRepository, "cosmosClient", cosmosClient);
org.springframework.test.util.ReflectionTestUtils.setField(mailService, "sesClient", sesClient);
org.springframework.test.util.ReflectionTestUtils.setField(mailService, "from", "[email protected]");
org.springframework.test.util.ReflectionTestUtils.setField(mailService, "mailto", "[email protected]");
org.springframework.test.util.ReflectionTestUtils.setField(nodoCalcService, "mailService", mailService);
org.springframework.test.util.ReflectionTestUtils.setField(eventHubService, "producer", producer);
org.springframework.test.util.ReflectionTestUtils.setField(
eventHubService, "om", new com.fasterxml.jackson.databind.ObjectMapper().findAndRegisterModules());
org.springframework.test.util.ReflectionTestUtils.setField(nodoCalcService, "eventHubService", eventHubService);
org.springframework.test.util.ReflectionTestUtils.setField(nodoCalcService, "slotMinutes", 1);
org.springframework.test.util.ReflectionTestUtils.setField(nodoCalcService, "rangeMinutes", 5);
org.springframework.test.util.ReflectionTestUtils.setField(nodoCalcService, "sendEvent", true);
org.springframework.test.util.ReflectionTestUtils.setField(nodoCalcService, "saveDB", true);
// when(kustoClient.execute(any(),any(),any())).thenReturn(new KustoOperationResult("{ \"Tables\":[ \"RE\"] }",""));
// when(cosmosNodeDataRepository.saveAll(any())).thenReturn(new ArrayIterator<>(null));
// when(standInStationsRepository.getStations()).thenReturn(List.of(new CosmosStandInStation("","station1", Instant.now()),new CosmosStandInStation("","station2", Instant.now())));

when(cosmosClient.getDatabase(any())).thenReturn(cosmosDatabase);
when(cosmosDatabase.getContainer(any())).thenReturn(cosmosContainer);
when(testBatch.tryAdd(any())).thenReturn(true);
when(testBatch.getCount()).thenReturn(2);
when(producer.createBatch()).thenReturn(testBatch);
when(sesClient.sendEmail(any(SendEmailRequest.class))).thenReturn(testResponse);
when(cosmosNodeDataRepository.getStationCounts(any()))
.thenReturn(
List.of(
Expand All @@ -65,10 +94,10 @@ void setUp() throws KustoServiceQueryError, DataServiceException, DataClientExce
@Test
void test1() throws Exception {
nodoCalcService.runCalculations();
verify(eventHubService, times(2)).publishEvent(any(),any(),any());
verify(producer, times(2)).send(any(EventDataBatch.class));
verify(databaseStationsRepository, times(2)).save(any());
verify(cosmosStationsRepository, times(2)).save(any());
verify(cosmosEventsRepository, times(2)).newEvent(any(),any(),any());
verify(awsSesClient, times(2)).sendEmail(any(),any());
verify(sesClient, times(2)).sendEmail(any(SendEmailRequest.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import static org.mockito.Mockito.*;

import com.azure.cosmos.CosmosClient;
import com.azure.cosmos.CosmosContainer;
import com.azure.cosmos.CosmosDatabase;
import com.microsoft.azure.kusto.data.Client;
import com.microsoft.azure.kusto.data.KustoOperationResult;
import com.microsoft.azure.kusto.data.exceptions.DataClientException;
Expand All @@ -17,31 +19,46 @@
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.testcontainers.shaded.com.fasterxml.jackson.databind.util.ArrayIterator;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

@ExtendWith(MockitoExtension.class)
class NodoMonitorServiceTest {



@Mock private Client kustoClient;
@Mock
private Client kustoClient;
@Mock private CosmosClient cosmosClient;
@Mock private CosmosNodeDataRepository cosmosNodeDataRepository = mock(CosmosNodeDataRepository.class);
@Mock private CosmosDatabase cosmosDatabase;
@Mock private CosmosContainer cosmosContainer;
private CosmosNodeDataRepository cosmosNodeDataRepository = spy(new CosmosNodeDataRepository());
// @Autowired
// private CosmosNodeDataRepository cosmosNodeDataRepository = spy(CosmosNodeDataRepository.class);

@InjectMocks
private NodoMonitorService nodoMonitorService;

@BeforeEach
void setUp() throws KustoServiceQueryError, DataServiceException, DataClientException {
// org.springframework.test.util.ReflectionTestUtils.setField(
// cosmosNodeDataRepository, "cosmosClient", cosmosClient);
when(kustoClient.execute(any(),any(),any())).thenReturn(new KustoOperationResult("{ \"Tables\":[ \"RE\"] }",""));
when(cosmosNodeDataRepository.saveAll(any())).thenReturn(new ArrayIterator<>(null));
void setUp() throws KustoServiceQueryError, DataServiceException, DataClientException, IOException {

String s = new String(getClass().getClassLoader().getResourceAsStream("kustoResponse.json").readAllBytes());
org.springframework.test.util.ReflectionTestUtils.setField(
cosmosNodeDataRepository, "cosmosClient", cosmosClient);
when(cosmosClient.getDatabase(any())).thenReturn(cosmosDatabase);
when(cosmosDatabase.getContainer(any())).thenReturn(cosmosContainer);
when(cosmosContainer.executeBulkOperations(any())).thenReturn(null);
when(kustoClient.execute(any(), any(), any()))
.thenReturn(
new KustoOperationResult(s, "v2"));
// when(cosmosNodeDataRepository.saveAll(any())).thenReturn(new ArrayIterator<>(null));
}

@Test
void test1() throws Exception {
nodoMonitorService.getAndSaveData();
verify(cosmosNodeDataRepository, times(1)).saveAll(any());
// verify(cosmosNodeDataRepository, times(1)).saveAll(any());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@

import com.azure.cosmos.CosmosClient;
import com.microsoft.azure.kusto.data.Client;
import com.microsoft.azure.kusto.data.KustoOperationResult;
import com.microsoft.azure.kusto.data.exceptions.DataClientException;
import com.microsoft.azure.kusto.data.exceptions.DataServiceException;
import com.microsoft.azure.kusto.data.exceptions.KustoServiceQueryError;
import it.gov.pagopa.standinmanager.client.ForwarderClient;
import it.gov.pagopa.standinmanager.config.model.ConfigDataV1;
import it.gov.pagopa.standinmanager.config.model.Service;
import it.gov.pagopa.standinmanager.config.model.Station;
import it.gov.pagopa.standinmanager.repository.CosmosEventsRepository;
import it.gov.pagopa.standinmanager.repository.CosmosNodeDataRepository;
import it.gov.pagopa.standinmanager.repository.CosmosStationDataRepository;
import it.gov.pagopa.standinmanager.repository.CosmosStationRepository;
Expand All @@ -28,7 +29,9 @@
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.testcontainers.shaded.com.fasterxml.jackson.databind.util.ArrayIterator;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

@ExtendWith(MockitoExtension.class)
class StationMonitorServiceTest {
Expand All @@ -40,8 +43,12 @@ class StationMonitorServiceTest {
@Mock private CosmosNodeDataRepository cosmosNodeDataRepository = mock(CosmosNodeDataRepository.class);
@Mock private CosmosStationDataRepository cosmosStationDataRepository = mock(CosmosStationDataRepository.class);
@Mock private CosmosStationRepository cosmosStationRepository = mock(CosmosStationRepository.class);
@Mock private ForwarderClient forwarderClient = mock(ForwarderClient.class);
@Mock private CosmosEventsRepository cosmosEventsRepository = mock(CosmosEventsRepository.class);


private ForwarderClient forwarderClient = new ForwarderClient();
@Mock private ConfigService configService = mock(ConfigService.class);
@Mock private RestTemplate restTemplate;


@InjectMocks
Expand All @@ -52,17 +59,30 @@ void setUp() throws KustoServiceQueryError, DataServiceException, DataClientExce
ConfigDataV1 configDataV1 = new ConfigDataV1();
Map<String, Station> stations = new HashMap<>();
Station station1 = new Station();
station1.setBrokerCode("broker1");
station1.setStationCode("station1");
Service service = new Service();
service.setTargetHost("http://test.it");
service.setTargetPath("/test");
service.setTargetPort(8080l);
station1.setServicePof(service);
Station station2 = new Station();
station2.setBrokerCode("broker2");
station2.setStationCode("station2");
station2.setServicePof(service);
stations.put("station1",station1);
stations.put("station2",station2);
configDataV1.setStations(stations);
// org.springframework.test.util.ReflectionTestUtils.setField(
// cosmosNodeDataRepository, "cosmosClient", cosmosClient);
when(configService.getCache()).thenReturn(configDataV1);

when(cosmosStationRepository.getStations()).thenReturn(List.of(new CosmosStandInStation("","station1", Instant.now().minusSeconds(600)),new CosmosStandInStation("","station2", Instant.now().minusSeconds(600))));
when(forwarderClient.verifyPaymentNotice(any())).thenReturn(true);
org.springframework.test.util.ReflectionTestUtils.setField(forwarderClient, "cosmosEventsRepository", cosmosEventsRepository);
org.springframework.test.util.ReflectionTestUtils.setField(forwarderClient, "url", "http://forwarder.it");
org.springframework.test.util.ReflectionTestUtils.setField(forwarderClient, "key", "key");
org.springframework.test.util.ReflectionTestUtils.setField(forwarderClient, "restTemplate", restTemplate);
org.springframework.test.util.ReflectionTestUtils.setField(stationMonitorService, "forwarderClient", forwarderClient);

when(configService.getCache()).thenReturn(configDataV1);
when(cosmosStationRepository.getStations()).thenReturn(List.of(new CosmosStandInStation("","station1", Instant.now().minusSeconds(600)),new CosmosStandInStation("","station2", Instant.now().minusSeconds(600))));
when(restTemplate.exchange(any(), any(Class.class))).thenReturn(new ResponseEntity<String>("", HttpStatus.OK));
}

@Test
Expand Down
Loading

0 comments on commit 809b2e0

Please sign in to comment.