This is an automated email from the ASF dual-hosted git repository. btellier pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 3b61bfa5c799c2ce04179a3d7f432da2f30920b4 Author: LanKhuat <[email protected]> AuthorDate: Mon Mar 23 09:56:04 2020 +0700 JAMES-3117 Add PeriodicalHealthChecksConfiguration/Test --- .../james/PeriodicalHealthChecksConfiguration.java | 114 ++++++++++++++ .../PeriodicalHealthChecksConfigurationTest.java | 164 +++++++++++++++++++++ 2 files changed, 278 insertions(+) diff --git a/server/container/guice/guice-common/src/main/java/org/apache/james/PeriodicalHealthChecksConfiguration.java b/server/container/guice/guice-common/src/main/java/org/apache/james/PeriodicalHealthChecksConfiguration.java new file mode 100644 index 0000000..e8e052f --- /dev/null +++ b/server/container/guice/guice-common/src/main/java/org/apache/james/PeriodicalHealthChecksConfiguration.java @@ -0,0 +1,114 @@ +/**************************************************************** + * 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; + +import java.util.Objects; + +import org.apache.commons.configuration2.Configuration; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Preconditions; + +public class PeriodicalHealthChecksConfiguration { + + static final String HEALTH_CHECK_INITIAL_DELAY = "healthcheck.initial.delay"; + static final String HEALTH_CHECK_PERIOD = "healthcheck.period"; + static final long DEFAULT_HEALTH_CHECK_INITIAL_DELAY = 60; + static final long DEFAULT_HEALTH_CHECK_PERIOD = 60; + static final long ZERO = 0; + public static final PeriodicalHealthChecksConfiguration DEFAULT_CONFIGURATION = builder() + .initialDelay(DEFAULT_HEALTH_CHECK_INITIAL_DELAY) + .period(DEFAULT_HEALTH_CHECK_PERIOD) + .build(); + + public interface Builder { + + @FunctionalInterface + interface RequiredInitialDelay { + RequiredPeriod initialDelay(long initialDelay); + } + + @FunctionalInterface + interface RequiredPeriod { + ReadyToBuild period(long period); + } + + class ReadyToBuild { + private final long initialDelay; + private final long period; + + ReadyToBuild(long initialDelay, long period) { + this.initialDelay = initialDelay; + this.period = period; + } + + PeriodicalHealthChecksConfiguration build() { + Preconditions.checkArgument(initialDelay > ZERO, "'initialDelay' must be positive"); + Preconditions.checkArgument(period > ZERO, "'period' must be positive"); + + return new PeriodicalHealthChecksConfiguration(initialDelay, period); + } + } + } + + public static Builder.RequiredInitialDelay builder() { + return initialDelay -> period -> new Builder.ReadyToBuild(initialDelay, period); + } + + public static PeriodicalHealthChecksConfiguration from(Configuration configuration) { + return builder() + .initialDelay(configuration.getLong(HEALTH_CHECK_INITIAL_DELAY, DEFAULT_HEALTH_CHECK_INITIAL_DELAY)) + .period(configuration.getLong(HEALTH_CHECK_PERIOD, DEFAULT_HEALTH_CHECK_PERIOD)) + .build(); + } + + private final long initialDelay; + private final long period; + + @VisibleForTesting + PeriodicalHealthChecksConfiguration(long initialDelay, long period) { + this.initialDelay = initialDelay; + this.period = period; + } + + public long getInitialDelay() { + return initialDelay; + } + + public long getPeriod() { + return period; + } + + @Override + public final boolean equals(Object o) { + if (o instanceof PeriodicalHealthChecksConfiguration) { + PeriodicalHealthChecksConfiguration that = (PeriodicalHealthChecksConfiguration) o; + + return Objects.equals(this.initialDelay, that.initialDelay) + && Objects.equals(this.period, that.period); + } + return false; + } + + @Override + public final int hashCode() { + return Objects.hash(initialDelay, period); + } +} \ No newline at end of file diff --git a/server/container/guice/guice-common/src/test/java/org/apache/james/PeriodicalHealthChecksConfigurationTest.java b/server/container/guice/guice-common/src/test/java/org/apache/james/PeriodicalHealthChecksConfigurationTest.java new file mode 100644 index 0000000..a51945a --- /dev/null +++ b/server/container/guice/guice-common/src/test/java/org/apache/james/PeriodicalHealthChecksConfigurationTest.java @@ -0,0 +1,164 @@ +/**************************************************************** + * 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; + +import static org.apache.james.PeriodicalHealthChecksConfiguration.DEFAULT_HEALTH_CHECK_INITIAL_DELAY; +import static org.apache.james.PeriodicalHealthChecksConfiguration.DEFAULT_HEALTH_CHECK_PERIOD; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import org.apache.commons.configuration2.PropertiesConfiguration; +import org.apache.commons.configuration2.ex.ConversionException; +import org.junit.jupiter.api.Test; + +import nl.jqno.equalsverifier.EqualsVerifier; + +public class PeriodicalHealthChecksConfigurationTest { + + private static final String EMPTY_STRING = ""; + private static final String RANDOM_STRING = "abcdsfsfs"; + private static final long NEGATIVE_NUMBER = -1; + private static final long INITIAL_DELAY = 10; + private static final long PERIOD = 5; + + @Test + void shouldMatchBeanContract() { + EqualsVerifier.forClass(PeriodicalHealthChecksConfiguration.class) + .verify(); + } + + @Test + void fromShouldThrowWhenInitialDelayIsEmpty() { + PropertiesConfiguration configuration = new PropertiesConfiguration(); + configuration.addProperty(PeriodicalHealthChecksConfiguration.HEALTH_CHECK_INITIAL_DELAY, EMPTY_STRING); + configuration.addProperty(PeriodicalHealthChecksConfiguration.HEALTH_CHECK_PERIOD, DEFAULT_HEALTH_CHECK_PERIOD); + + assertThatThrownBy(() -> PeriodicalHealthChecksConfiguration. from(configuration)) + .isInstanceOf(ConversionException.class); + } + + @Test + void fromShouldThrowWhenPeriodIsEmpty() { + PropertiesConfiguration configuration = new PropertiesConfiguration(); + configuration.addProperty(PeriodicalHealthChecksConfiguration.HEALTH_CHECK_INITIAL_DELAY, DEFAULT_HEALTH_CHECK_INITIAL_DELAY); + configuration.addProperty(PeriodicalHealthChecksConfiguration.HEALTH_CHECK_PERIOD, EMPTY_STRING); + + assertThatThrownBy(() -> PeriodicalHealthChecksConfiguration.from(configuration)) + .isInstanceOf(ConversionException.class); + } + + @Test + void fromShouldReturnConfigurationWithDefaultValueWhenInitialDelayIsMissing() { + PropertiesConfiguration configuration = new PropertiesConfiguration(); + configuration.addProperty(PeriodicalHealthChecksConfiguration.HEALTH_CHECK_PERIOD, PERIOD); + + assertThat(PeriodicalHealthChecksConfiguration.from(configuration)).isEqualTo(PeriodicalHealthChecksConfiguration.builder() + .initialDelay(DEFAULT_HEALTH_CHECK_INITIAL_DELAY) + .period(PERIOD) + .build()); + } + + @Test + void fromShouldReturnConfigurationWithDefaultValueWhenPeriodIsMissing() { + PropertiesConfiguration configuration = new PropertiesConfiguration(); + configuration.addProperty(PeriodicalHealthChecksConfiguration.HEALTH_CHECK_INITIAL_DELAY, INITIAL_DELAY); + + assertThat(PeriodicalHealthChecksConfiguration.from(configuration)).isEqualTo(PeriodicalHealthChecksConfiguration.builder() + .initialDelay(INITIAL_DELAY) + .period(DEFAULT_HEALTH_CHECK_PERIOD) + .build()); + } + + @Test + void fromShouldReturnConfigurationWithDefaultValueWhenInitialDelayIsNull() { + PropertiesConfiguration configuration = new PropertiesConfiguration(); + configuration.addProperty(PeriodicalHealthChecksConfiguration.HEALTH_CHECK_INITIAL_DELAY, null); + configuration.addProperty(PeriodicalHealthChecksConfiguration.HEALTH_CHECK_PERIOD, PERIOD); + + assertThat(PeriodicalHealthChecksConfiguration.from(configuration)).isEqualTo(PeriodicalHealthChecksConfiguration.builder() + .initialDelay(DEFAULT_HEALTH_CHECK_INITIAL_DELAY) + .period(PERIOD) + .build()); + } + + @Test + void fromShouldReturnConfigurationWithDefaultValueWhenPeriodIsNull() { + PropertiesConfiguration configuration = new PropertiesConfiguration(); + configuration.addProperty(PeriodicalHealthChecksConfiguration.HEALTH_CHECK_INITIAL_DELAY, INITIAL_DELAY); + configuration.addProperty(PeriodicalHealthChecksConfiguration.HEALTH_CHECK_PERIOD, null); + + assertThat(PeriodicalHealthChecksConfiguration.from(configuration)).isEqualTo(PeriodicalHealthChecksConfiguration.builder() + .initialDelay(INITIAL_DELAY) + .period(DEFAULT_HEALTH_CHECK_PERIOD) + .build()); + } + + @Test + void fromShouldThrowWhenInitialDelayIsNotANumber() { + PropertiesConfiguration configuration = new PropertiesConfiguration(); + configuration.addProperty(PeriodicalHealthChecksConfiguration.HEALTH_CHECK_INITIAL_DELAY, RANDOM_STRING); + configuration.addProperty(PeriodicalHealthChecksConfiguration.HEALTH_CHECK_PERIOD, PERIOD); + + assertThatThrownBy(() -> PeriodicalHealthChecksConfiguration.from(configuration)) + .isInstanceOf(ConversionException.class); + } + + @Test + void fromShouldThrowWhenInitialDelayIsNegative() { + PropertiesConfiguration configuration = new PropertiesConfiguration(); + configuration.addProperty(PeriodicalHealthChecksConfiguration.HEALTH_CHECK_INITIAL_DELAY, NEGATIVE_NUMBER); + configuration.addProperty(PeriodicalHealthChecksConfiguration.HEALTH_CHECK_PERIOD, PERIOD); + + assertThatThrownBy(() -> PeriodicalHealthChecksConfiguration.from(configuration)) + .isInstanceOf(IllegalArgumentException.class); + } + + @Test + void fromShouldThrowWhenPeriodIsNegative() { + PropertiesConfiguration configuration = new PropertiesConfiguration(); + configuration.addProperty(PeriodicalHealthChecksConfiguration.HEALTH_CHECK_INITIAL_DELAY, INITIAL_DELAY); + configuration.addProperty(PeriodicalHealthChecksConfiguration.HEALTH_CHECK_PERIOD, NEGATIVE_NUMBER); + + assertThatThrownBy(() -> PeriodicalHealthChecksConfiguration.from(configuration)) + .isInstanceOf(IllegalArgumentException.class); + } + + @Test + void fromShouldThrowWhenPeriodIsNotANumber() { + PropertiesConfiguration configuration = new PropertiesConfiguration(); + configuration.addProperty(PeriodicalHealthChecksConfiguration.HEALTH_CHECK_INITIAL_DELAY, INITIAL_DELAY); + configuration.addProperty(PeriodicalHealthChecksConfiguration.HEALTH_CHECK_PERIOD, RANDOM_STRING); + + assertThatThrownBy(() -> PeriodicalHealthChecksConfiguration.from(configuration)) + .isInstanceOf(ConversionException.class); + } + + @Test + void fromShouldReturnProvidedConfiguration() { + PropertiesConfiguration configuration = new PropertiesConfiguration(); + configuration.addProperty(PeriodicalHealthChecksConfiguration.HEALTH_CHECK_INITIAL_DELAY, INITIAL_DELAY); + configuration.addProperty(PeriodicalHealthChecksConfiguration.HEALTH_CHECK_PERIOD, PERIOD); + + assertThat(PeriodicalHealthChecksConfiguration.from(configuration)).isEqualTo(PeriodicalHealthChecksConfiguration.builder() + .initialDelay(INITIAL_DELAY) + .period(PERIOD) + .build()); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
