From 37ae3a402adb6b2598bc1db283619d0700ff7e88 Mon Sep 17 00:00:00 2001 From: Katia Aresti Date: Tue, 23 Jul 2024 14:55:29 +0200 Subject: [PATCH] ISPN-16317 Unit Test Remote Cache --- infinispan-remote/cache/pom.xml | 5 +++ .../simple/remote/InfinispanRemoteCache.java | 25 ++++++++++++--- .../remote/InfinispanRemoteCacheTest.java | 31 +++++++++++++++++++ .../connect/TutorialsConnectorHelper.java | 6 ++-- 4 files changed, 60 insertions(+), 7 deletions(-) create mode 100644 infinispan-remote/cache/src/test/java/org/infinispan/tutorial/simple/remote/InfinispanRemoteCacheTest.java diff --git a/infinispan-remote/cache/pom.xml b/infinispan-remote/cache/pom.xml index 2d8547c7..0d339402 100644 --- a/infinispan-remote/cache/pom.xml +++ b/infinispan-remote/cache/pom.xml @@ -46,5 +46,10 @@ org.infinispan infinispan-client-hotrod + + org.junit.jupiter + junit-jupiter + test + diff --git a/infinispan-remote/cache/src/main/java/org/infinispan/tutorial/simple/remote/InfinispanRemoteCache.java b/infinispan-remote/cache/src/main/java/org/infinispan/tutorial/simple/remote/InfinispanRemoteCache.java index f85bada3..e0b836cb 100644 --- a/infinispan-remote/cache/src/main/java/org/infinispan/tutorial/simple/remote/InfinispanRemoteCache.java +++ b/infinispan-remote/cache/src/main/java/org/infinispan/tutorial/simple/remote/InfinispanRemoteCache.java @@ -14,15 +14,30 @@ */ public class InfinispanRemoteCache { + static RemoteCacheManager cacheManager; + static RemoteCache cache; + public static void main(String[] args) { - // Connect to the server - RemoteCacheManager cacheManager = TutorialsConnectorHelper.connect(); - // Obtain the remote cache - RemoteCache cache = cacheManager.getCache(TUTORIAL_CACHE_NAME); - /// Store a value + connectToInfinispan(); + manipulateCache(); + deconnect(); + } + + 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 deconnect() { // Stop the cache manager and release all resources TutorialsConnectorHelper.stop(cacheManager); } diff --git a/infinispan-remote/cache/src/test/java/org/infinispan/tutorial/simple/remote/InfinispanRemoteCacheTest.java b/infinispan-remote/cache/src/test/java/org/infinispan/tutorial/simple/remote/InfinispanRemoteCacheTest.java new file mode 100644 index 00000000..074833f3 --- /dev/null +++ b/infinispan-remote/cache/src/test/java/org/infinispan/tutorial/simple/remote/InfinispanRemoteCacheTest.java @@ -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.deconnect(); + } + + @Test + public void testRemoteCache() { + assertNotNull(InfinispanRemoteCache.cache); + + InfinispanRemoteCache.manipulateCache(); + + assertEquals("value", InfinispanRemoteCache.cache.get("key")); + } + +} \ No newline at end of file diff --git a/infinispan-remote/connect-to-infinispan-server/src/main/java/org/infinispan/tutorial/simple/connect/TutorialsConnectorHelper.java b/infinispan-remote/connect-to-infinispan-server/src/main/java/org/infinispan/tutorial/simple/connect/TutorialsConnectorHelper.java index 031e0f4d..75e6c9f1 100644 --- a/infinispan-remote/connect-to-infinispan-server/src/main/java/org/infinispan/tutorial/simple/connect/TutorialsConnectorHelper.java +++ b/infinispan-remote/connect-to-infinispan-server/src/main/java/org/infinispan/tutorial/simple/connect/TutorialsConnectorHelper.java @@ -127,8 +127,10 @@ public static void stopInfinispanContainer() { } public static void stop(RemoteCacheManager cacheManager) { - cacheManager.stop(); - stopInfinispanContainer(); + if (cacheManager != null){ + cacheManager.stop(); + stopInfinispanContainer(); + } } }