From eb6893abe4f060f1d4d7d034d60ef4c8c590a047 Mon Sep 17 00:00:00 2001 From: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> Date: Sat, 23 Sep 2023 18:30:48 +0600 Subject: [PATCH] Polish Triggers and functions tests (#3554) Polish #3531 tests --- .../jedis/gears/resps/GearsLibraryInfo.java | 4 +- .../jedis/modules/gears/GearsTest.java | 169 +++++++++--------- 2 files changed, 85 insertions(+), 88 deletions(-) diff --git a/src/main/java/redis/clients/jedis/gears/resps/GearsLibraryInfo.java b/src/main/java/redis/clients/jedis/gears/resps/GearsLibraryInfo.java index 77f0351336..5ed6515ff6 100644 --- a/src/main/java/redis/clients/jedis/gears/resps/GearsLibraryInfo.java +++ b/src/main/java/redis/clients/jedis/gears/resps/GearsLibraryInfo.java @@ -99,7 +99,7 @@ public GearsLibraryInfo build(Object data) { String apiVersion = null; List clusterFunctions = Collections.emptyList(); - String code = ""; + String code = null; String configuration = null; String engine = null; List functions = Collections.emptyList(); @@ -156,7 +156,7 @@ public GearsLibraryInfo build(Object data) { int offset = withCode ? 2 : 0; apiVersion = STRING.build(list.get(1)); clusterFunctions = STRING_LIST.build(list.get(3)); - code = withCode ? STRING.build(list.get(5)) : ""; + code = withCode ? STRING.build(list.get(5)) : null; configuration = STRING.build(list.get(5 + offset)); engine = STRING.build(list.get(7 + offset)); functions = FUNCTION_INFO_LIST.build(list.get(9 + offset)); diff --git a/src/test/java/redis/clients/jedis/modules/gears/GearsTest.java b/src/test/java/redis/clients/jedis/modules/gears/GearsTest.java index f828d1129f..d30b282b30 100644 --- a/src/test/java/redis/clients/jedis/modules/gears/GearsTest.java +++ b/src/test/java/redis/clients/jedis/modules/gears/GearsTest.java @@ -1,9 +1,12 @@ package redis.clients.jedis.modules.gears; +import org.hamcrest.Matchers; import org.junit.After; +import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; +import redis.clients.jedis.RedisProtocol; import redis.clients.jedis.exceptions.JedisDataException; import redis.clients.jedis.gears.TFunctionListParams; import redis.clients.jedis.gears.TFunctionLoadParams; @@ -20,12 +23,19 @@ import java.util.stream.Collectors; import java.util.stream.Stream; -import static org.junit.Assert.*; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; public class GearsTest extends RedisModuleCommandsTestBase { + private static final String BAD_FUNCTION = "All Your Base Are Belong to Us"; - private static final int NUMBER_OF_LIBS = 6; - private static final List LOADED_LIBS = Arrays.asList("streamTriggers", "withFlags", "pingpong", "keyspaceTriggers", "hashitout", "withConfig"); + + private static final String[] LIBRARIES_ARRAY = new String[]{ + "streamTriggers", "withFlags", "pingpong", "keyspaceTriggers", "hashitout", "withConfig"}; @BeforeClass public static void prepare() { @@ -49,16 +59,14 @@ public void testFunctionLoad() throws IOException { client.tFunctionLoad(readLibrary("pingpong.js")); List libraries = client.tFunctionList(); - assertTrue(libraries.stream().map(GearsLibraryInfo::getName).collect(Collectors.toList()).contains("pingpong")); + assertEquals(Collections.singletonList("pingpong"), + libraries.stream().map(GearsLibraryInfo::getName).collect(Collectors.toList())); } @Test(expected = JedisDataException.class) public void testFunctionLoadAlreadyLoadedFails() throws IOException { client.tFunctionLoad(readLibrary("pingpong.js")); client.tFunctionLoad(readLibrary("pingpong.js")); - - List libraries = client.tFunctionList(); - assertTrue(libraries.stream().map(GearsLibraryInfo::getName).collect(Collectors.toList()).contains("pingpong")); } @Test @@ -67,7 +75,8 @@ public void testFunctionLoadWithReplace() throws IOException { client.tFunctionLoad(readLibrary("pingpong.js"), TFunctionLoadParams.loadParams().replace()); List libraries = client.tFunctionList(); - assertTrue(libraries.stream().map(GearsLibraryInfo::getName).collect(Collectors.toList()).contains("pingpong")); + assertEquals(Collections.singletonList("pingpong"), + libraries.stream().map(GearsLibraryInfo::getName).collect(Collectors.toList())); } @Test(expected = JedisDataException.class) @@ -75,14 +84,37 @@ public void testBadFunctionLoad() { client.tFunctionLoad(BAD_FUNCTION); } + private static void assertAllLibrariesNoCode(List libraryInfos) { + assertThat(libraryInfos, Matchers.hasSize(LIBRARIES_ARRAY.length)); + + List libraryNames = libraryInfos.stream().map(GearsLibraryInfo::getName).collect(Collectors.toList()); + assertThat(libraryNames, Matchers.containsInAnyOrder(LIBRARIES_ARRAY)); + + libraryInfos.stream().map(GearsLibraryInfo::getCode).forEach(Assert::assertNull); + } + + private static void assertAllLibrariesWithCode(List libraryInfos) { + assertThat(libraryInfos, Matchers.hasSize(LIBRARIES_ARRAY.length)); + + List libraryNames = libraryInfos.stream().map(GearsLibraryInfo::getName).collect(Collectors.toList()); + assertThat(libraryNames, Matchers.containsInAnyOrder(LIBRARIES_ARRAY)); + + libraryInfos.stream().map(GearsLibraryInfo::getCode).forEach(Assert::assertNotNull); + } + @Test - public void testFunctionListNoCodeVerboseZero() throws IOException { + public void tFunctionListAll() throws IOException { loadAllLibraries(); + List libraryInfos = client.tFunctionList(); - assertEquals(NUMBER_OF_LIBS, libraryInfos.size()); + assertAllLibrariesNoCode(libraryInfos); + } - List libraryNames = libraryInfos.stream().map(GearsLibraryInfo::getName).collect(Collectors.toList()); - assertTrue(libraryNames.containsAll(LOADED_LIBS)); + @Test + public void testFunctionListNoCodeVerboseZero() throws IOException { + loadAllLibraries(); + List libraryInfos = client.tFunctionList(TFunctionListParams.listParams().verbose(0)); + assertAllLibrariesNoCode(libraryInfos); Map>> libraryConditions = initializeTestLibraryConditions(); @@ -105,10 +137,7 @@ public void testFunctionListNoCodeVerboseZero() throws IOException { public void testFunctionListNoCodeVerboseOne() throws IOException { loadAllLibraries(); List libraryInfos = client.tFunctionList(TFunctionListParams.listParams().verbose(1)); - assertEquals(NUMBER_OF_LIBS, libraryInfos.size()); - - List libraryNames = libraryInfos.stream().map(GearsLibraryInfo::getName).collect(Collectors.toList()); - assertTrue(libraryNames.containsAll(LOADED_LIBS)); + assertAllLibrariesNoCode(libraryInfos); Map>> libraryConditions = initializeTestLibraryConditions(); @@ -133,10 +162,7 @@ public void testFunctionListNoCodeVerboseOne() throws IOException { public void testFunctionListNoCodeVerboseTwo() throws IOException { loadAllLibraries(); List libraryInfos = client.tFunctionList(TFunctionListParams.listParams().verbose(2)); - assertEquals(NUMBER_OF_LIBS, libraryInfos.size()); - - List libraryNames = libraryInfos.stream().map(GearsLibraryInfo::getName).collect(Collectors.toList()); - assertTrue(libraryNames.containsAll(LOADED_LIBS)); + assertAllLibrariesNoCode(libraryInfos); Map>> libraryConditions = initializeTestLibraryConditions(); @@ -161,10 +187,7 @@ public void testFunctionListNoCodeVerboseTwo() throws IOException { public void testFunctionListNoCodeVerboseThree() throws IOException { loadAllLibraries(); List libraryInfos = client.tFunctionList(TFunctionListParams.listParams().verbose(3)); - assertEquals(NUMBER_OF_LIBS, libraryInfos.size()); - - List libraryNames = libraryInfos.stream().map(GearsLibraryInfo::getName).collect(Collectors.toList()); - assertTrue(libraryNames.containsAll(LOADED_LIBS)); + assertAllLibrariesNoCode(libraryInfos); Map>> libraryConditions = initializeTestLibraryConditions(); @@ -189,13 +212,7 @@ public void testFunctionListNoCodeVerboseThree() throws IOException { public void testFunctionListWithCodeVerboseZero() throws IOException { loadAllLibraries(); List libraryInfos = client.tFunctionList(TFunctionListParams.listParams().withCode().verbose(0)); - assertEquals(NUMBER_OF_LIBS, libraryInfos.size()); - - List libraryNames = libraryInfos.stream().map(GearsLibraryInfo::getName).collect(Collectors.toList()); - assertTrue(libraryNames.containsAll(LOADED_LIBS)); - - List sources = libraryInfos.stream().map(GearsLibraryInfo::getCode).collect(Collectors.toList()); - assertTrue(sources.stream().allMatch(Objects::nonNull)); + assertAllLibrariesWithCode(libraryInfos); Map>> libraryConditions = initializeTestLibraryConditions(); @@ -218,14 +235,7 @@ public void testFunctionListWithCodeVerboseZero() throws IOException { public void testFunctionListWithCodeVerboseOne() throws IOException { loadAllLibraries(); List libraryInfos = client.tFunctionList(TFunctionListParams.listParams().withCode().verbose(1)); - - assertEquals(NUMBER_OF_LIBS, libraryInfos.size()); - - List libraryNames = libraryInfos.stream().map(GearsLibraryInfo::getName).collect(Collectors.toList()); - assertTrue(libraryNames.containsAll(LOADED_LIBS)); - - List sources = libraryInfos.stream().map(GearsLibraryInfo::getCode).collect(Collectors.toList()); - assertTrue(sources.stream().allMatch(Objects::nonNull)); + assertAllLibrariesWithCode(libraryInfos); Map>> libraryConditions = initializeTestLibraryConditions(); @@ -250,13 +260,7 @@ public void testFunctionListWithCodeVerboseOne() throws IOException { public void testFunctionListWithCodeVerboseTwo() throws IOException { loadAllLibraries(); List libraryInfos = client.tFunctionList(TFunctionListParams.listParams().withCode().verbose(2)); - assertEquals(NUMBER_OF_LIBS, libraryInfos.size()); - - List libraryNames = libraryInfos.stream().map(GearsLibraryInfo::getName).collect(Collectors.toList()); - assertTrue(libraryNames.containsAll(LOADED_LIBS)); - - List sources = libraryInfos.stream().map(GearsLibraryInfo::getCode).collect(Collectors.toList()); - assertTrue(sources.stream().allMatch(Objects::nonNull)); + assertAllLibrariesWithCode(libraryInfos); Map>> libraryConditions = initializeTestLibraryConditions(); @@ -281,13 +285,7 @@ public void testFunctionListWithCodeVerboseTwo() throws IOException { public void testFunctionListWithCodeVerboseThree() throws IOException { loadAllLibraries(); List libraryInfos = client.tFunctionList(TFunctionListParams.listParams().withCode().verbose(3)); - assertEquals(NUMBER_OF_LIBS, libraryInfos.size()); - - List libraryNames = libraryInfos.stream().map(GearsLibraryInfo::getName).collect(Collectors.toList()); - assertTrue(libraryNames.containsAll(LOADED_LIBS)); - - List sources = libraryInfos.stream().map(GearsLibraryInfo::getCode).collect(Collectors.toList()); - assertTrue(sources.stream().allMatch(Objects::nonNull)); + assertAllLibrariesWithCode(libraryInfos); Map>> libraryConditions = initializeTestLibraryConditions(); @@ -315,7 +313,7 @@ public void testFunctionLibraryListNoCodeVerboseZero() throws IOException { assertEquals(1, libraryInfos.size()); assertEquals("pingpong", libraryInfos.get(0).getName()); assertNull(libraryInfos.get(0).getFunctions().get(0).getDescription()); - assertTrue(libraryInfos.get(0).getCode().isEmpty()); + assertNull(libraryInfos.get(0).getCode()); } @Test @@ -326,7 +324,7 @@ public void testFunctionLibraryListNoCodeVerboseOne() throws IOException { assertEquals(1, libraryInfos.size()); assertEquals("pingpong", libraryInfos.get(0).getName()); assertEquals("You PING, we PONG", libraryInfos.get(0).getFunctions().get(0).getDescription()); - assertTrue(libraryInfos.get(0).getCode().isEmpty()); + assertNull(libraryInfos.get(0).getCode()); } @Test @@ -336,7 +334,7 @@ public void testFunctionLibraryListNoCodeVerboseTwo() throws IOException { assertEquals(1, libraryInfos.size()); assertEquals("pingpong", libraryInfos.get(0).getName()); assertEquals("You PING, we PONG", libraryInfos.get(0).getFunctions().get(0).getDescription()); - assertTrue(libraryInfos.get(0).getCode().isEmpty()); + assertNull(libraryInfos.get(0).getCode()); } @Test @@ -346,7 +344,7 @@ public void testFunctionLibraryListNoCodeVerboseThree() throws IOException { assertEquals(1, libraryInfos.size()); assertEquals("pingpong", libraryInfos.get(0).getName()); assertEquals("You PING, we PONG", libraryInfos.get(0).getFunctions().get(0).getDescription()); - assertTrue(libraryInfos.get(0).getCode().isEmpty()); + assertNull(libraryInfos.get(0).getCode()); } @Test @@ -356,7 +354,7 @@ public void testFunctionLibraryListWithCodeVerboseZero() throws IOException { assertEquals(1, libraryInfos.size()); assertEquals("pingpong", libraryInfos.get(0).getName()); assertNull(libraryInfos.get(0).getFunctions().get(0).getDescription()); - assertFalse(libraryInfos.get(0).getCode().isEmpty()); + assertNotNull(libraryInfos.get(0).getCode()); } @Test @@ -366,7 +364,7 @@ public void testFunctionLibraryListWithCodeVerboseOne() throws IOException { assertEquals(1, libraryInfos.size()); assertEquals("pingpong", libraryInfos.get(0).getName()); assertEquals("You PING, we PONG", libraryInfos.get(0).getFunctions().get(0).getDescription()); - assertFalse(libraryInfos.get(0).getCode().isEmpty()); + assertNotNull(libraryInfos.get(0).getCode()); } @Test @@ -376,7 +374,7 @@ public void testFunctionLibraryListWithCodeVerboseTwo() throws IOException { assertEquals(1, libraryInfos.size()); assertEquals("pingpong", libraryInfos.get(0).getName()); assertEquals("You PING, we PONG", libraryInfos.get(0).getFunctions().get(0).getDescription()); - assertFalse(libraryInfos.get(0).getCode().isEmpty()); + assertNotNull(libraryInfos.get(0).getCode()); } @Test @@ -386,23 +384,22 @@ public void testFunctionLibraryListWithCodeVerboseThree() throws IOException { assertEquals(1, libraryInfos.size()); assertEquals("pingpong", libraryInfos.get(0).getName()); assertEquals("You PING, we PONG", libraryInfos.get(0).getFunctions().get(0).getDescription()); - assertFalse(libraryInfos.get(0).getCode().isEmpty()); + assertNotNull(libraryInfos.get(0).getCode()); } @Test public void testLibraryDelete() throws IOException { loadAllLibraries(); - Object result = client.tFunctionDelete("pingpong"); - assertEquals("OK", result); + assertEquals("OK", client.tFunctionDelete("pingpong")); List libraryInfos = client.tFunctionList(); - assertEquals(NUMBER_OF_LIBS - 1, libraryInfos.size()); + assertEquals(LIBRARIES_ARRAY.length - 1, libraryInfos.size()); } @Test public void testLibraryCallStringResult() throws IOException { loadAllLibraries(); Object result = client.tFunctionCall("pingpong", "playPingPong", Collections.emptyList(), - Collections.emptyList()); + Collections.emptyList()); assertEquals(String.class, result.getClass()); assertEquals("PONG", result); } @@ -411,7 +408,7 @@ public void testLibraryCallStringResult() throws IOException { public void testLibraryCallSetValueResult() throws IOException { loadAllLibraries(); Object result = client.tFunctionCall("withFlags", "my_set", Collections.singletonList("MY_KEY"), - Collections.singletonList("MY_VALUE")); + Collections.singletonList("MY_VALUE")); assertEquals(String.class, result.getClass()); assertEquals("OK", result); assertEquals("MY_VALUE", client.get("MY_KEY")); @@ -420,7 +417,7 @@ public void testLibraryCallSetValueResult() throws IOException { @Test public void testLibraryCallHashResult() throws IOException { loadAllLibraries(); - Map payload = new HashMap<>(); + Map payload = new HashMap<>(); payload.put("C", "Dennis Ritchie"); payload.put("Python", "Guido van Rossum"); payload.put("C++", "Bjarne Stroustrup"); @@ -431,39 +428,39 @@ public void testLibraryCallHashResult() throws IOException { client.hmset("hash1", payload); Object result = client.tFunctionCall("hashitout", "hashy", Collections.singletonList("hash1"), - Collections.emptyList()); + Collections.emptyList()); assertEquals(ArrayList.class, result.getClass()); - List list = (List)result; + List list = (List) result; assertFalse(list.isEmpty()); boolean isResp3 = list.get(0) instanceof KeyValue; assertEquals(isResp3 ? 7 : 14, list.size()); - if (!isResp3) { - List asList = (List)result; + if (protocol != RedisProtocol.RESP3) { + List asList = (List) result; int indexOfJava = asList.indexOf("Java"); assertTrue(indexOfJava >= 0); - assertEquals("James Gosling", asList.get(indexOfJava+1)); + assertEquals("James Gosling", asList.get(indexOfJava + 1)); int indexOfJavaScript = asList.indexOf("JavaScript"); assertTrue(indexOfJavaScript >= 0); - assertEquals("Brendan Eich", asList.get(indexOfJavaScript+1)); + assertEquals("Brendan Eich", asList.get(indexOfJavaScript + 1)); int indexOfC = asList.indexOf("C"); assertTrue(indexOfC >= 0); - assertEquals("Dennis Ritchie", asList.get(indexOfC+1)); + assertEquals("Dennis Ritchie", asList.get(indexOfC + 1)); int indexOfRuby = asList.indexOf("Ruby"); assertTrue(indexOfRuby >= 0); - assertEquals("Yukihiro Matsumoto", asList.get(indexOfRuby+1)); + assertEquals("Yukihiro Matsumoto", asList.get(indexOfRuby + 1)); int indexOfPython = asList.indexOf("Python"); assertTrue(indexOfPython >= 0); - assertEquals("Guido van Rossum", asList.get(indexOfPython+1)); + assertEquals("Guido van Rossum", asList.get(indexOfPython + 1)); int indexOfCPP = asList.indexOf("C++"); assertTrue(indexOfCPP >= 0); - assertEquals("Bjarne Stroustrup", asList.get(indexOfCPP+1)); + assertEquals("Bjarne Stroustrup", asList.get(indexOfCPP + 1)); int indexOfLastUpdated = asList.indexOf("__last_updated__"); assertTrue(indexOfLastUpdated >= 0); - assertTrue(Integer.parseInt(asList.get(indexOfLastUpdated+1)) > 0); + assertTrue(Integer.parseInt(asList.get(indexOfLastUpdated + 1)) > 0); } else { - for (KeyValue kv : (List) result) { + for (KeyValue kv : (List>) result) { if (!kv.getKey().toString().equalsIgnoreCase("__last_updated__")) { assertTrue(payload.containsKey(kv.getKey())); assertEquals(payload.get(kv.getKey()), kv.getValue()); @@ -483,7 +480,7 @@ public void testFunctionLoadWithConfig() throws IOException { List argsAfter = Arrays.asList("Dictionary2", "Gallina", "Hen"); Object result = client.tFunctionCall("withConfig", "hset", Collections.emptyList(), argsAfter); - System.out.println(result); + assertEquals(2L, result); Map dict1 = client.hgetAll("Dictionary1"); Map dict2 = client.hgetAll("Dictionary2"); @@ -501,7 +498,7 @@ public void testFunctionLoadWithConfig() throws IOException { public void testLibraryCallSetValueResultAsync() throws IOException { loadAllLibraries(); Object result = client.tFunctionCallAsync("withFlags", "my_set", Collections.singletonList("KEY_TWO"), - Collections.singletonList("KEY_TWO_VALUE")); + Collections.singletonList("KEY_TWO_VALUE")); assertEquals(String.class, result.getClass()); assertEquals("OK", result); assertEquals("KEY_TWO_VALUE", client.get("KEY_TWO")); @@ -515,10 +512,10 @@ private static String readLibrary(String filename) throws IOException { private void loadAllLibraries() throws IOException { try (Stream walk = Files.walk(Paths.get("src/test/resources/functions/"))) { List libs = walk - .filter(p -> !Files.isDirectory(p)) // - .map(Path::toString) // - .filter(f -> f.endsWith(".js")) // - .collect(Collectors.toList()); + .filter(p -> !Files.isDirectory(p)) // + .map(Path::toString) // + .filter(f -> f.endsWith(".js")) // + .collect(Collectors.toList()); libs.forEach(lib -> { String code; @@ -532,7 +529,7 @@ private void loadAllLibraries() throws IOException { } } - private Map>> initializeTestLibraryConditions() { + private static Map>> initializeTestLibraryConditions() { Map>> libraryConditions = new HashMap<>(); libraryConditions.put("streamTriggers", new ArrayList<>()); libraryConditions.put("withFlags", new ArrayList<>());