From b2301fa85f270361d2057448a7711cf20d314fbe Mon Sep 17 00:00:00 2001 From: Andrew Carbonetto Date: Thu, 18 Jan 2024 11:38:14 -0800 Subject: [PATCH] Spotless Signed-off-by: Andrew Carbonetto --- .../main/java/glide/api/commands/Command.java | 16 +- .../glide/managers/ConnectionManagerTest.java | 442 +++++++++--------- 2 files changed, 229 insertions(+), 229 deletions(-) diff --git a/java/client/src/main/java/glide/api/commands/Command.java b/java/client/src/main/java/glide/api/commands/Command.java index 413ea63c30..8dba62553a 100644 --- a/java/client/src/main/java/glide/api/commands/Command.java +++ b/java/client/src/main/java/glide/api/commands/Command.java @@ -11,14 +11,14 @@ @EqualsAndHashCode public class Command { - /** Redis command request type */ - @NonNull final RequestType requestType; + /** Redis command request type */ + @NonNull final RequestType requestType; - /** List of Arguments for the Redis command request */ - @Builder.Default final String[] arguments = new String[] {}; + /** List of Arguments for the Redis command request */ + @Builder.Default final String[] arguments = new String[] {}; - public enum RequestType { - /** Call a custom command with list of string arguments */ - CUSTOM_COMMAND, - } + public enum RequestType { + /** Call a custom command with list of string arguments */ + CUSTOM_COMMAND, + } } diff --git a/java/client/src/test/java/glide/managers/ConnectionManagerTest.java b/java/client/src/test/java/glide/managers/ConnectionManagerTest.java index 42419f9703..50e8c97a95 100644 --- a/java/client/src/test/java/glide/managers/ConnectionManagerTest.java +++ b/java/client/src/test/java/glide/managers/ConnectionManagerTest.java @@ -34,225 +34,225 @@ import response.ResponseOuterClass.Response; public class ConnectionManagerTest { - ConnectionManager connectionManager; - - ChannelHandler channel; - - private static String HOST = "aws.com"; - private static int PORT = 9999; - - private static String USERNAME = "JohnDoe"; - private static String PASSWORD = "Password1"; - - private static int NUM_OF_RETRIES = 5; - private static int FACTOR = 10; - private static int EXPONENT_BASE = 50; - - private static int DATABASE_ID = 1; - - private static int REQUEST_TIMEOUT = 3; - - @BeforeEach - public void setUp() throws ExecutionException, InterruptedException { - channel = mock(ChannelHandler.class); - ChannelFuture closeFuture = mock(ChannelFuture.class); - when(closeFuture.syncUninterruptibly()).thenReturn(closeFuture); - when(channel.close()).thenReturn(closeFuture); - connectionManager = new ConnectionManager(channel); - } - - @SneakyThrows - @Test - public void ConnectionRequestProtobufGeneration_DefaultRedisClientConfiguration_returns() { - // setup - RedisClientConfiguration redisClientConfiguration = RedisClientConfiguration.builder().build(); - ConnectionRequest expectedProtobufConnectionRequest = - ConnectionRequest.newBuilder() - .setTlsMode(ConnectionRequestOuterClass.TlsMode.NoTls) - .setClusterModeEnabled(false) - .setReadFrom(ConnectionRequestOuterClass.ReadFrom.Primary) - .build(); - CompletableFuture completedFuture = new CompletableFuture<>(); - Response response = - Response.newBuilder().setConstantResponse(ResponseOuterClass.ConstantResponse.OK).build(); - completedFuture.complete(response); - - // execute - when(channel.connect(eq(expectedProtobufConnectionRequest))).thenReturn(completedFuture); - CompletableFuture result = connectionManager.connectToRedis(redisClientConfiguration); - - // verify - // no exception - assertNull(result.get()); - } - - @SneakyThrows - @Test - public void ConnectionRequestProtobufGeneration_DefaultRedisClusterClientConfiguration_returns() { - // setup - RedisClusterClientConfiguration redisClusterClientConfiguration = - RedisClusterClientConfiguration.builder().build(); - ConnectionRequest expectedProtobufConnectionRequest = - ConnectionRequest.newBuilder() - .setTlsMode(ConnectionRequestOuterClass.TlsMode.NoTls) - .setClusterModeEnabled(true) - .setReadFrom(ConnectionRequestOuterClass.ReadFrom.Primary) - .build(); - CompletableFuture completedFuture = new CompletableFuture<>(); - Response response = - Response.newBuilder().setConstantResponse(ResponseOuterClass.ConstantResponse.OK).build(); - completedFuture.complete(response); - - // execute - when(channel.connect(eq(expectedProtobufConnectionRequest))).thenReturn(completedFuture); - CompletableFuture result = - connectionManager.connectToRedis(redisClusterClientConfiguration); - - // verify - assertNull(result.get()); - verify(channel).connect(eq(expectedProtobufConnectionRequest)); - } - - @SneakyThrows - @Test - public void ConnectionRequestProtobufGeneration_RedisClientAllFieldsSet_returns() { - // setup - RedisClientConfiguration redisClientConfiguration = - RedisClientConfiguration.builder() - .address(NodeAddress.builder().host(HOST).port(PORT).build()) - .address(NodeAddress.builder().host(DEFAULT_HOST).port(DEFAULT_PORT).build()) - .useTLS(true) - .readFrom(ReadFrom.PREFER_REPLICA) - .credentials(RedisCredentials.builder().username(USERNAME).password(PASSWORD).build()) - .requestTimeout(REQUEST_TIMEOUT) - .reconnectStrategy( - BackoffStrategy.builder() - .numOfRetries(NUM_OF_RETRIES) - .exponentBase(EXPONENT_BASE) - .factor(FACTOR) - .build()) - .databaseId(DATABASE_ID) - .build(); - ConnectionRequest expectedProtobufConnectionRequest = - ConnectionRequest.newBuilder() - .addAddresses( - ConnectionRequestOuterClass.NodeAddress.newBuilder() - .setHost(HOST) - .setPort(PORT) - .build()) - .addAddresses( - ConnectionRequestOuterClass.NodeAddress.newBuilder() - .setHost(DEFAULT_HOST) - .setPort(DEFAULT_PORT) - .build()) - .setTlsMode(ConnectionRequestOuterClass.TlsMode.SecureTls) - .setReadFrom(ConnectionRequestOuterClass.ReadFrom.PreferReplica) - .setClusterModeEnabled(false) - .setAuthenticationInfo( - AuthenticationInfo.newBuilder().setUsername(USERNAME).setPassword(PASSWORD).build()) - .setRequestTimeout(REQUEST_TIMEOUT) - .setConnectionRetryStrategy( - ConnectionRetryStrategy.newBuilder() - .setNumberOfRetries(NUM_OF_RETRIES) - .setFactor(FACTOR) - .setExponentBase(EXPONENT_BASE) - .build()) - .setDatabaseId(DATABASE_ID) - .build(); - CompletableFuture completedFuture = new CompletableFuture<>(); - Response response = - Response.newBuilder().setConstantResponse(ResponseOuterClass.ConstantResponse.OK).build(); - completedFuture.complete(response); - - // execute - when(channel.connect(eq(expectedProtobufConnectionRequest))).thenReturn(completedFuture); - CompletableFuture result = connectionManager.connectToRedis(redisClientConfiguration); - - // verify - assertNull(result.get()); - verify(channel).connect(eq(expectedProtobufConnectionRequest)); - } - - @SneakyThrows - @Test - public void CheckRedisResponse_ConstantResponse_returnsSuccessfully() { - // setup - RedisClientConfiguration redisClientConfiguration = RedisClientConfiguration.builder().build(); - CompletableFuture completedFuture = new CompletableFuture<>(); - Response response = - Response.newBuilder().setConstantResponse(ResponseOuterClass.ConstantResponse.OK).build(); - completedFuture.complete(response); - - // execute - when(channel.connect(any())).thenReturn(completedFuture); - CompletableFuture result = connectionManager.connectToRedis(redisClientConfiguration); - - // verify - assertNull(result.get()); - verify(channel).connect(any()); - } - - @Test - public void onConnection_emptyResponse_throwsClosingException() { - // setup - RedisClientConfiguration redisClientConfiguration = RedisClientConfiguration.builder().build(); - CompletableFuture completedFuture = new CompletableFuture<>(); - Response response = Response.newBuilder().build(); - completedFuture.complete(response); - - // execute - when(channel.connect(any())).thenReturn(completedFuture); - ExecutionException executionException = - assertThrows( - ExecutionException.class, - () -> connectionManager.connectToRedis(redisClientConfiguration).get()); - - assertTrue(executionException.getCause() instanceof ClosingException); - assertEquals("Unexpected empty data in response", executionException.getCause().getMessage()); - verify(channel).close(); - } - - @Test - public void CheckRedisResponse_RequestError_throwsClosingException() { - // setup - RedisClientConfiguration redisClientConfiguration = RedisClientConfiguration.builder().build(); - CompletableFuture completedFuture = new CompletableFuture<>(); - Response response = - Response.newBuilder() - .setRequestError( - ResponseOuterClass.RequestError.newBuilder() - .setType(ResponseOuterClass.RequestErrorType.Timeout) - .setMessage("Timeout Occurred") - .build()) - .build(); - completedFuture.complete(response); - - // execute - when(channel.connect(any())).thenReturn(completedFuture); - CompletableFuture result = connectionManager.connectToRedis(redisClientConfiguration); - - // verify - ExecutionException exception = assertThrows(ExecutionException.class, result::get); - assertTrue(exception.getCause() instanceof ClosingException); - - verify(channel).close(); - } - - @Test - public void CheckRedisResponse_ClosingError_throwsClosingException() { - // setup - RedisClientConfiguration redisClientConfiguration = RedisClientConfiguration.builder().build(); - CompletableFuture completedFuture = new CompletableFuture<>(); - Response response = Response.newBuilder().setClosingError("Closing Error Occurred").build(); - completedFuture.complete(response); - - // execute - when(channel.connect(any())).thenReturn(completedFuture); - CompletableFuture result = connectionManager.connectToRedis(redisClientConfiguration); - - // verify - ExecutionException exception = assertThrows(ExecutionException.class, result::get); - assertTrue(exception.getCause() instanceof ClosingException); - assertEquals(response.getClosingError(), exception.getCause().getMessage()); - } + ConnectionManager connectionManager; + + ChannelHandler channel; + + private static String HOST = "aws.com"; + private static int PORT = 9999; + + private static String USERNAME = "JohnDoe"; + private static String PASSWORD = "Password1"; + + private static int NUM_OF_RETRIES = 5; + private static int FACTOR = 10; + private static int EXPONENT_BASE = 50; + + private static int DATABASE_ID = 1; + + private static int REQUEST_TIMEOUT = 3; + + @BeforeEach + public void setUp() throws ExecutionException, InterruptedException { + channel = mock(ChannelHandler.class); + ChannelFuture closeFuture = mock(ChannelFuture.class); + when(closeFuture.syncUninterruptibly()).thenReturn(closeFuture); + when(channel.close()).thenReturn(closeFuture); + connectionManager = new ConnectionManager(channel); + } + + @SneakyThrows + @Test + public void ConnectionRequestProtobufGeneration_DefaultRedisClientConfiguration_returns() { + // setup + RedisClientConfiguration redisClientConfiguration = RedisClientConfiguration.builder().build(); + ConnectionRequest expectedProtobufConnectionRequest = + ConnectionRequest.newBuilder() + .setTlsMode(ConnectionRequestOuterClass.TlsMode.NoTls) + .setClusterModeEnabled(false) + .setReadFrom(ConnectionRequestOuterClass.ReadFrom.Primary) + .build(); + CompletableFuture completedFuture = new CompletableFuture<>(); + Response response = + Response.newBuilder().setConstantResponse(ResponseOuterClass.ConstantResponse.OK).build(); + completedFuture.complete(response); + + // execute + when(channel.connect(eq(expectedProtobufConnectionRequest))).thenReturn(completedFuture); + CompletableFuture result = connectionManager.connectToRedis(redisClientConfiguration); + + // verify + // no exception + assertNull(result.get()); + } + + @SneakyThrows + @Test + public void ConnectionRequestProtobufGeneration_DefaultRedisClusterClientConfiguration_returns() { + // setup + RedisClusterClientConfiguration redisClusterClientConfiguration = + RedisClusterClientConfiguration.builder().build(); + ConnectionRequest expectedProtobufConnectionRequest = + ConnectionRequest.newBuilder() + .setTlsMode(ConnectionRequestOuterClass.TlsMode.NoTls) + .setClusterModeEnabled(true) + .setReadFrom(ConnectionRequestOuterClass.ReadFrom.Primary) + .build(); + CompletableFuture completedFuture = new CompletableFuture<>(); + Response response = + Response.newBuilder().setConstantResponse(ResponseOuterClass.ConstantResponse.OK).build(); + completedFuture.complete(response); + + // execute + when(channel.connect(eq(expectedProtobufConnectionRequest))).thenReturn(completedFuture); + CompletableFuture result = + connectionManager.connectToRedis(redisClusterClientConfiguration); + + // verify + assertNull(result.get()); + verify(channel).connect(eq(expectedProtobufConnectionRequest)); + } + + @SneakyThrows + @Test + public void ConnectionRequestProtobufGeneration_RedisClientAllFieldsSet_returns() { + // setup + RedisClientConfiguration redisClientConfiguration = + RedisClientConfiguration.builder() + .address(NodeAddress.builder().host(HOST).port(PORT).build()) + .address(NodeAddress.builder().host(DEFAULT_HOST).port(DEFAULT_PORT).build()) + .useTLS(true) + .readFrom(ReadFrom.PREFER_REPLICA) + .credentials(RedisCredentials.builder().username(USERNAME).password(PASSWORD).build()) + .requestTimeout(REQUEST_TIMEOUT) + .reconnectStrategy( + BackoffStrategy.builder() + .numOfRetries(NUM_OF_RETRIES) + .exponentBase(EXPONENT_BASE) + .factor(FACTOR) + .build()) + .databaseId(DATABASE_ID) + .build(); + ConnectionRequest expectedProtobufConnectionRequest = + ConnectionRequest.newBuilder() + .addAddresses( + ConnectionRequestOuterClass.NodeAddress.newBuilder() + .setHost(HOST) + .setPort(PORT) + .build()) + .addAddresses( + ConnectionRequestOuterClass.NodeAddress.newBuilder() + .setHost(DEFAULT_HOST) + .setPort(DEFAULT_PORT) + .build()) + .setTlsMode(ConnectionRequestOuterClass.TlsMode.SecureTls) + .setReadFrom(ConnectionRequestOuterClass.ReadFrom.PreferReplica) + .setClusterModeEnabled(false) + .setAuthenticationInfo( + AuthenticationInfo.newBuilder().setUsername(USERNAME).setPassword(PASSWORD).build()) + .setRequestTimeout(REQUEST_TIMEOUT) + .setConnectionRetryStrategy( + ConnectionRetryStrategy.newBuilder() + .setNumberOfRetries(NUM_OF_RETRIES) + .setFactor(FACTOR) + .setExponentBase(EXPONENT_BASE) + .build()) + .setDatabaseId(DATABASE_ID) + .build(); + CompletableFuture completedFuture = new CompletableFuture<>(); + Response response = + Response.newBuilder().setConstantResponse(ResponseOuterClass.ConstantResponse.OK).build(); + completedFuture.complete(response); + + // execute + when(channel.connect(eq(expectedProtobufConnectionRequest))).thenReturn(completedFuture); + CompletableFuture result = connectionManager.connectToRedis(redisClientConfiguration); + + // verify + assertNull(result.get()); + verify(channel).connect(eq(expectedProtobufConnectionRequest)); + } + + @SneakyThrows + @Test + public void CheckRedisResponse_ConstantResponse_returnsSuccessfully() { + // setup + RedisClientConfiguration redisClientConfiguration = RedisClientConfiguration.builder().build(); + CompletableFuture completedFuture = new CompletableFuture<>(); + Response response = + Response.newBuilder().setConstantResponse(ResponseOuterClass.ConstantResponse.OK).build(); + completedFuture.complete(response); + + // execute + when(channel.connect(any())).thenReturn(completedFuture); + CompletableFuture result = connectionManager.connectToRedis(redisClientConfiguration); + + // verify + assertNull(result.get()); + verify(channel).connect(any()); + } + + @Test + public void onConnection_emptyResponse_throwsClosingException() { + // setup + RedisClientConfiguration redisClientConfiguration = RedisClientConfiguration.builder().build(); + CompletableFuture completedFuture = new CompletableFuture<>(); + Response response = Response.newBuilder().build(); + completedFuture.complete(response); + + // execute + when(channel.connect(any())).thenReturn(completedFuture); + ExecutionException executionException = + assertThrows( + ExecutionException.class, + () -> connectionManager.connectToRedis(redisClientConfiguration).get()); + + assertTrue(executionException.getCause() instanceof ClosingException); + assertEquals("Unexpected empty data in response", executionException.getCause().getMessage()); + verify(channel).close(); + } + + @Test + public void CheckRedisResponse_RequestError_throwsClosingException() { + // setup + RedisClientConfiguration redisClientConfiguration = RedisClientConfiguration.builder().build(); + CompletableFuture completedFuture = new CompletableFuture<>(); + Response response = + Response.newBuilder() + .setRequestError( + ResponseOuterClass.RequestError.newBuilder() + .setType(ResponseOuterClass.RequestErrorType.Timeout) + .setMessage("Timeout Occurred") + .build()) + .build(); + completedFuture.complete(response); + + // execute + when(channel.connect(any())).thenReturn(completedFuture); + CompletableFuture result = connectionManager.connectToRedis(redisClientConfiguration); + + // verify + ExecutionException exception = assertThrows(ExecutionException.class, result::get); + assertTrue(exception.getCause() instanceof ClosingException); + + verify(channel).close(); + } + + @Test + public void CheckRedisResponse_ClosingError_throwsClosingException() { + // setup + RedisClientConfiguration redisClientConfiguration = RedisClientConfiguration.builder().build(); + CompletableFuture completedFuture = new CompletableFuture<>(); + Response response = Response.newBuilder().setClosingError("Closing Error Occurred").build(); + completedFuture.complete(response); + + // execute + when(channel.connect(any())).thenReturn(completedFuture); + CompletableFuture result = connectionManager.connectToRedis(redisClientConfiguration); + + // verify + ExecutionException exception = assertThrows(ExecutionException.class, result::get); + assertTrue(exception.getCause() instanceof ClosingException); + assertEquals(response.getClosingError(), exception.getCause().getMessage()); + } }