15
15
*/
16
16
package org .springframework .data .redis .connection .lettuce ;
17
17
18
- import static org .assertj .core .api .Assertions .*;
19
- import static org .mockito .Mockito .*;
20
-
21
- import io .lettuce .core .*;
18
+ import static org .assertj .core .api .Assertions .assertThat ;
19
+ import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
20
+ import static org .assertj .core .api .Assertions .assertThatIllegalArgumentException ;
21
+ import static org .assertj .core .api .Assertions .assertThatThrownBy ;
22
+ import static org .mockito .Mockito .any ;
23
+ import static org .mockito .Mockito .anyMap ;
24
+ import static org .mockito .Mockito .eq ;
25
+ import static org .mockito .Mockito .mock ;
26
+ import static org .mockito .Mockito .times ;
27
+ import static org .mockito .Mockito .verify ;
28
+ import static org .mockito .Mockito .when ;
29
+
30
+ import io .lettuce .core .KeyScanCursor ;
31
+ import io .lettuce .core .MapScanCursor ;
32
+ import io .lettuce .core .RedisClient ;
33
+ import io .lettuce .core .RedisFuture ;
34
+ import io .lettuce .core .ScanArgs ;
35
+ import io .lettuce .core .ScanCursor ;
36
+ import io .lettuce .core .ScoredValue ;
37
+ import io .lettuce .core .ScoredValueScanCursor ;
38
+ import io .lettuce .core .ValueScanCursor ;
39
+ import io .lettuce .core .XAddArgs ;
40
+ import io .lettuce .core .XClaimArgs ;
22
41
import io .lettuce .core .api .StatefulRedisConnection ;
23
42
import io .lettuce .core .api .async .RedisAsyncCommands ;
24
43
import io .lettuce .core .api .sync .RedisCommands ;
42
61
43
62
import org .junit .jupiter .api .BeforeEach ;
44
63
import org .junit .jupiter .api .Disabled ;
64
+ import org .junit .jupiter .api .Nested ;
45
65
import org .junit .jupiter .api .Test ;
46
66
import org .mockito .ArgumentCaptor ;
47
67
import org .mockito .Mockito ;
60
80
* @author Christoph Strobl
61
81
* @author Mark Paluch
62
82
*/
63
- public class LettuceConnectionUnitTests {
83
+ class LettuceConnectionUnitTests {
64
84
65
- @ SuppressWarnings ("rawtypes" )
66
- public static class BasicUnitTests extends AbstractConnectionUnitTestBase <RedisAsyncCommands > {
85
+ protected LettuceConnection connection ;
86
+ private RedisClient clientMock ;
87
+ StatefulRedisConnection <byte [], byte []> statefulConnectionMock ;
88
+ RedisAsyncCommands <byte [], byte []> asyncCommandsMock ;
89
+ RedisCommands <byte [], byte []> commandsMock ;
67
90
68
- protected LettuceConnection connection ;
69
- private RedisClient clientMock ;
70
- StatefulRedisConnection <byte [], byte []> statefulConnectionMock ;
71
- RedisAsyncCommands <byte [], byte []> asyncCommandsMock ;
72
- RedisCommands <byte [], byte []> commandsMock ;
91
+ @ BeforeEach
92
+ @ SuppressWarnings ({ "rawtypes" , "unchecked" })
93
+ public void setUp () throws InvocationTargetException , IllegalAccessException {
73
94
74
- @ SuppressWarnings ({ "unchecked" })
75
- @ BeforeEach
76
- public void setUp () throws InvocationTargetException , IllegalAccessException {
95
+ clientMock = mock ( RedisClient . class );
96
+ statefulConnectionMock = mock ( StatefulRedisConnection . class );
97
+ when ( clientMock . connect (( RedisCodec ) any ())). thenReturn ( statefulConnectionMock );
77
98
78
- clientMock = mock (RedisClient .class );
79
- statefulConnectionMock = mock (StatefulRedisConnection .class );
80
- when (clientMock .connect ((RedisCodec ) any ())).thenReturn (statefulConnectionMock );
99
+ asyncCommandsMock = Mockito .mock (RedisAsyncCommands .class , invocation -> {
81
100
82
- asyncCommandsMock = Mockito . mock ( RedisAsyncCommands . class , invocation -> {
101
+ if ( invocation . getMethod (). getReturnType (). equals ( RedisFuture . class )) {
83
102
84
- if (invocation .getMethod ().getReturnType ().equals (RedisFuture .class )) {
103
+ Command <?, ?, ?> cmd = new Command <>(CommandType .PING , new StatusOutput <>(StringCodec .UTF8 ));
104
+ AsyncCommand <?, ?, ?> async = new AsyncCommand <>(cmd );
105
+ async .complete ();
85
106
86
- Command <?, ?, ?> cmd = new Command <>(CommandType .PING , new StatusOutput <>(StringCodec .UTF8 ));
87
- AsyncCommand <?, ?, ?> async = new AsyncCommand <>(cmd );
88
- async .complete ();
107
+ return async ;
108
+ }
109
+ return null ;
110
+ });
111
+ commandsMock = Mockito .mock (RedisCommands .class );
89
112
90
- return async ;
91
- }
92
- return null ;
93
- });
94
- commandsMock = Mockito .mock (RedisCommands .class );
113
+ when (statefulConnectionMock .async ()).thenReturn (asyncCommandsMock );
114
+ when (statefulConnectionMock .sync ()).thenReturn (commandsMock );
115
+ connection = new LettuceConnection (0 , clientMock );
116
+ }
95
117
96
- when (statefulConnectionMock .async ()).thenReturn (asyncCommandsMock );
97
- when (statefulConnectionMock .sync ()).thenReturn (commandsMock );
98
- connection = new LettuceConnection (0 , clientMock );
99
- }
118
+ @ Nested
119
+ @ SuppressWarnings ({ "rawtypes" , "deprecation" })
120
+ class BasicUnitTests extends AbstractConnectionUnitTestBase <RedisAsyncCommands > {
100
121
101
122
@ Test // DATAREDIS-184
102
123
public void shutdownWithNullOptionsIsCalledCorrectly () {
@@ -178,8 +199,7 @@ void translatesUnknownExceptions() {
178
199
when (asyncCommandsMock .set (any (), any ())).thenThrow (exception );
179
200
connection = new LettuceConnection (null , 0 , clientMock , 1 );
180
201
181
- assertThatThrownBy (() -> connection .set ("foo" .getBytes (), "bar" .getBytes ()))
182
- .hasRootCause (exception );
202
+ assertThatThrownBy (() -> connection .set ("foo" .getBytes (), "bar" .getBytes ())).hasRootCause (exception );
183
203
}
184
204
185
205
@ Test // DATAREDIS-603
@@ -270,8 +290,8 @@ public List<byte[]> getKeys() {
270
290
sc .setCursor (cursorId );
271
291
sc .setFinished (false );
272
292
273
- Command <byte [], byte [], KeyScanCursor <byte []>> command = new Command <>(new LettuceConnection . CustomCommandType ( "SCAN" ),
274
- new ScanOutput <>(ByteArrayCodec .INSTANCE , sc ) {
293
+ Command <byte [], byte [], KeyScanCursor <byte []>> command = new Command <>(
294
+ new LettuceConnection . CustomCommandType ( "SCAN" ), new ScanOutput <>(ByteArrayCodec .INSTANCE , sc ) {
275
295
@ Override
276
296
protected void setOutput (ByteBuffer bytes ) {
277
297
@@ -280,10 +300,10 @@ protected void setOutput(ByteBuffer bytes) {
280
300
AsyncCommand <byte [], byte [], KeyScanCursor <byte []>> future = new AsyncCommand <>(command );
281
301
future .complete ();
282
302
283
- when (asyncCommandsMock .scan (any (ScanCursor .class ),any (ScanArgs .class ))).thenReturn (future , future );
303
+ when (asyncCommandsMock .scan (any (ScanCursor .class ), any (ScanArgs .class ))).thenReturn (future , future );
284
304
285
305
Cursor <byte []> cursor = connection .scan (KeyScanOptions .NONE );
286
- cursor .next (); //initial
306
+ cursor .next (); // initial
287
307
assertThat (cursor .getCursorId ()).isEqualTo (Long .parseUnsignedLong (cursorId ));
288
308
289
309
cursor .next (); // fetch next
@@ -305,8 +325,8 @@ public List<byte[]> getValues() {
305
325
sc .setCursor (cursorId );
306
326
sc .setFinished (false );
307
327
308
- Command <byte [], byte [], ValueScanCursor <byte []>> command = new Command <>(new LettuceConnection . CustomCommandType ( "SSCAN" ),
309
- new ScanOutput <>(ByteArrayCodec .INSTANCE , sc ) {
328
+ Command <byte [], byte [], ValueScanCursor <byte []>> command = new Command <>(
329
+ new LettuceConnection . CustomCommandType ( "SSCAN" ), new ScanOutput <>(ByteArrayCodec .INSTANCE , sc ) {
310
330
@ Override
311
331
protected void setOutput (ByteBuffer bytes ) {
312
332
@@ -315,10 +335,11 @@ protected void setOutput(ByteBuffer bytes) {
315
335
AsyncCommand <byte [], byte [], ValueScanCursor <byte []>> future = new AsyncCommand <>(command );
316
336
future .complete ();
317
337
318
- when (asyncCommandsMock .sscan (any (byte [].class ), any (ScanCursor .class ),any (ScanArgs .class ))).thenReturn (future , future );
338
+ when (asyncCommandsMock .sscan (any (byte [].class ), any (ScanCursor .class ), any (ScanArgs .class ))).thenReturn (future ,
339
+ future );
319
340
320
341
Cursor <byte []> cursor = connection .setCommands ().sScan ("key" .getBytes (), KeyScanOptions .NONE );
321
- cursor .next (); //initial
342
+ cursor .next (); // initial
322
343
assertThat (cursor .getCursorId ()).isEqualTo (Long .parseUnsignedLong (cursorId ));
323
344
324
345
cursor .next (); // fetch next
@@ -340,8 +361,8 @@ public List<ScoredValue<byte[]>> getValues() {
340
361
sc .setCursor (cursorId );
341
362
sc .setFinished (false );
342
363
343
- Command <byte [], byte [], ScoredValueScanCursor <byte []>> command = new Command <>(new LettuceConnection . CustomCommandType ( "ZSCAN" ),
344
- new ScanOutput <>(ByteArrayCodec .INSTANCE , sc ) {
364
+ Command <byte [], byte [], ScoredValueScanCursor <byte []>> command = new Command <>(
365
+ new LettuceConnection . CustomCommandType ( "ZSCAN" ), new ScanOutput <>(ByteArrayCodec .INSTANCE , sc ) {
345
366
@ Override
346
367
protected void setOutput (ByteBuffer bytes ) {
347
368
@@ -350,10 +371,11 @@ protected void setOutput(ByteBuffer bytes) {
350
371
AsyncCommand <byte [], byte [], ScoredValueScanCursor <byte []>> future = new AsyncCommand <>(command );
351
372
future .complete ();
352
373
353
- when (asyncCommandsMock .zscan (any (byte [].class ), any (ScanCursor .class ),any (ScanArgs .class ))).thenReturn (future , future );
374
+ when (asyncCommandsMock .zscan (any (byte [].class ), any (ScanCursor .class ), any (ScanArgs .class ))).thenReturn (future ,
375
+ future );
354
376
355
377
Cursor <Tuple > cursor = connection .zSetCommands ().zScan ("key" .getBytes (), KeyScanOptions .NONE );
356
- cursor .next (); //initial
378
+ cursor .next (); // initial
357
379
assertThat (cursor .getCursorId ()).isEqualTo (Long .parseUnsignedLong (cursorId ));
358
380
359
381
cursor .next (); // fetch next
@@ -375,8 +397,8 @@ public Map<byte[], byte[]> getMap() {
375
397
sc .setCursor (cursorId );
376
398
sc .setFinished (false );
377
399
378
- Command <byte [], byte [], MapScanCursor <byte [], byte []>> command = new Command <>(new LettuceConnection . CustomCommandType ( "HSCAN" ),
379
- new ScanOutput <>(ByteArrayCodec .INSTANCE , sc ) {
400
+ Command <byte [], byte [], MapScanCursor <byte [], byte []>> command = new Command <>(
401
+ new LettuceConnection . CustomCommandType ( "HSCAN" ), new ScanOutput <>(ByteArrayCodec .INSTANCE , sc ) {
380
402
@ Override
381
403
protected void setOutput (ByteBuffer bytes ) {
382
404
@@ -385,10 +407,11 @@ protected void setOutput(ByteBuffer bytes) {
385
407
AsyncCommand <byte [], byte [], MapScanCursor <byte [], byte []>> future = new AsyncCommand <>(command );
386
408
future .complete ();
387
409
388
- when (asyncCommandsMock .hscan (any (byte [].class ), any (ScanCursor .class ),any (ScanArgs .class ))).thenReturn (future , future );
410
+ when (asyncCommandsMock .hscan (any (byte [].class ), any (ScanCursor .class ), any (ScanArgs .class ))).thenReturn (future ,
411
+ future );
389
412
390
413
Cursor <Entry <byte [], byte []>> cursor = connection .hashCommands ().hScan ("key" .getBytes (), KeyScanOptions .NONE );
391
- cursor .next (); //initial
414
+ cursor .next (); // initial
392
415
assertThat (cursor .getCursorId ()).isEqualTo (Long .parseUnsignedLong (cursorId ));
393
416
394
417
cursor .next (); // fetch next
@@ -399,13 +422,12 @@ protected void setOutput(ByteBuffer bytes) {
399
422
400
423
}
401
424
402
- public static class LettucePipelineConnectionUnitTests extends BasicUnitTests {
425
+ @ Nested
426
+ class LettucePipelineConnectionUnitTests extends BasicUnitTests {
403
427
404
- @ Override
405
428
@ BeforeEach
406
429
public void setUp () throws InvocationTargetException , IllegalAccessException {
407
- super .setUp ();
408
- this .connection .openPipeline ();
430
+ connection .openPipeline ();
409
431
}
410
432
411
433
@ Test // DATAREDIS-528
0 commit comments