JAMES-2334 Provide a JUnit extension for RabbitMQ in Docker
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/8daf8951 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/8daf8951 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/8daf8951 Branch: refs/heads/master Commit: 8daf8951bfaeb1e23a8e2bfda749d516799a2edd Parents: 5041a0e Author: Antoine Duprat <[email protected]> Authored: Tue Feb 6 16:14:57 2018 +0100 Committer: Matthieu Baechler <[email protected]> Committed: Thu May 31 09:47:02 2018 +0200 ---------------------------------------------------------------------- server/queue/queue-rabbitmq/pom.xml | 33 +++++++++ .../james/queue/rabbitmq/DockerRabbitMQ.java | 74 ++++++++++++++++++++ .../queue/rabbitmq/DockerRabbitMQExtension.java | 55 +++++++++++++++ .../rabbitmq/DockerRabbitMQExtensionTest.java | 50 +++++++++++++ .../queue/rabbitmq/RabbitMQWaitStrategy.java | 67 ++++++++++++++++++ 5 files changed, 279 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/8daf8951/server/queue/queue-rabbitmq/pom.xml ---------------------------------------------------------------------- diff --git a/server/queue/queue-rabbitmq/pom.xml b/server/queue/queue-rabbitmq/pom.xml index 950089f..85d888d 100644 --- a/server/queue/queue-rabbitmq/pom.xml +++ b/server/queue/queue-rabbitmq/pom.xml @@ -33,5 +33,38 @@ <name>Apache James :: Server :: Mail Queue :: RabbitMQ</name> <dependencies> + <dependency> + <groupId>com.google.guava</groupId> + <artifactId>guava</artifactId> + </dependency> + <dependency> + <groupId>com.rabbitmq</groupId> + <artifactId>amqp-client</artifactId> + </dependency> + <dependency> + <groupId>org.assertj</groupId> + <artifactId>assertj-core</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.junit.jupiter</groupId> + <artifactId>junit-jupiter-engine</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.junit.platform</groupId> + <artifactId>junit-platform-launcher</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.junit.vintage</groupId> + <artifactId>junit-vintage-engine</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.testcontainers</groupId> + <artifactId>testcontainers</artifactId> + <scope>test</scope> + </dependency> </dependencies> </project> http://git-wip-us.apache.org/repos/asf/james-project/blob/8daf8951/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/DockerRabbitMQ.java ---------------------------------------------------------------------- diff --git a/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/DockerRabbitMQ.java b/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/DockerRabbitMQ.java new file mode 100644 index 0000000..0eab7a2 --- /dev/null +++ b/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/DockerRabbitMQ.java @@ -0,0 +1,74 @@ +/**************************************************************** + * 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.queue.rabbitmq; + +import org.testcontainers.containers.GenericContainer; + +import com.rabbitmq.client.ConnectionFactory; + +public class DockerRabbitMQ { + + private static final int DEFAULT_RABBITMQ_PORT = 5672; + private static final String DEFAULT_RABBITMQ_HOSTNAME = "my-rabbit"; + private static final String DEFAULT_RABBITMQ_USERNAME = "guest"; + private static final String DEFAULT_RABBITMQ_PASSWORD = "guest"; + + private GenericContainer<?> container; + + @SuppressWarnings("resource") + public DockerRabbitMQ() { + container = new GenericContainer<>("rabbitmq:3.7.5") + .withCreateContainerCmdModifier(cmd -> cmd.withHostName(DEFAULT_RABBITMQ_HOSTNAME)) + .withExposedPorts(DEFAULT_RABBITMQ_PORT) + .waitingFor(RabbitMQWaitStrategy.withDefaultTimeout(this)); + } + + public String getHostIp() { + return container.getContainerIpAddress(); + } + + public Integer getPort() { + return container.getMappedPort(DEFAULT_RABBITMQ_PORT); + } + + public String getUsername() { + return DEFAULT_RABBITMQ_USERNAME; + } + + public String getPassword() { + return DEFAULT_RABBITMQ_PASSWORD; + } + + public ConnectionFactory connectionFactory() { + ConnectionFactory connectionFactory = new ConnectionFactory(); + connectionFactory.setHost(getHostIp()); + connectionFactory.setPort(getPort()); + connectionFactory.setUsername(getUsername()); + connectionFactory.setPassword(getPassword()); + return connectionFactory; + } + + public void start() { + container.start(); + } + + public void stop() { + container.stop(); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/8daf8951/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/DockerRabbitMQExtension.java ---------------------------------------------------------------------- diff --git a/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/DockerRabbitMQExtension.java b/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/DockerRabbitMQExtension.java new file mode 100644 index 0000000..925ff08 --- /dev/null +++ b/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/DockerRabbitMQExtension.java @@ -0,0 +1,55 @@ +/**************************************************************** + * 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.queue.rabbitmq; + +import org.junit.jupiter.api.extension.AfterAllCallback; +import org.junit.jupiter.api.extension.AfterEachCallback; +import org.junit.jupiter.api.extension.BeforeAllCallback; +import org.junit.jupiter.api.extension.BeforeEachCallback; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.api.extension.ParameterContext; +import org.junit.jupiter.api.extension.ParameterResolutionException; +import org.junit.jupiter.api.extension.ParameterResolver; + +public class DockerRabbitMQExtension implements BeforeEachCallback, AfterEachCallback, ParameterResolver { + + private DockerRabbitMQ rabbitMQ; + + @Override + public void beforeEach(ExtensionContext context) { + rabbitMQ = new DockerRabbitMQ(); + rabbitMQ.start(); + } + + @Override + public void afterEach(ExtensionContext context) { + rabbitMQ.stop(); + } + + @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 rabbitMQ; + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/8daf8951/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/DockerRabbitMQExtensionTest.java ---------------------------------------------------------------------- diff --git a/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/DockerRabbitMQExtensionTest.java b/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/DockerRabbitMQExtensionTest.java new file mode 100644 index 0000000..f2a0cea --- /dev/null +++ b/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/DockerRabbitMQExtensionTest.java @@ -0,0 +1,50 @@ +/**************************************************************** + * 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.queue.rabbitmq; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; + +import com.rabbitmq.client.Connection; +import com.rabbitmq.client.ConnectionFactory; + +@ExtendWith(DockerRabbitMQExtension.class) +public class DockerRabbitMQExtensionTest { + + private ConnectionFactory connectionFactory; + + @BeforeEach + public void setup(DockerRabbitMQ rabbitMQ) { + connectionFactory = new ConnectionFactory(); + connectionFactory.setHost(rabbitMQ.getHostIp()); + connectionFactory.setPort(rabbitMQ.getPort()); + connectionFactory.setUsername(rabbitMQ.getUsername()); + connectionFactory.setPassword(rabbitMQ.getPassword()); + } + + @Test + public void containerShouldBeUp() throws Exception { + try (Connection connection = connectionFactory.newConnection()) { + assertThat(connection.isOpen()).isTrue(); + } + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/8daf8951/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/RabbitMQWaitStrategy.java ---------------------------------------------------------------------- diff --git a/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/RabbitMQWaitStrategy.java b/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/RabbitMQWaitStrategy.java new file mode 100644 index 0000000..ba6ce3b --- /dev/null +++ b/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/RabbitMQWaitStrategy.java @@ -0,0 +1,67 @@ +/**************************************************************** + * 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.queue.rabbitmq; + +import java.io.IOException; +import java.time.Duration; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import org.rnorth.ducttape.unreliables.Unreliables; +import org.testcontainers.containers.wait.strategy.WaitStrategy; +import org.testcontainers.containers.wait.strategy.WaitStrategyTarget; + +import com.google.common.primitives.Ints; +import com.rabbitmq.client.Connection; + +public class RabbitMQWaitStrategy implements WaitStrategy { + + private static final Duration DEFAULT_TIMEOUT = Duration.ofMinutes(1); + + public static RabbitMQWaitStrategy withDefaultTimeout(DockerRabbitMQ rabbitMQ) { + return new RabbitMQWaitStrategy(rabbitMQ, DEFAULT_TIMEOUT); + } + + private final DockerRabbitMQ rabbitMQ; + private final Duration timeout; + + public RabbitMQWaitStrategy(DockerRabbitMQ rabbitMQ, Duration timeout) { + this.rabbitMQ = rabbitMQ; + this.timeout = timeout; + } + + @Override + public void waitUntilReady(WaitStrategyTarget waitStrategyTarget) { + int seconds = Ints.checkedCast(this.timeout.getSeconds()); + + Unreliables.retryUntilTrue(seconds, TimeUnit.SECONDS, this::isConnected); + } + + private Boolean isConnected() throws IOException, TimeoutException { + try (Connection connection = rabbitMQ.connectionFactory().newConnection()) { + return connection.isOpen(); + } + } + + @Override + public WaitStrategy withStartupTimeout(Duration startupTimeout) { + return new RabbitMQWaitStrategy(rabbitMQ, startupTimeout); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
