Skip to content

Commit c7a1687

Browse files
gerzsesazzad16
andauthored
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]>
1 parent fac0ae9 commit c7a1687

File tree

51 files changed

+27158
-10503
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+27158
-10503
lines changed

Diff for: src/main/java/redis/clients/jedis/UnifiedJedis.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.json.JSONArray;
1111

1212
import redis.clients.jedis.annots.Experimental;
13+
import redis.clients.jedis.annots.VisibleForTesting;
1314
import redis.clients.jedis.args.*;
1415
import redis.clients.jedis.bloom.*;
1516
import redis.clients.jedis.commands.JedisCommands;
@@ -213,7 +214,8 @@ private UnifiedJedis(CommandExecutor executor, ConnectionProvider provider) {
213214
}
214215

215216
// Uses a fetched connection to process protocol. Should be avoided if possible.
216-
private UnifiedJedis(CommandExecutor executor, ConnectionProvider provider, CommandObjects commandObjects) {
217+
@VisibleForTesting
218+
public UnifiedJedis(CommandExecutor executor, ConnectionProvider provider, CommandObjects commandObjects) {
217219
this(executor, provider, commandObjects, null);
218220
if (this.provider != null) {
219221
try (Connection conn = this.provider.getConnection()) {

Diff for: src/test/java/redis/clients/jedis/PipeliningBaseTest.java

-10,502
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
1+
package redis.clients.jedis;
2+
3+
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.hamcrest.Matchers.equalTo;
5+
import static org.hamcrest.Matchers.sameInstance;
6+
import static org.mockito.ArgumentMatchers.any;
7+
import static org.mockito.Mockito.mock;
8+
import static org.mockito.Mockito.verify;
9+
import static org.mockito.Mockito.when;
10+
11+
import org.junit.Test;
12+
import org.mockito.ArgumentCaptor;
13+
import redis.clients.jedis.commands.ProtocolCommand;
14+
import redis.clients.jedis.mocked.unified.UnifiedJedisMockedTestBase;
15+
16+
/**
17+
* These tests are part of the mocked tests for {@link UnifiedJedis}, but, due to {@code protected}
18+
* visibility of some methods, they must reside in the same package as the tested class.
19+
*/
20+
public class UnifiedJedisCustomCommandsTest extends UnifiedJedisMockedTestBase {
21+
22+
@Test
23+
public void testSendCommandWithProtocolCommand() {
24+
ProtocolCommand cmd = mock(ProtocolCommand.class);
25+
CommandArguments commandArguments = mock(CommandArguments.class);
26+
27+
when(commandObjects.commandArguments(cmd)).thenReturn(commandArguments);
28+
when(commandExecutor.executeCommand(any())).thenReturn("OK");
29+
30+
Object result = jedis.sendCommand(cmd);
31+
32+
ArgumentCaptor<CommandObject<Object>> argumentCaptor = ArgumentCaptor.forClass(CommandObject.class);
33+
verify(commandExecutor).executeCommand(argumentCaptor.capture());
34+
35+
CommandObject<Object> commandObject = argumentCaptor.getValue();
36+
assertThat(commandObject.getArguments(), sameInstance(commandArguments));
37+
38+
assertThat(result, equalTo("OK"));
39+
40+
verify(commandObjects).commandArguments(cmd);
41+
}
42+
43+
@Test
44+
public void testSendCommandWithProtocolCommandAndByteArrayArgs() {
45+
ProtocolCommand cmd = mock(ProtocolCommand.class);
46+
byte[][] args = { "arg1".getBytes(), "arg2".getBytes() };
47+
CommandArguments commandArguments = mock(CommandArguments.class);
48+
CommandArguments commandArgumentsWithArgs = mock(CommandArguments.class);
49+
when(commandArguments.addObjects(args)).thenReturn(commandArgumentsWithArgs);
50+
51+
when(commandObjects.commandArguments(cmd)).thenReturn(commandArguments);
52+
when(commandExecutor.executeCommand(any())).thenReturn("OK");
53+
54+
Object result = jedis.sendCommand(cmd, args);
55+
56+
ArgumentCaptor<CommandObject<Object>> argumentCaptor = ArgumentCaptor.forClass(CommandObject.class);
57+
verify(commandExecutor).executeCommand(argumentCaptor.capture());
58+
59+
CommandObject<Object> commandObject = argumentCaptor.getValue();
60+
assertThat(commandObject.getArguments(), sameInstance(commandArgumentsWithArgs));
61+
62+
assertThat(result, equalTo("OK"));
63+
64+
verify(commandObjects).commandArguments(cmd);
65+
}
66+
67+
@Test
68+
public void testSendBlockingCommandWithProtocolCommandAndByteArrayArgs() {
69+
ProtocolCommand cmd = mock(ProtocolCommand.class);
70+
byte[][] args = { "arg1".getBytes(), "arg2".getBytes() };
71+
CommandArguments commandArguments = mock(CommandArguments.class);
72+
73+
CommandArguments commandArgumentsBlocking = mock(CommandArguments.class);
74+
CommandArguments commandArgumentsWithArgs = mock(CommandArguments.class);
75+
when(commandArgumentsWithArgs.blocking()).thenReturn(commandArgumentsBlocking);
76+
77+
when(commandArguments.addObjects(args)).thenReturn(commandArgumentsWithArgs);
78+
79+
when(commandObjects.commandArguments(cmd)).thenReturn(commandArguments);
80+
when(commandExecutor.executeCommand(any())).thenReturn("OK");
81+
82+
Object result = jedis.sendBlockingCommand(cmd, args);
83+
84+
ArgumentCaptor<CommandObject<Object>> argumentCaptor = ArgumentCaptor.forClass(CommandObject.class);
85+
verify(commandExecutor).executeCommand(argumentCaptor.capture());
86+
87+
CommandObject<Object> commandObject = argumentCaptor.getValue();
88+
assertThat(commandObject.getArguments(), sameInstance(commandArgumentsBlocking));
89+
90+
assertThat(result, equalTo("OK"));
91+
92+
verify(commandObjects).commandArguments(cmd);
93+
}
94+
95+
@Test
96+
public void testSendCommandWithProtocolCommandAndStringArgs() {
97+
ProtocolCommand cmd = mock(ProtocolCommand.class);
98+
String[] args = { "arg1", "arg2" };
99+
CommandArguments commandArguments = mock(CommandArguments.class);
100+
CommandArguments commandArgumentsWithArgs = mock(CommandArguments.class);
101+
when(commandArguments.addObjects(args)).thenReturn(commandArgumentsWithArgs);
102+
103+
when(commandObjects.commandArguments(cmd)).thenReturn(commandArguments);
104+
when(commandExecutor.executeCommand(any())).thenReturn("OK");
105+
106+
Object result = jedis.sendCommand(cmd, args);
107+
108+
ArgumentCaptor<CommandObject<Object>> argumentCaptor = ArgumentCaptor.forClass(CommandObject.class);
109+
verify(commandExecutor).executeCommand(argumentCaptor.capture());
110+
111+
CommandObject<Object> commandObject = argumentCaptor.getValue();
112+
assertThat(commandObject.getArguments(), sameInstance(commandArgumentsWithArgs));
113+
114+
assertThat(result, equalTo("OK"));
115+
116+
verify(commandObjects).commandArguments(cmd);
117+
}
118+
119+
@Test
120+
public void testSendBlockingCommandWithProtocolCommandAndStringArgs() {
121+
ProtocolCommand cmd = mock(ProtocolCommand.class);
122+
String[] args = { "arg1", "arg2" };
123+
CommandArguments commandArguments = mock(CommandArguments.class);
124+
125+
CommandArguments commandArgumentsBlocking = mock(CommandArguments.class);
126+
CommandArguments commandArgumentsWithArgs = mock(CommandArguments.class);
127+
when(commandArgumentsWithArgs.blocking()).thenReturn(commandArgumentsBlocking);
128+
129+
when(commandArguments.addObjects(args)).thenReturn(commandArgumentsWithArgs);
130+
131+
when(commandObjects.commandArguments(cmd)).thenReturn(commandArguments);
132+
when(commandExecutor.executeCommand(any())).thenReturn("OK");
133+
134+
Object result = jedis.sendBlockingCommand(cmd, args);
135+
136+
ArgumentCaptor<CommandObject<Object>> argumentCaptor = ArgumentCaptor.forClass(CommandObject.class);
137+
verify(commandExecutor).executeCommand(argumentCaptor.capture());
138+
139+
CommandObject<Object> commandObject = argumentCaptor.getValue();
140+
assertThat(commandObject.getArguments(), sameInstance(commandArgumentsBlocking));
141+
142+
assertThat(result, equalTo("OK"));
143+
144+
verify(commandObjects).commandArguments(cmd);
145+
}
146+
147+
@Test
148+
public void testSendCommandWithSampleKeyProtocolCommandAndByteArrayArgs() {
149+
byte[] sampleKey = "key".getBytes();
150+
ProtocolCommand cmd = mock(ProtocolCommand.class);
151+
byte[][] args = { "arg1".getBytes(), "arg2".getBytes() };
152+
CommandArguments commandArguments = mock(CommandArguments.class);
153+
CommandArguments commandArgumentsWithArgs = mock(CommandArguments.class);
154+
CommandArguments commandArgumentsWithKey = mock(CommandArguments.class);
155+
156+
when(commandArguments.addObjects((Object[]) args)).thenReturn(commandArgumentsWithArgs);
157+
when(commandArgumentsWithArgs.processKey(sampleKey)).thenReturn(commandArgumentsWithKey);
158+
159+
when(commandObjects.commandArguments(cmd)).thenReturn(commandArguments);
160+
when(commandExecutor.executeCommand(any())).thenReturn("OK");
161+
162+
Object result = jedis.sendCommand(sampleKey, cmd, args);
163+
164+
ArgumentCaptor<CommandObject<Object>> argumentCaptor = ArgumentCaptor.forClass(CommandObject.class);
165+
verify(commandExecutor).executeCommand(argumentCaptor.capture());
166+
167+
CommandObject<Object> commandObject = argumentCaptor.getValue();
168+
assertThat(commandObject.getArguments(), sameInstance(commandArgumentsWithKey));
169+
170+
assertThat(result, equalTo("OK"));
171+
172+
verify(commandObjects).commandArguments(cmd);
173+
}
174+
175+
@Test
176+
public void testSendBlockingCommandWithSampleKeyProtocolCommandAndByteArrayArgs() {
177+
byte[] sampleKey = "key".getBytes();
178+
ProtocolCommand cmd = mock(ProtocolCommand.class);
179+
byte[][] args = { "arg1".getBytes(), "arg2".getBytes() };
180+
CommandArguments commandArguments = mock(CommandArguments.class);
181+
CommandArguments commandArgumentsWithArgs = mock(CommandArguments.class);
182+
CommandArguments commandArgumentsBlocking = mock(CommandArguments.class);
183+
CommandArguments commandArgumentsWithKey = mock(CommandArguments.class);
184+
185+
when(commandArguments.addObjects((Object[]) args)).thenReturn(commandArgumentsWithArgs);
186+
when(commandArgumentsWithArgs.blocking()).thenReturn(commandArgumentsBlocking);
187+
when(commandArgumentsBlocking.processKey(sampleKey)).thenReturn(commandArgumentsWithKey);
188+
189+
when(commandObjects.commandArguments(cmd)).thenReturn(commandArguments);
190+
when(commandExecutor.executeCommand(any())).thenReturn("OK");
191+
192+
Object result = jedis.sendBlockingCommand(sampleKey, cmd, args);
193+
194+
ArgumentCaptor<CommandObject<Object>> argumentCaptor = ArgumentCaptor.forClass(CommandObject.class);
195+
verify(commandExecutor).executeCommand(argumentCaptor.capture());
196+
197+
CommandObject<Object> commandObject = argumentCaptor.getValue();
198+
assertThat(commandObject.getArguments(), sameInstance(commandArgumentsWithKey));
199+
200+
assertThat(result, equalTo("OK"));
201+
202+
verify(commandObjects).commandArguments(cmd);
203+
}
204+
205+
@Test
206+
public void testSendCommandWithStringSampleKeyProtocolCommandAndStringArgs() {
207+
String sampleKey = "key";
208+
ProtocolCommand cmd = mock(ProtocolCommand.class);
209+
String[] args = { "arg1", "arg2" };
210+
CommandArguments commandArguments = mock(CommandArguments.class);
211+
CommandArguments commandArgumentsWithArgs = mock(CommandArguments.class);
212+
CommandArguments commandArgumentsWithKey = mock(CommandArguments.class);
213+
214+
when(commandArguments.addObjects((Object[]) args)).thenReturn(commandArgumentsWithArgs);
215+
when(commandArgumentsWithArgs.processKey(sampleKey)).thenReturn(commandArgumentsWithKey);
216+
217+
when(commandObjects.commandArguments(cmd)).thenReturn(commandArguments);
218+
when(commandExecutor.executeCommand(any())).thenReturn("OK");
219+
220+
Object result = jedis.sendCommand(sampleKey, cmd, args);
221+
222+
ArgumentCaptor<CommandObject<Object>> argumentCaptor = ArgumentCaptor.forClass(CommandObject.class);
223+
verify(commandExecutor).executeCommand(argumentCaptor.capture());
224+
225+
CommandObject<Object> commandObject = argumentCaptor.getValue();
226+
assertThat(commandObject.getArguments(), sameInstance(commandArgumentsWithKey));
227+
228+
assertThat(result, equalTo("OK"));
229+
230+
verify(commandObjects).commandArguments(cmd);
231+
}
232+
233+
@Test
234+
public void testSendBlockingCommandWithStringSampleKeyProtocolCommandAndStringArgs() {
235+
String sampleKey = "key";
236+
ProtocolCommand cmd = mock(ProtocolCommand.class);
237+
String[] args = { "arg1", "arg2" };
238+
CommandArguments commandArguments = mock(CommandArguments.class);
239+
CommandArguments commandArgumentsWithArgs = mock(CommandArguments.class);
240+
CommandArguments commandArgumentsBlocking = mock(CommandArguments.class);
241+
CommandArguments commandArgumentsWithKey = mock(CommandArguments.class);
242+
243+
when(commandArguments.addObjects((Object[]) args)).thenReturn(commandArgumentsWithArgs);
244+
when(commandArgumentsWithArgs.blocking()).thenReturn(commandArgumentsBlocking);
245+
when(commandArgumentsBlocking.processKey(sampleKey)).thenReturn(commandArgumentsWithKey);
246+
247+
when(commandObjects.commandArguments(cmd)).thenReturn(commandArguments);
248+
when(commandExecutor.executeCommand(any())).thenReturn("OK");
249+
250+
Object result = jedis.sendBlockingCommand(sampleKey, cmd, args);
251+
252+
ArgumentCaptor<CommandObject<Object>> argumentCaptor = ArgumentCaptor.forClass(CommandObject.class);
253+
verify(commandExecutor).executeCommand(argumentCaptor.capture());
254+
255+
CommandObject<Object> commandObject = argumentCaptor.getValue();
256+
assertThat(commandObject.getArguments(), sameInstance(commandArgumentsWithKey));
257+
258+
assertThat(result, equalTo("OK"));
259+
260+
verify(commandObjects).commandArguments(cmd);
261+
}
262+
263+
}

0 commit comments

Comments
 (0)