Skip to content

Commit

Permalink
[PAGOPA-1075] feat: add enhancement for /info API
Browse files Browse the repository at this point in the history
  • Loading branch information
andrea-deri committed Jul 7, 2023
1 parent 001c93d commit ec8f899
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 2 deletions.
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("aks").name(name).build();
}
}
18 changes: 18 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,18 @@
package it.gov.pagopa.reporting.models;

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

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

private String name;
private String version;
private String environment;
}
88 changes: 88 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,88 @@
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.doReturn;
import static org.mockito.Mockito.mock;

@ExtendWith(MockitoExtension.class)
class InfoTest {

@Mock
ExecutionContext context;

@Spy
Info infoFunction;

@Test
void runOK() {
// 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 ec8f899

Please sign in to comment.