Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#160 Optional wiring of checks array as an object keyed by the health… #162

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,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 @@ -113,6 +112,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 @@ -341,7 +344,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 @@ -354,28 +357,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);
}

}