JAMES-2575 fix style, improve unit tests Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/d738a21e Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/d738a21e Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/d738a21e
Branch: refs/heads/master Commit: d738a21e37ca828db4734ef870f54b2bfbbd01d0 Parents: 0fa1961 Author: Michael Schnitzler <[email protected]> Authored: Sat Oct 27 23:46:50 2018 +0200 Committer: Benoit Tellier <[email protected]> Committed: Wed Oct 31 08:48:29 2018 +0700 ---------------------------------------------------------------------- .../dto/HealthCheckExecutionResultDto.java | 2 +- .../webadmin/routes/HealthCheckRoutes.java | 55 +++++++------ .../webadmin/routes/HealthCheckRoutesTest.java | 86 ++++++++++++++++---- 3 files changed, 98 insertions(+), 45 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/d738a21e/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/dto/HealthCheckExecutionResultDto.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/dto/HealthCheckExecutionResultDto.java b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/dto/HealthCheckExecutionResultDto.java index 1512cb4..2663b3e 100644 --- a/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/dto/HealthCheckExecutionResultDto.java +++ b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/dto/HealthCheckExecutionResultDto.java @@ -45,7 +45,7 @@ public class HealthCheckExecutionResultDto { } public String getStatus() { - return healthCheckResult.getStatus().toString(); + return healthCheckResult.getStatus().getValue(); } public Optional<String> getCause() { http://git-wip-us.apache.org/repos/asf/james-project/blob/d738a21e/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 acdfb22..45df88b 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 @@ -45,6 +45,7 @@ import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiResponse; import io.swagger.annotations.ApiResponses; +import spark.HaltException; import spark.Request; import spark.Response; import spark.Service; @@ -80,9 +81,9 @@ public class HealthCheckRoutes implements PublicRoutes { @GET @ApiOperation(value = "Validate all health checks") @ApiResponses(value = { - @ApiResponse(code = HttpStatus.OK_200, message = "OK"), - @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, - message = "Internal server error - When one check has failed.") + @ApiResponse(code = HttpStatus.OK_200, message = "OK"), + @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, + message = "Internal server error - When one check has failed.") }) public Object validateHealthchecks(Request request, Response response) { List<Result> anyUnhealthyOrDegraded = retrieveUnhealthyOrDegradedHealthChecks(); @@ -97,34 +98,25 @@ public class HealthCheckRoutes implements PublicRoutes { @ApiOperation(value = "Perform the component's health check") @ApiImplicitParams({ @ApiImplicitParam( - name="componentName", - required=true, - paramType="path", - dataType="String", - defaultValue="None", - example="/checks/Cassandra%20Backend", - value="The URL encoded name of the component to check.") + name = "componentName", + required = true, + paramType = "path", + dataType = "String", + defaultValue = "None", + example = "/checks/Cassandra%20Backend", + value = "The URL encoded name of the component to check.") }) public Object performHealthCheckForComponent(Request request, Response response) { String componentName = request.params("componentName"); Optional<HealthCheck> optHealthCheck = healthChecks.stream() - .filter(c -> c.componentName().getName().equals(componentName)) - .findFirst(); + .filter(c -> c.componentName().getName().equals(componentName)) + .findFirst(); - if (optHealthCheck.isPresent()) { - HealthCheck healthCheck = optHealthCheck.get(); - Result result = healthCheck.check(); - logFailedCheck(result); - response.status(getCorrespondingStatusCode(result)); - return new HealthCheckExecutionResultDto(result); - } - else { - throw ErrorResponder.builder() - .message(String.format("Component with name %s cannot be found", componentName)) - .statusCode(HttpStatus.NOT_FOUND_404) - .type(ErrorResponder.ErrorType.NOT_FOUND) - .haltError(); - } + HealthCheck healthCheck = optHealthCheck.orElseThrow(() -> throw404(componentName)); + Result result = healthCheck.check(); + logFailedCheck(result); + response.status(getCorrespondingStatusCode(result)); + return new HealthCheckExecutionResultDto(result); } private int getCorrespondingStatusCode(List<Result> anyUnhealthy) { @@ -136,7 +128,7 @@ public class HealthCheckRoutes implements PublicRoutes { } private int getCorrespondingStatusCode(Result result) { - switch(result.getStatus()) { + switch (result.getStatus()) { case HEALTHY: return HttpStatus.OK_200; case DEGRADED: @@ -170,4 +162,13 @@ public class HealthCheckRoutes implements PublicRoutes { .filter(result -> result.isUnHealthy() || result.isDegraded()) .collect(Guavate.toImmutableList()); } + + private HaltException throw404(String componentName) { + return ErrorResponder.builder() + .message(String.format("Component with name %s cannot be found", componentName)) + .statusCode(HttpStatus.NOT_FOUND_404) + .type(ErrorResponder.ErrorType.NOT_FOUND) + .haltError(); + } + } http://git-wip-us.apache.org/repos/asf/james-project/blob/d738a21e/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 fcc4d09..3326d24 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 @@ -19,16 +19,21 @@ 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.equalTo; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.nullValue; +import java.net.MalformedURLException; import java.util.HashSet; import java.util.Set; import org.apache.james.core.healthcheck.ComponentName; import org.apache.james.core.healthcheck.HealthCheck; import org.apache.james.core.healthcheck.Result; +import org.apache.james.core.healthcheck.ResultStatus; import org.apache.james.metrics.logger.DefaultMetricFactory; import org.apache.james.webadmin.WebAdminServer; import org.apache.james.webadmin.WebAdminUtils; @@ -139,48 +144,95 @@ public class HealthCheckRoutesTest { public void performHealthCheckShouldReturnOkWhenHealthCheckIsHealthy() { healthChecks.add(healthCheck(Result.healthy(COMPONENT_NAME_1))); - when() - .get("/checks/{componentName}", COMPONENT_NAME_1.getName()) + given() + .pathParam("componentName", COMPONENT_NAME_1.getName()) + .when() + .get("/checks/{componentName}") .then() - .statusCode(HttpStatus.OK_200); + .statusCode(HttpStatus.OK_200) + .body("componentName", equalTo("component-1")) + .body("escapedComponentName", equalTo("component-1")) + .body("status", equalTo(ResultStatus.HEALTHY.getValue())) + .body("cause", is(nullValue())); } @Test public void performHealthCheckShouldReturnNotFoundWhenComponentNameIsUnknown() { - when() - .get("/checks/{componentName}", "unknown") + + given() + .pathParam("componentName", "unknown") + .when() + .get("/checks/{componentName}") .then() - .statusCode(HttpStatus.NOT_FOUND_404); + .statusCode(HttpStatus.NOT_FOUND_404) + .body("details", is(nullValue())) + .body("type", equalTo("notFound")) + .body("message", equalTo("Component with name unknown cannot be found")) + .body("statusCode", is(404)); } @Test public void performHealthCheckShouldReturnInternalErrorWhenHealthCheckIsDegraded() { - healthChecks.add(healthCheck(Result.degraded(COMPONENT_NAME_1))); + healthChecks.add(healthCheck(Result.degraded(COMPONENT_NAME_1, "the cause"))); - when() - .get("/checks/{componentName}", COMPONENT_NAME_1.getName()) + given() + .pathParam("componentName", COMPONENT_NAME_1.getName()) + .when() + .get("/checks/{componentName}") .then() - .statusCode(HttpStatus.INTERNAL_SERVER_ERROR_500); + .statusCode(HttpStatus.INTERNAL_SERVER_ERROR_500) + .body("componentName", equalTo("component-1")) + .body("escapedComponentName", equalTo("component-1")) + .body("status", equalTo(ResultStatus.DEGRADED.getValue())) + .body("cause", equalTo("the cause")); } @Test public void performHealthCheckShouldReturnInternalErrorWhenHealthCheckIsUnhealthy() { healthChecks.add(healthCheck(Result.unhealthy(COMPONENT_NAME_1))); - when() - .get("/checks/{componentName}", COMPONENT_NAME_1.getName()) + given() + .pathParam("componentName", COMPONENT_NAME_1.getName()) + .when() + .get("/checks/{componentName}") .then() - .statusCode(HttpStatus.INTERNAL_SERVER_ERROR_500); + .statusCode(HttpStatus.INTERNAL_SERVER_ERROR_500) + .body("componentName", equalTo("component-1")) + .body("escapedComponentName", equalTo("component-1")) + .body("status", equalTo(ResultStatus.UNHEALTHY.getValue())) + .body("cause", is(nullValue())); } @Test public void performHealthCheckShouldReturnProperlyEscapedComponentName() { healthChecks.add(healthCheck(Result.healthy(COMPONENT_NAME_3))); - when() - .get("/checks/{componentName}", COMPONENT_NAME_3.getName()) + given() + .pathParam("componentName", COMPONENT_NAME_3.getName()) + .when() + .get("/checks/{componentName}") + .then() + .body("componentName", equalTo("component 3")) + .body("escapedComponentName", equalTo("component%203")) + .body("status", equalTo(ResultStatus.HEALTHY.getValue())) + .body("cause", is(nullValue())); + } + + @Test + public void performHealthCheckShouldWorkWithEscapedPathParam() throws MalformedURLException { + healthChecks.add(healthCheck(Result.healthy(COMPONENT_NAME_3))); + + // disable URL encoding + RestAssured.requestSpecification.urlEncodingEnabled(false); + + given() + .pathParam("componentName", "component%203") + .when() + .get("/checks/{componentName}") .then() - .body("componentName", equalTo("component 3"), - "escapedComponentName", equalTo("component%203")); + .body("componentName", equalTo("component 3")) + .body("escapedComponentName", equalTo("component%203")) + .body("status", equalTo(ResultStatus.HEALTHY.getValue())) + .body("cause", is(nullValue())); } } \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
