Skip to content

Commit

Permalink
Merge pull request #7 from pagopa/PAGOPA-1075-status-page-adeguamento…
Browse files Browse the repository at this point in the history
…-api-info-per-azure-functions

[PAGOPA-1075] feat: add enhancement for /info API
  • Loading branch information
andrea-deri committed Jul 7, 2023
2 parents 001c93d + b7af4db commit 4bb5529
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .devops/code-review-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ steps:
sonar.projectKey=$(SONARCLOUD_PROJECT_KEY)
sonar.projectName=$(SONARCLOUD_PROJECT_NAME)
sonar.coverage.exclusions=**/config/*,**/*Mock*,**/model/**,**/entity/*
sonar.cpd.exclusions=**/model/**,**/entity/*
sonar.cpd.exclusions=**/models/**,**/entity/*
- task: Maven@3
Expand Down
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-functions-template
description: Microservice description
type: application
version: 0.1.2
appVersion: 0.1.2
version: 0.1.2-1
appVersion: 0.1.2-1
dependencies:
- name: microservice-chart
version: 1.21.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: pagopadcommonacr.azurecr.io/pagopa<project-name> # TODO
tag: "0.1.2"
tag: "0.1.2-1"
pullPolicy: Always
# https://github.com/Azure/azure-functions-host/blob/dev/src/WebJobs.Script.WebHost/Controllers/HostController.cs
livenessProbe:
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: pagopapcommonacr.azurecr.io/pagopa<project-name> # TODO
tag: "0.1.2"
tag: "0.1.2-1"
pullPolicy: Always
# https://github.com/Azure/azure-functions-host/blob/dev/src/WebJobs.Script.WebHost/Controllers/HostController.cs
livenessProbe:
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: pagopaucommonacr.azurecr.io/pagopa<project-name> # TODO
tag: "0.1.2"
tag: "0.1.2-1"
pullPolicy: Always
# https://github.com/Azure/azure-functions-host/blob/dev/src/WebJobs.Script.WebHost/Controllers/HostController.cs
livenessProbe:
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>it.gov.pagopa.reporting</groupId>
<artifactId>reporting-batch</artifactId>
<version>0.1.2</version>
<version>0.1.2-1</version>
<packaging>jar</packaging>

<name>Azure Reporting-Batch Fn</name>
Expand Down
26 changes: 24 additions & 2 deletions src/main/java/it/gov/pagopa/reporting/Info.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.HttpTrigger;
import it.gov.pagopa.reporting.models.AppInfo;

import java.io.InputStream;
import java.util.Optional;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
Expand All @@ -28,7 +32,25 @@ public HttpResponseMessage run (

context.getLogger().log(Level.INFO, "Invoked health check HTTP trigger for pagopa-gpd-reporting-batch.");
return request.createResponseBuilder(HttpStatus.OK)
.header("Content-Type", "application/json")
.build();
.header("Content-Type", "application/json")
.body(getInfo(context.getLogger(), "/META-INF/maven/it.gov.pagopa.reporting/reporting-batch/pom.properties"))
.build();
}

public synchronized AppInfo getInfo(Logger logger, String path) {
String version = null;
String name = null;
try {
Properties properties = new Properties();
InputStream inputStream = getClass().getResourceAsStream(path);
if (inputStream != null) {
properties.load(inputStream);
version = properties.getProperty("version", null);
name = properties.getProperty("artifactId", null);
}
} catch (Exception e) {
logger.severe("Impossible to retrieve information from pom.properties file.");
}
return AppInfo.builder().version(version).environment("azure-fn").name(name).build();
}
}
17 changes: 17 additions & 0 deletions src/main/java/it/gov/pagopa/reporting/models/AppInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package it.gov.pagopa.reporting.models;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.*;

@Getter
@Builder(toBuilder = true)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class AppInfo {

private String name;
private String version;
private String environment;
}
92 changes: 92 additions & 0 deletions src/test/java/it/gov/pagopa/reporting/InfoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package it.gov.pagopa.reporting;

import com.microsoft.azure.functions.ExecutionContext;
import com.microsoft.azure.functions.HttpRequestMessage;
import com.microsoft.azure.functions.HttpResponseMessage;
import com.microsoft.azure.functions.HttpStatus;
import it.gov.pagopa.reporting.models.AppInfo;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Optional;
import java.util.logging.Logger;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
class InfoTest {

@Mock
ExecutionContext context;

@Spy
Info infoFunction;

@Test
void runOK() {

// Mocking service creation
Logger logger = Logger.getLogger("example-test-logger");
when(context.getLogger()).thenReturn(logger);

// test precondition
final HttpResponseMessage.Builder builder = mock(HttpResponseMessage.Builder.class);
@SuppressWarnings("unchecked")
HttpRequestMessage<Optional<String>> request = mock(HttpRequestMessage.class);

HttpResponseMessage responseMock = mock(HttpResponseMessage.class);
doReturn(HttpStatus.OK).when(responseMock).getStatus();
doReturn(builder).when(builder).body(any());
doReturn(responseMock).when(builder).build();
doReturn(builder).when(request).createResponseBuilder(any(HttpStatus.class));
doReturn(builder).when(builder).header(anyString(), anyString());

// test execution
HttpResponseMessage response = infoFunction.run(request, context);

// test assertion
assertEquals(HttpStatus.OK, response.getStatus());
}

@SneakyThrows
@Test
void getInfoOk() {

// Mocking service creation
Logger logger = Logger.getLogger("example-test-logger");
String path = "/META-INF/maven/it.gov.pagopa.reporting/reporting-batch/pom.properties";

// Execute function
AppInfo response = infoFunction.getInfo(logger, path);

// Checking assertions
assertNotNull(response.getName());
assertNotNull(response.getVersion());
assertNotNull(response.getEnvironment());
}

@SneakyThrows
@Test
void getInfoKo() {

// Mocking service creation
Logger logger = Logger.getLogger("example-test-logger");
String path = "/META-INF/maven/it.gov.pagopa.reporting/reporting-batch/fake";

// Execute function
AppInfo response = infoFunction.getInfo(logger, path);

// Checking assertions
assertNull(response.getName());
assertNull(response.getVersion());
assertNotNull(response.getEnvironment());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#Generated by Maven
#Fri Jul 07 10:12:25 CEST 2023
artifactId=reporting-batch
groupId=it.gov.pagopa.reporting
version=x.y.z

0 comments on commit 4bb5529

Please sign in to comment.