Skip to content

Commit

Permalink
Merge pull request #9 from pagopa/tests
Browse files Browse the repository at this point in the history
unit tests
  • Loading branch information
aomegax authored Feb 15, 2024
2 parents 773facb + 06d397d commit fb899c9
Show file tree
Hide file tree
Showing 23 changed files with 617 additions and 74 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/03_code_review.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
github_token: ${{ secrets.GITHUB_TOKEN }}
sonar_token: ${{ secrets.SONAR_TOKEN }}
project_key: ${{env.PROJECT_KEY}}
coverage_exclusions: "**/config/*,**/*Mock*,**/model/**,**/entity/*,**/repository/*"
coverage_exclusions: "**/scheduler/*,**/exception/*,**/model/*,**/entity/*,**/TestController.java"
cpd_exclusions: "**/model/**,**/entity/*"
java_version: '17'

Expand Down
29 changes: 29 additions & 0 deletions .github/workflows/04h_deploy_with_github_runner.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,32 @@ jobs:
resource_group_name: ${{ vars.CONTAINER_APP_ENVIRONMENT_RESOURCE_GROUP_NAME }}
runner_name: ${{ needs.create_runner.outputs.runner_name }}
pat_token: ${{ secrets.BOT_TOKEN_GITHUB }}

update_openapi:
needs: [ deploy ]
runs-on: ubuntu-latest
name: Update OpenAPI
if: ${{ inputs.target == inputs.environment }}
environment: ${{ inputs.environment }}
steps:
- name: Checkout
id: checkout
# from https://github.com/actions/checkout/commits/main
uses: actions/checkout@1f9a0c22da41e6ebfa534300ef656657ea2c6707
with:
persist-credentials: false

- name: Setup Terraform
# from https://github.com/hashicorp/setup-terraform/commits/main
uses: hashicorp/setup-terraform@8feba2b913ea459066180f9cb177f58a881cf146
with:
terraform_version: "1.3.6"

- name: Login
id: login
# from https://github.com/Azure/login/commits/master
uses: azure/login@92a5484dfaf04ca78a94597f4f19fea633851fa2
with:
client-id: ${{ secrets.CD_CLIENT_ID }}
tenant-id: ${{ secrets.TENANT_ID }}
subscription-id: ${{ secrets.SUBSCRIPTION_ID }}
4 changes: 2 additions & 2 deletions helm/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ apiVersion: v2
name: pagopa-stand-in-manager
description: Microservice that handles Stand-In activation/deactivation process
type: application
version: 0.56.0
appVersion: 0.0.6
version: 0.58.0
appVersion: 0.0.6-2-tests
dependencies:
- name: microservice-chart
version: 3.0.0
Expand Down
2 changes: 1 addition & 1 deletion helm/values-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ microservice-chart:
fullnameOverride: ""
image:
repository: ghcr.io/pagopa/pagopa-stand-in-manager
tag: "0.0.6"
tag: "0.0.6-2-tests"
pullPolicy: Always
livenessProbe:
httpGet:
Expand Down
2 changes: 1 addition & 1 deletion helm/values-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ microservice-chart:
fullnameOverride: ""
image:
repository: ghcr.io/pagopa/pagopa-stand-in-manager
tag: "0.0.6"
tag: "0.0.6-2-tests"
pullPolicy: Always
livenessProbe:
httpGet:
Expand Down
2 changes: 1 addition & 1 deletion helm/values-uat.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ microservice-chart:
fullnameOverride: ""
image:
repository: ghcr.io/pagopa/pagopa-stand-in-manager
tag: "0.0.6"
tag: "0.0.6-2-tests"
pullPolicy: Always
livenessProbe:
httpGet:
Expand Down
40 changes: 39 additions & 1 deletion openapi/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Stand in Manager",
"termsOfService": "https://www.pagopa.gov.it/",
"title": "stand-in-manager",
"version": "0.0.6"
"version": "0.0.6-2-tests"
},
"servers": [
{
Expand Down Expand Up @@ -380,6 +380,44 @@
}
}
]
},
"/test-event": {
"get": {
"operationId": "test5",
"responses": {
"200": {
"content": {
"*/*": {
"schema": {
"type": "string"
}
}
},
"description": "OK",
"headers": {
"X-Request-Id": {
"description": "This header identifies the call",
"schema": {
"type": "string"
}
}
}
}
},
"tags": [
"test-controller"
]
},
"parameters": [
{
"description": "This header identifies the call, if not passed it is self-generated. This ID is returned in the response.",
"in": "header",
"name": "X-Request-Id",
"schema": {
"type": "string"
}
}
]
}
},
"components": {
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<groupId>it.gov.pagopa</groupId>
<artifactId>stand-in-manager</artifactId>
<version>0.0.6</version>
<version>0.0.6-2-tests</version>
<description>Stand in Manager</description>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class ResponseValidator {
*/
// TODO: set your package
@AfterReturning(
pointcut = "execution(* it.gov.pagopa.microservice.controller.*.*(..))",
pointcut = "execution(* it.gov.pagopa.standinmanager.controller.*.*(..))",
returning = "result")
public void validateResponse(JoinPoint joinPoint, Object result) {
if (result instanceof ResponseEntity) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@
import it.gov.pagopa.standinmanager.service.DataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;

@RestController
@Slf4j
public class DataController {
Expand All @@ -32,7 +37,8 @@ public class DataController {
@ApiResponse(responseCode = "400", description = "Invalid request", content = @Content)
})
@GetMapping("/stations")
public GetResponse getEvents() {
return GetResponse.builder().stations(dataService.getStations()).build();
@NotEmpty
public ResponseEntity<GetResponse> getEvents() {
return ResponseEntity.status(HttpStatus.OK).body(GetResponse.builder().stations(dataService.getStations()).build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ s, new PartitionKey(s.getPartitionKey())))
return container.executeBulkOperations(cosmosItemOperationStream);
}

public CosmosItemResponse<CosmosNodeCallCounts> save(CosmosNodeCallCounts item) {
CosmosContainer container = cosmosClient.getDatabase(dbname).getContainer(tablename);
return container.createItem(item);
}

public List<CosmosNodeCallCounts> getStationCounts(ZonedDateTime dateFrom) {
List<SqlParameter> paramList = new ArrayList<>();
paramList.addAll(Arrays.asList(new SqlParameter("@from", dateFrom.toInstant())));
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,9 @@ public class NodoCalcService {
private Boolean sendEvent;

@Autowired private CosmosStationRepository cosmosStationRepository;
@Autowired private CosmosStationRepository standInStationsRepository;
@Autowired private CosmosNodeDataRepository cosmosRepository;
@Autowired private CosmosNodeDataRepository cosmosNodeDataRepository;
@Autowired private CosmosEventsRepository cosmosEventsRepository;
@Autowired private MailService awsSesClient;
@Autowired private MailService mailService;
@Autowired private DatabaseStationsRepository dbStationsRepository;
@Autowired private EventHubService eventHubService;

Expand All @@ -75,12 +74,12 @@ public void runCalculations()
slotMinutes);

Set<String> standInStations =
standInStationsRepository.getStations().stream()
cosmosStationRepository.getStations().stream()
.map(s -> s.getStation())
.collect(Collectors.toSet());

List<CosmosNodeCallCounts> allCounts =
cosmosRepository.getStationCounts(now.minusMinutes(rangeMinutes));
cosmosNodeDataRepository.getStationCounts(now.minusMinutes(rangeMinutes));

Map<String, List<CosmosNodeCallCounts>> allStationCounts =
allCounts.stream().collect(Collectors.groupingBy(CosmosNodeCallCounts::getStation));
Expand Down Expand Up @@ -166,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
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ public class StationCalcService {
@Value("#{'${sendEvent}'=='true'}")
private Boolean sendEvent;

@Autowired private CosmosStationRepository standInStationsRepository;
@Autowired private CosmosStationRepository cosmosStationRepository;
@Autowired private CosmosStationDataRepository cosmosRepository;
@Autowired private CosmosStationDataRepository cosmosStationDataRepository;
@Autowired private CosmosEventsRepository cosmosEventsRepository;
@Autowired private MailService awsSesClient;
@Autowired private DatabaseStationsRepository dbStationsRepository;
Expand All @@ -63,11 +62,11 @@ public void runCalculations()
rangeLimit);

Map<String, Instant> standInStations =
standInStationsRepository.getStations().stream()
cosmosStationRepository.getStations().stream()
.collect(Collectors.toMap(d -> d.getStation(), d -> d.getTimestamp()));

List<CosmosForwarderCallCounts> allCounts =
cosmosRepository.getStationCounts(now.minusMinutes(rangeMinutes));
cosmosStationDataRepository.getStationCounts(now.minusMinutes(rangeMinutes));
Map<String, List<CosmosForwarderCallCounts>> allStationCounts =
allCounts.stream().collect(Collectors.groupingBy(CosmosForwarderCallCounts::getStation));

Expand Down
31 changes: 0 additions & 31 deletions src/main/java/it/gov/pagopa/standinmanager/util/Util.java

This file was deleted.

28 changes: 28 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 All @@ -26,6 +27,7 @@
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.web.util.UriComponentsBuilder;

@SpringBootTest(classes = Application.class)
@AutoConfigureMockMvc
Expand All @@ -44,7 +46,33 @@ class ApiTest {
@MockBean private EntityManager entityManager;
@MockBean private DataSource dataSource;
@MockBean private CosmosClient cosmosClient;
@Test
void swagger() throws Exception {
mvc.perform(MockMvcRequestBuilders.get(UriComponentsBuilder.fromUriString("").build().toUri()).accept(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().is3xxRedirection());
}
@Test
void notfound() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/notfound").accept(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().is4xxClientError());
}
@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
Loading

0 comments on commit fb899c9

Please sign in to comment.