Skip to content

Commit

Permalink
smallrye#160 Optional wiring of checks array as an object keyed by th…
Browse files Browse the repository at this point in the history
…e health check name
  • Loading branch information
xstefank committed Aug 25, 2020
1 parent eed3159 commit cb64c6d
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -325,7 +328,7 @@ private Uni<SmallRyeHealth> getHealthAsync(Collection<Uni<HealthCheckResponse>>

return Uni.combine().all().unis(healthCheckUnis)
.combinedWith(responses -> {
JsonArrayBuilder results = jsonProvider.createArrayBuilder();
List<HealthCheckResponse> results = new ArrayList<>();
HealthCheckResponse.Status status = HealthCheckResponse.Status.UP;

for (Object o : responses) {
Expand All @@ -338,28 +341,35 @@ private Uni<SmallRyeHealth> getHealthAsync(Collection<Uni<HealthCheckResponse>>
}

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

}

0 comments on commit cb64c6d

Please sign in to comment.