From 97bb46738963ab544c5d7a777903fff767501926 Mon Sep 17 00:00:00 2001 From: Yury-Fridlyand Date: Tue, 20 Feb 2024 17:26:13 -0800 Subject: [PATCH] Rework tests and update javadocs. Signed-off-by: Yury-Fridlyand --- .../ServerManagementBaseCommands.java | 15 ++- .../ServerManagementClusterCommands.java | 34 +++++- .../glide/api/models/BaseTransaction.java | 5 +- .../test/java/glide/api/RedisClientTest.java | 112 ++++++++++++------ .../glide/api/RedisClusterClientTest.java | 112 ++++++++++++------ 5 files changed, 195 insertions(+), 83 deletions(-) diff --git a/java/client/src/main/java/glide/api/commands/ServerManagementBaseCommands.java b/java/client/src/main/java/glide/api/commands/ServerManagementBaseCommands.java index a0d5b58b69..a67cb93e46 100644 --- a/java/client/src/main/java/glide/api/commands/ServerManagementBaseCommands.java +++ b/java/client/src/main/java/glide/api/commands/ServerManagementBaseCommands.java @@ -16,15 +16,26 @@ public interface ServerManagementBaseCommands { * @see redis.io for details. * @return OK when the configuration was rewritten properly, otherwise an error is * raised. + * @example + *
+     * String response = client.configRewrite().get();
+     * assert response.equals("OK")
+     * 
*/ CompletableFuture configRewrite(); /** - * Reset the statistics reported by Redis using the INFO and LATENCY HISTOGRAM - * commands. + * Reset the statistics reported by Redis using the INFO and LATENCY HISTOGRAM commands. * * @see redis.io for details. * @return OK to confirm that the statistics were successfully reset. + * @example + *
+     * String response = client.configResetStat().get();
+     * assert response.equals("OK")
+     * 
*/ CompletableFuture configResetStat(); } diff --git a/java/client/src/main/java/glide/api/commands/ServerManagementClusterCommands.java b/java/client/src/main/java/glide/api/commands/ServerManagementClusterCommands.java index 809809c889..022119f912 100644 --- a/java/client/src/main/java/glide/api/commands/ServerManagementClusterCommands.java +++ b/java/client/src/main/java/glide/api/commands/ServerManagementClusterCommands.java @@ -73,20 +73,33 @@ public interface ServerManagementClusterCommands { CompletableFuture> info(InfoOptions options, Route route); /** - * Rewrite the configuration file with the current configuration. + * Rewrite the configuration file with the current configuration.
+ * The command will be routed automatically to all nodes. * * @see redis.io for details. * @return OK when the configuration was rewritten properly, otherwise an error is * raised. + * @example + *
+     * String response = client.configRewrite().get();
+     * assert response.equals("OK")
+     * 
*/ CompletableFuture configRewrite(); /** - * Reset the statistics reported by Redis using the INFO and LATENCY HISTOGRAM - * commands. + * Reset the statistics reported by Redis using the INFO and LATENCY HISTOGRAM commands.
+ * The command will be routed automatically to all nodes. * * @see redis.io for details. * @return OK to confirm that the statistics were successfully reset. + * @example + *
+     * String response = client.configResetStat().get();
+     * assert response.equals("OK")
+     * 
*/ CompletableFuture configResetStat(); @@ -98,17 +111,28 @@ public interface ServerManagementClusterCommands { * defined. * @return OK when the configuration was rewritten properly, otherwise an error is * raised. + * @example + *
+     * String response = client.configRewrite(ALL_PRIMARIES).get();
+     * assert response.equals("OK")
+     * 
*/ CompletableFuture configRewrite(Route route); /** - * Resets the statistics reported by Redis using the INFO and LATENCY HISTOGRAM - * commands. + * Reset the statistics reported by Redis using the INFO and LATENCY HISTOGRAM commands. * * @see redis.io for details. * @param route Routing configuration for the command. Client will route the command to the nodes * defined. * @return OK to confirm that the statistics were successfully reset. + * @example + *
+     * String response = client.configResetStat(ALL_PRIMARIES).get();
+     * assert response.equals("OK")
+     * 
*/ CompletableFuture configResetStat(Route route); } diff --git a/java/client/src/main/java/glide/api/models/BaseTransaction.java b/java/client/src/main/java/glide/api/models/BaseTransaction.java index a6abfecaf1..7376c87418 100644 --- a/java/client/src/main/java/glide/api/models/BaseTransaction.java +++ b/java/client/src/main/java/glide/api/models/BaseTransaction.java @@ -428,8 +428,9 @@ public T configRewrite() { } /** - * Reset the statistics reported by Redis using the INFO and LATENCY HISTOGRAM - * commands. + * Reset the statistics reported by Redis using the INFO and LATENCY HISTOGRAM commands. * * @see redis.io for details. * @return OK to confirm that the statistics were successfully reset. diff --git a/java/client/src/test/java/glide/api/RedisClientTest.java b/java/client/src/test/java/glide/api/RedisClientTest.java index 9c248020a0..7f39b80e1f 100644 --- a/java/client/src/test/java/glide/api/RedisClientTest.java +++ b/java/client/src/test/java/glide/api/RedisClientTest.java @@ -43,16 +43,10 @@ import java.util.Map; import java.util.Set; import java.util.concurrent.CompletableFuture; -import java.util.function.Function; -import java.util.stream.Stream; import lombok.SneakyThrows; import org.apache.commons.lang3.ArrayUtils; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.MethodSource; -import redis_request.RedisRequestOuterClass.RequestType; public class RedisClientTest { @@ -93,6 +87,26 @@ public void customCommand_returns_success() { assertEquals(value, payload); } + @SneakyThrows + @Test + public void ping_returns_success() { + // setup + CompletableFuture testResponse = mock(CompletableFuture.class); + when(testResponse.get()).thenReturn("PONG"); + + // match on protobuf request + when(commandManager.submitNewCommand(eq(Ping), eq(new String[0]), any())) + .thenReturn(testResponse); + + // exercise + CompletableFuture response = service.ping(); + String payload = response.get(); + + // verify + assertEquals(testResponse, response); + assertEquals("PONG", payload); + } + @SneakyThrows @Test public void ping_with_message_returns_success() { @@ -211,6 +225,25 @@ public void set_with_SetOptions_OnlyIfDoesNotExist_returns_success() { assertEquals(value, response.get()); } + @SneakyThrows + @Test + public void info_returns_success() { + // setup + CompletableFuture testResponse = mock(CompletableFuture.class); + String testPayload = "Key: Value"; + when(testResponse.get()).thenReturn(testPayload); + when(commandManager.submitNewCommand(eq(Info), eq(new String[0]), any())) + .thenReturn(testResponse); + + // exercise + CompletableFuture response = service.info(); + String payload = response.get(); + + // verify + assertEquals(testResponse, response); + assertEquals(testPayload, payload); + } + @SneakyThrows @Test public void info_with_multiple_InfoOptions_returns_success() { @@ -299,37 +332,6 @@ public void mset_returns_success() { CompletableFuture response = service.mset(keyValueMap); } - private static Stream getCommandsWithoutArgs() { - return Map.>>of( - ConfigRewrite, RedisClient::configRewrite, - ConfigResetStat, RedisClient::configResetStat, - Info, RedisClient::info, - Ping, RedisClient::ping) - .entrySet() - .stream() - .map(e -> Arguments.of(e.getKey(), e.getValue())); - } - - @SneakyThrows - @ParameterizedTest(name = "{0} returns success") - @MethodSource("getCommandsWithoutArgs") - public void no_arg_command_returns_success( - RequestType requestType, Function> command) { - // setup - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(OK); - when(commandManager.submitNewCommand(eq(requestType), eq(new String[0]), any())) - .thenReturn(testResponse); - - // exercise - CompletableFuture response = command.apply(service); - String payload = response.get(); - - // verify - assertEquals(testResponse, response); - assertEquals(OK, payload); - } - @SneakyThrows @Test public void incr_returns_success() { @@ -617,4 +619,40 @@ public void scard_returns_success() { assertEquals(testResponse, response); assertEquals(value, payload); } + + @SneakyThrows + @Test + public void configRewrite_returns_success() { + // setup + CompletableFuture testResponse = mock(CompletableFuture.class); + when(testResponse.get()).thenReturn(OK); + when(commandManager.submitNewCommand(eq(ConfigRewrite), eq(new String[0]), any())) + .thenReturn(testResponse); + + // exercise + CompletableFuture response = service.configRewrite(); + String payload = response.get(); + + // verify + assertEquals(testResponse, response); + assertEquals(OK, payload); + } + + @SneakyThrows + @Test + public void configResetStat_returns_success() { + // setup + CompletableFuture testResponse = mock(CompletableFuture.class); + when(testResponse.get()).thenReturn(OK); + when(commandManager.submitNewCommand(eq(ConfigResetStat), eq(new String[0]), any())) + .thenReturn(testResponse); + + // exercise + CompletableFuture response = service.configResetStat(); + String payload = response.get(); + + // verify + assertEquals(testResponse, response); + assertEquals(OK, payload); + } } diff --git a/java/client/src/test/java/glide/api/RedisClusterClientTest.java b/java/client/src/test/java/glide/api/RedisClusterClientTest.java index de09358fb2..0178b88671 100644 --- a/java/client/src/test/java/glide/api/RedisClusterClientTest.java +++ b/java/client/src/test/java/glide/api/RedisClusterClientTest.java @@ -26,17 +26,10 @@ import java.util.HashMap; import java.util.Map; import java.util.concurrent.CompletableFuture; -import java.util.function.BiFunction; -import java.util.function.Function; -import java.util.stream.Stream; import lombok.SneakyThrows; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.MethodSource; import redis_request.RedisRequestOuterClass.RedisRequest; -import redis_request.RedisRequestOuterClass.RequestType; import response.ResponseOuterClass.ConstantResponse; import response.ResponseOuterClass.Response; @@ -151,6 +144,28 @@ public CompletableFuture submitCommandToChannel( } } + @SneakyThrows + @Test + public void ping_returns_success() { + // setup + CompletableFuture testResponse = mock(CompletableFuture.class); + when(testResponse.get()).thenReturn("PONG"); + + Route route = ALL_NODES; + + // match on protobuf request + when(commandManager.submitNewCommand(eq(Ping), eq(new String[0]), eq(route), any())) + .thenReturn(testResponse); + + // exercise + CompletableFuture response = service.ping(route); + String payload = response.get(); + + // verify + assertEquals(testResponse, response); + assertEquals("PONG", payload); + } + @SneakyThrows @Test public void ping_with_message_returns_success() { @@ -303,39 +318,62 @@ public void info_with_options_and_multi_node_route_returns_multi_value() { () -> assertTrue(value.hasMultiData()), () -> assertEquals(data, value.getMultiValue())); } - private static Stream getCommandsWithoutArgs() { - return Map.>>of( - ConfigRewrite, RedisClusterClient::configRewrite, - ConfigResetStat, RedisClusterClient::configResetStat, - Ping, RedisClusterClient::ping) - .entrySet() - .stream() - .map(e -> Arguments.of(e.getKey(), e.getValue())); + @SneakyThrows + @Test + public void configRewrite_without_route_returns_success() { + // setup + CompletableFuture testResponse = mock(CompletableFuture.class); + when(testResponse.get()).thenReturn(OK); + + // match on protobuf request + when(commandManager.submitNewCommand(eq(ConfigRewrite), eq(new String[0]), any())) + .thenReturn(testResponse); + + // exercise + CompletableFuture response = service.configRewrite(); + String payload = response.get(); + + // verify + assertEquals(testResponse, response); + assertEquals(OK, payload); } - private static Stream getCommandsWithRouteWithoutArgs() { - return Map.>>of( - ConfigRewrite, RedisClusterClient::configRewrite, - ConfigResetStat, RedisClusterClient::configResetStat, - Ping, RedisClusterClient::ping) - .entrySet() - .stream() - .map(e -> Arguments.of(e.getKey(), e.getValue())); + @SneakyThrows + @Test + public void configRewrite_with_route_returns_success() { + // setup + CompletableFuture testResponse = mock(CompletableFuture.class); + when(testResponse.get()).thenReturn(OK); + + Route route = ALL_NODES; + + // match on protobuf request + when(commandManager.submitNewCommand( + eq(ConfigRewrite), eq(new String[0]), eq(route), any())) + .thenReturn(testResponse); + + // exercise + CompletableFuture response = service.configRewrite(route); + String payload = response.get(); + + // verify + assertEquals(testResponse, response); + assertEquals(OK, payload); } @SneakyThrows - @ParameterizedTest(name = "{0} returns success") - @MethodSource("getCommandsWithoutArgs") - public void no_arg_command_returns_success( - RequestType requestType, Function> command) { + @Test + public void configResetStat_without_route_returns_success() { // setup CompletableFuture testResponse = mock(CompletableFuture.class); when(testResponse.get()).thenReturn(OK); - when(commandManager.submitNewCommand(eq(requestType), eq(new String[0]), any())) + + // match on protobuf request + when(commandManager.submitNewCommand(eq(ConfigResetStat), eq(new String[0]), any())) .thenReturn(testResponse); // exercise - CompletableFuture response = command.apply(service); + CompletableFuture response = service.configResetStat(); String payload = response.get(); // verify @@ -344,21 +382,21 @@ public void no_arg_command_returns_success( } @SneakyThrows - @ParameterizedTest(name = "{0} returns success") - @MethodSource("getCommandsWithRouteWithoutArgs") - public void no_arg_command_with_route_returns_success( - RequestType requestType, - BiFunction> command) { + @Test + public void configResetStat_with_route_returns_success() { // setup CompletableFuture testResponse = mock(CompletableFuture.class); - var route = ALL_NODES; when(testResponse.get()).thenReturn(OK); + + Route route = ALL_NODES; + + // match on protobuf request when(commandManager.submitNewCommand( - eq(requestType), eq(new String[0]), eq(route), any())) + eq(ConfigResetStat), eq(new String[0]), eq(route), any())) .thenReturn(testResponse); // exercise - CompletableFuture response = command.apply(service, route); + CompletableFuture response = service.configResetStat(route); String payload = response.get(); // verify