Skip to content

Commit

Permalink
Issue-243 Updated HealthCheckResponse state property to status
Browse files Browse the repository at this point in the history
Signed-off-by: Prashanth G <pgunapal@ca.ibm.com>
  • Loading branch information
pgunapal committed Jun 4, 2020
1 parent 5c2f336 commit 761dec0
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 31 deletions.
10 changes: 5 additions & 5 deletions README.adoc
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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<Map<String, Object>> data;

Expand Down Expand Up @@ -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();
}
}
```
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -49,19 +49,19 @@ public class HealthCheckResponse {

private final String name;

private final State state;
private final Status status;

private final Optional<Map<String, Object>> 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<Map<String, Object>> data) {
public HealthCheckResponse(String name, Status status, Optional<Map<String, Object>> data) {
this.name = name;
this.state = state;
this.status = status;
this.data = data;
}

Expand All @@ -70,7 +70,7 @@ public HealthCheckResponse(String name, State state, Optional<Map<String, Object
*/
public HealthCheckResponse() {
name = null;
state = null;
status = null;
data = null;
}

Expand Down Expand Up @@ -146,14 +146,14 @@ private static HealthCheckResponseProvider getProvider() {

// the actual contract

public enum State {UP, DOWN}
public enum Status {UP, DOWN}

public String getName(){
return name;
}

public State getState(){
return state;
public Status getStatus(){
return status;
}

public Optional<Map<String, Object>> getData() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -75,15 +75,15 @@ 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
*/
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 <i>not</i> successful.
*
* @return this builder
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -21,6 +21,6 @@
/**
* <p>Microprofile Health</p>
**/
@org.osgi.annotation.versioning.Version("1.2")
@org.osgi.annotation.versioning.Version("2.0")
package org.eclipse.microprofile.health;

10 changes: 5 additions & 5 deletions spec/src/main/asciidoc/java-api.adoc
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -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<Map<String, Object>> getData();

Expand Down Expand Up @@ -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();
}
}
```
8 changes: 4 additions & 4 deletions spec/src/main/asciidoc/protocol-wireformat.adoc
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -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]]
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<HealthCheckResponse> hcr = mapper.readValue(checks.toString(), new TypeReference<List<HealthCheckResponse>>() {});

// 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.");
}
}
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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();
}
}

0 comments on commit 761dec0

Please sign in to comment.