JAMES-2526 Define the health check API
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/f30ffeb7 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/f30ffeb7 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/f30ffeb7 Branch: refs/heads/master Commit: f30ffeb729960319f03838fbc941736c698f1d56 Parents: 70f5318 Author: Antoine Duprat <adup...@linagora.com> Authored: Wed Aug 22 14:48:10 2018 +0200 Committer: Antoine Duprat <adup...@linagora.com> Committed: Mon Aug 27 14:18:06 2018 +0200 ---------------------------------------------------------------------- .../james/core/healthcheck/ComponentName.java | 32 ++++ .../james/core/healthcheck/HealthCheck.java | 25 +++ .../apache/james/core/healthcheck/Result.java | 78 ++++++++ .../james/core/healthcheck/ResultStatus.java | 23 +++ .../james/core/healthcheck/ResultTest.java | 183 +++++++++++++++++++ 5 files changed, 341 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/f30ffeb7/core/src/main/java/org/apache/james/core/healthcheck/ComponentName.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/james/core/healthcheck/ComponentName.java b/core/src/main/java/org/apache/james/core/healthcheck/ComponentName.java new file mode 100644 index 0000000..ecb248a --- /dev/null +++ b/core/src/main/java/org/apache/james/core/healthcheck/ComponentName.java @@ -0,0 +1,32 @@ +/**************************************************************** + * 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.core.healthcheck; + +public class ComponentName { + private final String name; + + public ComponentName(String name) { + this.name = name; + } + + public String getName() { + return name; + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/f30ffeb7/core/src/main/java/org/apache/james/core/healthcheck/HealthCheck.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/james/core/healthcheck/HealthCheck.java b/core/src/main/java/org/apache/james/core/healthcheck/HealthCheck.java new file mode 100644 index 0000000..f7659b0 --- /dev/null +++ b/core/src/main/java/org/apache/james/core/healthcheck/HealthCheck.java @@ -0,0 +1,25 @@ +/**************************************************************** + * 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.core.healthcheck; + +public interface HealthCheck { + ComponentName componentName(); + + Result check(); +} http://git-wip-us.apache.org/repos/asf/james-project/blob/f30ffeb7/core/src/main/java/org/apache/james/core/healthcheck/Result.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/james/core/healthcheck/Result.java b/core/src/main/java/org/apache/james/core/healthcheck/Result.java new file mode 100644 index 0000000..fe9657a --- /dev/null +++ b/core/src/main/java/org/apache/james/core/healthcheck/Result.java @@ -0,0 +1,78 @@ +/**************************************************************** + * 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.core.healthcheck; + +import java.util.Optional; + +public class Result { + + public static Result healthy(ComponentName componentName) { + return new Result(componentName, ResultStatus.HEALTHY, Optional.empty()); + } + + public static Result unhealthy(ComponentName componentName, String cause) { + return new Result(componentName, ResultStatus.UNHEALTHY, Optional.of(cause)); + } + + public static Result unhealthy(ComponentName componentName) { + return new Result(componentName, ResultStatus.UNHEALTHY, Optional.empty()); + } + + public static Result degraded(ComponentName componentName, String cause) { + return new Result(componentName, ResultStatus.DEGRADED, Optional.of(cause)); + } + + public static Result degraded(ComponentName componentName) { + return new Result(componentName, ResultStatus.DEGRADED, Optional.empty()); + } + + private final ComponentName componentName; + private final ResultStatus status; + private final Optional<String> cause; + + private Result(ComponentName componentName, ResultStatus status, Optional<String> cause) { + this.componentName = componentName; + this.status = status; + this.cause = cause; + } + + public ComponentName getComponentName() { + return componentName; + } + + public ResultStatus getStatus() { + return status; + } + + public boolean isHealthy() { + return status == ResultStatus.HEALTHY; + } + + public boolean isDegraded() { + return status == ResultStatus.DEGRADED; + } + + public boolean isUnHealthy() { + return status == ResultStatus.UNHEALTHY; + } + + public Optional<String> getCause() { + return cause; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/james-project/blob/f30ffeb7/core/src/main/java/org/apache/james/core/healthcheck/ResultStatus.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/james/core/healthcheck/ResultStatus.java b/core/src/main/java/org/apache/james/core/healthcheck/ResultStatus.java new file mode 100644 index 0000000..89109e9 --- /dev/null +++ b/core/src/main/java/org/apache/james/core/healthcheck/ResultStatus.java @@ -0,0 +1,23 @@ +/**************************************************************** + * 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.core.healthcheck; + +public enum ResultStatus { + HEALTHY, DEGRADED, UNHEALTHY; +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/james-project/blob/f30ffeb7/core/src/test/java/org/apache/james/core/healthcheck/ResultTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/james/core/healthcheck/ResultTest.java b/core/src/test/java/org/apache/james/core/healthcheck/ResultTest.java new file mode 100644 index 0000000..ece974a --- /dev/null +++ b/core/src/test/java/org/apache/james/core/healthcheck/ResultTest.java @@ -0,0 +1,183 @@ +/**************************************************************** + * 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.core.healthcheck; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import org.junit.jupiter.api.Test; + +public class ResultTest { + + private static final ComponentName COMPONENT_NAME = new ComponentName("component"); + + @Test + public void componentNameShouldBeKeptWhenHealthy() { + Result result = Result.healthy(COMPONENT_NAME); + + assertThat(result.getComponentName()).isEqualTo(COMPONENT_NAME); + } + + @Test + public void componentNameShouldBeKeptWhenUnhealthy() { + Result result = Result.unhealthy(COMPONENT_NAME); + + assertThat(result.getComponentName()).isEqualTo(COMPONENT_NAME); + } + + @Test + public void componentNameShouldBeKeptWhenDegraded() { + Result result = Result.degraded(COMPONENT_NAME); + + assertThat(result.getComponentName()).isEqualTo(COMPONENT_NAME); + } + + @Test + public void statusShouldBeHealthyWhenHealthy() { + Result result = Result.healthy(COMPONENT_NAME); + + assertThat(result.getStatus()).isEqualTo(ResultStatus.HEALTHY); + } + + @Test + public void causeShouldBeEmptyWhenHealthy() { + Result result = Result.healthy(COMPONENT_NAME); + + assertThat(result.getCause()).isEmpty(); + } + + @Test + public void isHealthyShouldBeTrueWhenHealthy() { + Result result = Result.healthy(COMPONENT_NAME); + + assertThat(result.isHealthy()).isTrue(); + } + + @Test + public void isDegradedShouldBeFalseWhenHealthy() { + Result result = Result.healthy(COMPONENT_NAME); + + assertThat(result.isDegraded()).isFalse(); + } + + @Test + public void isUnhealthyShouldBeFalseWhenHealthy() { + Result result = Result.healthy(COMPONENT_NAME); + + assertThat(result.isUnHealthy()).isFalse(); + } + + @Test + public void statusShouldBeDegradedWhenDegraded() { + Result result = Result.degraded(COMPONENT_NAME, "cause"); + + assertThat(result.getStatus()).isEqualTo(ResultStatus.DEGRADED); + } + + @Test + public void causeMayBeEmptyWhenDegraded() { + Result result = Result.degraded(COMPONENT_NAME); + + assertThat(result.getCause()).isEmpty(); + } + + @Test + public void degradedShouldThrowWhenNullCause() { + assertThatThrownBy(() -> Result.degraded(COMPONENT_NAME, null)) + .isInstanceOf(NullPointerException.class); + } + + @Test + public void causeShouldBeKeptWhenNotDegraded() { + String cause = "cause"; + Result result = Result.degraded(COMPONENT_NAME, cause); + + assertThat(result.getCause()).contains(cause); + } + + @Test + public void isHealthyShouldBeFalseWhenDegraded() { + Result result = Result.degraded(COMPONENT_NAME); + + assertThat(result.isHealthy()).isFalse(); + } + + @Test + public void isDegradedShouldBeFalseWhenDegraded() { + Result result = Result.degraded(COMPONENT_NAME); + + assertThat(result.isDegraded()).isTrue(); + } + + @Test + public void isUnhealthyShouldBeTrueWhenDegraded() { + Result result = Result.degraded(COMPONENT_NAME); + + assertThat(result.isUnHealthy()).isFalse(); + } + + @Test + public void statusShouldBeUnhealthyWhenUnhealthy() { + Result result = Result.unhealthy(COMPONENT_NAME, "cause"); + + assertThat(result.getStatus()).isEqualTo(ResultStatus.UNHEALTHY); + } + + @Test + public void causeMayBeEmptyWhenUnhealthy() { + Result result = Result.unhealthy(COMPONENT_NAME); + + assertThat(result.getCause()).isEmpty(); + } + + @Test + public void causeShouldBeKeptWhenNotEmpty() { + String cause = "cause"; + Result result = Result.unhealthy(COMPONENT_NAME, cause); + + assertThat(result.getCause()).contains(cause); + } + + @Test + public void isHealthyShouldBeFalseWhenUnhealthy() { + Result result = Result.unhealthy(COMPONENT_NAME); + + assertThat(result.isHealthy()).isFalse(); + } + + @Test + public void isDegradedShouldBeFalseWhenUnhealthy() { + Result result = Result.unhealthy(COMPONENT_NAME); + + assertThat(result.isDegraded()).isFalse(); + } + + @Test + public void isUnhealthyShouldBeTrueWhenUnhealthy() { + Result result = Result.unhealthy(COMPONENT_NAME); + + assertThat(result.isUnHealthy()).isTrue(); + } + + @Test + public void unhealthyShouldThrowWhenNullCause() { + assertThatThrownBy(() -> Result.unhealthy(COMPONENT_NAME, null)) + .isInstanceOf(NullPointerException.class); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org