From f6e226a1750c618b67d71ed856a02ea2a26fd9c5 Mon Sep 17 00:00:00 2001 From: Alessio Cialini Date: Tue, 17 Oct 2023 16:09:09 +0200 Subject: [PATCH] feat: add health check and updated helm chart values --- helm/values-dev.yaml | 4 +- helm/values-prod.yaml | 4 +- helm/values-uat.yaml | 4 +- .../pagopa/receipt/pdf/generator/Health.java | 32 ++++++++++++ .../receipt/pdf/generator/HealthTest.java | 49 +++++++++++++++++++ 5 files changed, 87 insertions(+), 6 deletions(-) create mode 100644 src/main/java/it/gov/pagopa/receipt/pdf/generator/Health.java create mode 100644 src/test/java/it/gov/pagopa/receipt/pdf/generator/HealthTest.java diff --git a/helm/values-dev.yaml b/helm/values-dev.yaml index 2738ac09..dc225659 100644 --- a/helm/values-dev.yaml +++ b/helm/values-dev.yaml @@ -9,14 +9,14 @@ microservice-chart: # https://github.com/Azure/azure-functions-host/blob/dev/src/WebJobs.Script.WebHost/Controllers/HostController.cs livenessProbe: httpGet: - path: /info + path: /health port: 80 initialDelaySeconds: 60 failureThreshold: 6 periodSeconds: 10 readinessProbe: httpGet: - path: /info + path: /health port: 80 initialDelaySeconds: 60 failureThreshold: 6 diff --git a/helm/values-prod.yaml b/helm/values-prod.yaml index 6e445e9e..a803432e 100644 --- a/helm/values-prod.yaml +++ b/helm/values-prod.yaml @@ -9,14 +9,14 @@ microservice-chart: # https://github.com/Azure/azure-functions-host/blob/dev/src/WebJobs.Script.WebHost/Controllers/HostController.cs livenessProbe: httpGet: - path: /info + path: /health port: 80 initialDelaySeconds: 60 failureThreshold: 6 periodSeconds: 10 readinessProbe: httpGet: - path: /info + path: /health port: 80 initialDelaySeconds: 60 failureThreshold: 6 diff --git a/helm/values-uat.yaml b/helm/values-uat.yaml index 71447a4d..04b367e2 100644 --- a/helm/values-uat.yaml +++ b/helm/values-uat.yaml @@ -9,14 +9,14 @@ microservice-chart: # https://github.com/Azure/azure-functions-host/blob/dev/src/WebJobs.Script.WebHost/Controllers/HostController.cs livenessProbe: httpGet: - path: /info + path: /health port: 80 initialDelaySeconds: 60 failureThreshold: 6 periodSeconds: 10 readinessProbe: httpGet: - path: /info + path: /health port: 80 initialDelaySeconds: 60 failureThreshold: 6 diff --git a/src/main/java/it/gov/pagopa/receipt/pdf/generator/Health.java b/src/main/java/it/gov/pagopa/receipt/pdf/generator/Health.java new file mode 100644 index 00000000..0a3415c3 --- /dev/null +++ b/src/main/java/it/gov/pagopa/receipt/pdf/generator/Health.java @@ -0,0 +1,32 @@ +package it.gov.pagopa.receipt.pdf.generator; + +import com.microsoft.azure.functions.*; +import com.microsoft.azure.functions.annotation.AuthorizationLevel; +import com.microsoft.azure.functions.annotation.FunctionName; +import com.microsoft.azure.functions.annotation.HttpTrigger; + +import java.util.Optional; + + +/** + * Azure Functions with Azure Http trigger. + */ +public class Health { + + /** + * This function will be invoked when a Http Trigger occurs + * + * @return response with HttpStatus.OK + */ + @FunctionName("Health") + public HttpResponseMessage run ( + @HttpTrigger(name = "HealthTrigger", + methods = {HttpMethod.GET}, + route = "health", + authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage> request, + final ExecutionContext context) { + + return request.createResponseBuilder(HttpStatus.OK) + .build(); + } +} \ No newline at end of file diff --git a/src/test/java/it/gov/pagopa/receipt/pdf/generator/HealthTest.java b/src/test/java/it/gov/pagopa/receipt/pdf/generator/HealthTest.java new file mode 100644 index 00000000..b7de4fda --- /dev/null +++ b/src/test/java/it/gov/pagopa/receipt/pdf/generator/HealthTest.java @@ -0,0 +1,49 @@ +package it.gov.pagopa.receipt.pdf.generator; + +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.receipt.pdf.generator.util.HttpResponseMessageMock; +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 org.mockito.stubbing.Answer; + +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.mock; + +@ExtendWith(MockitoExtension.class) +class HealthTest { + + @Mock + ExecutionContext executionContextMock; + + @Spy + Health sut; + + @Test + void runOK() { + @SuppressWarnings("unchecked") + HttpRequestMessage> request = mock(HttpRequestMessage.class); + + doAnswer((Answer) invocation -> { + HttpStatus status = (HttpStatus) invocation.getArguments()[0]; + return new HttpResponseMessageMock.HttpResponseMessageBuilderMock().status(status); + }).when(request).createResponseBuilder(any(HttpStatus.class)); + + // test execution + HttpResponseMessage response = sut.run(request, executionContextMock); + + // test assertion + assertNotNull(response); + assertEquals(HttpStatus.OK, response.getStatus()); + } +}