20
20
import java .util .Iterator ;
21
21
import java .util .stream .Stream ;
22
22
23
- import org .assertj .core .api .SoftAssertions ;
24
23
import org .junit .jupiter .api .Test ;
25
24
import org .junit .jupiter .params .ParameterizedTest ;
26
25
import org .junit .jupiter .params .provider .Arguments ;
27
26
import org .junit .jupiter .params .provider .MethodSource ;
27
+
28
28
import org .springframework .data .redis .connection .RedisClusterNode ;
29
29
import org .springframework .data .redis .connection .RedisClusterNode .Flag ;
30
30
import org .springframework .data .redis .connection .RedisClusterNode .LinkState ;
31
31
import org .springframework .data .redis .connection .RedisNode .NodeType ;
32
- import org .springframework .data .redis .connection .convert .Converters .ClusterNodesConverter ;
32
+ import org .springframework .data .redis .connection .convert .Converters .ClusterNodesConverter . AddressPortHostname ;
33
33
34
34
/**
35
35
* Unit tests for {@link Converters}.
36
36
*
37
37
* @author Christoph Strobl
38
38
* @author Mark Paluch
39
39
* @author Sorokin Evgeniy
40
+ * @author Marcin Grzejszczak
40
41
*/
41
42
class ConvertersUnitTests {
42
43
@@ -252,37 +253,35 @@ void toClusterNodeWithIPv6Hostname() {
252
253
assertThat (node .getSlotRange ().getSlots ().size ()).isEqualTo (5461 );
253
254
}
254
255
255
- @ Test // https://github.com/spring-projects/spring-data-redis/issues/ 2862
256
+ @ Test // GH- 2862
256
257
void toClusterNodeWithIPv4EmptyHostname () {
258
+
257
259
RedisClusterNode node = Converters .toClusterNode (CLUSTER_NODE_WITH_SINGLE_IPV4_EMPTY_HOSTNAME );
258
260
259
- SoftAssertions .assertSoftly (softAssertions -> {
260
- softAssertions .assertThat (node .getId ()).isEqualTo ("3765733728631672640db35fd2f04743c03119c6" );
261
- softAssertions .assertThat (node .getHost ()).isEqualTo ("10.180.0.33" );
262
- softAssertions .assertThat (node .hasValidHost ()).isTrue ();
263
- softAssertions .assertThat (node .getPort ()).isEqualTo (11003 );
264
- softAssertions .assertThat (node .getType ()).isEqualTo (NodeType .MASTER );
265
- softAssertions .assertThat (node .getFlags ()).contains (Flag .MASTER );
266
- softAssertions .assertThat (node .getLinkState ()).isEqualTo (LinkState .CONNECTED );
267
- softAssertions .assertThat (node .getSlotRange ().getSlots ().size ()).isEqualTo (5461 );
268
- });
261
+ assertThat (node .getId ()).isEqualTo ("3765733728631672640db35fd2f04743c03119c6" );
262
+ assertThat (node .getHost ()).isEqualTo ("10.180.0.33" );
263
+ assertThat (node .hasValidHost ()).isTrue ();
264
+ assertThat (node .getPort ()).isEqualTo (11003 );
265
+ assertThat (node .getType ()).isEqualTo (NodeType .MASTER );
266
+ assertThat (node .getFlags ()).contains (Flag .MASTER );
267
+ assertThat (node .getLinkState ()).isEqualTo (LinkState .CONNECTED );
268
+ assertThat (node .getSlotRange ().getSlots ().size ()).isEqualTo (5461 );
269
269
}
270
270
271
- @ Test // https://github.com/spring-projects/spring-data-redis/issues/ 2862
271
+ @ Test // GH- 2862
272
272
void toClusterNodeWithIPv4Hostname () {
273
+
273
274
RedisClusterNode node = Converters .toClusterNode (CLUSTER_NODE_WITH_SINGLE_IPV4_HOSTNAME );
274
275
275
- SoftAssertions .assertSoftly (softAssertions -> {
276
- softAssertions .assertThat (node .getId ()).isEqualTo ("3765733728631672640db35fd2f04743c03119c6" );
277
- softAssertions .assertThat (node .getHost ()).isEqualTo ("10.180.0.33" );
278
- softAssertions .assertThat (node .getName ()).isEqualTo ("hostname1" );
279
- softAssertions .assertThat (node .hasValidHost ()).isTrue ();
280
- softAssertions .assertThat (node .getPort ()).isEqualTo (11003 );
281
- softAssertions .assertThat (node .getType ()).isEqualTo (NodeType .MASTER );
282
- softAssertions .assertThat (node .getFlags ()).contains (Flag .MASTER );
283
- softAssertions .assertThat (node .getLinkState ()).isEqualTo (LinkState .CONNECTED );
284
- softAssertions .assertThat (node .getSlotRange ().getSlots ().size ()).isEqualTo (5461 );
285
- });
276
+ assertThat (node .getId ()).isEqualTo ("3765733728631672640db35fd2f04743c03119c6" );
277
+ assertThat (node .getHost ()).isEqualTo ("10.180.0.33" );
278
+ assertThat (node .getName ()).isEqualTo ("hostname1" );
279
+ assertThat (node .hasValidHost ()).isTrue ();
280
+ assertThat (node .getPort ()).isEqualTo (11003 );
281
+ assertThat (node .getType ()).isEqualTo (NodeType .MASTER );
282
+ assertThat (node .getFlags ()).contains (Flag .MASTER );
283
+ assertThat (node .getLinkState ()).isEqualTo (LinkState .CONNECTED );
284
+ assertThat (node .getSlotRange ().getSlots ().size ()).isEqualTo (5461 );
286
285
}
287
286
288
287
@ Test // GH-2678
@@ -308,34 +307,64 @@ void toClusterNodeWithInvalidIPv6Hostname() {
308
307
309
308
@ ParameterizedTest // GH-2678
310
309
@ MethodSource ("clusterNodesEndpoints" )
311
- void shouldAcceptHostPatterns (String endpoint , String expectedAddress , String expectedPort , String expectedHostname ) {
310
+ void shouldAcceptHostPatterns (String endpoint , AddressPortHostname expected ) {
312
311
313
- ClusterNodesConverter . AddressPortHostname addressPortHostname = ClusterNodesConverter . AddressPortHostname .of ( new String [] { "id" , endpoint } );
312
+ AddressPortHostname addressPortHostname = AddressPortHostname .parse ( endpoint );
314
313
315
- assertThat (addressPortHostname .addressPart ()).isEqualTo (expectedAddress );
316
- assertThat (addressPortHostname .portPart ()).isEqualTo (expectedPort );
317
- assertThat (addressPortHostname .hostnamePart ()).isEqualTo (expectedHostname );
314
+ assertThat (addressPortHostname ).isEqualTo (expected );
318
315
}
319
316
320
317
static Stream <Arguments > clusterNodesEndpoints () {
321
318
322
- return Stream .of (
319
+ Stream < Arguments > regular = Stream .of (
323
320
// IPv4 with Host, Redis 3
324
- Arguments .of ("1.2.4.4:7379" , "1.2.4.4" , "7379" , null ),
321
+ Arguments .of ("1.2.4.4:7379" , new AddressPortHostname ( "1.2.4.4" , "7379" , null ) ),
325
322
// IPv6 with Host, Redis 3
326
- Arguments .of ("6b8:c67:9c:0:6d8b:33da:5a2c:6380" , "6b8:c67:9c:0:6d8b:33da:5a2c" , "6380" , null ),
323
+ Arguments .of ("6b8:c67:9c:0:6d8b:33da:5a2c:6380" ,
324
+ new AddressPortHostname ("6b8:c67:9c:0:6d8b:33da:5a2c" , "6380" , null )),
327
325
// Assuming IPv6 in brackets with Host, Redis 3
328
- Arguments .of ("[6b8:c67:9c:0:6d8b:33da:5a2c]:6380" , "6b8:c67:9c:0:6d8b:33da:5a2c" , "6380" , null ),
326
+ Arguments .of ("[6b8:c67:9c:0:6d8b:33da:5a2c]:6380" ,
327
+ new AddressPortHostname ("6b8:c67:9c:0:6d8b:33da:5a2c" , "6380" , null )),
329
328
330
329
// IPv4 with Host and Bus Port, Redis 4
331
- Arguments .of ("127.0.0.1:7382@17382" , "127.0.0.1" , "7382" , null ),
330
+ Arguments .of ("127.0.0.1:7382@17382" , new AddressPortHostname ( "127.0.0.1" , "7382" , null ) ),
332
331
// IPv6 with Host and Bus Port, Redis 4
333
- Arguments .of ("6b8:c67:9c:0:6d8b:33da:5a2c:6380" , "6b8:c67:9c:0:6d8b:33da:5a2c" , "6380" , null ),
332
+ Arguments .of ("6b8:c67:9c:0:6d8b:33da:5a2c:6380" ,
333
+ new AddressPortHostname ("6b8:c67:9c:0:6d8b:33da:5a2c" , "6380" , null )),
334
334
335
335
// Hostname with Port and Bus Port, Redis 7
336
- Arguments .of ("my.host-name.com:7379@17379" , "my.host-name.com" , "7379" , null ),
336
+ Arguments .of ("my.host-name.com:7379@17379" , new AddressPortHostname ( "my.host-name.com" , "7379" , null ) ),
337
337
338
338
// With hostname, Redis 7
339
- Arguments .of ("1.2.4.4:7379@17379,my.host-name.com" , "1.2.4.4" , "7379" , "my.host-name.com" ));
339
+ Arguments .of ("1.2.4.4:7379@17379,my.host-name.com" ,
340
+ new AddressPortHostname ("1.2.4.4" , "7379" , "my.host-name.com" )));
341
+
342
+ Stream <Arguments > weird = Stream .of (
343
+ // Port-only
344
+ Arguments .of (":6380" , new AddressPortHostname ("" , "6380" , null )),
345
+
346
+ // Port-only with bus-port
347
+ Arguments .of (":6380@6381" , new AddressPortHostname ("" , "6380" , null )),
348
+ // IP with trailing comma
349
+ Arguments .of ("127.0.0.1:6380," , new AddressPortHostname ("127.0.0.1" , "6380" , null )),
350
+ // IPv6 with bus-port
351
+ Arguments .of ("2a02:6b8:c67:9c:0:6d8b:33da:5a2c:6380@6381" ,
352
+ new AddressPortHostname ("2a02:6b8:c67:9c:0:6d8b:33da:5a2c" , "6380" , null )),
353
+ // IPv6 with bus-port and hostname
354
+ Arguments .of ("2a02:6b8:c67:9c:0:6d8b:33da:5a2c:6380@6381,hostname1" ,
355
+ new AddressPortHostname ("2a02:6b8:c67:9c:0:6d8b:33da:5a2c" , "6380" , "hostname1" )),
356
+ // Port-only with hostname
357
+ Arguments .of (":6380,hostname1" , new AddressPortHostname ("" , "6380" , "hostname1" )),
358
+
359
+ // Port-only with bus-port
360
+ Arguments .of (":6380@6381,hostname1" , new AddressPortHostname ("" , "6380" , "hostname1" )),
361
+ // IPv6 in brackets with bus-port
362
+ Arguments .of ("[2a02:6b8:c67:9c:0:6d8b:33da:5a2c]:6380@6381" ,
363
+ new AddressPortHostname ("2a02:6b8:c67:9c:0:6d8b:33da:5a2c" , "6380" , null )),
364
+ // IPv6 in brackets with bus-port and hostname
365
+ Arguments .of ("[2a02:6b8:c67:9c:0:6d8b:33da:5a2c]:6380@6381,hostname1" ,
366
+ new AddressPortHostname ("2a02:6b8:c67:9c:0:6d8b:33da:5a2c" , "6380" , "hostname1" )));
367
+
368
+ return Stream .concat (regular , weird );
340
369
}
341
370
}
0 commit comments