From cb64c6dcbad920aeda3d7c757dd89f29de36761f Mon Sep 17 00:00:00 2001 From: xstefank Date: Tue, 25 Aug 2020 12:59:34 +0200 Subject: [PATCH] #160 Optional wiring of checks array as an object keyed by the health check name --- .../health/SmallRyeHealthReporter.java | 34 ++++++--- .../health/test/AllCustomFailedTest.java | 3 - .../health/test/ChecksAsObjectOutputTest.java | 74 +++++++++++++++++++ 3 files changed, 96 insertions(+), 15 deletions(-) create mode 100644 testsuite/experimental/src/test/java/io/smallrye/health/test/ChecksAsObjectOutputTest.java diff --git a/implementation/src/main/java/io/smallrye/health/SmallRyeHealthReporter.java b/implementation/src/main/java/io/smallrye/health/SmallRyeHealthReporter.java index d7342081..1681e0d1 100644 --- a/implementation/src/main/java/io/smallrye/health/SmallRyeHealthReporter.java +++ b/implementation/src/main/java/io/smallrye/health/SmallRyeHealthReporter.java @@ -21,7 +21,6 @@ import javax.enterprise.inject.spi.Bean; import javax.enterprise.inject.spi.BeanManager; import javax.inject.Inject; -import javax.json.JsonArray; import javax.json.JsonArrayBuilder; import javax.json.JsonObject; import javax.json.JsonObjectBuilder; @@ -104,6 +103,10 @@ public class SmallRyeHealthReporter { @ConfigProperty(name = "io.smallrye.health.timeout.seconds", defaultValue = "60") int timeoutSeconds; + @Inject + @ConfigProperty(name = "io.smallrye.health.response.checks.object", defaultValue = "false") + boolean checksAsObject; + /* specification defined configuration values */ @Inject @@ -325,7 +328,7 @@ private Uni getHealthAsync(Collection> return Uni.combine().all().unis(healthCheckUnis) .combinedWith(responses -> { - JsonArrayBuilder results = jsonProvider.createArrayBuilder(); + List results = new ArrayList<>(); HealthCheckResponse.Status status = HealthCheckResponse.Status.UP; for (Object o : responses) { @@ -338,28 +341,35 @@ private Uni getHealthAsync(Collection> } private SmallRyeHealth createEmptySmallRyeHealth(String emptyOutcome) { - return createSmallRyeHealth(jsonProvider.createArrayBuilder(), null, emptyOutcome); + return createSmallRyeHealth(new ArrayList<>(), null, emptyOutcome); } - private SmallRyeHealth createSmallRyeHealth(JsonArrayBuilder results, HealthCheckResponse.Status status, + private SmallRyeHealth createSmallRyeHealth(List results, HealthCheckResponse.Status status, String emptyOutcome) { JsonObjectBuilder builder = jsonProvider.createObjectBuilder(); - JsonArray checkResults = results.build(); + builder.add("status", results.isEmpty() ? emptyOutcome : status.toString()); + + if (!checksAsObject) { + JsonArrayBuilder checksArrayBuilder = jsonProvider.createArrayBuilder(); + results.forEach(r -> checksArrayBuilder.add(jsonObject(r))); - builder.add("status", checkResults.isEmpty() ? emptyOutcome : status.toString()); - builder.add("checks", checkResults); + builder.add("checks", checksArrayBuilder.build()); + } else { + JsonObjectBuilder checksObjectBuilder = jsonProvider.createObjectBuilder(); + results.forEach(r -> checksObjectBuilder.add(r.getName(), jsonObject(r))); + + builder.add("checks", checksObjectBuilder.build()); + } return new SmallRyeHealth(builder.build()); } - private HealthCheckResponse.Status handleResponse(HealthCheckResponse response, JsonArrayBuilder results, + private HealthCheckResponse.Status handleResponse(HealthCheckResponse response, List results, HealthCheckResponse.Status globalOutcome) { - JsonObject responseJson = jsonObject(response); - results.add(responseJson); + results.add(response); if (globalOutcome == HealthCheckResponse.Status.UP) { - String status = responseJson.getString("status"); - if (status.equals("DOWN")) { + if (response.getStatus().equals(HealthCheckResponse.Status.DOWN)) { return HealthCheckResponse.Status.DOWN; } } diff --git a/testsuite/experimental/src/test/java/io/smallrye/health/test/AllCustomFailedTest.java b/testsuite/experimental/src/test/java/io/smallrye/health/test/AllCustomFailedTest.java index e6972510..6614f2b1 100644 --- a/testsuite/experimental/src/test/java/io/smallrye/health/test/AllCustomFailedTest.java +++ b/testsuite/experimental/src/test/java/io/smallrye/health/test/AllCustomFailedTest.java @@ -34,9 +34,6 @@ import io.smallrye.health.deployment.FailedCustom; import io.smallrye.health.deployment.SuccessfulCustom; -/** - * @author Prashanth Gunapalasingam - */ public class AllCustomFailedTest extends TCKBase { @Deployment diff --git a/testsuite/experimental/src/test/java/io/smallrye/health/test/ChecksAsObjectOutputTest.java b/testsuite/experimental/src/test/java/io/smallrye/health/test/ChecksAsObjectOutputTest.java new file mode 100644 index 00000000..2c0d0cdf --- /dev/null +++ b/testsuite/experimental/src/test/java/io/smallrye/health/test/ChecksAsObjectOutputTest.java @@ -0,0 +1,74 @@ +/* + * 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 io.smallrye.health.test; + +import javax.json.JsonObject; + +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.container.test.api.RunAsClient; +import org.jboss.shrinkwrap.api.Archive; +import org.jboss.shrinkwrap.api.asset.StringAsset; +import org.testng.Assert; +import org.testng.annotations.Test; + +import io.smallrye.health.deployment.SuccessLiveness; +import io.smallrye.health.deployment.SuccessReadiness; + +public class ChecksAsObjectOutputTest extends TCKBase { + + @Deployment + public static Archive getDeployment() { + return DeploymentUtils.createWarFileWithClasses(ChecksAsObjectOutputTest.class.getSimpleName(), + SuccessLiveness.class, SuccessReadiness.class) + .addAsManifestResource(new StringAsset("io.smallrye.health.response.checks.object=true"), + "microprofile-config.properties"); + } + + /** + * Verifies the custom output format set by io.smallrye.health.response.checks.object config property. + */ + @Test + @RunAsClient + public void testPayload() { + Response response = getUrlHealthContents(); + + // status code + Assert.assertEquals(response.getStatus(), 200); + + JsonObject json = readJson(response); + + // response + JsonObject checks = json.getJsonObject("checks"); + Assert.assertNotNull(checks, "Checks object was not included in the response"); + + JsonObject liveness = checks.getJsonObject(SuccessLiveness.class.getName()); + Assert.assertNotNull(liveness, "Expected liveness check not included in the response"); + verifySuccessStatus(liveness); + + JsonObject readiness = checks.getJsonObject(SuccessReadiness.class.getName()); + Assert.assertNotNull(readiness, "Expected readiness check not included in the response"); + + assertOverallSuccess(json); + } + +}