Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISPN-16317 Unit Test Remote Cache #209

Merged
merged 17 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions infinispan-remote/cache-admin-api/README.adoc

This file was deleted.

5 changes: 5 additions & 0 deletions infinispan-remote/cache-admin-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,10 @@
<groupId>org.infinispan</groupId>
<artifactId>infinispan-client-hotrod</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/**
* The InfinispanRemoteAdminCache class shows how to remotely create caches
* with Hot Rod Java clients using different approaches.
* By default caches are permanent and survive cluster restarts.
* By default, caches are permanent and survive cluster restarts.
* To create volatile, temporary caches use "withFlags(AdminFlag.VOLATILE)".
* Data in temporary caches is lost on full cluster restart.
*
Expand All @@ -22,59 +22,66 @@
* @author <a href="mailto:wfink@redhat.com">Wolf Dieter Fink</a>
*/
public class InfinispanRemoteAdminCache {
private final RemoteCacheManager manager = TutorialsConnectorHelper.connect();
public static final String SIMPLE_CACHE = "SimpleCache";
public static final String CACHE_WITH_XMLCONFIGURATION = "CacheWithXMLConfiguration";
public static final String CACHE_WITH_TEMPLATE = "CacheWithTemplate";
static RemoteCacheManager cacheManager = TutorialsConnectorHelper.connect();

public static void main(String[] args) throws Exception {
InfinispanRemoteAdminCache client = new InfinispanRemoteAdminCache();
connectToInfinispan();

client.createSimpleCache();
client.cacheWithTemplate();
client.createCacheWithXMLConfiguration();
createSimpleCache();
cacheWithTemplate();
createCacheWithXMLConfiguration();

// Stop the Hot Rod client and release all resources.
client.stop();
disconnect();
}

private void stop() {
TutorialsConnectorHelper.stop(manager);
static void connectToInfinispan() {
// Connect to the server
cacheManager = TutorialsConnectorHelper.connect();
}

static void disconnect() {
// Stop the cache manager and release all resources
TutorialsConnectorHelper.stop(cacheManager);
}

/**
* Creates a cache named CacheWithXMLConfiguration and uses the
* StringConfiguration() method to pass the cache definition as
* valid infinispan.xml.
*/
private void createCacheWithXMLConfiguration() throws IOException {
String cacheName = "CacheWithXMLConfiguration";
String xml = Files.readString(Paths.get(this.getClass().getClassLoader().getResource("CacheWithXMLConfiguration.xml").getPath()));
manager.administration().getOrCreateCache(cacheName, new StringConfiguration(xml));
static void createCacheWithXMLConfiguration() throws IOException {
String xml = Files.readString(Paths.get(InfinispanRemoteAdminCache.class.getClassLoader().getResource("CacheWithXMLConfiguration.xml").getPath()));
cacheManager.administration().getOrCreateCache(CACHE_WITH_XMLCONFIGURATION, new StringConfiguration(xml));
System.out.println("Cache with configuration exists or is created.");
}

/**
* Creates a cache named CacheWithTemplate from the org.infinispan.DIST_SYNC
* template.
*/
private void cacheWithTemplate() throws IOException {
Path path = Paths.get(this.getClass().getClassLoader().getResource("cacheTemplate.xml").getPath());
static void cacheWithTemplate() throws IOException {
Path path = Paths.get(InfinispanRemoteAdminCache.class.getClassLoader().getResource("cacheTemplate.xml").getPath());
String xmlTemplate = Files.readString(path);
try {
manager.administration().createTemplate("template", new StringConfiguration(xmlTemplate));
cacheManager.administration().createTemplate("template", new StringConfiguration(xmlTemplate));
} catch (Exception ce) {
// If the
System.out.println(ce.getMessage());
}

manager.administration().getOrCreateCache("CacheWithTemplate", "template");
cacheManager.administration().getOrCreateCache(CACHE_WITH_TEMPLATE, "template");
System.out.println("Cache created from default template.");
}

/**
* Creates a simple, local cache with no configuration.
*/
private void createSimpleCache() {
static void createSimpleCache() {
try {
manager.administration().createCache("SimpleCache", (String)null);
cacheManager.administration().createCache(SIMPLE_CACHE, (String)null);
System.out.println("SimpleCache created.");
} catch (Exception e) {
System.out.println("Expected to fail for multiple invocations as the cache exists message: " + e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.infinispan.tutorial.simple.remote.admin;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.io.IOException;

import static org.infinispan.tutorial.simple.remote.admin.InfinispanRemoteAdminCache.CACHE_WITH_TEMPLATE;
import static org.infinispan.tutorial.simple.remote.admin.InfinispanRemoteAdminCache.CACHE_WITH_XMLCONFIGURATION;
import static org.infinispan.tutorial.simple.remote.admin.InfinispanRemoteAdminCache.SIMPLE_CACHE;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class InfinispanRemoteAdminCacheTest {

@BeforeAll
public static void start() {
InfinispanRemoteAdminCache.connectToInfinispan();
}

@AfterAll
public static void stop() {
InfinispanRemoteAdminCache.disconnect();
}

@Test
public void testCacheManager() {
assertNotNull(InfinispanRemoteAdminCache.cacheManager);
}

@Test
public void testCreateSimpleCache() {
InfinispanRemoteAdminCache.createSimpleCache();
assertNotNull(InfinispanRemoteAdminCache.cacheManager.getCache(SIMPLE_CACHE));
}

@Test
public void testCreateWithTemplate() throws IOException {
InfinispanRemoteAdminCache.cacheWithTemplate();
assertNotNull(InfinispanRemoteAdminCache.cacheManager.getCache(CACHE_WITH_TEMPLATE));
}

@Test
public void createCacheWithXMLConfiguration() throws IOException {
InfinispanRemoteAdminCache.createCacheWithXMLConfiguration();
assertNotNull(InfinispanRemoteAdminCache.cacheManager.getCache(CACHE_WITH_XMLCONFIGURATION));
}
}
5 changes: 5 additions & 0 deletions infinispan-remote/cache/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,10 @@
<groupId>org.infinispan</groupId>
<artifactId>infinispan-client-hotrod</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,30 @@
*/
public class InfinispanRemoteCache {

static RemoteCacheManager cacheManager;
static RemoteCache<String, String> cache;

public static void main(String[] args) {
// Connect to the server
RemoteCacheManager cacheManager = TutorialsConnectorHelper.connect();
// Obtain the remote cache
RemoteCache<String, String> cache = cacheManager.getCache(TUTORIAL_CACHE_NAME);
/// Store a value
connectToInfinispan();
manipulateCache();
disconnect();
}

static void manipulateCache() {
// Store a value
cache.put("key", "value");
// Retrieve the value and print it out
System.out.printf("key = %s\n", cache.get("key"));
}

static void connectToInfinispan() {
// Connect to the server
cacheManager = TutorialsConnectorHelper.connect();
// Obtain the remote cache
cache = cacheManager.getCache(TUTORIAL_CACHE_NAME);
}

static void disconnect() {
// Stop the cache manager and release all resources
TutorialsConnectorHelper.stop(cacheManager);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.infinispan.tutorial.simple.remote;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class InfinispanRemoteCacheTest {

@BeforeAll
public static void start() {
InfinispanRemoteCache.connectToInfinispan();
}

@AfterAll
public static void stop() {
InfinispanRemoteCache.disconnect();
}

@Test
public void testRemoteCache() {
assertNotNull(InfinispanRemoteCache.cache);

InfinispanRemoteCache.manipulateCache();

assertEquals("value", InfinispanRemoteCache.cache.get("key"));
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.infinispan.tutorial.simple.connect;

import org.infinispan.client.hotrod.RemoteCache;
import org.infinispan.client.hotrod.RemoteCacheManager;
import org.infinispan.client.hotrod.configuration.ClientIntelligence;
import org.infinispan.client.hotrod.configuration.ConfigurationBuilder;
Expand Down Expand Up @@ -67,13 +68,11 @@ public static final RemoteCacheManager connect() {
public static InfinispanContainer INFINISPAN_CONTAINER;

public static final RemoteCacheManager connect(ConfigurationBuilder builder) {

RemoteCacheManager cacheManager = null;
try {
builder.addServer().host(HOST).port(SINGLE_PORT);
cacheManager = new RemoteCacheManager(builder.build());
// Clear the cache in case it already exists from a previous running tutorial
cacheManager.getCache(TUTORIAL_CACHE_NAME).clear();
//ping
System.out.println("Get cache names: " + cacheManager.getCacheNames());
} catch (Exception ex) {
System.out.println("Unable to connect to a running server in localhost:11222. Try test containers");
if (cacheManager != null) {
Expand All @@ -85,26 +84,39 @@ public static final RemoteCacheManager connect(ConfigurationBuilder builder) {
if (cacheManager == null) {
try {
startInfinispanContainer();
builder.addServer().host(HOST).port(INFINISPAN_CONTAINER.getFirstMappedPort());
builder.addServer().host(HOST).port(INFINISPAN_CONTAINER.getMappedPort(SINGLE_PORT));
cacheManager = new RemoteCacheManager(builder.build());
// Clear the cache in case it already exists from a previous running tutorial
cacheManager.getCache(TUTORIAL_CACHE_NAME).clear();
//ping
System.out.println("Get cache names: " + cacheManager.getCacheNames());
} catch (Exception ex) {
System.out.println("Infinispan Server start with Testcontainers failed. Exit");
System.exit(0);
}
}
if (cacheManager != null) {
// Clear the cache in case it already exists from a previous running tutorial
RemoteCache<Object, Object> testCache = cacheManager.getCache(TUTORIAL_CACHE_NAME);
if (testCache != null) {
testCache.clear();
} else {
System.out.println("Test cache does not exist");
}
}
// Return the connected cache manager
return cacheManager;
}

public static InfinispanContainer startInfinispanContainer() {
return startInfinispanContainer(1000);
}

public static InfinispanContainer startInfinispanContainer(long millis) {
try {
INFINISPAN_CONTAINER = new InfinispanContainer();
INFINISPAN_CONTAINER.withUser(USER);
INFINISPAN_CONTAINER.withPassword(PASSWORD);
INFINISPAN_CONTAINER.start();
Thread.sleep(3000);
Thread.sleep(millis);
} catch (Exception ex) {
System.out.println("Unable to start Infinispan container");
return null;
Expand All @@ -127,8 +139,10 @@ public static void stopInfinispanContainer() {
}

public static void stop(RemoteCacheManager cacheManager) {
cacheManager.stop();
stopInfinispanContainer();
if (cacheManager != null){
cacheManager.stop();
stopInfinispanContainer();
}
}

}
5 changes: 5 additions & 0 deletions infinispan-remote/continuous-query/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@
<artifactId>protostream-processor</artifactId>
<version>${version.protostream}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>


Expand Down
Loading