|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2021 the original author or authors. |
| 2 | + * Copyright 2012-2022 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
@@ -292,53 +292,57 @@ void testRedisConfigurationWithSentinelAndDatabase() {
|
292 | 292 |
|
293 | 293 | @Test
|
294 | 294 | void testRedisConfigurationWithSentinelAndAuthentication() {
|
295 |
| - this.contextRunner.withPropertyValues("spring.redis.username=user", "spring.redis.password=password", |
296 |
| - "spring.redis.sentinel.master:mymaster", |
297 |
| - "spring.redis.sentinel.nodes:127.0.0.1:26379, 127.0.0.1:26380").run((context) -> { |
298 |
| - LettuceConnectionFactory connectionFactory = context.getBean(LettuceConnectionFactory.class); |
299 |
| - assertThat(getUserName(connectionFactory)).isEqualTo("user"); |
300 |
| - assertThat(connectionFactory.getPassword()).isEqualTo("password"); |
301 |
| - RedisSentinelConfiguration sentinelConfiguration = connectionFactory.getSentinelConfiguration(); |
| 295 | + this.contextRunner |
| 296 | + .withPropertyValues("spring.redis.username=user", "spring.redis.password=password", |
| 297 | + "spring.redis.sentinel.master:mymaster", |
| 298 | + "spring.redis.sentinel.nodes:127.0.0.1:26379, 127.0.0.1:26380") |
| 299 | + .run(assertSentinelConfiguration("user", "password", (sentinelConfiguration) -> { |
302 | 300 | assertThat(sentinelConfiguration.getSentinelPassword().isPresent()).isFalse();
|
303 |
| - Set<RedisNode> sentinels = connectionFactory.getSentinelConfiguration().getSentinels(); |
| 301 | + Set<RedisNode> sentinels = sentinelConfiguration.getSentinels(); |
304 | 302 | assertThat(sentinels.stream().map(Object::toString).collect(Collectors.toSet()))
|
305 | 303 | .contains("127.0.0.1:26379", "127.0.0.1:26380");
|
306 |
| - }); |
| 304 | + })); |
307 | 305 | }
|
308 | 306 |
|
309 | 307 | @Test
|
310 | 308 | void testRedisConfigurationWithSentinelPasswordAndDataNodePassword() {
|
311 |
| - this.contextRunner.withPropertyValues("spring.redis.password=password", "spring.redis.sentinel.password=secret", |
312 |
| - "spring.redis.sentinel.master:mymaster", |
313 |
| - "spring.redis.sentinel.nodes:127.0.0.1:26379, 127.0.0.1:26380").run((context) -> { |
314 |
| - LettuceConnectionFactory connectionFactory = context.getBean(LettuceConnectionFactory.class); |
315 |
| - assertThat(getUserName(connectionFactory)).isNull(); |
316 |
| - assertThat(connectionFactory.getPassword()).isEqualTo("password"); |
317 |
| - RedisSentinelConfiguration sentinelConfiguration = connectionFactory.getSentinelConfiguration(); |
| 309 | + this.contextRunner |
| 310 | + .withPropertyValues("spring.redis.password=password", "spring.redis.sentinel.password=secret", |
| 311 | + "spring.redis.sentinel.master:mymaster", |
| 312 | + "spring.redis.sentinel.nodes:127.0.0.1:26379, 127.0.0.1:26380") |
| 313 | + .run(assertSentinelConfiguration(null, "password", (sentinelConfiguration) -> { |
318 | 314 | assertThat(sentinelConfiguration.getSentinelUsername()).isNull();
|
319 | 315 | assertThat(new String(sentinelConfiguration.getSentinelPassword().get())).isEqualTo("secret");
|
320 | 316 | Set<RedisNode> sentinels = sentinelConfiguration.getSentinels();
|
321 | 317 | assertThat(sentinels.stream().map(Object::toString).collect(Collectors.toSet()))
|
322 | 318 | .contains("127.0.0.1:26379", "127.0.0.1:26380");
|
323 |
| - }); |
| 319 | + })); |
324 | 320 | }
|
325 | 321 |
|
326 | 322 | @Test
|
327 | 323 | void testRedisConfigurationWithSentinelAuthenticationAndDataNodeAuthentication() {
|
328 |
| - this.contextRunner.withPropertyValues("spring.redis.username=username", "spring.redis.password=password", |
329 |
| - "spring.redis.sentinel.username=sentinel", "spring.redis.sentinel.password=secret", |
330 |
| - "spring.redis.sentinel.master:mymaster", |
331 |
| - "spring.redis.sentinel.nodes:127.0.0.1:26379, 127.0.0.1:26380").run((context) -> { |
332 |
| - LettuceConnectionFactory connectionFactory = context.getBean(LettuceConnectionFactory.class); |
333 |
| - assertThat(getUserName(connectionFactory)).isEqualTo("username"); |
334 |
| - assertThat(connectionFactory.getPassword()).isEqualTo("password"); |
335 |
| - RedisSentinelConfiguration sentinelConfiguration = connectionFactory.getSentinelConfiguration(); |
| 324 | + this.contextRunner |
| 325 | + .withPropertyValues("spring.redis.username=username", "spring.redis.password=password", |
| 326 | + "spring.redis.sentinel.username=sentinel", "spring.redis.sentinel.password=secret", |
| 327 | + "spring.redis.sentinel.master:mymaster", |
| 328 | + "spring.redis.sentinel.nodes:127.0.0.1:26379, 127.0.0.1:26380") |
| 329 | + .run(assertSentinelConfiguration("username", "password", (sentinelConfiguration) -> { |
336 | 330 | assertThat(sentinelConfiguration.getSentinelUsername()).isEqualTo("sentinel");
|
337 | 331 | assertThat(new String(sentinelConfiguration.getSentinelPassword().get())).isEqualTo("secret");
|
338 | 332 | Set<RedisNode> sentinels = sentinelConfiguration.getSentinels();
|
339 | 333 | assertThat(sentinels.stream().map(Object::toString).collect(Collectors.toSet()))
|
340 | 334 | .contains("127.0.0.1:26379", "127.0.0.1:26380");
|
341 |
| - }); |
| 335 | + })); |
| 336 | + } |
| 337 | + |
| 338 | + private ContextConsumer<AssertableApplicationContext> assertSentinelConfiguration(String userName, String password, |
| 339 | + Consumer<RedisSentinelConfiguration> sentinelConfiguration) { |
| 340 | + return (context) -> { |
| 341 | + LettuceConnectionFactory connectionFactory = context.getBean(LettuceConnectionFactory.class); |
| 342 | + assertThat(getUserName(connectionFactory)).isEqualTo(userName); |
| 343 | + assertThat(connectionFactory.getPassword()).isEqualTo(password); |
| 344 | + assertThat(connectionFactory.getSentinelConfiguration()).satisfies(sentinelConfiguration); |
| 345 | + }; |
342 | 346 | }
|
343 | 347 |
|
344 | 348 | @Test
|
|
0 commit comments