-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add extensive tests for UnifiedJedis (#3788)
* Align Pipelined commands with UnifiedJedis Add some Pipelined commands that are available in UnifiedJedis but are missing the pipelined version. * Add extensive tests for UnifiedJedis Add mocked tests for UnifiedJedis. Most of the code is covered, except the constructors and some iterator methods. Those are left for later. Take the opportunity to split PipeliningTests into smaller test classes. The idea is to have one superclass (MockCommandObjectsTest) that exposes a variety of mocked CommandObjects. From there we branch in two directions: towards PipeliningBase and towards UnifiedJedis. Each has a test superclass that prepares mocked instances, and a set of concrete test classes that contain the actual tests. The tests are grouped in categories. The names of the categories are the same as used on the redis.io website in the commands documentation page. Inside each section (i.e. test class), the tested commands are ordered alphabetically. The tests run in pairs, one for the string-based command, and one for the equivalent byte-array-based command. Hopefully this gives a good structure for following the code. A new public constructor is needed in UnifiedJedis, in order to inject the mocks. Also, due to method visibility, one test class for UnifiedJedis must reside in the same package as the class itself. * Rename to subject under test to jedis * Undo script commands changes * Remove tests for removed code * Mark new constructor as visible for testing * React to code review Rename classes as suggested. Move some tests into better suited classed. Remove everything Graph related. * Fix unit tests And add two tests for recently added code. --------- Co-authored-by: Gabriel Erzse <[email protected]> Co-authored-by: M Sazzadul Hoque <[email protected]>
- Loading branch information
1 parent
fac0ae9
commit c7a1687
Showing
51 changed files
with
27,158 additions
and
10,503 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10,502 changes: 0 additions & 10,502 deletions
10,502
src/test/java/redis/clients/jedis/PipeliningBaseTest.java
This file was deleted.
Oops, something went wrong.
263 changes: 263 additions & 0 deletions
263
src/test/java/redis/clients/jedis/UnifiedJedisCustomCommandsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
Oops, something went wrong.