2020import java .util .Iterator ;
2121import java .util .stream .Stream ;
2222
23- import org .assertj .core .api .SoftAssertions ;
2423import org .junit .jupiter .api .Test ;
2524import org .junit .jupiter .params .ParameterizedTest ;
2625import org .junit .jupiter .params .provider .Arguments ;
2726import org .junit .jupiter .params .provider .MethodSource ;
27+
2828import org .springframework .data .redis .connection .RedisClusterNode ;
2929import org .springframework .data .redis .connection .RedisClusterNode .Flag ;
3030import org .springframework .data .redis .connection .RedisClusterNode .LinkState ;
3131import 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 ;
3333
3434/**
3535 * Unit tests for {@link Converters}.
3636 *
3737 * @author Christoph Strobl
3838 * @author Mark Paluch
3939 * @author Sorokin Evgeniy
40+ * @author Marcin Grzejszczak
4041 */
4142class ConvertersUnitTests {
4243
@@ -252,37 +253,35 @@ void toClusterNodeWithIPv6Hostname() {
252253assertThat (node .getSlotRange ().getSlots ().size ()).isEqualTo (5461 );
253254}
254255
255- @ Test // https://github.com/spring-projects/spring-data-redis/issues/ 2862
256+ @ Test // GH- 2862
256257void toClusterNodeWithIPv4EmptyHostname () {
258+
257259RedisClusterNode node = Converters .toClusterNode (CLUSTER_NODE_WITH_SINGLE_IPV4_EMPTY_HOSTNAME );
258260
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 );
269269}
270270
271- @ Test // https://github.com/spring-projects/spring-data-redis/issues/ 2862
271+ @ Test // GH- 2862
272272void toClusterNodeWithIPv4Hostname () {
273+
273274RedisClusterNode node = Converters .toClusterNode (CLUSTER_NODE_WITH_SINGLE_IPV4_HOSTNAME );
274275
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 );
286285}
287286
288287@ Test // GH-2678
@@ -308,34 +307,64 @@ void toClusterNodeWithInvalidIPv6Hostname() {
308307
309308@ ParameterizedTest // GH-2678
310309@ MethodSource ("clusterNodesEndpoints" )
311- void shouldAcceptHostPatterns (String endpoint , String expectedAddress , String expectedPort , String expectedHostname ) {
310+ void shouldAcceptHostPatterns (String endpoint , AddressPortHostname expected ) {
312311
313- ClusterNodesConverter . AddressPortHostname addressPortHostname = ClusterNodesConverter . AddressPortHostname .of ( new String [] { "id" , endpoint } );
312+ AddressPortHostname addressPortHostname = AddressPortHostname .parse ( endpoint );
314313
315- assertThat (addressPortHostname .addressPart ()).isEqualTo (expectedAddress );
316- assertThat (addressPortHostname .portPart ()).isEqualTo (expectedPort );
317- assertThat (addressPortHostname .hostnamePart ()).isEqualTo (expectedHostname );
314+ assertThat (addressPortHostname ).isEqualTo (expected );
318315}
319316
320317static Stream <Arguments > clusterNodesEndpoints () {
321318
322- return Stream .of (
319+ Stream < Arguments > regular = Stream .of (
323320// 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 ) ),
325322// 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 )),
327325// 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 )),
329328
330329// 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 ) ),
332331// 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 )),
334334
335335// 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 ) ),
337337
338338// 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 );
340369}
341370}
0 commit comments