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
10 changes: 10 additions & 0 deletions 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 @@ -239,6 +240,15 @@ private UnifiedJedis(CommandExecutor executor, ConnectionProvider provider, Comm
this.graphCommandObjects.setBaseCommandArgumentsCreator((comm) -> this.commandObjects.commandArguments(comm));
}

@VisibleForTesting
public UnifiedJedis(CommandObjects commandObjects, GraphCommandObjects graphCommandObjects,
CommandExecutor executor, ConnectionProvider provider) {
sazzad16 marked this conversation as resolved.
Show resolved Hide resolved
sazzad16 marked this conversation as resolved.
Show resolved Hide resolved
this.provider = provider;
this.executor = executor;
this.commandObjects = commandObjects;
this.graphCommandObjects = graphCommandObjects;
}

@Override
public void close() {
IOUtils.closeQuietly(this.executor);
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/UnifiedJedisTest.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.UnifiedJedisTestBase;

/**
* 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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you mention which ones? We can check if making those public makes sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I was wrong, it's not UnifiedJedis. It is about:

  • CommandObjects.commandArguments(ProtocolComand)
  • CommandArguments.processKey(*)

*/
public class UnifiedJedisTest extends UnifiedJedisTestBase {
sazzad16 marked this conversation as resolved.
Show resolved Hide resolved

@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