From 7e6daf8280eebde71d02837ff6333c7654385f46 Mon Sep 17 00:00:00 2001 From: HoussemNasri Date: Wed, 30 Oct 2024 14:31:28 +0100 Subject: [PATCH] Add integration tests --- tmail-backend/apps/distributed/pom.xml | 13 +++++ ...verWithOpenPaasRabbitMqConfiguredTest.java | 48 +++++++++++++++++++ tmail-backend/apps/memory/pom.xml | 17 +++++++ ...verWithOpenPaasRabbitMqConfiguredTest.java | 38 +++++++++++++++ .../tmail-third-party/openpaas/pom.xml | 10 ++++ .../com/linagora/tmail/OpenPaasModule.java | 18 +++---- .../tmail/OpenPaasRabbitMQExtension.java | 43 +++++++++++++++++ .../tmail/TestOpenPaasRabbitMQModule.java | 31 ++++++++++++ 8 files changed, 207 insertions(+), 11 deletions(-) create mode 100644 tmail-backend/apps/distributed/src/test/java/com/linagora/tmail/james/app/DistributedServerWithOpenPaasRabbitMqConfiguredTest.java create mode 100644 tmail-backend/apps/memory/src/test/java/com/linagora/tmail/james/app/MemoryServerWithOpenPaasRabbitMqConfiguredTest.java create mode 100644 tmail-backend/tmail-third-party/openpaas/src/test/java/com/linagora/tmail/OpenPaasRabbitMQExtension.java create mode 100644 tmail-backend/tmail-third-party/openpaas/src/test/java/com/linagora/tmail/TestOpenPaasRabbitMQModule.java diff --git a/tmail-backend/apps/distributed/pom.xml b/tmail-backend/apps/distributed/pom.xml index 83b000f398..7412456c54 100644 --- a/tmail-backend/apps/distributed/pom.xml +++ b/tmail-backend/apps/distributed/pom.xml @@ -98,6 +98,14 @@ tmail-openpaas 1.0.0-SNAPSHOT + + com.linagora.tmail + tmail-openpaas + 1.0.0-SNAPSHOT + test + test-jar + + ${project.groupId} tmail-rate-limiter-cassandra @@ -363,6 +371,11 @@ com.linagora.tmail logback-json-classic + + org.mockito + mockito-core + test + org.testcontainers testcontainers diff --git a/tmail-backend/apps/distributed/src/test/java/com/linagora/tmail/james/app/DistributedServerWithOpenPaasRabbitMqConfiguredTest.java b/tmail-backend/apps/distributed/src/test/java/com/linagora/tmail/james/app/DistributedServerWithOpenPaasRabbitMqConfiguredTest.java new file mode 100644 index 0000000000..d36e24707a --- /dev/null +++ b/tmail-backend/apps/distributed/src/test/java/com/linagora/tmail/james/app/DistributedServerWithOpenPaasRabbitMqConfiguredTest.java @@ -0,0 +1,48 @@ +package com.linagora.tmail.james.app; + +import org.apache.james.JamesServerBuilder; +import org.apache.james.JamesServerExtension; +import org.apache.james.SearchConfiguration; +import org.apache.james.backends.redis.RedisExtension; +import org.apache.james.utils.GuiceProbe; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import com.google.inject.multibindings.Multibinder; + +import com.linagora.tmail.OpenPaasModuleChooserConfiguration; +import com.linagora.tmail.OpenPaasRabbitMQExtension; +import com.linagora.tmail.combined.identity.LdapExtension; +import com.linagora.tmail.combined.identity.UsersRepositoryClassProbe; +import com.linagora.tmail.combined.identity.UsersRepositoryModuleChooser; +import com.linagora.tmail.module.LinagoraTestJMAPServerModule; + +public class DistributedServerWithOpenPaasRabbitMqConfiguredTest { + @RegisterExtension + static JamesServerExtension + testExtension = new JamesServerBuilder(tmpDir -> + DistributedJamesConfiguration.builder() + .workingDirectory(tmpDir) + .configurationFromClasspath() + .searchConfiguration(SearchConfiguration.openSearch()) + .usersRepository(UsersRepositoryModuleChooser.Implementation.COMBINED) + .eventBusKeysChoice(EventBusKeysChoice.REDIS) + .openPassModuleChooserConfiguration(OpenPaasModuleChooserConfiguration.ENABLED) + .build()) + .server(configuration -> DistributedServer.createServer(configuration) + .overrideWith(new LinagoraTestJMAPServerModule()) + .overrideWith(binder -> Multibinder.newSetBinder(binder, GuiceProbe.class).addBinding().to(UsersRepositoryClassProbe.class))) + .extension(new DockerOpenSearchExtension()) + .extension(new CassandraExtension()) + .extension(new RabbitMQExtension()) + .extension(new OpenPaasRabbitMQExtension()) + .extension(new LdapExtension()) + .extension(new RedisExtension()) + .lifeCycle(JamesServerExtension.Lifecycle.PER_CLASS) + .build(); + + @Test + void serverShouldStartWithOpenPaasRabbitMqConfigured() { + } + +} diff --git a/tmail-backend/apps/memory/pom.xml b/tmail-backend/apps/memory/pom.xml index b14f848089..bde5379ce7 100755 --- a/tmail-backend/apps/memory/pom.xml +++ b/tmail-backend/apps/memory/pom.xml @@ -68,6 +68,23 @@ tmail-openpaas 1.0.0-SNAPSHOT + + com.linagora.tmail + tmail-openpaas + 1.0.0-SNAPSHOT + test + test-jar + + + ${james.groupId} + apache-james-backends-rabbitmq + + + ${james.groupId} + apache-james-backends-rabbitmq + test + test-jar + ${project.groupId} tmail-webadmin-mailbox diff --git a/tmail-backend/apps/memory/src/test/java/com/linagora/tmail/james/app/MemoryServerWithOpenPaasRabbitMqConfiguredTest.java b/tmail-backend/apps/memory/src/test/java/com/linagora/tmail/james/app/MemoryServerWithOpenPaasRabbitMqConfiguredTest.java new file mode 100644 index 0000000000..25b013dda1 --- /dev/null +++ b/tmail-backend/apps/memory/src/test/java/com/linagora/tmail/james/app/MemoryServerWithOpenPaasRabbitMqConfiguredTest.java @@ -0,0 +1,38 @@ +package com.linagora.tmail.james.app; + +import static org.apache.james.data.UsersRepositoryModuleChooser.Implementation.DEFAULT; + +import org.apache.james.JamesServerBuilder; +import org.apache.james.JamesServerExtension; +import org.apache.james.utils.GuiceProbe; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import com.google.inject.multibindings.Multibinder; +import com.linagora.tmail.OpenPaasModuleChooserConfiguration; +import com.linagora.tmail.OpenPaasRabbitMQExtension; +import com.linagora.tmail.encrypted.MailboxConfiguration; +import com.linagora.tmail.encrypted.MailboxManagerClassProbe; +import com.linagora.tmail.module.LinagoraTestJMAPServerModule; + +class MemoryServerWithOpenPaasRabbitMqConfiguredTest { + @RegisterExtension + static JamesServerExtension jamesServerExtension = new JamesServerBuilder(tmpDir -> + MemoryConfiguration.builder() + .workingDirectory(tmpDir) + .configurationFromClasspath() + .mailbox(new MailboxConfiguration(false)) + .usersRepository(DEFAULT) + .openPaasModuleChooserConfiguration(OpenPaasModuleChooserConfiguration.ENABLED) + .build()) + .server(configuration -> MemoryServer.createServer(configuration) + .overrideWith(new LinagoraTestJMAPServerModule()) + .overrideWith(binder -> Multibinder.newSetBinder(binder, GuiceProbe.class).addBinding().to(MailboxManagerClassProbe.class))) + .extension(new OpenPaasRabbitMQExtension()) + .build(); + + @Test + public void serverShouldStartWithOpenPaasRabbitMqConfigured() { + + } +} \ No newline at end of file diff --git a/tmail-backend/tmail-third-party/openpaas/pom.xml b/tmail-backend/tmail-third-party/openpaas/pom.xml index f1805f3017..266becabf3 100644 --- a/tmail-backend/tmail-third-party/openpaas/pom.xml +++ b/tmail-backend/tmail-third-party/openpaas/pom.xml @@ -18,6 +18,16 @@ ${project.groupId} jmap-extensions + + ${james.groupId} + james-server-guice-common + + + ${james.groupId} + james-server-guice-common + test + test-jar + ${james.groupId} apache-james-backends-rabbitmq diff --git a/tmail-backend/tmail-third-party/openpaas/src/main/java/com/linagora/tmail/OpenPaasModule.java b/tmail-backend/tmail-third-party/openpaas/src/main/java/com/linagora/tmail/OpenPaasModule.java index 4bcb658d34..1e9b7b4e20 100644 --- a/tmail-backend/tmail-third-party/openpaas/src/main/java/com/linagora/tmail/OpenPaasModule.java +++ b/tmail-backend/tmail-third-party/openpaas/src/main/java/com/linagora/tmail/OpenPaasModule.java @@ -66,23 +66,21 @@ public OpenPaasRestClient provideOpenPaasRestCLient(OpenPaasConfiguration openPa @Provides @Named(OPENPAAS_INJECTION_KEY) @Singleton - public RabbitMQConfiguration provideRabbitMQConfiguration(OpenPaasConfiguration openPaasConfiguration, RabbitMQConfiguration fallbackRabbitMQConfiguration) { + public RabbitMQConfiguration provideRabbitMQConfiguration(OpenPaasConfiguration openPaasConfiguration) { return openPaasConfiguration.maybeRabbitMqUri() .map(AmqpUri::toRabbitMqConfiguration) - .orElseGet(() -> { - LOGGER.debug("RabbitMQ URI not configured correctly in openpaas.properties, falling back to rabbitmq.properties."); - return fallbackRabbitMQConfiguration; - }); + .orElseThrow((() -> { + throw new IllegalStateException("RabbitMQ URI not configured correctly in openpaas.properties."); + })); } @Provides @Named(OPENPAAS_INJECTION_KEY) @Singleton - public SimpleConnectionPool provideSimpleConnectionPool(@Named(OPENPAAS_INJECTION_KEY) RabbitMQConfiguration rabbitMQConfiguration, - @Named(OPENPAAS_CONFIGURATION_NAME) Provider configuration) { + public SimpleConnectionPool provideSimpleConnectionPool(@Named(OPENPAAS_INJECTION_KEY) RabbitMQConfiguration rabbitMQConfiguration) { RabbitMQConnectionFactory rabbitMQConnectionFactory = new RabbitMQConnectionFactory(rabbitMQConfiguration); try { - return new SimpleConnectionPool(rabbitMQConnectionFactory, SimpleConnectionPool.Configuration.from(configuration.get())); + return new SimpleConnectionPool(rabbitMQConnectionFactory, SimpleConnectionPool.Configuration.DEFAULT); } catch (Exception e) { LOGGER.info("Error while retrieving SimpleConnectionPool.Configuration, falling back to defaults.", e); return new SimpleConnectionPool(rabbitMQConnectionFactory, SimpleConnectionPool.Configuration.DEFAULT); @@ -93,14 +91,12 @@ public SimpleConnectionPool provideSimpleConnectionPool(@Named(OPENPAAS_INJECTIO @Named(OPENPAAS_INJECTION_KEY) @Singleton public ReactorRabbitMQChannelPool provideReactorRabbitMQChannelPool( - OpenPaasConfiguration openPaasConfiguration, @Named(OPENPAAS_INJECTION_KEY) SimpleConnectionPool simpleConnectionPool, - ReactorRabbitMQChannelPool.Configuration configuration, MetricFactory metricFactory, GaugeRegistry gaugeRegistry) { ReactorRabbitMQChannelPool channelPool = new ReactorRabbitMQChannelPool( simpleConnectionPool.getResilientConnection(), - configuration, + ReactorRabbitMQChannelPool.Configuration.DEFAULT, metricFactory, gaugeRegistry); channelPool.start(); diff --git a/tmail-backend/tmail-third-party/openpaas/src/test/java/com/linagora/tmail/OpenPaasRabbitMQExtension.java b/tmail-backend/tmail-third-party/openpaas/src/test/java/com/linagora/tmail/OpenPaasRabbitMQExtension.java new file mode 100644 index 0000000000..b5e1541d77 --- /dev/null +++ b/tmail-backend/tmail-third-party/openpaas/src/test/java/com/linagora/tmail/OpenPaasRabbitMQExtension.java @@ -0,0 +1,43 @@ +package com.linagora.tmail; + +import org.apache.james.GuiceModuleTestExtension; +import org.apache.james.backends.rabbitmq.DockerRabbitMQ; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.api.extension.ParameterContext; +import org.junit.jupiter.api.extension.ParameterResolutionException; + +import com.google.inject.Module; + +public class OpenPaasRabbitMQExtension implements GuiceModuleTestExtension { + private final DockerRabbitMQ dockerRabbitMQ = DockerRabbitMQ.withoutCookie(); + + @Override + public void afterAll(ExtensionContext extensionContext) { + dockerRabbitMQ.stop(); + } + + @Override + public void beforeAll(ExtensionContext extensionContext) { + dockerRabbitMQ.start(); + } + + @Override + public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws + ParameterResolutionException { + return parameterContext.getParameter().getType() == DockerRabbitMQ.class; + } + + @Override + public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { + return dockerRabbitMQ; + } + + @Override + public Module getModule() { + return new TestOpenPaasRabbitMQModule(dockerRabbitMQ); + } + + public DockerRabbitMQ dockerRabbitMQ() { + return dockerRabbitMQ; + } +} diff --git a/tmail-backend/tmail-third-party/openpaas/src/test/java/com/linagora/tmail/TestOpenPaasRabbitMQModule.java b/tmail-backend/tmail-third-party/openpaas/src/test/java/com/linagora/tmail/TestOpenPaasRabbitMQModule.java new file mode 100644 index 0000000000..dd7ce410c7 --- /dev/null +++ b/tmail-backend/tmail-third-party/openpaas/src/test/java/com/linagora/tmail/TestOpenPaasRabbitMQModule.java @@ -0,0 +1,31 @@ +package com.linagora.tmail; + +import java.net.URI; +import java.net.URISyntaxException; + +import jakarta.inject.Singleton; + +import org.apache.james.backends.rabbitmq.DockerRabbitMQ; + +import com.google.inject.AbstractModule; +import com.google.inject.Provides; +import com.linagora.tmail.configuration.OpenPaasConfiguration; + +public class TestOpenPaasRabbitMQModule extends AbstractModule { + private final DockerRabbitMQ dockerRabbitMQ; + + public TestOpenPaasRabbitMQModule(DockerRabbitMQ dockerRabbitMQ) { + this.dockerRabbitMQ = dockerRabbitMQ; + } + + @Provides + @Singleton + public OpenPaasConfiguration provideOpenPaasConfiguration() throws URISyntaxException { + return new OpenPaasConfiguration( + AmqpUri.from(dockerRabbitMQ.amqpUri()).asOptional(), + URI.create("http://localhost:8081"), + "user", + "password" + ); + } +}