diff --git a/README.md b/README.md index cd6b4a2..856bd75 100644 --- a/README.md +++ b/README.md @@ -9,14 +9,17 @@ At the moment this is a rudimentary implementation and by no means complete. Fee # Usage -For general usage info on Testcontainers please look at the examples of the project. +Here's simple example how you can use the `InfinispanContainer`. ``` @ClassRule -public static InfinispanContainer infinispan = InfinispanContainerFactory - .standalone("9.1.3.Final") - .configurationFile("infinispan-standalone.xml") - .expose() - .hotrod() - .build(); -``` \ No newline at end of file +public static InfinispanContainer infinispan = new InfinispanContainer() + .withProtocolVersion(ProtocolVersion.PROTOCOL_VERSION_26) + .withCaches("testCache"); +``` +If you want, you can retrieve a `RemoteCacheManager` from the container: +``` +infinispan.getCacheManager() +``` + +For general usage info on Testcontainers please look at the examples of the project. diff --git a/pom.xml b/pom.xml index 6a86fe6..2b8ae71 100644 --- a/pom.xml +++ b/pom.xml @@ -30,7 +30,6 @@ org.infinispan infinispan-client-hotrod ${infinispan.version} - test diff --git a/src/main/java/de/rpr/testcontainers/infinispan/InfinispanContainer.java b/src/main/java/de/rpr/testcontainers/infinispan/InfinispanContainer.java index 4259127..76a18bd 100644 --- a/src/main/java/de/rpr/testcontainers/infinispan/InfinispanContainer.java +++ b/src/main/java/de/rpr/testcontainers/infinispan/InfinispanContainer.java @@ -1,8 +1,94 @@ package de.rpr.testcontainers.infinispan; -import org.junit.rules.TestRule; -import org.testcontainers.containers.Container; +import com.github.dockerjava.api.command.InspectContainerResponse; +import org.infinispan.client.hotrod.ProtocolVersion; +import org.infinispan.client.hotrod.RemoteCacheManager; +import org.infinispan.client.hotrod.configuration.ConfigurationBuilder; +import org.jetbrains.annotations.NotNull; +import org.junit.runner.Description; +import org.testcontainers.containers.BindMode; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.wait.LogMessageWaitStrategy; -public interface InfinispanContainer> extends Container, TestRule { +import java.io.File; +import java.net.URL; +import java.time.Duration; +import java.util.*; +@SuppressWarnings("ALL") +public class InfinispanContainer extends GenericContainer { + + private static final String IMAGE_NAME = "jboss/infinispan-server"; + + private static Set incompatibleProtocolVersions = new HashSet<>(); + + static { + incompatibleProtocolVersions.add(ProtocolVersion.PROTOCOL_VERSION_10); + incompatibleProtocolVersions.add(ProtocolVersion.PROTOCOL_VERSION_11); + incompatibleProtocolVersions.add(ProtocolVersion.PROTOCOL_VERSION_12); + incompatibleProtocolVersions.add(ProtocolVersion.PROTOCOL_VERSION_13); + } + + private ProtocolVersion protocolVersion; + private Collection cacheNames; + + private RemoteCacheManager cacheManager; + + public InfinispanContainer() { + this("latest"); + } + + public InfinispanContainer(final String version) { + super(IMAGE_NAME + ":" + version); + withStartupTimeout(Duration.ofMillis(20000)); + withCommand("standalone"); + waitingFor(new LogMessageWaitStrategy().withRegEx(".*Infinispan Server.*started in.*\\s")); + } + + public InfinispanContainer withProtocolVersion(final ProtocolVersion protocolVersion) { + if (incompatibleProtocolVersions.contains(protocolVersion)) { + throw new IllegalArgumentException("You have to use a Hotrod protocol version of 2.0 at least. 1.x can't create caches through the API."); + } + this.protocolVersion = protocolVersion; + return this; + } + + public InfinispanContainer withCaches(final Collection cacheNames) { + this.cacheNames = cacheNames; + return this; + } + + public InfinispanContainer withCaches(final String... cacheNames) { + return withCaches(Arrays.asList(cacheNames)); + } + + @Override + protected void containerIsStarted(final InspectContainerResponse containerInfo) { + cacheManager = new RemoteCacheManager(new ConfigurationBuilder() + .addServers(getServerAddress()) + .version(getProtocolVersion()) + .build()); + + this.cacheNames.forEach(cacheName -> { + cacheManager.administration().createCache(cacheName, null); + }); + } + + @Override + protected void finished(final Description description) { + cacheManager.stop(); + super.finished(description); + } + + private ProtocolVersion getProtocolVersion() { + return protocolVersion != null ? protocolVersion : ProtocolVersion.PROTOCOL_VERSION_26; + } + + public String getServerAddress() { + return getContainerIpAddress() + ":" + getMappedPort(11222); + } + + public RemoteCacheManager getCacheManager() { + return cacheManager; + } } diff --git a/src/main/java/de/rpr/testcontainers/infinispan/InfinispanContainerBuilder.java b/src/main/java/de/rpr/testcontainers/infinispan/InfinispanContainerBuilder.java deleted file mode 100644 index ad6e152..0000000 --- a/src/main/java/de/rpr/testcontainers/infinispan/InfinispanContainerBuilder.java +++ /dev/null @@ -1,8 +0,0 @@ -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 deleted file mode 100644 index 798304a..0000000 --- a/src/main/java/de/rpr/testcontainers/infinispan/InfinispanContainerExposedPortsBuilder.java +++ /dev/null @@ -1,13 +0,0 @@ -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 deleted file mode 100644 index 53ee48b..0000000 --- a/src/main/java/de/rpr/testcontainers/infinispan/InfinispanContainerFactory.java +++ /dev/null @@ -1,97 +0,0 @@ -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(".*Infinispan Server.*started in.*\\s")); - } - -} diff --git a/src/main/java/de/rpr/testcontainers/infinispan/InfinispanContainerStandaloneBuilder.java b/src/main/java/de/rpr/testcontainers/infinispan/InfinispanContainerStandaloneBuilder.java deleted file mode 100644 index 3004feb..0000000 --- a/src/main/java/de/rpr/testcontainers/infinispan/InfinispanContainerStandaloneBuilder.java +++ /dev/null @@ -1,9 +0,0 @@ -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 deleted file mode 100644 index a06ac37..0000000 --- a/src/main/resources/infinispan-standalone.xml +++ /dev/null @@ -1,286 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 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/GenericInfinispanContainerIntegrationTest.java b/src/test/java/GenericInfinispanContainerIntegrationTest.java new file mode 100644 index 0000000..b11b2b5 --- /dev/null +++ b/src/test/java/GenericInfinispanContainerIntegrationTest.java @@ -0,0 +1,58 @@ +import org.infinispan.client.hotrod.ProtocolVersion; +import org.infinispan.client.hotrod.RemoteCache; +import org.infinispan.client.hotrod.RemoteCacheManager; +import org.infinispan.client.hotrod.configuration.ConfigurationBuilder; +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Test; +import org.testcontainers.containers.BindMode; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.wait.LogMessageWaitStrategy; + +import java.time.Duration; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; + +import static org.junit.Assert.assertNotNull; + +public class GenericInfinispanContainerIntegrationTest { + + ExecutorService executorService = Executors.newCachedThreadPool(); + RemoteCacheManager cacheManager; + + @ClassRule + public static GenericContainer container = + new GenericContainer("jboss/infinispan-server:9.1.3.Final") + .waitingFor(new LogMessageWaitStrategy().withRegEx(".*Infinispan Server.*started in.*\\s")) + .withStartupTimeout(Duration.ofMillis(20000)) + .withClasspathResourceMapping( + "infinispan-standalone.xml", + "/opt/jboss/infinispan-server/standalone/configuration/standalone.xml", + BindMode.READ_ONLY) + .withCommand("standalone"); + + @Before + public void setup() { + cacheManager = new RemoteCacheManager(new ConfigurationBuilder() + .addServers(getServerAddress()) + .version(ProtocolVersion.PROTOCOL_VERSION_26) + .build()); + } + + @Test + public void should_be_able_to_retrieve_a_cache() throws Exception { + Future> result = executorService.submit(() -> cacheManager.getCache()); + assertNotNull(result.get(1500, TimeUnit.MILLISECONDS)); + } + + @Test + public void should_be_able_to_retrieve_a_configured_cache() { + assertNotNull(cacheManager.getCache("testCache")); + } + + private String getServerAddress() { + return container.getContainerIpAddress() + ":" + container.getMappedPort(11222); + } +} diff --git a/src/test/java/InfinispanContainerIntegrationTest.java b/src/test/java/InfinispanContainerIntegrationTest.java index d1c1c15..5e3b8a7 100644 --- a/src/test/java/InfinispanContainerIntegrationTest.java +++ b/src/test/java/InfinispanContainerIntegrationTest.java @@ -1,8 +1,5 @@ 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; @@ -12,12 +9,9 @@ public class InfinispanContainerIntegrationTest { @ClassRule public static InfinispanContainer infinispan = - InfinispanContainerFactory - .standalone("9.1.3.Final") - .configurationFile("infinispan-standalone.xml") - .expose() - .hotrod() - .build(); + new InfinispanContainer("9.1.3.Final") + .withProtocolVersion(ProtocolVersion.PROTOCOL_VERSION_26) + .withCaches("testCache"); @Test public void rule_should_have_mapped_hotrod_port() { @@ -26,10 +20,6 @@ public class InfinispanContainerIntegrationTest { @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")); + assertNotNull(infinispan.getCacheManager().getCache("testCache")); } }