Repository: james-project Updated Branches: refs/heads/master 38b3d7a59 -> 633b0c263
JAMES-2574 Implemented route for listing health checks and added tests and documentation Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/633b0c26 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/633b0c26 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/633b0c26 Branch: refs/heads/master Commit: 633b0c2638562d3fd73a6d772db89f4944e82d25 Parents: 38b3d7a Author: kratostaine <madhu.bhat...@gmail.com> Authored: Wed Oct 31 19:49:43 2018 +0530 Committer: Benoit Tellier <btell...@linagora.com> Committed: Thu Nov 1 11:58:15 2018 +0700 ---------------------------------------------------------------------- .../james/webadmin/dto/HealthCheckDto.java | 44 ++++++++++++++++++++ .../webadmin/routes/HealthCheckRoutes.java | 14 +++++++ .../webadmin/routes/HealthCheckRoutesTest.java | 35 ++++++++++++++++ src/site/markdown/server/manage-webadmin.md | 24 +++++++++++ 4 files changed, 117 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/633b0c26/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/dto/HealthCheckDto.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/dto/HealthCheckDto.java b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/dto/HealthCheckDto.java new file mode 100644 index 0000000..c77ba86 --- /dev/null +++ b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/dto/HealthCheckDto.java @@ -0,0 +1,44 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you 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. * + ****************************************************************/ + +package org.apache.james.webadmin.dto; + +import org.apache.james.core.healthcheck.ComponentName; + +import com.fasterxml.jackson.annotation.JsonPropertyOrder; +import com.google.common.net.UrlEscapers; + +@JsonPropertyOrder({"componentName", "escapedComponentName"}) +public class HealthCheckDto { + + private ComponentName componentName; + + public HealthCheckDto(ComponentName componentName) { + this.componentName = componentName; + } + + public String getComponentName() { + return componentName.getName(); + } + + public String getEscapedComponentName() { + return UrlEscapers.urlPathSegmentEscaper().escape(componentName.getName()); + } +} + http://git-wip-us.apache.org/repos/asf/james-project/blob/633b0c26/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/routes/HealthCheckRoutes.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/routes/HealthCheckRoutes.java b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/routes/HealthCheckRoutes.java index 6c11455..0ac1a3f 100644 --- a/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/routes/HealthCheckRoutes.java +++ b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/routes/HealthCheckRoutes.java @@ -29,6 +29,7 @@ import javax.ws.rs.Path; import org.apache.james.core.healthcheck.HealthCheck; import org.apache.james.core.healthcheck.Result; import org.apache.james.webadmin.PublicRoutes; +import org.apache.james.webadmin.dto.HealthCheckDto; import org.apache.james.webadmin.dto.HealthCheckExecutionResultDto; import org.apache.james.webadmin.utils.ErrorResponder; import org.apache.james.webadmin.utils.JsonTransformer; @@ -56,6 +57,7 @@ public class HealthCheckRoutes implements PublicRoutes { private static final Logger LOGGER = LoggerFactory.getLogger(HealthCheckRoutes.class); public static final String HEALTHCHECK = "/healthcheck"; + public static final String CHECKS = "/checks"; private static final String PARAM_COMPONENT_NAME = "componentName"; @@ -77,6 +79,7 @@ public class HealthCheckRoutes implements PublicRoutes { public void define(Service service) { service.get(HEALTHCHECK, this::validateHealthchecks, jsonTransformer); service.get(HEALTHCHECK + "/checks/:" + PARAM_COMPONENT_NAME, this::performHealthCheckForComponent, jsonTransformer); + service.get(HEALTHCHECK + CHECKS, this::getHealthChecks, jsonTransformer); } @GET @@ -120,6 +123,17 @@ public class HealthCheckRoutes implements PublicRoutes { return new HealthCheckExecutionResultDto(result); } + @GET + @Path(CHECKS) + @ApiOperation(value = "List all health checks") + @ApiResponse(code = HttpStatus.OK_200, message = "List of all health checks", + response = HealthCheckDto.class, responseContainer = "List") + public Object getHealthChecks(Request request, Response response) { + return healthChecks.stream() + .map(healthCheck -> new HealthCheckDto(healthCheck.componentName())) + .collect(Guavate.toImmutableList()); + } + private int getCorrespondingStatusCode(List<Result> anyUnhealthy) { if (anyUnhealthy.isEmpty()) { return HttpStatus.OK_200; http://git-wip-us.apache.org/repos/asf/james-project/blob/633b0c26/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/routes/HealthCheckRoutesTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/routes/HealthCheckRoutesTest.java b/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/routes/HealthCheckRoutesTest.java index 6235f3b..7bed3ab 100644 --- a/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/routes/HealthCheckRoutesTest.java +++ b/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/routes/HealthCheckRoutesTest.java @@ -22,6 +22,7 @@ package org.apache.james.webadmin.routes; import static io.restassured.RestAssured.given; import static io.restassured.RestAssured.when; import static org.apache.james.webadmin.WebAdminServer.NO_CONFIGURATION; +import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.nullValue; @@ -240,4 +241,38 @@ public class HealthCheckRoutesTest { .body("status", equalTo(ResultStatus.HEALTHY.getValue())) .body("cause", is(nullValue())); } + + @Test + public void getHealthchecksShouldReturnEmptyWhenNoHealthChecks() { + when() + .get(HealthCheckRoutes.CHECKS) + .then() + .body(is("[]")) + .body("", hasSize(0)) + .statusCode(HttpStatus.OK_200); + } + + @Test + public void getHealthchecksShouldReturnHealthCheckWhenHealthCheckPresent() { + healthChecks.add(healthCheck(Result.healthy(COMPONENT_NAME_3))); + when() + .get(HealthCheckRoutes.CHECKS) + .then() + .body("", hasSize(1)) + .body("componentName[0]", equalTo(NAME_3)) + .body("escapedComponentName[0]", equalTo(NAME_3_ESCAPED)) + .statusCode(HttpStatus.OK_200); + } + + @Test + public void getHealthchecksShouldReturnHealthChecksWhenHealthChecksPresent() { + healthChecks.add(healthCheck(Result.healthy(COMPONENT_NAME_2))); + healthChecks.add(healthCheck(Result.healthy(COMPONENT_NAME_3))); + when() + .get(HealthCheckRoutes.CHECKS) + .then() + .body("", hasSize(2)) + .statusCode(HttpStatus.OK_200); + } + } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/james-project/blob/633b0c26/src/site/markdown/server/manage-webadmin.md ---------------------------------------------------------------------- diff --git a/src/site/markdown/server/manage-webadmin.md b/src/site/markdown/server/manage-webadmin.md index 308a85b..5ce90ac 100644 --- a/src/site/markdown/server/manage-webadmin.md +++ b/src/site/markdown/server/manage-webadmin.md @@ -48,6 +48,7 @@ as exposed above). To avoid information duplication, this is ommited on endpoint - [Check all components](#Check_all_components) - [Check single component](#Check_single_component) + - [List all health checks](#List_all_health_checks) ### Check all components @@ -88,6 +89,29 @@ Response codes: - 200: The check has answered with a Healthy status. - 404: A component with the given name was not found. - 500: The check has anwered with a Unhealthy or Degraded status. + +### List all health checks + +This endpoint lists all the available health checks. + +``` +curl -XGET http://ip:port/healthcheck/checks +``` + +Will return the list of all available health checks. + +``` +[ + { + "componentName": "Cassandra Backend", + "escapedComponentName": "Cassandra%20Backend" + } +] +``` + +Response codes: + + - 200: List of available health checks ## Administrating domains --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org