JAMES-2553 Rewrite JamesServerWithRetryConnectionTest test in JUNIT 5 Note that we need to use custom extensions
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/89d72341 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/89d72341 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/89d72341 Branch: refs/heads/master Commit: 89d723418668c3279e922f493770eba6166b9aa6 Parents: a697f68 Author: Benoit Tellier <[email protected]> Authored: Mon Oct 1 11:10:20 2018 +0700 Committer: Benoit Tellier <[email protected]> Committed: Thu Oct 4 17:48:43 2018 +0700 ---------------------------------------------------------------------- .../apache/james/DockerElasticSearchRule.java | 81 ------------- .../JamesServerWithRetryConnectionTest.java | 115 +++++++++++++------ 2 files changed, 80 insertions(+), 116 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/89d72341/server/container/guice/cassandra-guice/src/test/java/org/apache/james/DockerElasticSearchRule.java ---------------------------------------------------------------------- diff --git a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/DockerElasticSearchRule.java b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/DockerElasticSearchRule.java deleted file mode 100644 index ff56488..0000000 --- a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/DockerElasticSearchRule.java +++ /dev/null @@ -1,81 +0,0 @@ -/**************************************************************** - * 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 org.apache.james.mailbox.elasticsearch.IndexAttachments; -import org.apache.james.modules.mailbox.ElasticSearchConfiguration; -import org.apache.james.util.Host; -import org.apache.james.util.docker.Images; -import org.apache.james.util.docker.RateLimiters; -import org.apache.james.util.docker.SwarmGenericContainer; -import org.junit.runner.Description; -import org.junit.runners.model.Statement; -import org.testcontainers.containers.wait.strategy.HostPortWaitStrategy; - -import com.google.inject.Module; - -public class DockerElasticSearchRule implements GuiceModuleTestRule { - - private static final int ELASTIC_SEARCH_PORT = 9300; - public static final int ELASTIC_SEARCH_HTTP_PORT = 9200; - - public ElasticSearchConfiguration getElasticSearchConfigurationForDocker() { - return ElasticSearchConfiguration.builder() - .addHost(Host.from(getIp(), elasticSearchContainer.getMappedPort(ELASTIC_SEARCH_PORT))) - .indexAttachment(IndexAttachments.NO) - .build(); - } - - private SwarmGenericContainer elasticSearchContainer = new SwarmGenericContainer(Images.ELASTICSEARCH) - .withExposedPorts(ELASTIC_SEARCH_HTTP_PORT, ELASTIC_SEARCH_PORT) - .waitingFor(new HostPortWaitStrategy().withRateLimiter(RateLimiters.DEFAULT)); - - @Override - public Statement apply(Statement base, Description description) { - return elasticSearchContainer.apply(base, description); - } - - @Override - public void await() { - } - - @Override - public Module getModule() { - return (binder) -> - binder.bind(ElasticSearchConfiguration.class) - .toInstance(getElasticSearchConfigurationForDocker()); - } - - public String getIp() { - return elasticSearchContainer.getHostIp(); - } - - public SwarmGenericContainer getElasticSearchContainer() { - return elasticSearchContainer; - } - - public void pause() { - elasticSearchContainer.pause(); - } - - public void unpause() { - elasticSearchContainer.unpause(); - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/89d72341/server/container/guice/cassandra-guice/src/test/java/org/apache/james/JamesServerWithRetryConnectionTest.java ---------------------------------------------------------------------- diff --git a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/JamesServerWithRetryConnectionTest.java b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/JamesServerWithRetryConnectionTest.java index 075c8a0..bc2d6cb 100644 --- a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/JamesServerWithRetryConnectionTest.java +++ b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/JamesServerWithRetryConnectionTest.java @@ -19,6 +19,7 @@ package org.apache.james; +import static org.apache.james.CassandraJamesServerMain.ALL_BUT_JMX_CASSANDRA_MODULE; import static org.assertj.core.api.Assertions.assertThat; import java.io.IOException; @@ -30,66 +31,110 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; +import org.apache.james.mailbox.elasticsearch.IndexAttachments; +import org.apache.james.mailbox.extractor.TextExtractor; +import org.apache.james.mailbox.store.search.PDFTextExtractor; +import org.apache.james.modules.TestJMAPServerModule; +import org.apache.james.modules.mailbox.ElasticSearchConfiguration; import org.apache.james.modules.protocols.ImapGuiceProbe; -import org.junit.After; -import org.junit.Before; -import org.junit.ClassRule; -import org.junit.Rule; -import org.junit.Test; +import org.apache.james.util.Host; +import org.apache.james.util.docker.Images; +import org.apache.james.util.docker.SwarmGenericContainer; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtensionContext; + +import com.google.inject.Module; + +class JamesServerWithRetryConnectionTest { + private static class DockerElasticSearchRegistrableExtension implements GuiceModuleTestExtension { + private final SwarmGenericContainer elasticSearchContainer; + + private DockerElasticSearchRegistrableExtension(SwarmGenericContainer elasticSearchContainer) { + this.elasticSearchContainer = elasticSearchContainer; + } + + @Override + public void beforeEach(ExtensionContext extensionContext) { + elasticSearchContainer.start(); + } + + @Override + public void afterEach(ExtensionContext extensionContext) { + elasticSearchContainer.stop(); + } + + @Override + public Module getModule() { + return binder -> binder.bind(ElasticSearchConfiguration.class) + .toInstance(getElasticSearchConfigurationForDocker()); + } + + private ElasticSearchConfiguration getElasticSearchConfigurationForDocker() { + return ElasticSearchConfiguration.builder() + .addHost(Host.from(elasticSearchContainer.getHostIp(), elasticSearchContainer.getMappedPort(ELASTIC_SEARCH_PORT))) + .indexAttachment(IndexAttachments.NO) + .build(); + } + } -public class JamesServerWithRetryConnectionTest { + private static final int LIMIT_TO_10_MESSAGES = 10; private static final long WAITING_TIME = TimeUnit.MILLISECONDS.convert(10, TimeUnit.SECONDS); - @ClassRule - public static DockerCassandraRule dockerCassandraRule = new DockerCassandraRule(); - private final DockerElasticSearchRule dockerElasticSearchRule = new DockerElasticSearchRule(); + private static final int ELASTIC_SEARCH_PORT = 9300; + private static final int ELASTIC_SEARCH_HTTP_PORT = 9200; + + private static SwarmGenericContainer elasticSearchContainer = new SwarmGenericContainer(Images.ELASTICSEARCH) + .withExposedPorts(ELASTIC_SEARCH_HTTP_PORT, ELASTIC_SEARCH_PORT); + private static final DockerCassandraRule cassandraRule = new DockerCassandraRule(); - @Rule - public CassandraJmapTestRule cassandraJmapTestRule = new CassandraJmapTestRule(dockerElasticSearchRule); + static JamesServerExtension testExtension = new JamesServerExtensionBuilder() + .extension(new DockerElasticSearchRegistrableExtension(elasticSearchContainer)) + .extension(new CassandraExtension()) + .server(configuration -> GuiceJamesServer.forConfiguration(configuration) + .combineWith(ALL_BUT_JMX_CASSANDRA_MODULE) + .overrideWith(binder -> binder.bind(TextExtractor.class).to(PDFTextExtractor.class)) + .overrideWith(new TestJMAPServerModule(LIMIT_TO_10_MESSAGES))) + .disableAutoStart() + .build(); - private GuiceJamesServer jamesServer; private SocketChannel socketChannel; private ExecutorService executorService; - @Before - public void setUp() throws IOException { + @BeforeEach + void setUp() throws IOException { executorService = Executors.newFixedThreadPool(1); socketChannel = SocketChannel.open(); } - @After - public void after() throws IOException { + @AfterEach + void after() throws IOException { socketChannel.close(); - if (jamesServer != null) { - jamesServer.stop(); - } executorService.shutdownNow(); } @Test - public void serverShouldStartAtDefault() throws Exception { - jamesServer = cassandraJmapTestRule.jmapServer(dockerCassandraRule.getModule()); - assertThatServerStartCorrectly(); + void serverShouldStartAtDefault(GuiceJamesServer server) throws Exception { + assertThatServerStartCorrectly(server); } @Test - public void serverShouldRetryToConnectToCassandraWhenStartService() throws Exception { - jamesServer = cassandraJmapTestRule.jmapServer(dockerCassandraRule.getModule()); - dockerCassandraRule.pause(); + void serverShouldRetryToConnectToCassandraWhenStartService(GuiceJamesServer server) throws Exception { + cassandraRule.pause(); - waitToStartContainer(WAITING_TIME, dockerCassandraRule::unpause); + waitToStartContainer(WAITING_TIME, cassandraRule::unpause); - assertThatServerStartCorrectly(); + assertThatServerStartCorrectly(server); } @Test - public void serverShouldRetryToConnectToElasticSearchWhenStartService() throws Exception { - jamesServer = cassandraJmapTestRule.jmapServer(dockerCassandraRule.getModule()); - dockerElasticSearchRule.pause(); + void serverShouldRetryToConnectToElasticSearchWhenStartService(GuiceJamesServer server) throws Exception { + elasticSearchContainer.pause(); - waitToStartContainer(WAITING_TIME, dockerElasticSearchRule::unpause); + waitToStartContainer(WAITING_TIME, elasticSearchContainer::unpause); - assertThatServerStartCorrectly(); + assertThatServerStartCorrectly(server); } interface StartAction { @@ -107,9 +152,9 @@ public class JamesServerWithRetryConnectionTest { }); } - private void assertThatServerStartCorrectly() throws Exception { - jamesServer.start(); - socketChannel.connect(new InetSocketAddress("127.0.0.1", jamesServer.getProbe(ImapGuiceProbe.class).getImapPort())); + private void assertThatServerStartCorrectly(GuiceJamesServer server) throws Exception { + server.start(); + socketChannel.connect(new InetSocketAddress("127.0.0.1", server.getProbe(ImapGuiceProbe.class).getImapPort())); assertThat(getServerConnectionResponse(socketChannel)).startsWith("* OK JAMES IMAP4rev1 Server"); } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
