1818import static org .assertj .core .api .Assertions .*;
1919
2020import java .util .Iterator ;
21+ import java .util .regex .Matcher ;
22+ import java .util .stream .Stream ;
2123
2224import org .junit .jupiter .api .Test ;
23-
25+ import org .junit .jupiter .params .ParameterizedTest ;
26+ import org .junit .jupiter .params .provider .Arguments ;
27+ import org .junit .jupiter .params .provider .MethodSource ;
2428import org .springframework .data .redis .connection .RedisClusterNode ;
2529import org .springframework .data .redis .connection .RedisClusterNode .Flag ;
2630import org .springframework .data .redis .connection .RedisClusterNode .LinkState ;
2731import org .springframework .data .redis .connection .RedisNode .NodeType ;
32+ import org .springframework .data .redis .connection .convert .Converters .ClusterNodesConverter ;
2833
2934/**
3035 * Unit tests for {@link Converters}.
@@ -184,8 +189,8 @@ void toSetOfRedisClusterNodesShouldConvertNodesWithSingleSlotCorrectly() {
184189 @ Test // DATAREDIS-315
185190 void toSetOfRedisClusterNodesShouldParseLinkStateAndDisconnectedCorrectly () {
186191
187- Iterator <RedisClusterNode > nodes = Converters . toSetOfRedisClusterNodes (
188- CLUSTER_NODE_WITH_FAIL_FLAG_AND_DISCONNECTED_LINK_STATE ).iterator ();
192+ Iterator <RedisClusterNode > nodes = Converters
193+ . toSetOfRedisClusterNodes ( CLUSTER_NODE_WITH_FAIL_FLAG_AND_DISCONNECTED_LINK_STATE ).iterator ();
189194
190195 RedisClusterNode node = nodes .next ();
191196 assertThat (node .getId ()).isEqualTo ("b8b5ee73b1d1997abff694b3fe8b2397d2138b6d" );
@@ -243,8 +248,9 @@ void toClusterNodeWithIPv6Hostname() {
243248 assertThat (node .getSlotRange ().getSlots ().size ()).isEqualTo (5461 );
244249 }
245250
246- @ Test // https://github.com/spring-projects/spring-data-redis/issues/ 2678
251+ @ Test // GH- 2678
247252 void toClusterNodeWithIPv6HostnameSquareBrackets () {
253+
248254 RedisClusterNode node = Converters .toClusterNode (CLUSTER_NODE_WITH_SINGLE_IPV6_HOST_SQUARE_BRACKETS );
249255
250256 assertThat (node .getId ()).isEqualTo ("67adfe3df1058896e3cb49d2863e0f70e7e159fa" );
@@ -257,8 +263,43 @@ void toClusterNodeWithIPv6HostnameSquareBrackets() {
257263 assertThat (node .getSlotRange ().getSlots ().size ()).isEqualTo (5461 );
258264 }
259265
260- @ Test // https://github.com/spring-projects/spring-data-redis/issues/ 2678
266+ @ Test // GH- 2678
261267 void toClusterNodeWithInvalidIPv6Hostname () {
262- assertThatIllegalArgumentException ().isThrownBy (() -> Converters .toClusterNode (CLUSTER_NODE_WITH_SINGLE_INVALID_IPV6_HOST ));
268+ assertThatIllegalArgumentException ()
269+ .isThrownBy (() -> Converters .toClusterNode (CLUSTER_NODE_WITH_SINGLE_INVALID_IPV6_HOST ));
270+ }
271+
272+ @ ParameterizedTest // GH-2678
273+ @ MethodSource ("clusterNodesEndpoints" )
274+ void shouldAcceptHostPatterns (String endpoint , String expectedAddress , String expectedPort , String expectedHostname ) {
275+
276+ Matcher matcher = ClusterNodesConverter .clusterEndpointPattern .matcher (endpoint );
277+ assertThat (matcher .matches ()).isTrue ();
278+
279+ assertThat (matcher .group (1 )).isEqualTo (expectedAddress );
280+ assertThat (matcher .group (2 )).isEqualTo (expectedPort );
281+ assertThat (matcher .group (3 )).isEqualTo (expectedHostname );
282+ }
283+
284+ static Stream <Arguments > clusterNodesEndpoints () {
285+
286+ return Stream .of (
287+ // IPv4 with Host, Redis 3
288+ Arguments .of ("1.2.4.4:7379" , "1.2.4.4" , "7379" , null ),
289+ // IPv6 with Host, Redis 3
290+ Arguments .of ("6b8:c67:9c:0:6d8b:33da:5a2c:6380" , "6b8:c67:9c:0:6d8b:33da:5a2c" , "6380" , null ),
291+ // Assuming IPv6 in brackets with Host, Redis 3
292+ Arguments .of ("[6b8:c67:9c:0:6d8b:33da:5a2c]:6380" , "6b8:c67:9c:0:6d8b:33da:5a2c" , "6380" , null ),
293+
294+ // IPv4 with Host and Bus Port, Redis 4
295+ Arguments .of ("127.0.0.1:7382@17382" , "127.0.0.1" , "7382" , null ),
296+ // IPv6 with Host and Bus Port, Redis 4
297+ Arguments .of ("6b8:c67:9c:0:6d8b:33da:5a2c:6380" , "6b8:c67:9c:0:6d8b:33da:5a2c" , "6380" , null ),
298+
299+ // Hostname with Port and Bus Port, Redis 7
300+ Arguments .of ("my.host-name.com:7379@17379" , "my.host-name.com" , "7379" , null ),
301+
302+ // With hostname, Redis 7
303+ Arguments .of ("1.2.4.4:7379@17379,my.host-name.com" , "1.2.4.4" , "7379" , "my.host-name.com" ));
263304 }
264305}
0 commit comments