Repository: james-project Updated Branches: refs/heads/master 5b46ce3c1 -> b0687e850
JAMES-2617 Move project tests to JUnit 5 Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/b6ee1457 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/b6ee1457 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/b6ee1457 Branch: refs/heads/master Commit: b6ee145745181c23cacc7a1abb8540b4015baa35 Parents: ca6d6da Author: Raphael Ouazana <[email protected]> Authored: Wed Dec 5 14:49:11 2018 +0100 Committer: Raphael Ouazana <[email protected]> Committed: Wed Dec 5 15:19:18 2018 +0100 ---------------------------------------------------------------------- .../metrics/metrics-es-reporter/pom.xml | 19 ++++-- .../es/DockerElasticSearch2Extension.java | 61 ++++++++++++++++++++ .../metric/es/ESReportedConfigurationTest.java | 48 +++++++-------- .../apache/james/metric/es/ESReporterTest.java | 38 +++++------- 4 files changed, 109 insertions(+), 57 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/b6ee1457/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 8a9583d..5756b45 100644 --- a/server/container/metrics/metrics-es-reporter/pom.xml +++ b/server/container/metrics/metrics-es-reporter/pom.xml @@ -53,6 +53,10 @@ <artifactId>guava</artifactId> </dependency> <dependency> + <groupId>commons-logging</groupId> + <artifactId>commons-logging</artifactId> + </dependency> + <dependency> <groupId>io.dropwizard.metrics</groupId> <artifactId>metrics-core</artifactId> </dependency> @@ -66,11 +70,6 @@ <artifactId>javax.inject</artifactId> </dependency> <dependency> - <groupId>junit</groupId> - <artifactId>junit</artifactId> - <scope>test</scope> - </dependency> - <dependency> <groupId>org.assertj</groupId> <artifactId>assertj-core</artifactId> <scope>test</scope> @@ -80,6 +79,16 @@ <artifactId>metrics-elasticsearch-reporter</artifactId> </dependency> <dependency> + <groupId>org.junit.jupiter</groupId> + <artifactId>junit-jupiter-engine</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.junit.platform</groupId> + <artifactId>junit-platform-launcher</artifactId> + <scope>test</scope> + </dependency> + <dependency> <groupId>org.testcontainers</groupId> <artifactId>testcontainers</artifactId> <scope>test</scope> http://git-wip-us.apache.org/repos/asf/james-project/blob/b6ee1457/server/container/metrics/metrics-es-reporter/src/test/java/org/apache/james/metric/es/DockerElasticSearch2Extension.java ---------------------------------------------------------------------- diff --git a/server/container/metrics/metrics-es-reporter/src/test/java/org/apache/james/metric/es/DockerElasticSearch2Extension.java b/server/container/metrics/metrics-es-reporter/src/test/java/org/apache/james/metric/es/DockerElasticSearch2Extension.java new file mode 100644 index 0000000..76e6006 --- /dev/null +++ b/server/container/metrics/metrics-es-reporter/src/test/java/org/apache/james/metric/es/DockerElasticSearch2Extension.java @@ -0,0 +1,61 @@ +/**************************************************************** + * 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 org.apache.james.util.docker.Images; +import org.apache.james.util.docker.RateLimiters; +import org.apache.james.util.docker.SwarmGenericContainer; +import org.junit.jupiter.api.extension.AfterEachCallback; +import org.junit.jupiter.api.extension.BeforeEachCallback; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.api.extension.ParameterContext; +import org.junit.jupiter.api.extension.ParameterResolutionException; +import org.junit.jupiter.api.extension.ParameterResolver; +import org.testcontainers.containers.wait.strategy.HostPortWaitStrategy; + +public class DockerElasticSearch2Extension implements ParameterResolver, BeforeEachCallback, AfterEachCallback { + private final SwarmGenericContainer elasticSearchContainer; + + private DockerElasticSearch2Extension() { + this.elasticSearchContainer = new SwarmGenericContainer(Images.ELASTICSEARCH) + .withAffinityToContainer() + .withExposedPorts(ESReporterTest.ES_HTTP_PORT) + .waitingFor(new HostPortWaitStrategy().withRateLimiter(RateLimiters.TWENTIES_PER_SECOND)); + } + + @Override + public void beforeEach(ExtensionContext extensionContext) { + elasticSearchContainer.start(); + } + + @Override + public void afterEach(ExtensionContext extensionContext) { + elasticSearchContainer.stop(); + } + + @Override + public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { + return (parameterContext.getParameter().getType() == SwarmGenericContainer.class); + } + + @Override + public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { + return elasticSearchContainer; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/james-project/blob/b6ee1457/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 index 4359688..ae25bc9 100644 --- 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 @@ -20,43 +20,36 @@ package org.apache.james.metric.es; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import org.apache.james.metrics.es.ESReporterConfiguration; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; +import org.junit.jupiter.api.Test; -public class ESReportedConfigurationTest { - - @Rule - public ExpectedException expectedException = ExpectedException.none(); +class ESReportedConfigurationTest { @Test - public void builderShouldThrowWhenNotToldIfEnabled() { - expectedException.expect(IllegalStateException.class); - - ESReporterConfiguration.builder().build(); + void builderShouldThrowWhenNotToldIfEnabled() { + assertThatThrownBy(() -> ESReporterConfiguration.builder().build()) + .isInstanceOf(IllegalStateException.class); } @Test - public void builderShouldThrowIfEnabledWithoutHostAndPort() { - expectedException.expect(IllegalStateException.class); - - ESReporterConfiguration.builder() - .enabled() - .build(); + void builderShouldThrowIfEnabledWithoutHostAndPort() { + assertThatThrownBy(() -> ESReporterConfiguration.builder() + .enabled() + .build()) + .isInstanceOf(IllegalStateException.class); } @Test - public void builderShouldThrowOnNullHost() { - expectedException.expect(NullPointerException.class); - - ESReporterConfiguration.builder() - .onHost(null, 18); + void builderShouldThrowOnNullHost() { + assertThatThrownBy(() -> ESReporterConfiguration.builder() + .onHost(null, 18)) + .isInstanceOf(NullPointerException.class); } @Test - public void builderShouldWorkWhenDisabled() { + void builderShouldWorkWhenDisabled() { ESReporterConfiguration configuration = ESReporterConfiguration.builder() .disabled() .build(); @@ -67,18 +60,17 @@ public class ESReportedConfigurationTest { } @Test - public void getHostWithPortShouldThrowWhenDisabled() { + void getHostWithPortShouldThrowWhenDisabled() { ESReporterConfiguration configuration = ESReporterConfiguration.builder() .disabled() .build(); - expectedException.expect(IllegalStateException.class); - - configuration.getHostWithPort(); + assertThatThrownBy(() -> configuration.getHostWithPort()) + .isInstanceOf(IllegalStateException.class); } @Test - public void builderShouldWorkWhenEnabled() { + void builderShouldWorkWhenEnabled() { int port = 14; String host = "host"; ESReporterConfiguration configuration = ESReporterConfiguration.builder() http://git-wip-us.apache.org/repos/asf/james-project/blob/b6ee1457/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 37e7e21..9ae03bc 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 @@ -32,39 +32,31 @@ import org.apache.james.metrics.api.TimeMetric; import org.apache.james.metrics.dropwizard.DropWizardMetricFactory; import org.apache.james.metrics.es.ESMetricReporter; import org.apache.james.metrics.es.ESReporterConfiguration; -import org.apache.james.util.docker.Images; -import org.apache.james.util.docker.RateLimiters; import org.apache.james.util.docker.SwarmGenericContainer; import org.awaitility.Duration; -import org.junit.After; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.testcontainers.containers.wait.strategy.HostPortWaitStrategy; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import com.codahale.metrics.MetricRegistry; import io.restassured.RestAssured; -public class ESReporterTest { +@ExtendWith(DockerElasticSearch2Extension.class) +class ESReporterTest { 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_HTTP_PORT = 9200; - @Rule - public SwarmGenericContainer esContainer = new SwarmGenericContainer(Images.ELASTICSEARCH) - .withAffinityToContainer() - .withExposedPorts(ES_HTTP_PORT) - .waitingFor(new HostPortWaitStrategy().withRateLimiter(RateLimiters.TWENTIES_PER_SECOND)); - private ESMetricReporter esMetricReporter; private MetricRegistry registry; private Timer timer; - @Before - public void setUp() { + @BeforeEach + void setUp(SwarmGenericContainer esContainer) { RestAssured.baseURI = String.format("http://%s:%d", esContainer.getHostIp(), esContainer.getMappedPort(ES_HTTP_PORT)); await().atMost(Duration.ONE_MINUTE) .untilAsserted(() -> elasticSearchStarted()); @@ -79,18 +71,18 @@ public class ESReporterTest { .periodInSecond(PERIOD_IN_SECOND) .build(), registry); + + esMetricReporter.start(); } - @After - public void tearDown() { + @AfterEach + void tearDown() { timer.cancel(); esMetricReporter.stop(); } @Test - public void esMetricReporterShouldProduceDocumentsOnAnElasticsearchContainer() { - esMetricReporter.start(); - + void esMetricReporterShouldProduceDocumentsOnAnElasticsearchContainer(SwarmGenericContainer esContainer) { Metric metric = new DropWizardMetricFactory(registry).generate("probe"); TimerTask timerTask = new TimerTask() { @Override @@ -105,9 +97,7 @@ public class ESReporterTest { } @Test - public void esMetricReporterShouldProduceDocumentsOnAnElasticsearchContainerWhenRecordingTimeMetric() { - esMetricReporter.start(); - + void esMetricReporterShouldProduceDocumentsOnAnElasticsearchContainerWhenRecordingTimeMetric(SwarmGenericContainer esContainer) { TimeMetric metric = new DropWizardMetricFactory(registry).timer("itstime"); TimerTask timerTask = new TimerTask() { @Override --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
