From 761dec09f0d0fdb458e8f4621fcb42923dbac0df Mon Sep 17 00:00:00 2001 From: Prashanth G Date: Wed, 3 Jun 2020 12:59:26 -0400 Subject: [PATCH] Issue-243 Updated HealthCheckResponse state property to status Signed-off-by: Prashanth G --- README.adoc | 10 +-- .../health/HealthCheckResponse.java | 18 ++-- .../health/HealthCheckResponseBuilder.java | 8 +- .../microprofile/health/package-info.java | 4 +- spec/src/main/asciidoc/java-api.adoc | 10 +-- .../main/asciidoc/protocol-wireformat.adoc | 8 +- .../HealthCheckResponseValidationTest.java | 82 +++++++++++++++++++ .../health/tck/deployment/DelegateHealth.java | 4 +- 8 files changed, 113 insertions(+), 31 deletions(-) create mode 100644 tck/src/main/java/org/eclipse/microprofile/health/tck/HealthCheckResponseValidationTest.java diff --git a/README.adoc b/README.adoc index cdda79b..a3812ca 100644 --- a/README.adoc +++ b/README.adoc @@ -1,5 +1,5 @@ // -// Copyright (c) 2016-2019 Contributors to the Eclipse Foundation +// Copyright (c) 2016-2020 Contributors to the Eclipse Foundation // // See the NOTICES file(s) distributed with this work for additional // information regarding copyright ownership. @@ -60,11 +60,11 @@ The runtime will _call()_ the _HealthCheck_ which in turn creates a _HealthCheck ```java public class HealthCheckResponse { - public enum State { UP, DOWN } + public enum Status { UP, DOWN } private final String name; - private final State state; + private final Status status; private final Optional> data; @@ -149,13 +149,13 @@ class MyChecks { @Produces @Liveness HealthCheck check1() { - return () -> HealthCheckResponse.named("heap-memory").state(getMemUsage() < 0.9).build(); + return () -> HealthCheckResponse.named("heap-memory").status(getMemUsage() < 0.9).build(); } @Produces @Readiness HealthCheck check2() { - return () -> HealthCheckResponse.named("cpu-usage").state(getCpuUsage() < 0.9).build(); + return () -> HealthCheckResponse.named("cpu-usage").status(getCpuUsage() < 0.9).build(); } } ``` diff --git a/api/src/main/java/org/eclipse/microprofile/health/HealthCheckResponse.java b/api/src/main/java/org/eclipse/microprofile/health/HealthCheckResponse.java index 3c54cf3..1ad47ed 100644 --- a/api/src/main/java/org/eclipse/microprofile/health/HealthCheckResponse.java +++ b/api/src/main/java/org/eclipse/microprofile/health/HealthCheckResponse.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017 Contributors to the Eclipse Foundation + * Copyright (c) 2017-2020 Contributors to the Eclipse Foundation * * See the NOTICES file(s) distributed with this work for additional * information regarding copyright ownership. @@ -49,19 +49,19 @@ public class HealthCheckResponse { private final String name; - private final State state; + private final Status status; private final Optional> data; /** * Constructor allowing instantiation from 3rd party framework like MicroProfile Rest client * @param name Health Check procedure's name - * @param state Health Check procedure's state + * @param status Health Check procedure's status * @param data additional data for Health Check procedure */ - public HealthCheckResponse(String name, State state, Optional> data) { + public HealthCheckResponse(String name, Status status, Optional> data) { this.name = name; - this.state = state; + this.status = status; this.data = data; } @@ -70,7 +70,7 @@ public HealthCheckResponse(String name, State state, Optional> getData() { diff --git a/api/src/main/java/org/eclipse/microprofile/health/HealthCheckResponseBuilder.java b/api/src/main/java/org/eclipse/microprofile/health/HealthCheckResponseBuilder.java index 43ebf25..768215e 100644 --- a/api/src/main/java/org/eclipse/microprofile/health/HealthCheckResponseBuilder.java +++ b/api/src/main/java/org/eclipse/microprofile/health/HealthCheckResponseBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017 Contributors to the Eclipse Foundation + * Copyright (c) 2017-2020 Contributors to the Eclipse Foundation * * See the NOTICES file(s) distributed with this work for additional * information regarding copyright ownership. @@ -75,7 +75,7 @@ public abstract class HealthCheckResponseBuilder { public abstract HealthCheckResponseBuilder withData(String key, boolean value); /** - * Sets the status of the health check response to {@link HealthCheckResponse.State#UP}. + * Sets the status of the health check response to {@link HealthCheckResponse.Status#UP}. * This implies that the health check was successful. * * @return this builder @@ -83,7 +83,7 @@ public abstract class HealthCheckResponseBuilder { public abstract HealthCheckResponseBuilder up(); /** - * Sets the status of the health check response to {@link HealthCheckResponse.State#DOWN}. + * Sets the status of the health check response to {@link HealthCheckResponse.Status#DOWN}. * This implies that the health check was not successful. * * @return this builder @@ -96,7 +96,7 @@ public abstract class HealthCheckResponseBuilder { * @param up the status * @return this builder */ - public abstract HealthCheckResponseBuilder state(boolean up); + public abstract HealthCheckResponseBuilder status(boolean up); /** * Creates a {@link HealthCheckResponse} from the current builder. diff --git a/api/src/main/java/org/eclipse/microprofile/health/package-info.java b/api/src/main/java/org/eclipse/microprofile/health/package-info.java index 21d8c18..970bd27 100644 --- a/api/src/main/java/org/eclipse/microprofile/health/package-info.java +++ b/api/src/main/java/org/eclipse/microprofile/health/package-info.java @@ -1,6 +1,6 @@ /* ******************************************************************************* - * Copyright (c) 2016-2017 Contributors to the Eclipse Foundation + * Copyright (c) 2016-2020 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -21,6 +21,6 @@ /** *

Microprofile Health

**/ -@org.osgi.annotation.versioning.Version("1.2") +@org.osgi.annotation.versioning.Version("2.0") package org.eclipse.microprofile.health; diff --git a/spec/src/main/asciidoc/java-api.adoc b/spec/src/main/asciidoc/java-api.adoc index b891034..9118f95 100644 --- a/spec/src/main/asciidoc/java-api.adoc +++ b/spec/src/main/asciidoc/java-api.adoc @@ -1,5 +1,5 @@ // -// Copyright (c) 2016-2019 Eclipse Microprofile Contributors: +// Copyright (c) 2016-2020 Eclipse Microprofile Contributors: // See overview.adoc // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -78,11 +78,11 @@ The runtime will `call()` each `HealthCheck` which in turn creates a `HealthChec ``` public class HealthCheckResponse { - public enum State { UP, DOWN } + public enum Status { UP, DOWN } public abstract String getName(); - public abstract State getState(); + public abstract Status getStatus(); public abstract Optional> getData(); @@ -177,13 +177,13 @@ class MyChecks { @Produces @Liveness HealthCheck check1() { - return () -> HealthCheckResponse.named("heap-memory").state(getMemUsage() < 0.9).build(); + return () -> HealthCheckResponse.named("heap-memory").status(getMemUsage() < 0.9).build(); } @Produces @Readiness HealthCheck check2() { - return () -> HealthCheckResponse.named("cpu-usage").state(getCpuUsage() < 0.9).build(); + return () -> HealthCheckResponse.named("cpu-usage").status(getCpuUsage() < 0.9).build(); } } ``` diff --git a/spec/src/main/asciidoc/protocol-wireformat.adoc b/spec/src/main/asciidoc/protocol-wireformat.adoc index 96c3a93..b3246c6 100644 --- a/spec/src/main/asciidoc/protocol-wireformat.adoc +++ b/spec/src/main/asciidoc/protocol-wireformat.adoc @@ -1,5 +1,5 @@ // -// Copyright (c) 2016-2017 Eclipse Microprofile Contributors: +// Copyright (c) 2016-2020 Eclipse Microprofile Contributors: // See overview.adoc // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -120,11 +120,11 @@ Each provider MUST provide the REST/HTTP interaction, but MAY provide other prot === Wireformats -* Producer MUST support JSON encoded payload with simple UP/DOWN states +* Producer MUST support JSON encoded payload with simple UP/DOWN statuses * Producers MAY support an additional information holder with key/value pairs to provide further context (i.e. disk.free.space=120mb). * The JSON response payload MUST be compatible with the one described in Appendix B * The JSON response MUST contain the `name` entry specifying the name of the check, to support protocols that support external identifier (i.e. URI) -* The JSON response MUST contain the `status` entry specifying the state as String: “UP” or “DOWN” +* The JSON response MUST contain the `status` entry specifying the status as String: “UP” or “DOWN” * The JSON MAY support an additional information holder to carry key value pairs that provide additional context [[health-check-procedures]] @@ -205,7 +205,7 @@ Aspects regarding the secure access of health check information. The following table gives valid health check responses for all kinds of health checks: |=== -| Request | HTTP Status | JSON Payload | State | Comment +| Request | HTTP Status | JSON Payload | Status | Comment | /health/live /health/ready /health diff --git a/tck/src/main/java/org/eclipse/microprofile/health/tck/HealthCheckResponseValidationTest.java b/tck/src/main/java/org/eclipse/microprofile/health/tck/HealthCheckResponseValidationTest.java new file mode 100644 index 0000000..1182859 --- /dev/null +++ b/tck/src/main/java/org/eclipse/microprofile/health/tck/HealthCheckResponseValidationTest.java @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2020 Contributors to the Eclipse Foundation + * + * See the NOTICES file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * + */ + +package org.eclipse.microprofile.health.tck; + +import static org.eclipse.microprofile.health.tck.DeploymentUtils.createWarFileWithClasses; + +import java.util.List; + +import javax.json.JsonArray; +import javax.json.JsonObject; + +import org.eclipse.microprofile.health.HealthCheckResponse; +import org.eclipse.microprofile.health.tck.deployment.SuccessfulLiveness; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.container.test.api.RunAsClient; +import org.jboss.shrinkwrap.api.Archive; +import org.testng.Assert; +import org.testng.annotations.Test; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; + +/** + * @author Prashanth Gunapalasingam + */ +public class HealthCheckResponseValidationTest extends TCKBase { + + @Deployment + public static Archive getDeployment() { + return createWarFileWithClasses(HealthCheckResponseValidationTest.class.getSimpleName(), SuccessfulLiveness.class); + } + + /** + * Validates the HealthCheckResponse concrete class definition by verifying if its + * deserialized properties correctly maps to the JSON schema protocol defined by the + * specification and the JSON health check response returned by the implementation. + */ + @Test + @RunAsClient + public void testValidateConcreteHealthCheckResponse() throws Exception { + Response response = getUrlHealthContents(); + + Assert.assertEquals(response.getStatus(), 200); + + JsonObject json = readJson(response); + JsonArray checks = json.getJsonArray("checks"); + + ObjectMapper mapper = new ObjectMapper(); + List hcr = mapper.readValue(checks.toString(), new TypeReference>() {}); + + // Validates the name property from the HealthCheckResponse class + Assert.assertEquals( + hcr.get(0).getName(), + "successful-check", + String.format("Unexpected value for the HealthCheckResponse \"name\" property : %s", hcr.get(0).getName()) + ); + + // Validates the status property from the HealthCheckResponse class + Assert.assertEquals(hcr.get(0).getStatus(), HealthCheckResponse.Status.UP, + "Expected a successful check status for the HealthCheckResponse \"status\" property."); + } +} diff --git a/tck/src/main/java/org/eclipse/microprofile/health/tck/deployment/DelegateHealth.java b/tck/src/main/java/org/eclipse/microprofile/health/tck/deployment/DelegateHealth.java index 16d897f..2ea0a5d 100644 --- a/tck/src/main/java/org/eclipse/microprofile/health/tck/deployment/DelegateHealth.java +++ b/tck/src/main/java/org/eclipse/microprofile/health/tck/deployment/DelegateHealth.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017 Contributors to the Eclipse Foundation + * Copyright (c) 2017-2020 Contributors to the Eclipse Foundation * * See the NOTICES file(s) distributed with this work for additional * information regarding copyright ownership. @@ -40,6 +40,6 @@ public class DelegateHealth implements HealthCheck { @Override public HealthCheckResponse call() { - return HealthCheckResponse.named("delegate-check").state(delegate.isTheSystemHealthy()).build(); + return HealthCheckResponse.named("delegate-check").status(delegate.isTheSystemHealthy()).build(); } }