JAMES-2553 Rewrite CassandraNodeConfTest test in 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/2b17c920 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/2b17c920 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/2b17c920 Branch: refs/heads/master Commit: 2b17c9209ba4ace78bfe3724a6ed35eb6d777f49 Parents: c001475 Author: Benoit Tellier <[email protected]> Authored: Mon Oct 1 13:23:02 2018 +0700 Committer: Benoit Tellier <[email protected]> Committed: Thu Oct 4 17:48:43 2018 +0700 ---------------------------------------------------------------------- .../org/apache/james/CassandraNodeConfTest.java | 123 ++++++++++++------- 1 file changed, 77 insertions(+), 46 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/2b17c920/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraNodeConfTest.java ---------------------------------------------------------------------- diff --git a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraNodeConfTest.java b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraNodeConfTest.java index 81cadcc..9c12467 100644 --- a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraNodeConfTest.java +++ b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraNodeConfTest.java @@ -18,6 +18,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; @@ -27,82 +28,113 @@ import java.nio.channels.SocketChannel; import java.nio.charset.Charset; import org.apache.james.backends.cassandra.init.configuration.ClusterConfiguration; +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.protocols.ImapGuiceProbe; import org.apache.james.util.Host; -import org.junit.After; -import org.junit.Before; -import org.junit.ClassRule; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; import org.testcontainers.DockerClientFactory; -public class CassandraNodeConfTest { - +class CassandraNodeConfTest { private static final int CASSANDRA_PORT = 9042; + private static final int LIMIT_TO_10_MESSAGES = 10; private static String getDockerHostIp() { - return DockerClientFactory.instance().dockerHostIpAddress(); + DockerClientFactory clientFactory = DockerClientFactory.instance(); + clientFactory.client(); + return clientFactory.dockerHostIpAddress(); } - @ClassRule - public static final DockerCassandraRule dockerCassandraRule = new DockerCassandraRule(); - - @Rule - public CassandraJmapTestRule cassandraJmapTestRule = CassandraJmapTestRule.defaultTestRule(); - - private GuiceJamesServer jamesServer; private SocketChannel socketChannel; - @Before - public void setUp() throws IOException { + @BeforeEach + void setUp() throws IOException { socketChannel = SocketChannel.open(); } - @After - public void after() throws IOException { + @AfterEach + void after() throws IOException { socketChannel.close(); - if (jamesServer != null) { - jamesServer.stop(); - } } - @Test - public void serverShouldStartServiceWhenNodeIsReachable() throws Exception { - jamesServer = cassandraJmapTestRule.jmapServer(dockerCassandraRule.getModule()); + @Nested + class NormalBehaviour { + @RegisterExtension + JamesServerExtension testExtension = new JamesServerExtensionBuilder() + .extension(new EmbeddedElasticSearchExtension()) + .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(); - assertThatServerStartCorrectly(); + @Test + void serverShouldStartServiceWhenNodeIsReachable (GuiceJamesServer server) throws Exception { + assertThatServerStartCorrectly(server); + } } - @Test - public void serverShouldStartWhenOneCassandraNodeIsUnreachable() throws Exception { + @Nested + class OneFailedNode { String unreachableNode = "10.2.3.42"; - - jamesServer = cassandraJmapTestRule.jmapServer(dockerCassandraRule.getModule()) - .overrideWith( - (binder) -> binder.bind(ClusterConfiguration.class) + private final DockerCassandraRule cassandra = new DockerCassandraRule(); + + @RegisterExtension + JamesServerExtension testExtension = new JamesServerExtensionBuilder() + .extension(new EmbeddedElasticSearchExtension()) + .extension(new CassandraExtension(cassandra)) + .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)) + .overrideWith(binder -> binder.bind(ClusterConfiguration.class) .toInstance(clusterWithHosts( Host.from(unreachableNode, 9042), - dockerCassandraRule.getHost()))); + cassandra.getHost())))) + .disableAutoStart() + .build(); - assertThatServerStartCorrectly(); + @Test + void serverShouldStartServiceWhenNodeIsReachable (GuiceJamesServer server) throws Exception { + assertThatServerStartCorrectly(server); + } } - - @Test - public void configShouldWorkWithNonDefaultPort() throws Exception { - jamesServer = cassandraJmapTestRule.jmapServer(dockerCassandraRule.getModule()) - .overrideWith( - (binder) -> binder.bind(ClusterConfiguration.class) + @Nested + class UseMappedPort { + private final DockerCassandraRule cassandra = new DockerCassandraRule(); + + @RegisterExtension + JamesServerExtension testExtension = new JamesServerExtensionBuilder() + .extension(new EmbeddedElasticSearchExtension()) + .extension(new CassandraExtension(cassandra)) + .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)) + .overrideWith(binder -> binder.bind(ClusterConfiguration.class) .toInstance(clusterWithHosts( - Host.from(getDockerHostIp(), dockerCassandraRule.getMappedPort(CASSANDRA_PORT))))); + Host.from(getDockerHostIp(), cassandra.getMappedPort(CASSANDRA_PORT)))))) + .disableAutoStart() + .build(); - assertThatServerStartCorrectly(); + @Test + void configShouldWorkWithNonDefaultPort(GuiceJamesServer server) throws Exception { + assertThatServerStartCorrectly(server); + } } - 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"); } @@ -122,5 +154,4 @@ public class CassandraNodeConfTest { byte[] bytes = byteBuffer.array(); return new String(bytes, Charset.forName("UTF-8")); } - } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
