JAMES-1901 Introduce a builder for ESReporterConfiguration
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/263976aa Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/263976aa Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/263976aa Branch: refs/heads/master Commit: 263976aa4be8936fabee7c10bdbe456ee32cd519 Parents: 649b62d Author: Benoit Tellier <[email protected]> Authored: Thu Feb 2 11:08:36 2017 +0700 Committer: Benoit Tellier <[email protected]> Committed: Fri Feb 3 16:43:43 2017 +0700 ---------------------------------------------------------------------- .../modules/server/ESMetricReporterModule.java | 17 ++-- .../metrics/metrics-es-reporter/pom.xml | 6 ++ .../metrics/es/ESReporterConfiguration.java | 63 +++++++++---- .../metric/es/ESReportedConfigurationTest.java | 95 ++++++++++++++++++++ .../apache/james/metric/es/ESReporterTest.java | 12 ++- 5 files changed, 164 insertions(+), 29 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/263976aa/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/server/ESMetricReporterModule.java ---------------------------------------------------------------------- diff --git a/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/server/ESMetricReporterModule.java b/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/server/ESMetricReporterModule.java index 06cf7a7..f43c5cf 100644 --- a/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/server/ESMetricReporterModule.java +++ b/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/server/ESMetricReporterModule.java @@ -21,7 +21,6 @@ package org.apache.james.modules.server; import java.io.FileNotFoundException; import java.util.List; -import java.util.Optional; import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.HierarchicalConfiguration; @@ -60,16 +59,20 @@ public class ESMetricReporterModule extends AbstractModule { PropertiesConfiguration propertiesReader = getPropertiesConfiguration(fileSystem); if (isMetricEnable(propertiesReader)) { - return ESReporterConfiguration.enabled( - propertiesReader.getString(ElasticSearchMailboxModule.ELASTICSEARCH_MASTER_HOST), - propertiesReader.getInt("elasticsearch.http.port", DEFAULT_ES_HTTP_PORT), - Optional.ofNullable(propertiesReader.getString("elasticsearch.metrics.reports.index", null)), - Optional.ofNullable(propertiesReader.getLong("elasticsearch.metrics.reports.period", null))); + return ESReporterConfiguration.builder() + .enabled() + .onHost(propertiesReader.getString(ElasticSearchMailboxModule.ELASTICSEARCH_MASTER_HOST), + propertiesReader.getInt("elasticsearch.http.port", DEFAULT_ES_HTTP_PORT)) + .onIndex(propertiesReader.getString("elasticsearch.metrics.reports.index", null)) + .periodInSecond(propertiesReader.getLong("elasticsearch.metrics.reports.period", null)) + .build(); } } catch (FileNotFoundException e) { LOGGER.info("Can not locate " + ElasticSearchMailboxModule.ES_CONFIG_FILE); } - return ESReporterConfiguration.disabled(); + return ESReporterConfiguration.builder() + .disabled() + .build(); } private boolean isMetricEnable(PropertiesConfiguration propertiesReader) { http://git-wip-us.apache.org/repos/asf/james-project/blob/263976aa/server/container/metrics/metrics-es-reporter/pom.xml ---------------------------------------------------------------------- diff --git a/server/container/metrics/metrics-es-reporter/pom.xml b/server/container/metrics/metrics-es-reporter/pom.xml index 97f2500..a4a9fce 100644 --- a/server/container/metrics/metrics-es-reporter/pom.xml +++ b/server/container/metrics/metrics-es-reporter/pom.xml @@ -159,6 +159,12 @@ <scope>test</scope> </dependency> <dependency> + <groupId>org.assertj</groupId> + <artifactId>assertj-core</artifactId> + <version>${assertj-3.version}</version> + <scope>test</scope> + </dependency> + <dependency> <groupId>org.elasticsearch</groupId> <artifactId>metrics-elasticsearch-reporter</artifactId> </dependency> http://git-wip-us.apache.org/repos/asf/james-project/blob/263976aa/server/container/metrics/metrics-es-reporter/src/main/java/org/apache/james/metrics/es/ESReporterConfiguration.java ---------------------------------------------------------------------- diff --git a/server/container/metrics/metrics-es-reporter/src/main/java/org/apache/james/metrics/es/ESReporterConfiguration.java b/server/container/metrics/metrics-es-reporter/src/main/java/org/apache/james/metrics/es/ESReporterConfiguration.java index 5b7ad0f..afed836 100644 --- a/server/container/metrics/metrics-es-reporter/src/main/java/org/apache/james/metrics/es/ESReporterConfiguration.java +++ b/server/container/metrics/metrics-es-reporter/src/main/java/org/apache/james/metrics/es/ESReporterConfiguration.java @@ -25,29 +25,56 @@ import com.google.common.base.Preconditions; public class ESReporterConfiguration { + public static Builder builder() { + return new Builder(); + } + + public static class Builder { + private Optional<String> host = Optional.empty(); + private Optional<Integer> port = Optional.empty(); + private Optional<Boolean> enabled = Optional.empty(); + private Optional<String> index = Optional.empty(); + private Optional<Long> periodInSecond = Optional.empty(); + + public Builder enabled() { + this.enabled = Optional.of(ENABLED); + return this; + } + + public Builder disabled() { + this.enabled = Optional.of(DISABLED); + return this; + } + + public Builder onHost(String host, int port) { + this.host = Optional.of(host); + this.port = Optional.of(port); + return this; + } + + public Builder onIndex(String index) { + this.index = Optional.ofNullable(index); + return this; + } + + public Builder periodInSecond(Long periodInSecond) { + this.periodInSecond = Optional.ofNullable(periodInSecond); + return this; + } + + public ESReporterConfiguration build() { + Preconditions.checkState(enabled.isPresent(), "You must specify either enabled or disabled"); + Preconditions.checkState(!enabled.get() || host.isPresent(), "You must specify host when enabled"); + Preconditions.checkState(!enabled.get() || port.isPresent(), "You must specify port when enabled"); + return new ESReporterConfiguration(host, port, enabled.get(), index, periodInSecond); + } + } + public static final boolean ENABLED = true; public static final boolean DISABLED = !ENABLED; public static final String DEFAULT_INDEX = "james-metrics"; public static final long DEFAULT_PERIOD_IN_SECOND = 60L; - public static ESReporterConfiguration disabled() { - return new ESReporterConfiguration( - Optional.empty(), - Optional.empty(), - DISABLED, - Optional.empty(), - Optional.empty()); - } - - public static ESReporterConfiguration enabled(String host, int port, Optional<String> index, Optional<Long> periodInSecond) { - return new ESReporterConfiguration( - Optional.of(host), - Optional.of(port), - ENABLED, - index, - periodInSecond); - } - private final Optional<String> host; private final Optional<Integer> port; private final boolean enabled; http://git-wip-us.apache.org/repos/asf/james-project/blob/263976aa/server/container/metrics/metrics-es-reporter/src/test/java/org/apache/james/metric/es/ESReportedConfigurationTest.java ---------------------------------------------------------------------- diff --git a/server/container/metrics/metrics-es-reporter/src/test/java/org/apache/james/metric/es/ESReportedConfigurationTest.java b/server/container/metrics/metrics-es-reporter/src/test/java/org/apache/james/metric/es/ESReportedConfigurationTest.java new file mode 100644 index 0000000..4359688 --- /dev/null +++ b/server/container/metrics/metrics-es-reporter/src/test/java/org/apache/james/metric/es/ESReportedConfigurationTest.java @@ -0,0 +1,95 @@ +/**************************************************************** + * 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.metric.es; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.apache.james.metrics.es.ESReporterConfiguration; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +public class ESReportedConfigurationTest { + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + @Test + public void builderShouldThrowWhenNotToldIfEnabled() { + expectedException.expect(IllegalStateException.class); + + ESReporterConfiguration.builder().build(); + } + + @Test + public void builderShouldThrowIfEnabledWithoutHostAndPort() { + expectedException.expect(IllegalStateException.class); + + ESReporterConfiguration.builder() + .enabled() + .build(); + } + + @Test + public void builderShouldThrowOnNullHost() { + expectedException.expect(NullPointerException.class); + + ESReporterConfiguration.builder() + .onHost(null, 18); + } + + @Test + public void builderShouldWorkWhenDisabled() { + ESReporterConfiguration configuration = ESReporterConfiguration.builder() + .disabled() + .build(); + + assertThat(configuration.isEnabled()).isFalse(); + assertThat(configuration.getIndex()).isEqualTo(ESReporterConfiguration.DEFAULT_INDEX); + assertThat(configuration.getPeriodInSecond()).isEqualTo(ESReporterConfiguration.DEFAULT_PERIOD_IN_SECOND); + } + + @Test + public void getHostWithPortShouldThrowWhenDisabled() { + ESReporterConfiguration configuration = ESReporterConfiguration.builder() + .disabled() + .build(); + + expectedException.expect(IllegalStateException.class); + + configuration.getHostWithPort(); + } + + @Test + public void builderShouldWorkWhenEnabled() { + int port = 14; + String host = "host"; + ESReporterConfiguration configuration = ESReporterConfiguration.builder() + .enabled() + .onHost(host, port) + .build(); + + assertThat(configuration.isEnabled()).isTrue(); + assertThat(configuration.getHostWithPort()).isEqualTo(host + ":" + port); + assertThat(configuration.getIndex()).isEqualTo(ESReporterConfiguration.DEFAULT_INDEX); + assertThat(configuration.getPeriodInSecond()).isEqualTo(ESReporterConfiguration.DEFAULT_PERIOD_IN_SECOND); + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/263976aa/server/container/metrics/metrics-es-reporter/src/test/java/org/apache/james/metric/es/ESReporterTest.java ---------------------------------------------------------------------- diff --git a/server/container/metrics/metrics-es-reporter/src/test/java/org/apache/james/metric/es/ESReporterTest.java b/server/container/metrics/metrics-es-reporter/src/test/java/org/apache/james/metric/es/ESReporterTest.java index 633a62c..fedc02d 100644 --- a/server/container/metrics/metrics-es-reporter/src/test/java/org/apache/james/metric/es/ESReporterTest.java +++ b/server/container/metrics/metrics-es-reporter/src/test/java/org/apache/james/metric/es/ESReporterTest.java @@ -21,7 +21,6 @@ package org.apache.james.metric.es; import static com.jayway.awaitility.Awaitility.await; -import java.util.Optional; import java.util.Timer; import java.util.TimerTask; @@ -44,8 +43,8 @@ import com.jayway.awaitility.Duration; public class ESReporterTest { - public static final Optional<String> INDEX = Optional.of("index_name"); - public static final Optional<Long> PERIOD_IN_SECOND = Optional.of(1L); + public static final String INDEX = "index_name"; + public static final long PERIOD_IN_SECOND = 1L; public static final int DELAY_IN_MS = 100; public static final int PERIOD_IN_MS = 100; public static final int ES_APPLICATIVE_PORT = 9300; @@ -69,7 +68,12 @@ public class ESReporterTest { registry = new MetricRegistry(); timer = new Timer(); esMetricReporter = new ESMetricReporter( - ESReporterConfiguration.enabled(getContainerIp(), ES_HTTP_PORT, INDEX, PERIOD_IN_SECOND), + ESReporterConfiguration.builder() + .enabled() + .onHost(getContainerIp(), ES_HTTP_PORT) + .onIndex(INDEX) + .periodInSecond(PERIOD_IN_SECOND) + .build(), registry); } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
