|
| 1 | +/* |
| 2 | + * Copyright 2015-2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.redis.connection.lettuce; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.*; |
| 19 | + |
| 20 | +import io.lettuce.core.RedisURI; |
| 21 | + |
| 22 | +import org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration; |
| 23 | +import org.junit.jupiter.api.Nested; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +import org.springframework.data.redis.connection.RedisConfiguration; |
| 27 | +import org.springframework.data.redis.connection.RedisNode; |
| 28 | +import org.springframework.data.redis.connection.RedisSentinelConfiguration; |
| 29 | +import org.springframework.data.redis.connection.RedisSocketConfiguration; |
| 30 | +import org.springframework.data.redis.connection.RedisStandaloneConfiguration; |
| 31 | + |
| 32 | +/** |
| 33 | + * Unit tests for the {@link LettuceConnectionFactory#createRedisConfiguration(RedisURI)} factory method. |
| 34 | + * |
| 35 | + * @author Chris Bono |
| 36 | + */ |
| 37 | +class LettuceConnectionFactoryCreateRedisConfigurationUnitTests { |
| 38 | + |
| 39 | + @Test |
| 40 | + void requiresRedisURI() { |
| 41 | + assertThatThrownBy(() -> LettuceConnectionFactory.createRedisConfiguration(null)) |
| 42 | + .isInstanceOf(IllegalArgumentException.class) |
| 43 | + .hasMessage("RedisURI must not be null"); |
| 44 | + } |
| 45 | + |
| 46 | + @Nested // GH-2117 |
| 47 | + class CreateRedisSentinelConfiguration { |
| 48 | + |
| 49 | + @Test |
| 50 | + void minimalFieldsSetOnRedisURI() { |
| 51 | + RedisURI redisURI = RedisURI.create("redis-sentinel://myserver?sentinelMasterId=5150"); |
| 52 | + RedisSentinelConfiguration expectedSentinelConfiguration = new RedisSentinelConfiguration(); |
| 53 | + expectedSentinelConfiguration.setMaster("5150"); |
| 54 | + expectedSentinelConfiguration.addSentinel(new RedisNode("myserver", 26379)); |
| 55 | + |
| 56 | + RedisConfiguration sentinelConfiguration = LettuceConnectionFactory.createRedisConfiguration(redisURI); |
| 57 | + |
| 58 | + assertThat(sentinelConfiguration).usingRecursiveComparison(sentinelCompareConfig()) |
| 59 | + .isInstanceOf(RedisSentinelConfiguration.class) |
| 60 | + .isEqualTo(expectedSentinelConfiguration); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void allFieldsSetOnRedisURI() { |
| 65 | + RedisURI redisURI = RedisURI.create("redis-sentinel://fooUser:fooPass@myserver1:111,myserver2:222/7?sentinelMasterId=5150"); |
| 66 | + // Set the passwords directly on the sentinels so that it gets picked up by converter |
| 67 | + char[] sentinelPass = "changeme".toCharArray(); |
| 68 | + redisURI.getSentinels().forEach(sentinelRedisUri -> sentinelRedisUri.setPassword(sentinelPass)); |
| 69 | + RedisSentinelConfiguration expectedSentinelConfiguration = new RedisSentinelConfiguration(); |
| 70 | + expectedSentinelConfiguration.setMaster("5150"); |
| 71 | + expectedSentinelConfiguration.setDatabase(7); |
| 72 | + expectedSentinelConfiguration.setUsername("fooUser"); |
| 73 | + expectedSentinelConfiguration.setPassword("fooPass"); |
| 74 | + expectedSentinelConfiguration.setSentinelPassword(sentinelPass); |
| 75 | + expectedSentinelConfiguration.addSentinel(new RedisNode("myserver1", 111)); |
| 76 | + expectedSentinelConfiguration.addSentinel(new RedisNode("myserver2", 222)); |
| 77 | + |
| 78 | + RedisConfiguration sentinelConfiguration = LettuceConnectionFactory.createRedisConfiguration(redisURI); |
| 79 | + |
| 80 | + assertThat(sentinelConfiguration).usingRecursiveComparison(sentinelCompareConfig()) |
| 81 | + .isInstanceOf(RedisSentinelConfiguration.class) |
| 82 | + .isEqualTo(expectedSentinelConfiguration); |
| 83 | + } |
| 84 | + |
| 85 | + // RedisSentinelConfiguration does not provide equals impl |
| 86 | + private RecursiveComparisonConfiguration sentinelCompareConfig() { |
| 87 | + return RecursiveComparisonConfiguration.builder().withComparedFields( |
| 88 | + "master", |
| 89 | + "username", |
| 90 | + "password", |
| 91 | + "sentinelPassword", |
| 92 | + "database", |
| 93 | + "sentinels") |
| 94 | + .build(); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + @Nested // GH-2117 |
| 99 | + class CreateRedisSocketConfiguration { |
| 100 | + |
| 101 | + @Test |
| 102 | + void minimalFieldsSetOnRedisURI() { |
| 103 | + RedisURI redisURI = RedisURI.builder() |
| 104 | + .socket("mysocket") |
| 105 | + .build(); |
| 106 | + RedisSocketConfiguration expectedSocketConfiguration = new RedisSocketConfiguration(); |
| 107 | + expectedSocketConfiguration.setSocket("mysocket"); |
| 108 | + |
| 109 | + RedisConfiguration socketConfiguration = LettuceConnectionFactory.createRedisConfiguration(redisURI); |
| 110 | + |
| 111 | + assertThat(socketConfiguration).usingRecursiveComparison(socketCompareConfig()) |
| 112 | + .isInstanceOf(RedisSocketConfiguration.class) |
| 113 | + .isEqualTo(expectedSocketConfiguration); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + void allFieldsSetOnRedisURI() { |
| 118 | + RedisURI redisURI = RedisURI.builder() |
| 119 | + .socket("mysocket") |
| 120 | + .withAuthentication("fooUser", "fooPass".toCharArray()) |
| 121 | + .withDatabase(7) |
| 122 | + .build(); |
| 123 | + RedisSocketConfiguration expectedSocketConfiguration = new RedisSocketConfiguration(); |
| 124 | + expectedSocketConfiguration.setSocket("mysocket"); |
| 125 | + expectedSocketConfiguration.setUsername("fooUser"); |
| 126 | + expectedSocketConfiguration.setPassword("fooPass"); |
| 127 | + expectedSocketConfiguration.setDatabase(7); |
| 128 | + |
| 129 | + RedisConfiguration socketConfiguration = LettuceConnectionFactory.createRedisConfiguration(redisURI); |
| 130 | + |
| 131 | + assertThat(socketConfiguration).usingRecursiveComparison(socketCompareConfig()) |
| 132 | + .isInstanceOf(RedisSocketConfiguration.class) |
| 133 | + .isEqualTo(expectedSocketConfiguration); |
| 134 | + } |
| 135 | + |
| 136 | + // RedisSocketConfiguration does not provide equals impl |
| 137 | + private RecursiveComparisonConfiguration socketCompareConfig() { |
| 138 | + return RecursiveComparisonConfiguration.builder().withComparedFields( |
| 139 | + "socket", |
| 140 | + "username", |
| 141 | + "password", |
| 142 | + "database") |
| 143 | + .build(); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + @Nested // GH-2117 |
| 148 | + class CreateRedisStandaloneConfiguration { |
| 149 | + |
| 150 | + @Test |
| 151 | + void minimalFieldsSetOnRedisURI() { |
| 152 | + RedisURI redisURI = RedisURI.create("redis://myserver"); |
| 153 | + RedisStandaloneConfiguration expectedStandaloneConfiguration = new RedisStandaloneConfiguration(); |
| 154 | + expectedStandaloneConfiguration.setHostName("myserver"); |
| 155 | + |
| 156 | + RedisConfiguration StandaloneConfiguration = LettuceConnectionFactory.createRedisConfiguration(redisURI); |
| 157 | + |
| 158 | + assertThat(StandaloneConfiguration).usingRecursiveComparison(standaloneCompareConfig()) |
| 159 | + .isInstanceOf(RedisStandaloneConfiguration.class) |
| 160 | + .isEqualTo(expectedStandaloneConfiguration); |
| 161 | + } |
| 162 | + |
| 163 | + @Test |
| 164 | + void allFieldsSetOnRedisURI() { |
| 165 | + RedisURI redisURI = RedisURI.create("redis://fooUser:fooPass@myserver1:111/7"); |
| 166 | + RedisStandaloneConfiguration expectedStandaloneConfiguration = new RedisStandaloneConfiguration(); |
| 167 | + expectedStandaloneConfiguration.setHostName("myserver1"); |
| 168 | + expectedStandaloneConfiguration.setPort(111); |
| 169 | + expectedStandaloneConfiguration.setDatabase(7); |
| 170 | + expectedStandaloneConfiguration.setUsername("fooUser"); |
| 171 | + expectedStandaloneConfiguration.setPassword("fooPass"); |
| 172 | + |
| 173 | + RedisConfiguration StandaloneConfiguration = LettuceConnectionFactory.createRedisConfiguration(redisURI); |
| 174 | + |
| 175 | + assertThat(StandaloneConfiguration).usingRecursiveComparison(standaloneCompareConfig()) |
| 176 | + .isInstanceOf(RedisStandaloneConfiguration.class) |
| 177 | + .isEqualTo(expectedStandaloneConfiguration); |
| 178 | + |
| 179 | + } |
| 180 | + |
| 181 | + // RedisStandaloneConfiguration does not provide equals impl |
| 182 | + private RecursiveComparisonConfiguration standaloneCompareConfig() { |
| 183 | + return RecursiveComparisonConfiguration.builder().withComparedFields( |
| 184 | + "host", |
| 185 | + "port", |
| 186 | + "username", |
| 187 | + "password", |
| 188 | + "database") |
| 189 | + .build(); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | +} |
0 commit comments