-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #557 from xstefank/filters
Implement health filters feature
- Loading branch information
Showing
6 changed files
with
195 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
api/src/main/java/io/smallrye/health/api/HealthContentFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.smallrye.health.api; | ||
|
||
import jakarta.json.JsonObject; | ||
|
||
import io.smallrye.common.annotation.Experimental; | ||
|
||
/** | ||
* Implementations of this interface are invoked prior the health report to filter(remove, add, or modify) fields in the | ||
* returned JSON object. Individual implementations are collected through CDI so they must be annotated with some of the | ||
* bean-defining annotations (e.g., {@link jakarta.enterprise.context.ApplicationScoped}). | ||
*/ | ||
@FunctionalInterface | ||
@Experimental("Health content filtering") | ||
public interface HealthContentFilter { | ||
|
||
/** | ||
* Filters (removes, adds, or modifies) fields in the provided {@link JsonObject}. | ||
* | ||
* @param payload An object representing the payload to be reported to the caller. | ||
*/ | ||
JsonObject filter(JsonObject payload); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...ite/experimental/src/test/java/io/smallrye/health/deployment/TestHealthContentFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package io.smallrye.health.deployment; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.json.Json; | ||
import jakarta.json.JsonObject; | ||
|
||
import io.smallrye.health.api.HealthContentFilter; | ||
|
||
@ApplicationScoped | ||
public class TestHealthContentFilter implements HealthContentFilter { | ||
|
||
@Override | ||
public JsonObject filter(JsonObject payload) { | ||
return Json.createObjectBuilder(payload) | ||
.add("foo", "bar") | ||
.build(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...te/experimental/src/test/java/io/smallrye/health/deployment/TestHealthContentFilter2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package io.smallrye.health.deployment; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.json.Json; | ||
import jakarta.json.JsonObject; | ||
|
||
import io.smallrye.health.api.HealthContentFilter; | ||
|
||
@ApplicationScoped | ||
public class TestHealthContentFilter2 implements HealthContentFilter { | ||
|
||
@Override | ||
public JsonObject filter(JsonObject payload) { | ||
return Json.createObjectBuilder(payload) | ||
.add("foo2", "bar2") | ||
.build(); | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
testsuite/experimental/src/test/java/io/smallrye/health/test/HealthContentFilterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* 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 jakarta.json.JsonArray; | ||
import jakarta.json.JsonObject; | ||
|
||
import org.jboss.arquillian.container.test.api.Deployment; | ||
import org.jboss.arquillian.container.test.api.OperateOnDeployment; | ||
import org.jboss.arquillian.container.test.api.RunAsClient; | ||
import org.jboss.shrinkwrap.api.Archive; | ||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
import io.smallrye.health.deployment.SuccessLiveness; | ||
import io.smallrye.health.deployment.TestHealthContentFilter; | ||
import io.smallrye.health.deployment.TestHealthContentFilter2; | ||
|
||
@RunAsClient | ||
public class HealthContentFilterTest extends TCKBase { | ||
|
||
private static final String DEPLOYMENT_1 = "deployment1"; | ||
private static final String DEPLOYMENT_2 = "deployment2"; | ||
|
||
@Deployment(name = DEPLOYMENT_1) | ||
public static Archive getDeployment() { | ||
return DeploymentUtils.createWarFileWithClasses(HealthContentFilterTest.class.getSimpleName(), | ||
TestHealthContentFilter.class, SuccessLiveness.class, TCKBase.class); | ||
} | ||
|
||
@Deployment(name = DEPLOYMENT_2) | ||
public static Archive getDeployment2() { | ||
return DeploymentUtils.createWarFileWithClasses(HealthContentFilterTest.class.getSimpleName() + "2", | ||
TestHealthContentFilter.class, TestHealthContentFilter2.class, SuccessLiveness.class, TCKBase.class); | ||
} | ||
|
||
/** | ||
* Verifies that the filter implementations process the payload before its returned to the caller. | ||
*/ | ||
@Test | ||
@OperateOnDeployment(DEPLOYMENT_1) | ||
public void testHealthContentFilterFiltersReturnedJson() { | ||
Response response = getUrlHealthContents(); | ||
|
||
// status code | ||
Assert.assertEquals(response.getStatus(), 200); | ||
|
||
JsonObject json = readJson(response); | ||
|
||
// response size | ||
JsonArray checks = json.getJsonArray("checks"); | ||
Assert.assertEquals(checks.size(), 1, "Expected one check response"); | ||
|
||
JsonObject checkJson = checks.getJsonObject(0); | ||
Assert.assertEquals(SuccessLiveness.class.getName(), checkJson.getString("name")); | ||
verifySuccessStatus(checkJson); | ||
|
||
assertOverallSuccess(json); | ||
|
||
System.out.println(json); | ||
Assert.assertEquals("bar", json.getString("foo")); | ||
} | ||
|
||
/** | ||
* Verifies that the filter implementations process the payload before its returned to the caller. | ||
*/ | ||
@Test | ||
@OperateOnDeployment(DEPLOYMENT_2) | ||
public void testMultipleHealthContentFiltersFilterReturnedJson() { | ||
Response response = getUrlHealthContents(); | ||
|
||
// status code | ||
Assert.assertEquals(response.getStatus(), 200); | ||
|
||
JsonObject json = readJson(response); | ||
|
||
// response size | ||
JsonArray checks = json.getJsonArray("checks"); | ||
Assert.assertEquals(checks.size(), 1, "Expected one check response"); | ||
|
||
JsonObject checkJson = checks.getJsonObject(0); | ||
Assert.assertEquals(SuccessLiveness.class.getName(), checkJson.getString("name")); | ||
verifySuccessStatus(checkJson); | ||
|
||
assertOverallSuccess(json); | ||
|
||
System.out.println(json); | ||
Assert.assertEquals("bar", json.getString("foo")); | ||
Assert.assertEquals("bar2", json.getString("foo2")); | ||
} | ||
|
||
} |