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

Add extensive tests for UnifiedJedis #3788

Merged
merged 11 commits into from
Apr 3, 2024
4 changes: 3 additions & 1 deletion src/main/java/redis/clients/jedis/UnifiedJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.json.JSONArray;

import redis.clients.jedis.annots.Experimental;
import redis.clients.jedis.annots.VisibleForTesting;
import redis.clients.jedis.args.*;
import redis.clients.jedis.bloom.*;
import redis.clients.jedis.commands.JedisCommands;
Expand Down Expand Up @@ -213,7 +214,8 @@ private UnifiedJedis(CommandExecutor executor, ConnectionProvider provider) {
}

// Uses a fetched connection to process protocol. Should be avoided if possible.
private UnifiedJedis(CommandExecutor executor, ConnectionProvider provider, CommandObjects commandObjects) {
@VisibleForTesting
public UnifiedJedis(CommandExecutor executor, ConnectionProvider provider, CommandObjects commandObjects) {
this(executor, provider, commandObjects, null);
if (this.provider != null) {
try (Connection conn = this.provider.getConnection()) {
Expand Down
10,502 changes: 0 additions & 10,502 deletions src/test/java/redis/clients/jedis/PipeliningBaseTest.java

This file was deleted.

263 changes: 263 additions & 0 deletions src/test/java/redis/clients/jedis/UnifiedJedisCustomCommandsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,263 @@
package redis.clients.jedis;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.sameInstance;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import org.junit.Test;
import org.mockito.ArgumentCaptor;
import redis.clients.jedis.commands.ProtocolCommand;
import redis.clients.jedis.mocked.unified.UnifiedJedisMockedTestBase;

/**
* These tests are part of the mocked tests for {@link UnifiedJedis}, but, due to {@code protected}
* visibility of some methods, they must reside in the same package as the tested class.
*/
public class UnifiedJedisCustomCommandsTest extends UnifiedJedisMockedTestBase {

@Test
public void testSendCommandWithProtocolCommand() {
ProtocolCommand cmd = mock(ProtocolCommand.class);
CommandArguments commandArguments = mock(CommandArguments.class);

when(commandObjects.commandArguments(cmd)).thenReturn(commandArguments);
when(commandExecutor.executeCommand(any())).thenReturn("OK");

Object result = jedis.sendCommand(cmd);

ArgumentCaptor<CommandObject<Object>> argumentCaptor = ArgumentCaptor.forClass(CommandObject.class);
verify(commandExecutor).executeCommand(argumentCaptor.capture());

CommandObject<Object> commandObject = argumentCaptor.getValue();
assertThat(commandObject.getArguments(), sameInstance(commandArguments));

assertThat(result, equalTo("OK"));

verify(commandObjects).commandArguments(cmd);
}

@Test
public void testSendCommandWithProtocolCommandAndByteArrayArgs() {
ProtocolCommand cmd = mock(ProtocolCommand.class);
byte[][] args = { "arg1".getBytes(), "arg2".getBytes() };
CommandArguments commandArguments = mock(CommandArguments.class);
CommandArguments commandArgumentsWithArgs = mock(CommandArguments.class);
when(commandArguments.addObjects(args)).thenReturn(commandArgumentsWithArgs);

when(commandObjects.commandArguments(cmd)).thenReturn(commandArguments);
when(commandExecutor.executeCommand(any())).thenReturn("OK");

Object result = jedis.sendCommand(cmd, args);

ArgumentCaptor<CommandObject<Object>> argumentCaptor = ArgumentCaptor.forClass(CommandObject.class);
verify(commandExecutor).executeCommand(argumentCaptor.capture());

CommandObject<Object> commandObject = argumentCaptor.getValue();
assertThat(commandObject.getArguments(), sameInstance(commandArgumentsWithArgs));

assertThat(result, equalTo("OK"));

verify(commandObjects).commandArguments(cmd);
}

@Test
public void testSendBlockingCommandWithProtocolCommandAndByteArrayArgs() {
ProtocolCommand cmd = mock(ProtocolCommand.class);
byte[][] args = { "arg1".getBytes(), "arg2".getBytes() };
CommandArguments commandArguments = mock(CommandArguments.class);

CommandArguments commandArgumentsBlocking = mock(CommandArguments.class);
CommandArguments commandArgumentsWithArgs = mock(CommandArguments.class);
when(commandArgumentsWithArgs.blocking()).thenReturn(commandArgumentsBlocking);

when(commandArguments.addObjects(args)).thenReturn(commandArgumentsWithArgs);

when(commandObjects.commandArguments(cmd)).thenReturn(commandArguments);
when(commandExecutor.executeCommand(any())).thenReturn("OK");

Object result = jedis.sendBlockingCommand(cmd, args);

ArgumentCaptor<CommandObject<Object>> argumentCaptor = ArgumentCaptor.forClass(CommandObject.class);
verify(commandExecutor).executeCommand(argumentCaptor.capture());

CommandObject<Object> commandObject = argumentCaptor.getValue();
assertThat(commandObject.getArguments(), sameInstance(commandArgumentsBlocking));

assertThat(result, equalTo("OK"));

verify(commandObjects).commandArguments(cmd);
}

@Test
public void testSendCommandWithProtocolCommandAndStringArgs() {
ProtocolCommand cmd = mock(ProtocolCommand.class);
String[] args = { "arg1", "arg2" };
CommandArguments commandArguments = mock(CommandArguments.class);
CommandArguments commandArgumentsWithArgs = mock(CommandArguments.class);
when(commandArguments.addObjects(args)).thenReturn(commandArgumentsWithArgs);

when(commandObjects.commandArguments(cmd)).thenReturn(commandArguments);
when(commandExecutor.executeCommand(any())).thenReturn("OK");

Object result = jedis.sendCommand(cmd, args);

ArgumentCaptor<CommandObject<Object>> argumentCaptor = ArgumentCaptor.forClass(CommandObject.class);
verify(commandExecutor).executeCommand(argumentCaptor.capture());

CommandObject<Object> commandObject = argumentCaptor.getValue();
assertThat(commandObject.getArguments(), sameInstance(commandArgumentsWithArgs));

assertThat(result, equalTo("OK"));

verify(commandObjects).commandArguments(cmd);
}

@Test
public void testSendBlockingCommandWithProtocolCommandAndStringArgs() {
ProtocolCommand cmd = mock(ProtocolCommand.class);
String[] args = { "arg1", "arg2" };
CommandArguments commandArguments = mock(CommandArguments.class);

CommandArguments commandArgumentsBlocking = mock(CommandArguments.class);
CommandArguments commandArgumentsWithArgs = mock(CommandArguments.class);
when(commandArgumentsWithArgs.blocking()).thenReturn(commandArgumentsBlocking);

when(commandArguments.addObjects(args)).thenReturn(commandArgumentsWithArgs);

when(commandObjects.commandArguments(cmd)).thenReturn(commandArguments);
when(commandExecutor.executeCommand(any())).thenReturn("OK");

Object result = jedis.sendBlockingCommand(cmd, args);

ArgumentCaptor<CommandObject<Object>> argumentCaptor = ArgumentCaptor.forClass(CommandObject.class);
verify(commandExecutor).executeCommand(argumentCaptor.capture());

CommandObject<Object> commandObject = argumentCaptor.getValue();
assertThat(commandObject.getArguments(), sameInstance(commandArgumentsBlocking));

assertThat(result, equalTo("OK"));

verify(commandObjects).commandArguments(cmd);
}

@Test
public void testSendCommandWithSampleKeyProtocolCommandAndByteArrayArgs() {
byte[] sampleKey = "key".getBytes();
ProtocolCommand cmd = mock(ProtocolCommand.class);
byte[][] args = { "arg1".getBytes(), "arg2".getBytes() };
CommandArguments commandArguments = mock(CommandArguments.class);
CommandArguments commandArgumentsWithArgs = mock(CommandArguments.class);
CommandArguments commandArgumentsWithKey = mock(CommandArguments.class);

when(commandArguments.addObjects((Object[]) args)).thenReturn(commandArgumentsWithArgs);
when(commandArgumentsWithArgs.processKey(sampleKey)).thenReturn(commandArgumentsWithKey);

when(commandObjects.commandArguments(cmd)).thenReturn(commandArguments);
when(commandExecutor.executeCommand(any())).thenReturn("OK");

Object result = jedis.sendCommand(sampleKey, cmd, args);

ArgumentCaptor<CommandObject<Object>> argumentCaptor = ArgumentCaptor.forClass(CommandObject.class);
verify(commandExecutor).executeCommand(argumentCaptor.capture());

CommandObject<Object> commandObject = argumentCaptor.getValue();
assertThat(commandObject.getArguments(), sameInstance(commandArgumentsWithKey));

assertThat(result, equalTo("OK"));

verify(commandObjects).commandArguments(cmd);
}

@Test
public void testSendBlockingCommandWithSampleKeyProtocolCommandAndByteArrayArgs() {
byte[] sampleKey = "key".getBytes();
ProtocolCommand cmd = mock(ProtocolCommand.class);
byte[][] args = { "arg1".getBytes(), "arg2".getBytes() };
CommandArguments commandArguments = mock(CommandArguments.class);
CommandArguments commandArgumentsWithArgs = mock(CommandArguments.class);
CommandArguments commandArgumentsBlocking = mock(CommandArguments.class);
CommandArguments commandArgumentsWithKey = mock(CommandArguments.class);

when(commandArguments.addObjects((Object[]) args)).thenReturn(commandArgumentsWithArgs);
when(commandArgumentsWithArgs.blocking()).thenReturn(commandArgumentsBlocking);
when(commandArgumentsBlocking.processKey(sampleKey)).thenReturn(commandArgumentsWithKey);

when(commandObjects.commandArguments(cmd)).thenReturn(commandArguments);
when(commandExecutor.executeCommand(any())).thenReturn("OK");

Object result = jedis.sendBlockingCommand(sampleKey, cmd, args);

ArgumentCaptor<CommandObject<Object>> argumentCaptor = ArgumentCaptor.forClass(CommandObject.class);
verify(commandExecutor).executeCommand(argumentCaptor.capture());

CommandObject<Object> commandObject = argumentCaptor.getValue();
assertThat(commandObject.getArguments(), sameInstance(commandArgumentsWithKey));

assertThat(result, equalTo("OK"));

verify(commandObjects).commandArguments(cmd);
}

@Test
public void testSendCommandWithStringSampleKeyProtocolCommandAndStringArgs() {
String sampleKey = "key";
ProtocolCommand cmd = mock(ProtocolCommand.class);
String[] args = { "arg1", "arg2" };
CommandArguments commandArguments = mock(CommandArguments.class);
CommandArguments commandArgumentsWithArgs = mock(CommandArguments.class);
CommandArguments commandArgumentsWithKey = mock(CommandArguments.class);

when(commandArguments.addObjects((Object[]) args)).thenReturn(commandArgumentsWithArgs);
when(commandArgumentsWithArgs.processKey(sampleKey)).thenReturn(commandArgumentsWithKey);

when(commandObjects.commandArguments(cmd)).thenReturn(commandArguments);
when(commandExecutor.executeCommand(any())).thenReturn("OK");

Object result = jedis.sendCommand(sampleKey, cmd, args);

ArgumentCaptor<CommandObject<Object>> argumentCaptor = ArgumentCaptor.forClass(CommandObject.class);
verify(commandExecutor).executeCommand(argumentCaptor.capture());

CommandObject<Object> commandObject = argumentCaptor.getValue();
assertThat(commandObject.getArguments(), sameInstance(commandArgumentsWithKey));

assertThat(result, equalTo("OK"));

verify(commandObjects).commandArguments(cmd);
}

@Test
public void testSendBlockingCommandWithStringSampleKeyProtocolCommandAndStringArgs() {
String sampleKey = "key";
ProtocolCommand cmd = mock(ProtocolCommand.class);
String[] args = { "arg1", "arg2" };
CommandArguments commandArguments = mock(CommandArguments.class);
CommandArguments commandArgumentsWithArgs = mock(CommandArguments.class);
CommandArguments commandArgumentsBlocking = mock(CommandArguments.class);
CommandArguments commandArgumentsWithKey = mock(CommandArguments.class);

when(commandArguments.addObjects((Object[]) args)).thenReturn(commandArgumentsWithArgs);
when(commandArgumentsWithArgs.blocking()).thenReturn(commandArgumentsBlocking);
when(commandArgumentsBlocking.processKey(sampleKey)).thenReturn(commandArgumentsWithKey);

when(commandObjects.commandArguments(cmd)).thenReturn(commandArguments);
when(commandExecutor.executeCommand(any())).thenReturn("OK");

Object result = jedis.sendBlockingCommand(sampleKey, cmd, args);

ArgumentCaptor<CommandObject<Object>> argumentCaptor = ArgumentCaptor.forClass(CommandObject.class);
verify(commandExecutor).executeCommand(argumentCaptor.capture());

CommandObject<Object> commandObject = argumentCaptor.getValue();
assertThat(commandObject.getArguments(), sameInstance(commandArgumentsWithKey));

assertThat(result, equalTo("OK"));

verify(commandObjects).commandArguments(cmd);
}

}
Loading
Loading