1515 */
1616package org .springframework .data .redis .connection .lettuce ;
1717
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 ;
2241import io .lettuce .core .api .StatefulRedisConnection ;
2342import io .lettuce .core .api .async .RedisAsyncCommands ;
2443import io .lettuce .core .api .sync .RedisCommands ;
4261
4362import org .junit .jupiter .api .BeforeEach ;
4463import org .junit .jupiter .api .Disabled ;
64+ import org .junit .jupiter .api .Nested ;
4565import org .junit .jupiter .api .Test ;
4666import org .mockito .ArgumentCaptor ;
4767import org .mockito .Mockito ;
6080 * @author Christoph Strobl
6181 * @author Mark Paluch
6282 */
63- public class LettuceConnectionUnitTests {
83+ class LettuceConnectionUnitTests {
6484
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 ;
6790
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 {
7394
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 );
7798
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 -> {
81100
82- asyncCommandsMock = Mockito . mock ( RedisAsyncCommands . class , invocation -> {
101+ if ( invocation . getMethod (). getReturnType (). equals ( RedisFuture . class )) {
83102
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 ();
85106
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 );
89112
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+ }
95117
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 > {
100121
101122 @ Test // DATAREDIS-184
102123 public void shutdownWithNullOptionsIsCalledCorrectly () {
@@ -178,8 +199,7 @@ void translatesUnknownExceptions() {
178199 when (asyncCommandsMock .set (any (), any ())).thenThrow (exception );
179200 connection = new LettuceConnection (null , 0 , clientMock , 1 );
180201
181- assertThatThrownBy (() -> connection .set ("foo" .getBytes (), "bar" .getBytes ()))
182- .hasRootCause (exception );
202+ assertThatThrownBy (() -> connection .set ("foo" .getBytes (), "bar" .getBytes ())).hasRootCause (exception );
183203 }
184204
185205 @ Test // DATAREDIS-603
@@ -270,8 +290,8 @@ public List<byte[]> getKeys() {
270290 sc .setCursor (cursorId );
271291 sc .setFinished (false );
272292
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 ) {
275295 @ Override
276296 protected void setOutput (ByteBuffer bytes ) {
277297
@@ -280,10 +300,10 @@ protected void setOutput(ByteBuffer bytes) {
280300 AsyncCommand <byte [], byte [], KeyScanCursor <byte []>> future = new AsyncCommand <>(command );
281301 future .complete ();
282302
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 );
284304
285305 Cursor <byte []> cursor = connection .scan (KeyScanOptions .NONE );
286- cursor .next (); //initial
306+ cursor .next (); // initial
287307 assertThat (cursor .getCursorId ()).isEqualTo (Long .parseUnsignedLong (cursorId ));
288308
289309 cursor .next (); // fetch next
@@ -305,8 +325,8 @@ public List<byte[]> getValues() {
305325 sc .setCursor (cursorId );
306326 sc .setFinished (false );
307327
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 ) {
310330 @ Override
311331 protected void setOutput (ByteBuffer bytes ) {
312332
@@ -315,10 +335,11 @@ protected void setOutput(ByteBuffer bytes) {
315335 AsyncCommand <byte [], byte [], ValueScanCursor <byte []>> future = new AsyncCommand <>(command );
316336 future .complete ();
317337
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 );
319340
320341 Cursor <byte []> cursor = connection .setCommands ().sScan ("key" .getBytes (), KeyScanOptions .NONE );
321- cursor .next (); //initial
342+ cursor .next (); // initial
322343 assertThat (cursor .getCursorId ()).isEqualTo (Long .parseUnsignedLong (cursorId ));
323344
324345 cursor .next (); // fetch next
@@ -340,8 +361,8 @@ public List<ScoredValue<byte[]>> getValues() {
340361 sc .setCursor (cursorId );
341362 sc .setFinished (false );
342363
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 ) {
345366 @ Override
346367 protected void setOutput (ByteBuffer bytes ) {
347368
@@ -350,10 +371,11 @@ protected void setOutput(ByteBuffer bytes) {
350371 AsyncCommand <byte [], byte [], ScoredValueScanCursor <byte []>> future = new AsyncCommand <>(command );
351372 future .complete ();
352373
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 );
354376
355377 Cursor <Tuple > cursor = connection .zSetCommands ().zScan ("key" .getBytes (), KeyScanOptions .NONE );
356- cursor .next (); //initial
378+ cursor .next (); // initial
357379 assertThat (cursor .getCursorId ()).isEqualTo (Long .parseUnsignedLong (cursorId ));
358380
359381 cursor .next (); // fetch next
@@ -375,8 +397,8 @@ public Map<byte[], byte[]> getMap() {
375397 sc .setCursor (cursorId );
376398 sc .setFinished (false );
377399
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 ) {
380402 @ Override
381403 protected void setOutput (ByteBuffer bytes ) {
382404
@@ -385,10 +407,11 @@ protected void setOutput(ByteBuffer bytes) {
385407 AsyncCommand <byte [], byte [], MapScanCursor <byte [], byte []>> future = new AsyncCommand <>(command );
386408 future .complete ();
387409
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 );
389412
390413 Cursor <Entry <byte [], byte []>> cursor = connection .hashCommands ().hScan ("key" .getBytes (), KeyScanOptions .NONE );
391- cursor .next (); //initial
414+ cursor .next (); // initial
392415 assertThat (cursor .getCursorId ()).isEqualTo (Long .parseUnsignedLong (cursorId ));
393416
394417 cursor .next (); // fetch next
@@ -399,13 +422,12 @@ protected void setOutput(ByteBuffer bytes) {
399422
400423 }
401424
402- public static class LettucePipelineConnectionUnitTests extends BasicUnitTests {
425+ @ Nested
426+ class LettucePipelineConnectionUnitTests extends BasicUnitTests {
403427
404- @ Override
405428 @ BeforeEach
406429 public void setUp () throws InvocationTargetException , IllegalAccessException {
407- super .setUp ();
408- this .connection .openPipeline ();
430+ connection .openPipeline ();
409431 }
410432
411433 @ Test // DATAREDIS-528
0 commit comments