18
18
import static org .assertj .core .api .Assertions .*;
19
19
20
20
import java .util .Iterator ;
21
+ import java .util .regex .Matcher ;
22
+ import java .util .stream .Stream ;
21
23
22
24
import 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 ;
24
28
import org .springframework .data .redis .connection .RedisClusterNode ;
25
29
import org .springframework .data .redis .connection .RedisClusterNode .Flag ;
26
30
import org .springframework .data .redis .connection .RedisClusterNode .LinkState ;
27
31
import org .springframework .data .redis .connection .RedisNode .NodeType ;
32
+ import org .springframework .data .redis .connection .convert .Converters .ClusterNodesConverter ;
28
33
29
34
/**
30
35
* Unit tests for {@link Converters}.
@@ -184,8 +189,8 @@ void toSetOfRedisClusterNodesShouldConvertNodesWithSingleSlotCorrectly() {
184
189
@ Test // DATAREDIS-315
185
190
void toSetOfRedisClusterNodesShouldParseLinkStateAndDisconnectedCorrectly () {
186
191
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 ();
189
194
190
195
RedisClusterNode node = nodes .next ();
191
196
assertThat (node .getId ()).isEqualTo ("b8b5ee73b1d1997abff694b3fe8b2397d2138b6d" );
@@ -243,8 +248,9 @@ void toClusterNodeWithIPv6Hostname() {
243
248
assertThat (node .getSlotRange ().getSlots ().size ()).isEqualTo (5461 );
244
249
}
245
250
246
- @ Test // https://github.com/spring-projects/spring-data-redis/issues/ 2678
251
+ @ Test // GH- 2678
247
252
void toClusterNodeWithIPv6HostnameSquareBrackets () {
253
+
248
254
RedisClusterNode node = Converters .toClusterNode (CLUSTER_NODE_WITH_SINGLE_IPV6_HOST_SQUARE_BRACKETS );
249
255
250
256
assertThat (node .getId ()).isEqualTo ("67adfe3df1058896e3cb49d2863e0f70e7e159fa" );
@@ -257,8 +263,43 @@ void toClusterNodeWithIPv6HostnameSquareBrackets() {
257
263
assertThat (node .getSlotRange ().getSlots ().size ()).isEqualTo (5461 );
258
264
}
259
265
260
- @ Test // https://github.com/spring-projects/spring-data-redis/issues/ 2678
266
+ @ Test // GH- 2678
261
267
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" ));
263
304
}
264
305
}
0 commit comments