http://git-wip-us.apache.org/repos/asf/james-project/blob/a18a4488/mpt/impl/managesieve/cassandra/src/test/java/org/apache/james/mpt/managesieve/cassandra/CassandraNoopTest.java ---------------------------------------------------------------------- diff --git a/mpt/impl/managesieve/cassandra/src/test/java/org/apache/james/mpt/managesieve/cassandra/CassandraNoopTest.java b/mpt/impl/managesieve/cassandra/src/test/java/org/apache/james/mpt/managesieve/cassandra/CassandraNoopTest.java index 5245878..1663e8b 100644 --- a/mpt/impl/managesieve/cassandra/src/test/java/org/apache/james/mpt/managesieve/cassandra/CassandraNoopTest.java +++ b/mpt/impl/managesieve/cassandra/src/test/java/org/apache/james/mpt/managesieve/cassandra/CassandraNoopTest.java @@ -19,21 +19,25 @@ package org.apache.james.mpt.managesieve.cassandra; +import org.apache.james.backends.cassandra.DockerCassandraRule; import org.apache.james.mpt.host.ManageSieveHostSystem; import org.apache.james.mpt.testsuite.NoopTest; import org.junit.After; import org.junit.Before; +import org.junit.ClassRule; import com.google.inject.Guice; import com.google.inject.Injector; public class CassandraNoopTest extends NoopTest { + @ClassRule public static DockerCassandraRule cassandraServer = new DockerCassandraRule(); + private ManageSieveHostSystem system; @Before public void setUp() throws Exception { - Injector injector = Guice.createInjector(new CassandraModule()); + Injector injector = Guice.createInjector(new CassandraModule(cassandraServer.getIp(), cassandraServer.getBindingPort())); system = injector.getInstance(ManageSieveHostSystem.class); system.beforeTest(); super.setUp();
http://git-wip-us.apache.org/repos/asf/james-project/blob/a18a4488/mpt/impl/managesieve/cassandra/src/test/java/org/apache/james/mpt/managesieve/cassandra/CassandraPutScriptTest.java ---------------------------------------------------------------------- diff --git a/mpt/impl/managesieve/cassandra/src/test/java/org/apache/james/mpt/managesieve/cassandra/CassandraPutScriptTest.java b/mpt/impl/managesieve/cassandra/src/test/java/org/apache/james/mpt/managesieve/cassandra/CassandraPutScriptTest.java index e406fe3..9ba8908 100644 --- a/mpt/impl/managesieve/cassandra/src/test/java/org/apache/james/mpt/managesieve/cassandra/CassandraPutScriptTest.java +++ b/mpt/impl/managesieve/cassandra/src/test/java/org/apache/james/mpt/managesieve/cassandra/CassandraPutScriptTest.java @@ -19,21 +19,25 @@ package org.apache.james.mpt.managesieve.cassandra; +import org.apache.james.backends.cassandra.DockerCassandraRule; import org.apache.james.mpt.host.ManageSieveHostSystem; import org.apache.james.mpt.testsuite.PutScriptTest; import org.junit.After; import org.junit.Before; +import org.junit.ClassRule; import com.google.inject.Guice; import com.google.inject.Injector; public class CassandraPutScriptTest extends PutScriptTest { + @ClassRule public static DockerCassandraRule cassandraServer = new DockerCassandraRule(); + private ManageSieveHostSystem system; @Before public void setUp() throws Exception { - Injector injector = Guice.createInjector(new CassandraModule()); + Injector injector = Guice.createInjector(new CassandraModule(cassandraServer.getIp(), cassandraServer.getBindingPort())); system = injector.getInstance(ManageSieveHostSystem.class); system.beforeTest(); super.setUp(); http://git-wip-us.apache.org/repos/asf/james-project/blob/a18a4488/mpt/impl/managesieve/cassandra/src/test/java/org/apache/james/mpt/managesieve/cassandra/CassandraRenameScriptTest.java ---------------------------------------------------------------------- diff --git a/mpt/impl/managesieve/cassandra/src/test/java/org/apache/james/mpt/managesieve/cassandra/CassandraRenameScriptTest.java b/mpt/impl/managesieve/cassandra/src/test/java/org/apache/james/mpt/managesieve/cassandra/CassandraRenameScriptTest.java index a95f9d1..2ae6162 100644 --- a/mpt/impl/managesieve/cassandra/src/test/java/org/apache/james/mpt/managesieve/cassandra/CassandraRenameScriptTest.java +++ b/mpt/impl/managesieve/cassandra/src/test/java/org/apache/james/mpt/managesieve/cassandra/CassandraRenameScriptTest.java @@ -19,21 +19,25 @@ package org.apache.james.mpt.managesieve.cassandra; +import org.apache.james.backends.cassandra.DockerCassandraRule; import org.apache.james.mpt.host.ManageSieveHostSystem; import org.apache.james.mpt.testsuite.RenameScriptTest; import org.junit.After; import org.junit.Before; +import org.junit.ClassRule; import com.google.inject.Guice; import com.google.inject.Injector; public class CassandraRenameScriptTest extends RenameScriptTest { + @ClassRule public static DockerCassandraRule cassandraServer = new DockerCassandraRule(); + private ManageSieveHostSystem system; @Before public void setUp() throws Exception { - Injector injector = Guice.createInjector(new CassandraModule()); + Injector injector = Guice.createInjector(new CassandraModule(cassandraServer.getIp(), cassandraServer.getBindingPort())); system = injector.getInstance(ManageSieveHostSystem.class); system.beforeTest(); super.setUp(); http://git-wip-us.apache.org/repos/asf/james-project/blob/a18a4488/mpt/impl/managesieve/cassandra/src/test/java/org/apache/james/mpt/managesieve/cassandra/CassandraSetActiveTest.java ---------------------------------------------------------------------- diff --git a/mpt/impl/managesieve/cassandra/src/test/java/org/apache/james/mpt/managesieve/cassandra/CassandraSetActiveTest.java b/mpt/impl/managesieve/cassandra/src/test/java/org/apache/james/mpt/managesieve/cassandra/CassandraSetActiveTest.java index a82b69f..f47b3c6 100644 --- a/mpt/impl/managesieve/cassandra/src/test/java/org/apache/james/mpt/managesieve/cassandra/CassandraSetActiveTest.java +++ b/mpt/impl/managesieve/cassandra/src/test/java/org/apache/james/mpt/managesieve/cassandra/CassandraSetActiveTest.java @@ -19,21 +19,25 @@ package org.apache.james.mpt.managesieve.cassandra; +import org.apache.james.backends.cassandra.DockerCassandraRule; import org.apache.james.mpt.host.ManageSieveHostSystem; import org.apache.james.mpt.testsuite.SetActiveTest; import org.junit.After; import org.junit.Before; +import org.junit.ClassRule; import com.google.inject.Guice; import com.google.inject.Injector; public class CassandraSetActiveTest extends SetActiveTest { + @ClassRule public static DockerCassandraRule cassandraServer = new DockerCassandraRule(); + private ManageSieveHostSystem system; @Before public void setUp() throws Exception { - Injector injector = Guice.createInjector(new CassandraModule()); + Injector injector = Guice.createInjector(new CassandraModule(cassandraServer.getIp(), cassandraServer.getBindingPort())); system = injector.getInstance(ManageSieveHostSystem.class); system.beforeTest(); super.setUp(); http://git-wip-us.apache.org/repos/asf/james-project/blob/a18a4488/mpt/impl/managesieve/cassandra/src/test/java/org/apache/james/mpt/managesieve/cassandra/CassandraStartTlsTest.java ---------------------------------------------------------------------- diff --git a/mpt/impl/managesieve/cassandra/src/test/java/org/apache/james/mpt/managesieve/cassandra/CassandraStartTlsTest.java b/mpt/impl/managesieve/cassandra/src/test/java/org/apache/james/mpt/managesieve/cassandra/CassandraStartTlsTest.java index 15a2a7a..e4506d1 100644 --- a/mpt/impl/managesieve/cassandra/src/test/java/org/apache/james/mpt/managesieve/cassandra/CassandraStartTlsTest.java +++ b/mpt/impl/managesieve/cassandra/src/test/java/org/apache/james/mpt/managesieve/cassandra/CassandraStartTlsTest.java @@ -19,21 +19,25 @@ package org.apache.james.mpt.managesieve.cassandra; +import org.apache.james.backends.cassandra.DockerCassandraRule; import org.apache.james.mpt.host.ManageSieveHostSystem; import org.apache.james.mpt.testsuite.StartTlsTest; import org.junit.After; import org.junit.Before; +import org.junit.ClassRule; import com.google.inject.Guice; import com.google.inject.Injector; public class CassandraStartTlsTest extends StartTlsTest { + @ClassRule public static DockerCassandraRule cassandraServer = new DockerCassandraRule(); + private ManageSieveHostSystem system; @Before public void setUp() throws Exception { - Injector injector = Guice.createInjector(new CassandraModule()); + Injector injector = Guice.createInjector(new CassandraModule(cassandraServer.getIp(), cassandraServer.getBindingPort())); system = injector.getInstance(ManageSieveHostSystem.class); system.beforeTest(); super.setUp(); http://git-wip-us.apache.org/repos/asf/james-project/blob/a18a4488/mpt/impl/managesieve/cassandra/src/test/java/org/apache/james/mpt/managesieve/cassandra/CassandraUnauthenticatedTest.java ---------------------------------------------------------------------- diff --git a/mpt/impl/managesieve/cassandra/src/test/java/org/apache/james/mpt/managesieve/cassandra/CassandraUnauthenticatedTest.java b/mpt/impl/managesieve/cassandra/src/test/java/org/apache/james/mpt/managesieve/cassandra/CassandraUnauthenticatedTest.java index 66b524a..bf3a509 100644 --- a/mpt/impl/managesieve/cassandra/src/test/java/org/apache/james/mpt/managesieve/cassandra/CassandraUnauthenticatedTest.java +++ b/mpt/impl/managesieve/cassandra/src/test/java/org/apache/james/mpt/managesieve/cassandra/CassandraUnauthenticatedTest.java @@ -19,21 +19,25 @@ package org.apache.james.mpt.managesieve.cassandra; +import org.apache.james.backends.cassandra.DockerCassandraRule; import org.apache.james.mpt.host.ManageSieveHostSystem; import org.apache.james.mpt.testsuite.UnauthenticatedTest; import org.junit.After; import org.junit.Before; +import org.junit.ClassRule; import com.google.inject.Guice; import com.google.inject.Injector; public class CassandraUnauthenticatedTest extends UnauthenticatedTest { + @ClassRule public static DockerCassandraRule cassandraServer = new DockerCassandraRule(); + private ManageSieveHostSystem system; @Before public void setUp() throws Exception { - Injector injector = Guice.createInjector(new CassandraModule()); + Injector injector = Guice.createInjector(new CassandraModule(cassandraServer.getIp(), cassandraServer.getBindingPort())); system = injector.getInstance(ManageSieveHostSystem.class); system.beforeTest(); super.setUp(); http://git-wip-us.apache.org/repos/asf/james-project/blob/a18a4488/mpt/impl/managesieve/cassandra/src/test/java/org/apache/james/mpt/managesieve/cassandra/host/CassandraHostSystem.java ---------------------------------------------------------------------- diff --git a/mpt/impl/managesieve/cassandra/src/test/java/org/apache/james/mpt/managesieve/cassandra/host/CassandraHostSystem.java b/mpt/impl/managesieve/cassandra/src/test/java/org/apache/james/mpt/managesieve/cassandra/host/CassandraHostSystem.java index 3b24879..28c89df 100644 --- a/mpt/impl/managesieve/cassandra/src/test/java/org/apache/james/mpt/managesieve/cassandra/host/CassandraHostSystem.java +++ b/mpt/impl/managesieve/cassandra/src/test/java/org/apache/james/mpt/managesieve/cassandra/host/CassandraHostSystem.java @@ -19,6 +19,7 @@ package org.apache.james.mpt.managesieve.cassandra.host; +import org.apache.james.backends.cassandra.CassandraCluster; import org.apache.james.backends.cassandra.init.CassandraModuleComposite; import org.apache.james.backends.cassandra.utils.CassandraUtils; import org.apache.james.mpt.host.JamesManageSieveHostSystem; @@ -30,34 +31,39 @@ import org.apache.james.sieve.cassandra.CassandraSieveRepositoryModule; import org.apache.james.sieverepository.api.SieveRepository; import org.apache.james.user.api.UsersRepository; import org.apache.james.user.cassandra.CassandraUsersRepository; -import org.apache.james.backends.cassandra.CassandraCluster; import org.apache.james.user.cassandra.CassandraUsersRepositoryModule; public class CassandraHostSystem extends JamesManageSieveHostSystem { - private static CassandraCluster CASSANDRA_CLUSTER = CassandraCluster.create(new CassandraModuleComposite( - new CassandraSieveRepositoryModule(), - new CassandraUsersRepositoryModule())); + + private final String cassandraHost; + private final int cassandraPort; + private CassandraCluster cassandra; - public CassandraHostSystem() throws Exception { - super(createUsersRepository(), createSieveRepository()); + public CassandraHostSystem(String cassandraHost, int cassandraPort) { + this.cassandraHost = cassandraHost; + this.cassandraPort = cassandraPort; + } + + @Override + public void beforeTest() throws Exception { + CassandraModuleComposite modules = new CassandraModuleComposite( + new CassandraSieveRepositoryModule(), + new CassandraUsersRepositoryModule()); + cassandra = CassandraCluster.create(modules, cassandraHost, cassandraPort); + super.beforeTest(); } - protected static SieveRepository createSieveRepository() throws Exception { + protected SieveRepository createSieveRepository() throws Exception { return new CassandraSieveRepository( - new CassandraSieveDAO(CASSANDRA_CLUSTER.getConf()), - new CassandraSieveQuotaDAO(CASSANDRA_CLUSTER.getConf()), - new CassandraActiveScriptDAO(CASSANDRA_CLUSTER.getConf())); + new CassandraSieveDAO(cassandra.getConf()), + new CassandraSieveQuotaDAO(cassandra.getConf()), + new CassandraActiveScriptDAO(cassandra.getConf())); } - protected static UsersRepository createUsersRepository() { - CassandraUsersRepository cassandraUsersRepository = new CassandraUsersRepository(CASSANDRA_CLUSTER.getConf(), CassandraUtils.WITH_DEFAULT_CONFIGURATION); + protected UsersRepository createUsersRepository() { + CassandraUsersRepository cassandraUsersRepository = new CassandraUsersRepository(cassandra.getConf(), CassandraUtils.WITH_DEFAULT_CONFIGURATION); cassandraUsersRepository.setEnableVirtualHosting(false); return cassandraUsersRepository; } - @Override - protected void resetData() throws Exception { - CASSANDRA_CLUSTER.clearAllTables(); - } - } http://git-wip-us.apache.org/repos/asf/james-project/blob/a18a4488/mpt/impl/managesieve/core/pom.xml ---------------------------------------------------------------------- diff --git a/mpt/impl/managesieve/core/pom.xml b/mpt/impl/managesieve/core/pom.xml index b808c33..9773cd1 100644 --- a/mpt/impl/managesieve/core/pom.xml +++ b/mpt/impl/managesieve/core/pom.xml @@ -55,6 +55,10 @@ <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> </dependency> + <dependency> + <groupId>org.testcontainers</groupId> + <artifactId>testcontainers</artifactId> + </dependency> </dependencies> <build> http://git-wip-us.apache.org/repos/asf/james-project/blob/a18a4488/mpt/impl/managesieve/core/src/main/java/org/apache/james/mpt/host/JamesManageSieveHostSystem.java ---------------------------------------------------------------------- diff --git a/mpt/impl/managesieve/core/src/main/java/org/apache/james/mpt/host/JamesManageSieveHostSystem.java b/mpt/impl/managesieve/core/src/main/java/org/apache/james/mpt/host/JamesManageSieveHostSystem.java index a69e521..5cec736 100644 --- a/mpt/impl/managesieve/core/src/main/java/org/apache/james/mpt/host/JamesManageSieveHostSystem.java +++ b/mpt/impl/managesieve/core/src/main/java/org/apache/james/mpt/host/JamesManageSieveHostSystem.java @@ -31,17 +31,26 @@ import org.apache.jsieve.ConfigurationManager; public abstract class JamesManageSieveHostSystem implements ManageSieveHostSystem { - private final UsersRepository usersRepository; - protected final SieveRepository sieveRepository; - private final ManageSieveProcessor processor; + private UsersRepository usersRepository; + private SieveRepository sieveRepository; + private ManageSieveProcessor processor; - public JamesManageSieveHostSystem(UsersRepository usersRepository, SieveRepository sieveRepository) throws Exception { - this.usersRepository = usersRepository; - this.sieveRepository = sieveRepository; + @Override + public void beforeTest() throws Exception { + this.usersRepository = createUsersRepository(); + this.sieveRepository = createSieveRepository(); this.processor = new ManageSieveProcessor(new ArgumentParser(new CoreProcessor(sieveRepository, usersRepository, new Parser(new ConfigurationManager())))); } @Override + public void afterTest() throws Exception { + } + + protected abstract SieveRepository createSieveRepository() throws Exception; + + protected abstract UsersRepository createUsersRepository(); + + @Override public boolean addUser(String user, String password) throws Exception { usersRepository.addUser(user, password); return true; @@ -57,19 +66,4 @@ public abstract class JamesManageSieveHostSystem implements ManageSieveHostSyste return new ManageSieveSession(processor, continuation); } - @Override - public void beforeTests() throws Exception {} - - @Override - public void afterTests() throws Exception {} - - @Override - public void beforeTest() throws Exception {} - - @Override - public void afterTest() throws Exception { - resetData(); - } - - protected abstract void resetData() throws Exception; } http://git-wip-us.apache.org/repos/asf/james-project/blob/a18a4488/mpt/impl/managesieve/file/src/test/java/org/apache/james/mpt/managesieve/file/host/FileHostSystem.java ---------------------------------------------------------------------- diff --git a/mpt/impl/managesieve/file/src/test/java/org/apache/james/mpt/managesieve/file/host/FileHostSystem.java b/mpt/impl/managesieve/file/src/test/java/org/apache/james/mpt/managesieve/file/host/FileHostSystem.java index 7bd28dc..92e218f 100644 --- a/mpt/impl/managesieve/file/src/test/java/org/apache/james/mpt/managesieve/file/host/FileHostSystem.java +++ b/mpt/impl/managesieve/file/src/test/java/org/apache/james/mpt/managesieve/file/host/FileHostSystem.java @@ -30,6 +30,7 @@ import org.apache.james.filesystem.api.FileSystem; import org.apache.james.mpt.host.JamesManageSieveHostSystem; import org.apache.james.sieverepository.api.SieveRepository; import org.apache.james.sieverepository.file.SieveFileRepository; +import org.apache.james.user.api.UsersRepository; import org.apache.james.user.memory.MemoryUsersRepository; public class FileHostSystem extends JamesManageSieveHostSystem { @@ -51,16 +52,19 @@ public class FileHostSystem extends JamesManageSieveHostSystem { }; } - protected static SieveRepository createSieveRepository() throws Exception { - return new SieveFileRepository(fileSystem); + @Override + protected UsersRepository createUsersRepository() { + return MemoryUsersRepository.withoutVirtualHosting(); } - public FileHostSystem() throws Exception { - super(MemoryUsersRepository.withoutVirtualHosting(), createSieveRepository()); + @Override + protected SieveRepository createSieveRepository() throws Exception { + return new SieveFileRepository(fileSystem); } - + @Override - protected void resetData() throws Exception { + public void afterTest() throws Exception { + super.afterTest(); File root = fileSystem.getFile(SIEVE_ROOT); // Remove files from the previous test, if any if (root.exists()) { http://git-wip-us.apache.org/repos/asf/james-project/blob/a18a4488/mpt/impl/smtp/cassandra/pom.xml ---------------------------------------------------------------------- diff --git a/mpt/impl/smtp/cassandra/pom.xml b/mpt/impl/smtp/cassandra/pom.xml index 46f9120..f9ded92 100644 --- a/mpt/impl/smtp/cassandra/pom.xml +++ b/mpt/impl/smtp/cassandra/pom.xml @@ -94,11 +94,6 @@ <artifactId>guava</artifactId> </dependency> <dependency> - <groupId>org.cassandraunit</groupId> - <artifactId>cassandra-unit</artifactId> - <scope>test</scope> - </dependency> - <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> </dependency> http://git-wip-us.apache.org/repos/asf/james-project/blob/a18a4488/mpt/impl/smtp/cassandra/src/test/java/org/apache/james/mpt/smtp/CassandraForwardSmtpTest.java ---------------------------------------------------------------------- diff --git a/mpt/impl/smtp/cassandra/src/test/java/org/apache/james/mpt/smtp/CassandraForwardSmtpTest.java b/mpt/impl/smtp/cassandra/src/test/java/org/apache/james/mpt/smtp/CassandraForwardSmtpTest.java index 0cda129..0adbeb0 100644 --- a/mpt/impl/smtp/cassandra/src/test/java/org/apache/james/mpt/smtp/CassandraForwardSmtpTest.java +++ b/mpt/impl/smtp/cassandra/src/test/java/org/apache/james/mpt/smtp/CassandraForwardSmtpTest.java @@ -19,19 +19,24 @@ package org.apache.james.mpt.smtp; +import org.apache.james.backends.cassandra.DockerCassandraRule; import org.junit.After; import org.junit.Before; +import org.junit.ClassRule; import com.google.inject.Guice; import com.google.inject.Injector; public class CassandraForwardSmtpTest extends ForwardSmtpTest { + @ClassRule public static DockerCassandraRule cassandraServer = new DockerCassandraRule(); + private SmtpHostSystem system; @Before public void setUp() throws Exception { - Injector injector = Guice.createInjector(new SmtpTestModule(SmtpTestModule.Port.SMTP)); + Injector injector = Guice.createInjector( + new SmtpTestModule(SmtpTestModule.Port.SMTP, cassandraServer.getIp(), cassandraServer.getBindingPort())); system = injector.getInstance(SmtpHostSystem.class); system.beforeTest(); super.setUp(); http://git-wip-us.apache.org/repos/asf/james-project/blob/a18a4488/mpt/impl/smtp/cassandra/src/test/java/org/apache/james/mpt/smtp/CassandraSmtpStarttlsCommandTest.java ---------------------------------------------------------------------- diff --git a/mpt/impl/smtp/cassandra/src/test/java/org/apache/james/mpt/smtp/CassandraSmtpStarttlsCommandTest.java b/mpt/impl/smtp/cassandra/src/test/java/org/apache/james/mpt/smtp/CassandraSmtpStarttlsCommandTest.java index d807faf..5fa2d7a 100644 --- a/mpt/impl/smtp/cassandra/src/test/java/org/apache/james/mpt/smtp/CassandraSmtpStarttlsCommandTest.java +++ b/mpt/impl/smtp/cassandra/src/test/java/org/apache/james/mpt/smtp/CassandraSmtpStarttlsCommandTest.java @@ -19,19 +19,24 @@ package org.apache.james.mpt.smtp; +import org.apache.james.backends.cassandra.DockerCassandraRule; import org.junit.After; import org.junit.Before; +import org.junit.ClassRule; import com.google.inject.Guice; import com.google.inject.Injector; public class CassandraSmtpStarttlsCommandTest extends SmtpStarttlsCommandTest { + @ClassRule public static DockerCassandraRule cassandraServer = new DockerCassandraRule(); + private SmtpHostSystem system; @Before public void setUp() throws Exception { - Injector injector = Guice.createInjector(new SmtpTestModule(SmtpTestModule.Port.SMTP_START_TTLS)); + Injector injector = Guice.createInjector( + new SmtpTestModule(SmtpTestModule.Port.SMTP_START_TTLS, cassandraServer.getIp(), cassandraServer.getBindingPort())); system = injector.getInstance(SmtpHostSystem.class); system.beforeTest(); super.setUp(); http://git-wip-us.apache.org/repos/asf/james-project/blob/a18a4488/mpt/impl/smtp/cassandra/src/test/java/org/apache/james/mpt/smtp/SmtpTestModule.java ---------------------------------------------------------------------- diff --git a/mpt/impl/smtp/cassandra/src/test/java/org/apache/james/mpt/smtp/SmtpTestModule.java b/mpt/impl/smtp/cassandra/src/test/java/org/apache/james/mpt/smtp/SmtpTestModule.java index e86db9d..d383838 100644 --- a/mpt/impl/smtp/cassandra/src/test/java/org/apache/james/mpt/smtp/SmtpTestModule.java +++ b/mpt/impl/smtp/cassandra/src/test/java/org/apache/james/mpt/smtp/SmtpTestModule.java @@ -35,15 +35,19 @@ public class SmtpTestModule extends AbstractModule { } } - private Port smtpPort; + private final Port smtpPort; + private final String cassandraHost; + private final int cassandraPort; - public SmtpTestModule(Port smtpPort) { + public SmtpTestModule(Port smtpPort, String cassandraHost, int cassandraPort) { this.smtpPort = smtpPort; + this.cassandraHost = cassandraHost; + this.cassandraPort = cassandraPort; } @Override protected void configure() { - bind(SmtpHostSystem.class).toInstance(new CassandraJamesSmtpHostSystem(smtpPort.port)); + bind(SmtpHostSystem.class).toInstance(new CassandraJamesSmtpHostSystem(smtpPort.port, cassandraHost, cassandraPort)); } } http://git-wip-us.apache.org/repos/asf/james-project/blob/a18a4488/mpt/impl/smtp/cassandra/src/test/java/org/apache/james/mpt/smtp/host/CassandraJamesSmtpHostSystem.java ---------------------------------------------------------------------- diff --git a/mpt/impl/smtp/cassandra/src/test/java/org/apache/james/mpt/smtp/host/CassandraJamesSmtpHostSystem.java b/mpt/impl/smtp/cassandra/src/test/java/org/apache/james/mpt/smtp/host/CassandraJamesSmtpHostSystem.java index 82ffba8..5dcb1ae 100644 --- a/mpt/impl/smtp/cassandra/src/test/java/org/apache/james/mpt/smtp/host/CassandraJamesSmtpHostSystem.java +++ b/mpt/impl/smtp/cassandra/src/test/java/org/apache/james/mpt/smtp/host/CassandraJamesSmtpHostSystem.java @@ -23,9 +23,9 @@ import java.util.Iterator; import org.apache.james.CassandraJamesServerMain; import org.apache.james.GuiceJamesServer; -import org.apache.james.backends.cassandra.EmbeddedCassandra; import org.apache.james.backends.es.EmbeddedElasticSearch; import org.apache.james.dnsservice.api.DNSService; +import org.apache.james.dnsservice.api.InMemoryDNSService; import org.apache.james.mailbox.elasticsearch.MailboxElasticsearchConstants; import org.apache.james.modules.CassandraJmapServerModule; import org.apache.james.modules.protocols.ProtocolHandlerModule; @@ -33,7 +33,6 @@ import org.apache.james.modules.protocols.SMTPServerModule; import org.apache.james.mpt.monitor.SystemLoggingMonitor; import org.apache.james.mpt.session.ExternalSessionFactory; import org.apache.james.mpt.smtp.SmtpHostSystem; -import org.apache.james.dnsservice.api.InMemoryDNSService; import org.apache.james.utils.DataProbeImpl; import org.junit.rules.TemporaryFolder; @@ -43,15 +42,18 @@ import com.google.common.base.Splitter; public class CassandraJamesSmtpHostSystem extends ExternalSessionFactory implements SmtpHostSystem { private TemporaryFolder folder; - private EmbeddedCassandra embeddedCassandra; private EmbeddedElasticSearch embeddedElasticSearch; private GuiceJamesServer jamesServer; private InMemoryDNSService inMemoryDNSService; + private final String cassandraHost; + private final int cassandraPort; - public CassandraJamesSmtpHostSystem(int smtpPort) { + public CassandraJamesSmtpHostSystem(int smtpPort, String cassandraHost, int cassandraPort) { super("localhost", smtpPort, new SystemLoggingMonitor(), "220 mydomain.tld smtp"); + this.cassandraHost = cassandraHost; + this.cassandraPort = cassandraPort; } @Override @@ -78,21 +80,12 @@ public class CassandraJamesSmtpHostSystem extends ExternalSessionFactory impleme } @Override - public void beforeTests() throws Exception { - } - - @Override - public void afterTests() throws Exception { - } - - @Override public void beforeTest() throws Exception { inMemoryDNSService = new InMemoryDNSService(); folder = new TemporaryFolder(); folder.create(); embeddedElasticSearch = new EmbeddedElasticSearch(folder.getRoot().toPath(), MailboxElasticsearchConstants.MAILBOX_INDEX); embeddedElasticSearch.before(); - embeddedCassandra = EmbeddedCassandra.createStartServer(); jamesServer = createJamesServer(); jamesServer.start(); } @@ -110,7 +103,7 @@ public class CassandraJamesSmtpHostSystem extends ExternalSessionFactory impleme protected GuiceJamesServer createJamesServer() { return new GuiceJamesServer() .combineWith(CassandraJamesServerMain.cassandraServerModule, new SMTPServerModule(), new ProtocolHandlerModule()) - .overrideWith(new CassandraJmapServerModule(folder::getRoot, embeddedElasticSearch, embeddedCassandra), + .overrideWith(new CassandraJmapServerModule(folder::getRoot, embeddedElasticSearch, cassandraHost, cassandraPort), (binder) -> binder.bind(DNSService.class).toInstance(inMemoryDNSService)); } } http://git-wip-us.apache.org/repos/asf/james-project/blob/a18a4488/mpt/impl/smtp/core/src/main/java/org/apache/james/mpt/smtp/ForwardSmtpTest.java ---------------------------------------------------------------------- diff --git a/mpt/impl/smtp/core/src/main/java/org/apache/james/mpt/smtp/ForwardSmtpTest.java b/mpt/impl/smtp/core/src/main/java/org/apache/james/mpt/smtp/ForwardSmtpTest.java index fdc47e7..8510c53 100644 --- a/mpt/impl/smtp/core/src/main/java/org/apache/james/mpt/smtp/ForwardSmtpTest.java +++ b/mpt/impl/smtp/core/src/main/java/org/apache/james/mpt/smtp/ForwardSmtpTest.java @@ -35,7 +35,6 @@ import org.junit.rules.RuleChain; import org.junit.rules.TemporaryFolder; import com.google.common.base.Charsets; -import com.google.common.net.InetAddresses; import com.jayway.restassured.RestAssured; import com.jayway.restassured.builder.RequestSpecBuilder; import com.jayway.restassured.http.ContentType; @@ -49,6 +48,7 @@ public abstract class ForwardSmtpTest { private final TemporaryFolder folder = new TemporaryFolder(); private final SwarmGenericContainer fakeSmtp = new SwarmGenericContainer("weave/rest-smtp-sink:latest") + .withExposedPorts(25) .withAffinityToContainer(); @Rule @@ -67,7 +67,7 @@ public abstract class ForwardSmtpTest { .withLocale(Locale.US) .withUser(USER_AT_DOMAIN, PASSWORD); - InetAddress containerIp = InetAddresses.forString(fakeSmtp.getIp()); + InetAddress containerIp = InetAddress.getByName(fakeSmtp.getContainerIp()); hostSystem.getInMemoryDnsService() .registerRecord("yopmail.com", containerIp, "yopmail.com"); http://git-wip-us.apache.org/repos/asf/james-project/blob/a18a4488/server/container/guice/cassandra-guice/pom.xml ---------------------------------------------------------------------- diff --git a/server/container/guice/cassandra-guice/pom.xml b/server/container/guice/cassandra-guice/pom.xml index 6f8123d..53f397c 100644 --- a/server/container/guice/cassandra-guice/pom.xml +++ b/server/container/guice/cassandra-guice/pom.xml @@ -43,6 +43,7 @@ <groupId>${project.groupId}</groupId> <artifactId>apache-james-backends-cassandra</artifactId> <type>test-jar</type> + <scope>test</scope> </dependency> <dependency> <groupId>${project.groupId}</groupId> @@ -205,11 +206,6 @@ <scope>test</scope> </dependency> <dependency> - <groupId>org.cassandraunit</groupId> - <artifactId>cassandra-unit</artifactId> - <scope>test</scope> - </dependency> - <dependency> <groupId>org.hdrhistogram</groupId> <artifactId>HdrHistogram</artifactId> <version>2.1.4</version> http://git-wip-us.apache.org/repos/asf/james-project/blob/a18a4488/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/CassandraSessionConfiguration.java ---------------------------------------------------------------------- diff --git a/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/CassandraSessionConfiguration.java b/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/CassandraSessionConfiguration.java deleted file mode 100644 index 33c755c..0000000 --- a/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/CassandraSessionConfiguration.java +++ /dev/null @@ -1,29 +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.modules.mailbox; - -import org.apache.commons.configuration.ConfigurationException; -import org.apache.commons.configuration.PropertiesConfiguration; - -public interface CassandraSessionConfiguration { - - PropertiesConfiguration getConfiguration() throws ConfigurationException; - -} http://git-wip-us.apache.org/repos/asf/james-project/blob/a18a4488/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/CassandraSessionModule.java ---------------------------------------------------------------------- diff --git a/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/CassandraSessionModule.java b/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/CassandraSessionModule.java index 144d5d3..885e39a 100644 --- a/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/CassandraSessionModule.java +++ b/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/CassandraSessionModule.java @@ -19,24 +19,18 @@ package org.apache.james.modules.mailbox; import java.io.FileNotFoundException; -import java.time.LocalDateTime; -import java.util.Arrays; import java.util.List; import java.util.Optional; import java.util.Set; -import java.util.concurrent.ExecutionException; import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.PropertiesConfiguration; -import org.apache.james.backends.cassandra.CassandraConfiguration; import org.apache.james.backends.cassandra.components.CassandraModule; +import org.apache.james.backends.cassandra.init.CassandraConfiguration; import org.apache.james.backends.cassandra.init.CassandraModuleComposite; +import org.apache.james.backends.cassandra.init.CassandraSessionConfiguration; import org.apache.james.backends.cassandra.init.CassandraZonedDateTimeModule; -import org.apache.james.backends.cassandra.init.ClusterBuilder; -import org.apache.james.backends.cassandra.init.ClusterWithKeyspaceCreatedFactory; -import org.apache.james.backends.cassandra.init.QueryLoggerConfiguration; import org.apache.james.backends.cassandra.init.SessionWithInitializedTablesFactory; import org.apache.james.backends.cassandra.utils.CassandraUtils; import org.apache.james.backends.cassandra.versions.CassandraSchemaVersionDAO; @@ -45,21 +39,13 @@ import org.apache.james.backends.cassandra.versions.CassandraSchemaVersionManage import org.apache.james.backends.cassandra.versions.CassandraSchemaVersionModule; import org.apache.james.lifecycle.api.Configurable; import org.apache.james.mailbox.store.BatchSizes; -import org.apache.james.util.Host; import org.apache.james.utils.ConfigurationPerformer; import org.apache.james.utils.PropertiesProvider; -import org.apache.james.utils.RetryExecutorUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.datastax.driver.core.Cluster; -import com.datastax.driver.core.HostDistance; -import com.datastax.driver.core.PerHostPercentileTracker; -import com.datastax.driver.core.PoolingOptions; -import com.datastax.driver.core.QueryLogger; import com.datastax.driver.core.Session; -import com.datastax.driver.core.exceptions.NoHostAvailableException; -import com.github.steveash.guavate.Guavate; import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.ImmutableList; import com.google.inject.AbstractModule; @@ -69,21 +55,12 @@ import com.google.inject.Scopes; import com.google.inject.Singleton; import com.google.inject.multibindings.Multibinder; import com.nurkiewicz.asyncretry.AsyncRetryExecutor; -import com.nurkiewicz.asyncretry.function.RetryCallable; public class CassandraSessionModule extends AbstractModule { private static final Logger LOGGER = LoggerFactory.getLogger(CassandraSessionModule.class); - private static final int DEFAULT_CONNECTION_MAX_RETRIES = 10; - private static final int DEFAULT_CONNECTION_MIN_DELAY = 5000; - private static final long CASSANDRA_HIGHEST_TRACKABLE_LATENCY_MILLIS = TimeUnit.SECONDS.toMillis(10); - private static final int DEFAULT_REPLICATION_FACTOR = 1; - private static final String DEFAULT_KEYSPACE = "apache_james"; - private static final String CASSANDRA_NODES = "cassandra.nodes"; private static final String LOCALHOST = "127.0.0.1"; - private static final int DEFAULT_READ_TIMEOUT_MILLIS = 5000; - private static final int DEFAULT_CONNECT_TIMEOUT_MILLIS = 5000; private static final String BATCHSIZES_FILE_NAME = "batchsizes"; private static final String MAILBOX_MAX_RETRY_ACL = "mailbox.max.retry.acl"; private static final String MAILBOX_MAX_RETRY_MODSEQ = "mailbox.max.retry.modseq"; @@ -95,11 +72,14 @@ public class CassandraSessionModule extends AbstractModule { private static final String CHUNK_SIZE_MESSAGE_READ = "chunk.size.message.read"; private static final String CHUNK_SIZE_EXPUNGE = "chunk.size.expunge"; private static final String BLOB_PART_SIZE = "mailbox.blob.part.size"; + private static final String CASSANDRA_NODES = "cassandra.nodes"; @Override protected void configure() { bind(ScheduledExecutorService.class).toProvider(ScheduledExecutorServiceProvider.class); bind(CassandraUtils.class).in(Scopes.SINGLETON); + bind(Session.class).toProvider(SessionWithInitializedTablesFactory.class); + bind(Cluster.class).toProvider(ResilientClusterProvider.class); Multibinder<CassandraModule> cassandraDataDefinitions = Multibinder.newSetBinder(binder(), CassandraModule.class); cassandraDataDefinitions.addBinding().to(CassandraZonedDateTimeModule.class); @@ -119,14 +99,6 @@ public class CassandraSessionModule extends AbstractModule { @Provides @Singleton - Session provideSession(CassandraSessionConfiguration configuration, Cluster cluster, CassandraModule cassandraModule) - throws FileNotFoundException, ConfigurationException{ - String keyspace = configuration.getConfiguration().getString("cassandra.keyspace", DEFAULT_KEYSPACE); - return new SessionWithInitializedTablesFactory(cassandraModule).createSession(cluster, keyspace); - } - - @Provides - @Singleton CassandraSessionConfiguration getCassandraSessionConfiguration(PropertiesProvider propertiesProvider) { return () -> getConfiguration(propertiesProvider); } @@ -152,115 +124,6 @@ public class CassandraSessionModule extends AbstractModule { } } - @Provides - @Singleton - Cluster provideCluster(CassandraSessionConfiguration cassandraSessionConfiguration, AsyncRetryExecutor executor) throws ConfigurationException, ExecutionException, InterruptedException { - PropertiesConfiguration configuration = cassandraSessionConfiguration.getConfiguration(); - List<Host> servers = listCassandraServers(configuration); - QueryLoggerConfiguration queryLoggerConfiguration = getCassandraQueryLoggerConf(configuration); - - int maxRetries = configuration.getInt("cassandra.retryConnection.maxRetries", DEFAULT_CONNECTION_MAX_RETRIES); - int minDelay = configuration.getInt("cassandra.retryConnection.minDelay", DEFAULT_CONNECTION_MIN_DELAY); - - return RetryExecutorUtil.retryOnExceptions(executor, maxRetries, minDelay, NoHostAvailableException.class) - .getWithRetry(getClusterRetryCallable(configuration, servers, queryLoggerConfiguration)) - .get(); - } - - private RetryCallable<Cluster> getClusterRetryCallable(PropertiesConfiguration configuration, List<Host> servers, QueryLoggerConfiguration queryLoggerConfiguration) { - LOGGER.info("Trying to connect to Cassandra service at {}", LocalDateTime.now()); - - return context -> ClusterWithKeyspaceCreatedFactory - .config(ClusterBuilder.builder() - .servers(servers) - .poolingOptions(readPoolingOptions(configuration)) - .queryLoggerConfiguration(queryLoggerConfiguration) - .readTimeoutMillis(configuration.getInt("cassandra.readTimeoutMillis", DEFAULT_READ_TIMEOUT_MILLIS)) - .connectTimeoutMillis(configuration.getInt("cassandra.connectTimeoutMillis", DEFAULT_CONNECT_TIMEOUT_MILLIS)) - .build(), - configuration.getString("cassandra.keyspace", DEFAULT_KEYSPACE)) - .replicationFactor(configuration.getInt("cassandra.replication.factor", DEFAULT_REPLICATION_FACTOR)) - .clusterWithInitializedKeyspace(); - } - - private Optional<PoolingOptions> readPoolingOptions(PropertiesConfiguration configuration) { - - Optional<Integer> maxConnections = Optional.ofNullable(configuration.getInteger("cassandra.pooling.local.max.connections", null)); - Optional<Integer> maxRequests = Optional.ofNullable(configuration.getInteger("cassandra.pooling.local.max.requests", null)); - Optional<Integer> poolingTimeout = Optional.ofNullable(configuration.getInteger("cassandra.pooling.timeout", null)); - Optional<Integer> heartbeatTimeout = Optional.ofNullable(configuration.getInteger("cassandra.pooling.heartbeat.timeout", null)); - - if (!maxConnections.isPresent() - && !maxRequests.isPresent() - && !poolingTimeout.isPresent() - && !heartbeatTimeout.isPresent()) { - return Optional.empty(); - } - PoolingOptions result = new PoolingOptions(); - - maxConnections.ifPresent(value -> { - result.setMaxConnectionsPerHost(HostDistance.LOCAL, value); - result.setMaxConnectionsPerHost(HostDistance.REMOTE, value); - }); - maxRequests.ifPresent(value -> { - result.setMaxRequestsPerConnection(HostDistance.LOCAL, value); - result.setMaxRequestsPerConnection(HostDistance.REMOTE, value); - }); - poolingTimeout.ifPresent(result::setPoolTimeoutMillis); - heartbeatTimeout.ifPresent(result::setHeartbeatIntervalSeconds); - - return Optional.of(result); - } - - private List<Host> listCassandraServers(PropertiesConfiguration configuration) { - String[] ipAndPorts = configuration.getStringArray(CASSANDRA_NODES); - - return Arrays.stream(ipAndPorts) - .map(string -> Host.parseConfString(string, ClusterBuilder.DEFAULT_CASSANDRA_PORT)) - .collect(Guavate.toImmutableList()); - } - - private Optional<Integer> getOptionalIntegerFromConf(PropertiesConfiguration configuration, String key) { - return Optional.ofNullable(configuration.getInteger(key, null)); - } - - private Optional<Double> getOptionalDoubleFromConf(PropertiesConfiguration configuration, String key) { - return Optional.ofNullable(configuration.getDouble(key, null)); - } - - private QueryLoggerConfiguration getCassandraQueryLoggerConf(PropertiesConfiguration configuration) { - QueryLoggerConfiguration.Builder builder = QueryLoggerConfiguration.builder(); - - Optional<Long> constantThreshold = getOptionalIntegerFromConf(configuration, "cassandra.query.logger.constant.threshold") - .map(Long::valueOf); - - constantThreshold.ifPresent(builder::withConstantThreshold); - - getOptionalIntegerFromConf(configuration, "cassandra.query.logger.max.logged.parameters") - .ifPresent(builder::withMaxLoggedParameters); - - getOptionalIntegerFromConf(configuration, "cassandra.query.logger.max.query.string.length") - .ifPresent(builder::withMaxQueryStringLength); - - getOptionalIntegerFromConf(configuration, "cassandra.query.logger.max.parameter.value.length") - .ifPresent(builder::withMaxParameterValueLength); - - Optional<Double> percentileLatencyConf = getOptionalDoubleFromConf(configuration, "cassandra.query.slow.query.latency.threshold.percentile"); - - if (!percentileLatencyConf.isPresent() && !constantThreshold.isPresent()) { - percentileLatencyConf = Optional.of(QueryLogger.DEFAULT_SLOW_QUERY_THRESHOLD_PERCENTILE); - } - - percentileLatencyConf.ifPresent(slowQueryLatencyThresholdPercentile -> { - PerHostPercentileTracker tracker = PerHostPercentileTracker - .builder(CASSANDRA_HIGHEST_TRACKABLE_LATENCY_MILLIS) - .build(); - - builder.withDynamicThreshold(tracker, slowQueryLatencyThresholdPercentile); - }); - - return builder.build(); - } @Provides private AsyncRetryExecutor provideAsyncRetryExecutor(ScheduledExecutorService scheduler) { http://git-wip-us.apache.org/repos/asf/james-project/blob/a18a4488/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/ResilientClusterProvider.java ---------------------------------------------------------------------- diff --git a/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/ResilientClusterProvider.java b/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/ResilientClusterProvider.java new file mode 100644 index 0000000..c8be620 --- /dev/null +++ b/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/ResilientClusterProvider.java @@ -0,0 +1,178 @@ +package org.apache.james.modules.mailbox; + +import java.time.LocalDateTime; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; + +import javax.annotation.PreDestroy; +import javax.inject.Inject; +import javax.inject.Provider; +import javax.inject.Singleton; + +import org.apache.commons.configuration.ConfigurationException; +import org.apache.commons.configuration.PropertiesConfiguration; +import org.apache.james.backends.cassandra.init.CassandraSessionConfiguration; +import org.apache.james.backends.cassandra.init.ClusterBuilder; +import org.apache.james.backends.cassandra.init.ClusterWithKeyspaceCreatedFactory; +import org.apache.james.backends.cassandra.init.QueryLoggerConfiguration; +import org.apache.james.util.Host; +import org.apache.james.utils.RetryExecutorUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.datastax.driver.core.Cluster; +import com.datastax.driver.core.HostDistance; +import com.datastax.driver.core.PerHostPercentileTracker; +import com.datastax.driver.core.PoolingOptions; +import com.datastax.driver.core.QueryLogger; +import com.datastax.driver.core.exceptions.NoHostAvailableException; +import com.github.steveash.guavate.Guavate; +import com.nurkiewicz.asyncretry.AsyncRetryExecutor; +import com.nurkiewicz.asyncretry.function.RetryCallable; + +@Singleton +public class ResilientClusterProvider implements Provider<Cluster> { + + private static final int DEFAULT_CONNECTION_MAX_RETRIES = 10; + private static final int DEFAULT_CONNECTION_MIN_DELAY = 5000; + private static final long CASSANDRA_HIGHEST_TRACKABLE_LATENCY_MILLIS = TimeUnit.SECONDS.toMillis(10); + private static final int DEFAULT_REPLICATION_FACTOR = 1; + private static final String DEFAULT_KEYSPACE = "apache_james"; + private static final int DEFAULT_READ_TIMEOUT_MILLIS = 5000; + private static final int DEFAULT_CONNECT_TIMEOUT_MILLIS = 5000; + private static final String CASSANDRA_NODES = "cassandra.nodes"; + + private static final Logger LOGGER = LoggerFactory.getLogger(ResilientClusterProvider.class); + private final Cluster cluster; + + @Inject + private ResilientClusterProvider(CassandraSessionConfiguration cassandraSessionConfiguration, AsyncRetryExecutor executor) throws ConfigurationException, ExecutionException, InterruptedException { + PropertiesConfiguration configuration = cassandraSessionConfiguration.getConfiguration(); + List<Host> servers = listCassandraServers(configuration); + LOGGER.warn("CASSANDRA NODES : {}", servers); + QueryLoggerConfiguration queryLoggerConfiguration = getCassandraQueryLoggerConf(configuration); + + int maxRetries = configuration.getInt("cassandra.retryConnection.maxRetries", DEFAULT_CONNECTION_MAX_RETRIES); + int minDelay = configuration.getInt("cassandra.retryConnection.minDelay", DEFAULT_CONNECTION_MIN_DELAY); + + cluster = RetryExecutorUtil.retryOnExceptions(executor, maxRetries, minDelay, NoHostAvailableException.class) + .getWithRetry(getClusterRetryCallable(configuration, servers, queryLoggerConfiguration)) + .get(); + } + + private RetryCallable<Cluster> getClusterRetryCallable(PropertiesConfiguration configuration, List<Host> servers, QueryLoggerConfiguration queryLoggerConfiguration) { + LOGGER.info("Trying to connect to Cassandra service at {} (list {})", LocalDateTime.now(), servers); + + return context -> { + Cluster cluster = ClusterBuilder.builder() + .servers(servers) + .poolingOptions(readPoolingOptions(configuration)) + .queryLoggerConfiguration(queryLoggerConfiguration) + .readTimeoutMillis(configuration.getInt("cassandra.readTimeoutMillis", DEFAULT_READ_TIMEOUT_MILLIS)) + .connectTimeoutMillis(configuration.getInt("cassandra.connectTimeoutMillis", DEFAULT_CONNECT_TIMEOUT_MILLIS)) + .build(); + try { + return ClusterWithKeyspaceCreatedFactory + .config(cluster, + configuration.getString("cassandra.keyspace", DEFAULT_KEYSPACE)) + .replicationFactor(configuration.getInt("cassandra.replication.factor", DEFAULT_REPLICATION_FACTOR)) + .clusterWithInitializedKeyspace(); + } catch (Exception e) { + cluster.close(); + throw e; + } + }; + } + + private Optional<PoolingOptions> readPoolingOptions(PropertiesConfiguration configuration) { + + Optional<Integer> maxConnections = Optional.ofNullable(configuration.getInteger("cassandra.pooling.local.max.connections", null)); + Optional<Integer> maxRequests = Optional.ofNullable(configuration.getInteger("cassandra.pooling.local.max.requests", null)); + Optional<Integer> poolingTimeout = Optional.ofNullable(configuration.getInteger("cassandra.pooling.timeout", null)); + Optional<Integer> heartbeatTimeout = Optional.ofNullable(configuration.getInteger("cassandra.pooling.heartbeat.timeout", null)); + + if (!maxConnections.isPresent() + && !maxRequests.isPresent() + && !poolingTimeout.isPresent() + && !heartbeatTimeout.isPresent()) { + return Optional.empty(); + } + PoolingOptions result = new PoolingOptions(); + + maxConnections.ifPresent(value -> { + result.setMaxConnectionsPerHost(HostDistance.LOCAL, value); + result.setMaxConnectionsPerHost(HostDistance.REMOTE, value); + }); + maxRequests.ifPresent(value -> { + result.setMaxRequestsPerConnection(HostDistance.LOCAL, value); + result.setMaxRequestsPerConnection(HostDistance.REMOTE, value); + }); + poolingTimeout.ifPresent(result::setPoolTimeoutMillis); + heartbeatTimeout.ifPresent(result::setHeartbeatIntervalSeconds); + + return Optional.of(result); + } + + private List<Host> listCassandraServers(PropertiesConfiguration configuration) { + String[] ipAndPorts = configuration.getStringArray(CASSANDRA_NODES); + + return Arrays.stream(ipAndPorts) + .map(string -> Host.parseConfString(string, ClusterBuilder.DEFAULT_CASSANDRA_PORT)) + .collect(Guavate.toImmutableList()); + } + + private Optional<Integer> getOptionalIntegerFromConf(PropertiesConfiguration configuration, String key) { + return Optional.ofNullable(configuration.getInteger(key, null)); + } + + private Optional<Double> getOptionalDoubleFromConf(PropertiesConfiguration configuration, String key) { + return Optional.ofNullable(configuration.getDouble(key, null)); + } + + private QueryLoggerConfiguration getCassandraQueryLoggerConf(PropertiesConfiguration configuration) { + QueryLoggerConfiguration.Builder builder = QueryLoggerConfiguration.builder(); + + Optional<Long> constantThreshold = getOptionalIntegerFromConf(configuration, "cassandra.query.logger.constant.threshold") + .map(Long::valueOf); + + constantThreshold.ifPresent(builder::withConstantThreshold); + + getOptionalIntegerFromConf(configuration, "cassandra.query.logger.max.logged.parameters") + .ifPresent(builder::withMaxLoggedParameters); + + getOptionalIntegerFromConf(configuration, "cassandra.query.logger.max.query.string.length") + .ifPresent(builder::withMaxQueryStringLength); + + getOptionalIntegerFromConf(configuration, "cassandra.query.logger.max.parameter.value.length") + .ifPresent(builder::withMaxParameterValueLength); + + Optional<Double> percentileLatencyConf = getOptionalDoubleFromConf(configuration, "cassandra.query.slow.query.latency.threshold.percentile"); + + if (!percentileLatencyConf.isPresent() && !constantThreshold.isPresent()) { + percentileLatencyConf = Optional.of(QueryLogger.DEFAULT_SLOW_QUERY_THRESHOLD_PERCENTILE); + } + + percentileLatencyConf.ifPresent(slowQueryLatencyThresholdPercentile -> { + PerHostPercentileTracker tracker = PerHostPercentileTracker + .builder(CASSANDRA_HIGHEST_TRACKABLE_LATENCY_MILLIS) + .build(); + + builder.withDynamicThreshold(tracker, slowQueryLatencyThresholdPercentile); + }); + + return builder.build(); + } + + public Cluster get() { + return cluster; + } + + @PreDestroy + public void stop() { + cluster.closeAsync(); + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/a18a4488/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraJamesServerTest.java ---------------------------------------------------------------------- diff --git a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraJamesServerTest.java b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraJamesServerTest.java index b15491e..5b4dbd6 100644 --- a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraJamesServerTest.java +++ b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraJamesServerTest.java @@ -19,16 +19,20 @@ package org.apache.james; +import org.junit.ClassRule; import org.junit.Rule; public class CassandraJamesServerTest extends AbstractJmapJamesServerTest { + @ClassRule + public static DockerCassandraRule cassandra = new DockerCassandraRule(); + @Rule public CassandraJmapTestRule cassandraJmap = CassandraJmapTestRule.defaultTestRule(); @Override protected GuiceJamesServer createJamesServer() { - return cassandraJmap.jmapServer(); + return cassandraJmap.jmapServer(cassandra.getModule()); } @Override http://git-wip-us.apache.org/repos/asf/james-project/blob/a18a4488/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraJmapTestRule.java ---------------------------------------------------------------------- diff --git a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraJmapTestRule.java b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraJmapTestRule.java index d57d171..5b031a4 100644 --- a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraJmapTestRule.java +++ b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraJmapTestRule.java @@ -34,8 +34,7 @@ public class CassandraJmapTestRule implements TestRule { private static final int LIMIT_TO_3_MESSAGES = 3; public static CassandraJmapTestRule defaultTestRule() { - return new CassandraJmapTestRule( - AggregateGuiceModuleTestRule.of(new EmbeddedElasticSearchRule(), new EmbeddedCassandraRule())); + return new CassandraJmapTestRule(new EmbeddedElasticSearchRule()); } private GuiceModuleTestRule guiceModuleTestRule; http://git-wip-us.apache.org/repos/asf/james-project/blob/a18a4488/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraLogConfigurationTest.java ---------------------------------------------------------------------- diff --git a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraLogConfigurationTest.java b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraLogConfigurationTest.java index 659453e..090c1ff 100644 --- a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraLogConfigurationTest.java +++ b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraLogConfigurationTest.java @@ -27,7 +27,7 @@ import java.nio.channels.SocketChannel; import java.nio.charset.Charset; import org.apache.commons.configuration.PropertiesConfiguration; -import org.apache.james.modules.mailbox.CassandraSessionConfiguration; +import org.apache.james.backends.cassandra.init.CassandraSessionConfiguration; import org.junit.After; import org.junit.Before; import org.junit.Rule; @@ -69,7 +69,7 @@ public class CassandraLogConfigurationTest { public void serverShouldStartWithMinimalConfigAboutAConstantThresholdSlowQueryLogger() throws Exception { jamesServer = cassandraJmapTestRule.jmapServer( (binder) -> binder.bind(CassandraSessionConfiguration.class).toInstance(() -> { - PropertiesConfiguration configuration = dockerCassandraRule.getCassandraConfigurationForDocker(); + PropertiesConfiguration configuration = dockerCassandraRule.getCassandraConfigurationForDocker("keyspace1"); configuration.addProperty("cassandra.query.logger.constant.threshold", 100); @@ -84,7 +84,7 @@ public class CassandraLogConfigurationTest { jamesServer = cassandraJmapTestRule.jmapServer( (binder) -> binder.bind(CassandraSessionConfiguration.class).toInstance(() -> { - PropertiesConfiguration configuration = dockerCassandraRule.getCassandraConfigurationForDocker(); + PropertiesConfiguration configuration = dockerCassandraRule.getCassandraConfigurationForDocker("keyspace2"); configuration.addProperty("cassandra.query.slow.query.latency.threshold.percentile", 90); configuration.addProperty("cassandra.query.logger.max.logged.parameters", 9); @@ -104,7 +104,7 @@ public class CassandraLogConfigurationTest { jamesServer = cassandraJmapTestRule.jmapServer( (binder) -> binder.bind(CassandraSessionConfiguration.class).toInstance(() -> { - PropertiesConfiguration configuration = dockerCassandraRule.getCassandraConfigurationForDocker(); + PropertiesConfiguration configuration = dockerCassandraRule.getCassandraConfigurationForDocker("keyspace3"); configuration.addProperty("cassandra.query.slow.query.latency.threshold.percentile", 90); configuration.addProperty("cassandra.query.logger.constant.threshold", 100); http://git-wip-us.apache.org/repos/asf/james-project/blob/a18a4488/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraMessageIdManagerInjectionTest.java ---------------------------------------------------------------------- diff --git a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraMessageIdManagerInjectionTest.java b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraMessageIdManagerInjectionTest.java index e8395cb..b88289a 100644 --- a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraMessageIdManagerInjectionTest.java +++ b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraMessageIdManagerInjectionTest.java @@ -30,6 +30,7 @@ import org.apache.james.mailbox.MessageIdManager; import org.apache.james.utils.ConfigurationPerformer; import org.junit.After; import org.junit.Before; +import org.junit.ClassRule; import org.junit.Rule; import org.junit.Test; @@ -40,8 +41,12 @@ import com.google.inject.multibindings.Multibinder; public class CassandraMessageIdManagerInjectionTest { + @ClassRule + public static DockerCassandraRule cassandra = new DockerCassandraRule(); + @Rule public CassandraJmapTestRule cassandraJmap = CassandraJmapTestRule.defaultTestRule(); + private GuiceJamesServer server; @Before @@ -52,7 +57,7 @@ public class CassandraMessageIdManagerInjectionTest { Multibinder.newSetBinder(binder(), ConfigurationPerformer.class).addBinding().to(CallMe.class); } }; - server = cassandraJmap.jmapServer(module); + server = cassandraJmap.jmapServer(module, cassandra.getModule()); server.start(); } http://git-wip-us.apache.org/repos/asf/james-project/blob/a18a4488/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 9bc6de5..1124382 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 @@ -27,15 +27,18 @@ import java.nio.channels.SocketChannel; import java.nio.charset.Charset; import org.apache.commons.configuration.PropertiesConfiguration; -import org.apache.james.modules.mailbox.CassandraSessionConfiguration; +import org.apache.james.backends.cassandra.init.CassandraSessionConfiguration; import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.testcontainers.DockerClientFactory; +import com.google.common.base.Joiner; + public class CassandraNodeConfTest { + private static final int CASSANDRA_PORT = 9042; private static final int IMAP_PORT = 1143; private static String getDockerHostIp() { @@ -74,7 +77,10 @@ public class CassandraNodeConfTest { @Test public void serverShouldStartWhenOneCassandraNodeIsUnreachable() throws Exception { String unreachableNode = "10.2.3.42"; - PropertiesConfiguration configuration = getCassandraConfigurationForDocker(unreachableNode + "," + dockerCassandraRule.getIp()); + PropertiesConfiguration configuration = getCassandraConfigurationForDocker( + Joiner.on(',') + .join(unreachableNode, + dockerCassandraRule.getIp() + ":" + dockerCassandraRule.getMappedPort(CASSANDRA_PORT))); jamesServer = cassandraJmapTestRule.jmapServer( (binder) -> binder.bind(CassandraSessionConfiguration.class).toInstance(() -> configuration)); @@ -84,7 +90,7 @@ public class CassandraNodeConfTest { @Test public void configShouldWorkWithNonDefaultPort() throws Exception { - PropertiesConfiguration configuration = getCassandraConfigurationForDocker(getDockerHostIp() + ":" + dockerCassandraRule.getBindingPort()); + PropertiesConfiguration configuration = getCassandraConfigurationForDocker(getDockerHostIp() + ":" + dockerCassandraRule.getMappedPort(CASSANDRA_PORT)); jamesServer = cassandraJmapTestRule.jmapServer( (binder) -> binder.bind(CassandraSessionConfiguration.class).toInstance(() -> configuration)); http://git-wip-us.apache.org/repos/asf/james-project/blob/a18a4488/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraVersionCheckingTest.java ---------------------------------------------------------------------- diff --git a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraVersionCheckingTest.java b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraVersionCheckingTest.java index f66c7bb..9e71aa6 100644 --- a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraVersionCheckingTest.java +++ b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraVersionCheckingTest.java @@ -34,6 +34,7 @@ import org.apache.james.backends.cassandra.versions.CassandraSchemaVersionDAO; import org.apache.james.backends.cassandra.versions.CassandraSchemaVersionManager; import org.junit.After; import org.junit.Before; +import org.junit.ClassRule; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -45,6 +46,10 @@ public class CassandraVersionCheckingTest { private static final int MIN_VERSION = 2; private static final int MAX_VERSION = 4; + + @ClassRule + public static DockerCassandraRule cassandra = new DockerCassandraRule(); + @Rule public CassandraJmapTestRule cassandraJmapTestRule = CassandraJmapTestRule.defaultTestRule(); @@ -75,6 +80,7 @@ public class CassandraVersionCheckingTest { .thenReturn(CompletableFuture.completedFuture(Optional.of(MAX_VERSION))); jamesServer = cassandraJmapTestRule.jmapServer( + cassandra.getModule(), binder -> binder.bind(CassandraSchemaVersionDAO.class) .toInstance(versionDAO), binder -> binder.bind(CassandraSchemaVersionManager.class) @@ -89,6 +95,7 @@ public class CassandraVersionCheckingTest { .thenReturn(CompletableFuture.completedFuture(Optional.of(MIN_VERSION + 1))); jamesServer = cassandraJmapTestRule.jmapServer( + cassandra.getModule(), binder -> binder.bind(CassandraSchemaVersionDAO.class) .toInstance(versionDAO), binder -> binder.bind(CassandraSchemaVersionManager.class) @@ -103,6 +110,7 @@ public class CassandraVersionCheckingTest { .thenReturn(CompletableFuture.completedFuture(Optional.of(MIN_VERSION))); jamesServer = cassandraJmapTestRule.jmapServer( + cassandra.getModule(), binder -> binder.bind(CassandraSchemaVersionDAO.class) .toInstance(versionDAO), binder -> binder.bind(CassandraSchemaVersionManager.class) @@ -117,6 +125,7 @@ public class CassandraVersionCheckingTest { .thenReturn(CompletableFuture.completedFuture(Optional.of(MIN_VERSION - 1))); jamesServer = cassandraJmapTestRule.jmapServer( + cassandra.getModule(), binder -> binder.bind(CassandraSchemaVersionDAO.class) .toInstance(versionDAO), binder -> binder.bind(CassandraSchemaVersionManager.class) @@ -133,6 +142,7 @@ public class CassandraVersionCheckingTest { .thenReturn(CompletableFuture.completedFuture(Optional.of(MAX_VERSION + 1))); jamesServer = cassandraJmapTestRule.jmapServer( + cassandra.getModule(), binder -> binder.bind(CassandraSchemaVersionDAO.class) .toInstance(versionDAO), binder -> binder.bind(CassandraSchemaVersionManager.class) http://git-wip-us.apache.org/repos/asf/james-project/blob/a18a4488/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraWithTikaTest.java ---------------------------------------------------------------------- diff --git a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraWithTikaTest.java b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraWithTikaTest.java index 5894933..479a82d 100644 --- a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraWithTikaTest.java +++ b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraWithTikaTest.java @@ -29,7 +29,7 @@ public class CassandraWithTikaTest extends AbstractJmapJamesServerTest { public CassandraJmapTestRule cassandraJmap = new CassandraJmapTestRule( AggregateGuiceModuleTestRule.of( new EmbeddedElasticSearchRule(), - new EmbeddedCassandraRule(), + new DockerCassandraRule(), guiceTikaRule)); @Override http://git-wip-us.apache.org/repos/asf/james-project/blob/a18a4488/server/container/guice/cassandra-guice/src/test/java/org/apache/james/DefaultCassandraJamesServerTest.java ---------------------------------------------------------------------- diff --git a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/DefaultCassandraJamesServerTest.java b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/DefaultCassandraJamesServerTest.java index 4c41e12..c2960e4 100644 --- a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/DefaultCassandraJamesServerTest.java +++ b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/DefaultCassandraJamesServerTest.java @@ -27,11 +27,15 @@ import org.apache.james.utils.FailingPropertiesProvider; import org.apache.james.utils.PropertiesProvider; import org.junit.After; import org.junit.Before; +import org.junit.ClassRule; import org.junit.Rule; import org.junit.Test; public class DefaultCassandraJamesServerTest { + @ClassRule + public static DockerCassandraRule cassandra = new DockerCassandraRule(); + @Rule public CassandraJmapTestRule cassandraJmap = CassandraJmapTestRule.defaultTestRule(); @@ -39,7 +43,7 @@ public class DefaultCassandraJamesServerTest { @Before public void setUp() { - guiceJamesServer = cassandraJmap.jmapServer() + guiceJamesServer = cassandraJmap.jmapServer(cassandra.getModule()) .overrideWith(binder -> binder.bind(PropertiesProvider.class).to(FailingPropertiesProvider.class)) .overrideWith(binder -> binder.bind(ConfigurationProvider.class).toInstance(s -> new HierarchicalConfiguration())); } http://git-wip-us.apache.org/repos/asf/james-project/blob/a18a4488/server/container/guice/cassandra-guice/src/test/java/org/apache/james/DockerCassandraRule.java ---------------------------------------------------------------------- diff --git a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/DockerCassandraRule.java b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/DockerCassandraRule.java index 232c57b..dc7c199 100644 --- a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/DockerCassandraRule.java +++ b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/DockerCassandraRule.java @@ -18,33 +18,25 @@ ****************************************************************/ package org.apache.james; -import java.util.Arrays; - import org.apache.commons.configuration.PropertiesConfiguration; -import org.apache.james.modules.mailbox.CassandraSessionConfiguration; -import org.apache.james.util.streams.SwarmGenericContainer; +import org.apache.commons.lang3.RandomStringUtils; +import org.apache.james.backends.cassandra.init.CassandraSessionConfiguration; import org.junit.runner.Description; import org.junit.runners.model.Statement; +import org.testcontainers.containers.GenericContainer; -import com.github.dockerjava.api.model.ExposedPort; -import com.github.dockerjava.api.model.Ports; import com.google.inject.Module; public class DockerCassandraRule implements GuiceModuleTestRule { - private static final int CASSANDRA_PORT = 9042; - - private static boolean isBindingToEveryThing(Ports.Binding binding) { - String bindingIp = binding.getHostIp(); - return bindingIp == null || bindingIp.equals("0.0.0.0"); - } - - public PropertiesConfiguration getCassandraConfigurationForDocker() { + private org.apache.james.backends.cassandra.DockerCassandraRule cassandraContainer = new org.apache.james.backends.cassandra.DockerCassandraRule(); + + public PropertiesConfiguration getCassandraConfigurationForDocker(String keyspace) { PropertiesConfiguration configuration = new PropertiesConfiguration(); - configuration.addProperty("cassandra.nodes", getIp() + ":" + CASSANDRA_PORT); - configuration.addProperty("cassandra.keyspace", "apache_james"); + configuration.addProperty("cassandra.nodes", cassandraContainer.getIp() + ":" + cassandraContainer.getBindingPort()); + configuration.addProperty("cassandra.keyspace", keyspace); configuration.addProperty("cassandra.replication.factor", 1); configuration.addProperty("cassandra.retryConnection.maxRetries", 20); configuration.addProperty("cassandra.retryConnection.minDelay", 5000); @@ -52,9 +44,6 @@ public class DockerCassandraRule implements GuiceModuleTestRule { return configuration; } - private SwarmGenericContainer cassandraContainer = new SwarmGenericContainer("cassandra:2.2") - .withExposedPorts(CASSANDRA_PORT); - @Override public Statement apply(Statement base, Description description) { return cassandraContainer.apply(base, description); @@ -66,29 +55,36 @@ public class DockerCassandraRule implements GuiceModuleTestRule { @Override public Module getModule() { - return (binder) -> binder.bind(CassandraSessionConfiguration.class).toInstance(this::getCassandraConfigurationForDocker); + String keyspace = RandomStringUtils.randomAlphabetic(12); + return (binder) -> binder.bind(CassandraSessionConfiguration.class).toInstance(() -> getCassandraConfigurationForDocker(keyspace)); } public String getIp() { return cassandraContainer.getIp(); } - public int getBindingPort() { - Ports.Binding[] bindings = cassandraContainer - .getContainerInfo() - .getNetworkSettings() - .getPorts() - .getBindings() - .get(ExposedPort.tcp(CASSANDRA_PORT)); - - return Integer.valueOf( - Arrays.stream(bindings) - .filter(DockerCassandraRule::isBindingToEveryThing) - .map(Ports.Binding::getHostPortSpec) - .findFirst().get()); + public Integer getMappedPort(int originalPort) { + return cassandraContainer.getBindingPort(); + } + + public void start() { + cassandraContainer.start(); } - public SwarmGenericContainer getCassandraContainer() { - return cassandraContainer; + public void stop() { + cassandraContainer.stop(); } + + public GenericContainer<?> getRawContainer() { + return cassandraContainer.getRawContainer(); + } + + public void pause() { + cassandraContainer.pause(); + } + + public void unpause() { + cassandraContainer.unpause(); + } + } http://git-wip-us.apache.org/repos/asf/james-project/blob/a18a4488/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 index 4cd4186..75fe4a3 100644 --- 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 @@ -37,7 +37,7 @@ public class DockerElasticSearchRule implements GuiceModuleTestRule { PropertiesConfiguration configuration = new PropertiesConfiguration(); configuration.addProperty("elasticsearch.masterHost", getIp()); - configuration.addProperty("elasticsearch.port", ELASTIC_SEARCH_PORT); + configuration.addProperty("elasticsearch.port", elasticSearchContainer.getMappedPort(ELASTIC_SEARCH_PORT)); configuration.addProperty("elasticsearch.nb.shards", 1); configuration.addProperty("elasticsearch.nb.replica", 0); @@ -45,7 +45,7 @@ public class DockerElasticSearchRule implements GuiceModuleTestRule { configuration.addProperty("elasticsearch.retryConnection.minDelay", 3000); configuration.addProperty("elasticsearch.indexAttachments", false); configuration.addProperty("elasticsearch.http.host", getIp()); - configuration.addProperty("elasticsearch.http.port", ELASTIC_SEARCH_HTTP_PORT); + configuration.addProperty("elasticsearch.http.port", elasticSearchContainer.getMappedPort(ELASTIC_SEARCH_HTTP_PORT)); configuration.addProperty("elasticsearch.metrics.reports.enabled", true); configuration.addProperty("elasticsearch.metrics.reports.period", 30); configuration.addProperty("elasticsearch.metrics.reports.index", "james-metrics"); @@ -71,10 +71,18 @@ public class DockerElasticSearchRule implements GuiceModuleTestRule { } public String getIp() { - return elasticSearchContainer.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/a18a4488/server/container/guice/cassandra-guice/src/test/java/org/apache/james/ESReporterTest.java ---------------------------------------------------------------------- diff --git a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/ESReporterTest.java b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/ESReporterTest.java index 2cc679d..2e46abf 100644 --- a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/ESReporterTest.java +++ b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/ESReporterTest.java @@ -68,7 +68,7 @@ public class ESReporterTest { private Timer timer; @Rule - public CassandraJmapTestRule cassandraJmap = new CassandraJmapTestRule(embeddedElasticSearchRule, new EmbeddedCassandraRule()); + public CassandraJmapTestRule cassandraJmap = new CassandraJmapTestRule(embeddedElasticSearchRule, new DockerCassandraRule()); private GuiceJamesServer server; private AccessToken accessToken; http://git-wip-us.apache.org/repos/asf/james-project/blob/a18a4488/server/container/guice/cassandra-guice/src/test/java/org/apache/james/EmbeddedCassandraRule.java ---------------------------------------------------------------------- diff --git a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/EmbeddedCassandraRule.java b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/EmbeddedCassandraRule.java deleted file mode 100644 index c9a65a7..0000000 --- a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/EmbeddedCassandraRule.java +++ /dev/null @@ -1,75 +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 javax.inject.Inject; -import javax.inject.Provider; - -import org.apache.james.backends.cassandra.CassandraCluster; -import org.apache.james.backends.cassandra.EmbeddedCassandra; -import org.junit.runner.Description; -import org.junit.runners.model.Statement; - -import com.datastax.driver.core.Session; -import com.google.inject.Module; -import com.google.inject.Scopes; -import com.google.inject.util.Modules; - - -public class EmbeddedCassandraRule implements GuiceModuleTestRule { - - public static class SessionProvider implements Provider<Session> { - - private final Session session; - - @Inject - private SessionProvider(CassandraCluster cluster) { - session = cluster.getConf(); - } - - @Override - public Session get() { - return session; - } - } - - private EmbeddedCassandra cassandra; - - @Override - public Statement apply(Statement base, Description description) { - return new Statement() { - @Override - public void evaluate() throws Throwable { - cassandra = EmbeddedCassandra.createStartServer(); - base.evaluate(); - } - }; - } - - @Override - public void await() { - } - - @Override - public Module getModule() { - return Modules.combine( - (binder) -> binder.bind(EmbeddedCassandra.class).toInstance(cassandra), - (binder) -> binder.bind(Session.class).toProvider(SessionProvider.class).in(Scopes.SINGLETON)); - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/a18a4488/server/container/guice/cassandra-guice/src/test/java/org/apache/james/JamesCapabilitiesServerTest.java ---------------------------------------------------------------------- diff --git a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/JamesCapabilitiesServerTest.java b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/JamesCapabilitiesServerTest.java index 3dbce73..5fee8a5 100644 --- a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/JamesCapabilitiesServerTest.java +++ b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/JamesCapabilitiesServerTest.java @@ -26,8 +26,6 @@ import java.util.EnumSet; import org.apache.activemq.store.PersistenceAdapter; import org.apache.activemq.store.memory.MemoryPersistenceAdapter; -import org.apache.james.backends.cassandra.CassandraCluster; -import org.apache.james.backends.cassandra.components.CassandraModule; import org.apache.james.backends.es.EmbeddedElasticSearch; import org.apache.james.jmap.methods.GetMessageListMethod; import org.apache.james.mailbox.MailboxManager; @@ -41,24 +39,22 @@ import org.junit.Test; import org.junit.rules.RuleChain; import org.junit.rules.TemporaryFolder; -import com.datastax.driver.core.Session; -import com.google.inject.AbstractModule; import com.google.inject.Module; -import com.google.inject.Provides; -import com.google.inject.Singleton; public class JamesCapabilitiesServerTest { private GuiceJamesServer server; private TemporaryFolder temporaryFolder = new TemporaryFolder(); private EmbeddedElasticSearch embeddedElasticSearch = new EmbeddedElasticSearch(temporaryFolder, MailboxElasticsearchConstants.MAILBOX_INDEX); - + private DockerCassandraRule cassandraServer = new DockerCassandraRule(); + @Rule - public RuleChain chain = RuleChain.outerRule(temporaryFolder).around(embeddedElasticSearch); + public RuleChain chain = RuleChain.outerRule(temporaryFolder).around(embeddedElasticSearch).around(cassandraServer); @After public void teardown() { server.stop(); + } private GuiceJamesServer createCassandraJamesServer(final MailboxManager mailboxManager) { @@ -69,22 +65,9 @@ public class JamesCapabilitiesServerTest { .overrideWith((binder) -> binder.bind(PersistenceAdapter.class).to(MemoryPersistenceAdapter.class)) .overrideWith(new TestElasticSearchModule(embeddedElasticSearch), new TestFilesystemModule(temporaryFolder), + cassandraServer.getModule(), new TestJMAPServerModule(GetMessageListMethod.DEFAULT_MAXIMUM_LIMIT), - mockMailboxManager, - new AbstractModule() { - - @Override - protected void configure() { - } - - @Provides - @Singleton - Session provideSession(CassandraModule cassandraModule) { - CassandraCluster cassandra = CassandraCluster.create(cassandraModule); - return cassandra.getConf(); - } - - }); + mockMailboxManager); } @Test --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org