From 7a32c77ad17ff61df3bdeabbe5cfe2d554dcb2d8 Mon Sep 17 00:00:00 2001 From: Reinhard Prechtl Date: Thu, 7 Dec 2017 11:49:52 +0100 Subject: [PATCH] Adding initial InfinispanContainer implementation --- pom.xml | 38 +++ .../infinispan/InfinispanContainer.java | 8 + .../InfinispanContainerBuilder.java | 8 + ...nfinispanContainerExposedPortsBuilder.java | 13 + .../InfinispanContainerFactory.java | 97 ++++++ .../InfinispanContainerStandaloneBuilder.java | 9 + src/main/resources/infinispan-standalone.xml | 286 ++++++++++++++++++ .../InfinispanContainerIntegrationTest.java | 34 +++ 8 files changed, 493 insertions(+) create mode 100644 pom.xml create mode 100644 src/main/java/de/rpr/testcontainers/infinispan/InfinispanContainer.java create mode 100644 src/main/java/de/rpr/testcontainers/infinispan/InfinispanContainerBuilder.java create mode 100644 src/main/java/de/rpr/testcontainers/infinispan/InfinispanContainerExposedPortsBuilder.java create mode 100644 src/main/java/de/rpr/testcontainers/infinispan/InfinispanContainerFactory.java create mode 100644 src/main/java/de/rpr/testcontainers/infinispan/InfinispanContainerStandaloneBuilder.java create mode 100644 src/main/resources/infinispan-standalone.xml create mode 100644 src/test/java/InfinispanContainerIntegrationTest.java diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..6a86fe6 --- /dev/null +++ b/pom.xml @@ -0,0 +1,38 @@ + + + 4.0.0 + + de.rpr + testcontainers-infinispan + 1.0.0-SNAPSHOT + + + 1.8 + 1.8 + UTF-8 + UTF-8 + + + 1.4.3 + + 9.1.3.Final + + + + + org.testcontainers + testcontainers + ${testcontainers.version} + + + org.infinispan + infinispan-client-hotrod + ${infinispan.version} + test + + + + + \ No newline at end of file diff --git a/src/main/java/de/rpr/testcontainers/infinispan/InfinispanContainer.java b/src/main/java/de/rpr/testcontainers/infinispan/InfinispanContainer.java new file mode 100644 index 0000000..4259127 --- /dev/null +++ b/src/main/java/de/rpr/testcontainers/infinispan/InfinispanContainer.java @@ -0,0 +1,8 @@ +package de.rpr.testcontainers.infinispan; + +import org.junit.rules.TestRule; +import org.testcontainers.containers.Container; + +public interface InfinispanContainer> extends Container, TestRule { + +} diff --git a/src/main/java/de/rpr/testcontainers/infinispan/InfinispanContainerBuilder.java b/src/main/java/de/rpr/testcontainers/infinispan/InfinispanContainerBuilder.java new file mode 100644 index 0000000..ad6e152 --- /dev/null +++ b/src/main/java/de/rpr/testcontainers/infinispan/InfinispanContainerBuilder.java @@ -0,0 +1,8 @@ +package de.rpr.testcontainers.infinispan; + +public interface InfinispanContainerBuilder { + + InfinispanContainerExposedPortsBuilder expose(); + + InfinispanContainer build(); +} diff --git a/src/main/java/de/rpr/testcontainers/infinispan/InfinispanContainerExposedPortsBuilder.java b/src/main/java/de/rpr/testcontainers/infinispan/InfinispanContainerExposedPortsBuilder.java new file mode 100644 index 0000000..798304a --- /dev/null +++ b/src/main/java/de/rpr/testcontainers/infinispan/InfinispanContainerExposedPortsBuilder.java @@ -0,0 +1,13 @@ +package de.rpr.testcontainers.infinispan; + +public interface InfinispanContainerExposedPortsBuilder { + + InfinispanContainerBuilder hotrod(); + + InfinispanContainerBuilder hotrod(int port); + + InfinispanContainerBuilder rest(); + + InfinispanContainerBuilder rest(int port); + +} diff --git a/src/main/java/de/rpr/testcontainers/infinispan/InfinispanContainerFactory.java b/src/main/java/de/rpr/testcontainers/infinispan/InfinispanContainerFactory.java new file mode 100644 index 0000000..715deaa --- /dev/null +++ b/src/main/java/de/rpr/testcontainers/infinispan/InfinispanContainerFactory.java @@ -0,0 +1,97 @@ +package de.rpr.testcontainers.infinispan; + +import org.testcontainers.containers.BindMode; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.wait.LogMessageWaitStrategy; + +import java.io.File; +import java.net.URL; +import java.util.HashMap; +import java.util.Map; + +public class InfinispanContainerFactory + extends GenericContainer + implements InfinispanContainer, InfinispanContainerExposedPortsBuilder, InfinispanContainerBuilder, + InfinispanContainerStandaloneBuilder { + + private static final String IMAGE_NAME = "jboss/infinispan-server:"; + + @Override + public InfinispanContainerBuilder configurationFile(final String classPathResourceName) { + URL resourceUrl = this.getClass().getClassLoader().getResource(classPathResourceName); + if (resourceUrl == null) { + throw new IllegalArgumentException("Cannot read configuration resource"); + } + return withFileSystemBind(new File(resourceUrl.getFile()).getAbsolutePath(), + "/opt/jboss/infinispan-server/standalone/configuration/standalone.xml", + BindMode.READ_ONLY); + } + + @Override + public InfinispanContainerBuilder and() { + return this; + } + + private enum Protocol { + HOTROD(11222), + REST(8080); + + private final int defaultPort; + + Protocol(final int defaultPort) { + this.defaultPort = defaultPort; + } + } + + private final Map exposedPorts = new HashMap<>(); + + private InfinispanContainerFactory(final String dockerImageName) { + super(dockerImageName); + } + + public static InfinispanContainerStandaloneBuilder standalone() { + return standalone("9.1.3.Final"); + } + + public static InfinispanContainerStandaloneBuilder standalone(final String version) { + InfinispanContainer container = new InfinispanContainerFactory(IMAGE_NAME + version); + container.withCommand("standalone"); + return (InfinispanContainerStandaloneBuilder) container; + } + + @Override + public InfinispanContainerExposedPortsBuilder expose() { + return this; + } + + @Override + public InfinispanContainerBuilder hotrod() { + return hotrod(Protocol.HOTROD.defaultPort); + } + + @Override + public InfinispanContainerBuilder hotrod(final int port) { + return exposePort(Protocol.HOTROD, port); + } + + @Override + public InfinispanContainerBuilder rest() { + return rest(Protocol.REST.defaultPort); + } + + @Override + public InfinispanContainerBuilder rest(final int port) { + return exposePort(Protocol.REST, port); + } + + private InfinispanContainerFactory exposePort(final Protocol service, final int port) { + exposedPorts.put(service, port); + return this; + } + + @Override + public InfinispanContainer build() { + return waitingFor(new LogMessageWaitStrategy().withRegEx(".*HotRodServer listening.*\\s")); + } + +} diff --git a/src/main/java/de/rpr/testcontainers/infinispan/InfinispanContainerStandaloneBuilder.java b/src/main/java/de/rpr/testcontainers/infinispan/InfinispanContainerStandaloneBuilder.java new file mode 100644 index 0000000..3004feb --- /dev/null +++ b/src/main/java/de/rpr/testcontainers/infinispan/InfinispanContainerStandaloneBuilder.java @@ -0,0 +1,9 @@ +package de.rpr.testcontainers.infinispan; + +public interface InfinispanContainerStandaloneBuilder { + + InfinispanContainerBuilder configurationFile(String classPathResourceName); + + InfinispanContainerBuilder and(); + +} diff --git a/src/main/resources/infinispan-standalone.xml b/src/main/resources/infinispan-standalone.xml new file mode 100644 index 0000000..a06ac37 --- /dev/null +++ b/src/main/resources/infinispan-standalone.xml @@ -0,0 +1,286 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE + h2 + + sa + sa + + + + + org.h2.jdbcx.JdbcDataSource + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/java/InfinispanContainerIntegrationTest.java b/src/test/java/InfinispanContainerIntegrationTest.java new file mode 100644 index 0000000..3a1c141 --- /dev/null +++ b/src/test/java/InfinispanContainerIntegrationTest.java @@ -0,0 +1,34 @@ +import de.rpr.testcontainers.infinispan.InfinispanContainer; +import de.rpr.testcontainers.infinispan.InfinispanContainerFactory; +import org.infinispan.client.hotrod.ProtocolVersion; +import org.infinispan.client.hotrod.RemoteCacheManager; +import org.infinispan.client.hotrod.configuration.ConfigurationBuilder; +import org.junit.ClassRule; +import org.junit.Test; + +import static org.junit.Assert.assertNotNull; + +public class InfinispanContainerIntegrationTest { + + @ClassRule + public static InfinispanContainer infinispan = InfinispanContainerFactory + .standalone("9.1.3.Final") + .configurationFile("infinispan-standalone.xml") + .expose() + .hotrod() + .build(); + + @Test + public void rule_should_have_mapped_hotrod_port() { + assertNotNull(infinispan.getMappedPort(11222)); + } + + @Test + public void should_get_existing_cache() { + RemoteCacheManager cacheManager = new RemoteCacheManager(new ConfigurationBuilder() + .addServers(infinispan.getContainerIpAddress() + ":" + infinispan.getMappedPort(11222)) + .version(ProtocolVersion.PROTOCOL_VERSION_26) + .build()); + assertNotNull(cacheManager.getCache("testCache")); + } +}