|
| 1 | +/* |
| 2 | + * Copyright 2017-2023 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.listener; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.mockito.ArgumentMatchers.any; |
| 20 | +import static org.mockito.ArgumentMatchers.eq; |
| 21 | +import static org.mockito.ArgumentMatchers.isNull; |
| 22 | +import static org.mockito.Mockito.doReturn; |
| 23 | +import static org.mockito.Mockito.mock; |
| 24 | +import static org.mockito.Mockito.never; |
| 25 | +import static org.mockito.Mockito.spy; |
| 26 | +import static org.mockito.Mockito.times; |
| 27 | +import static org.mockito.Mockito.verify; |
| 28 | +import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 29 | +import static org.mockito.Mockito.withSettings; |
| 30 | + |
| 31 | +import java.util.Properties; |
| 32 | +import java.util.function.Consumer; |
| 33 | + |
| 34 | +import org.junit.jupiter.api.Test; |
| 35 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 36 | +import org.mockito.Mock; |
| 37 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 38 | +import org.mockito.quality.Strictness; |
| 39 | + |
| 40 | +import org.springframework.data.redis.connection.Message; |
| 41 | +import org.springframework.data.redis.connection.RedisConnection; |
| 42 | +import org.springframework.data.redis.connection.RedisConnectionFactory; |
| 43 | +import org.springframework.data.redis.connection.RedisServerCommands; |
| 44 | +import org.springframework.lang.Nullable; |
| 45 | + |
| 46 | +/** |
| 47 | + * Unit tests for {@link KeyspaceEventMessageListener}. |
| 48 | + * |
| 49 | + * @author John Blum |
| 50 | + */ |
| 51 | +@ExtendWith(MockitoExtension.class) |
| 52 | +class KeyspaceEventMessageListenerUnitTests { |
| 53 | + |
| 54 | + @Mock |
| 55 | + private RedisMessageListenerContainer mockMessageListenerContainer; |
| 56 | + |
| 57 | + private Message mockMessage(@Nullable String channel, @Nullable String body) { |
| 58 | + |
| 59 | + Message mockMessage = mock(Message.class, withSettings().strictness(Strictness.LENIENT)); |
| 60 | + |
| 61 | + doReturn(toBytes(body)).when(mockMessage).getBody(); |
| 62 | + doReturn(toBytes(channel)).when(mockMessage).getChannel(); |
| 63 | + |
| 64 | + return mockMessage; |
| 65 | + } |
| 66 | + |
| 67 | + @Nullable |
| 68 | + private byte[] toBytes(@Nullable String value) { |
| 69 | + return value != null ? value.getBytes() : null; |
| 70 | + } |
| 71 | + |
| 72 | + private KeyspaceEventMessageListener newKeyspaceEventMessageListener() { |
| 73 | + return newKeyspaceEventMessageListener(listener -> { }); |
| 74 | + } |
| 75 | + |
| 76 | + private KeyspaceEventMessageListener newKeyspaceEventMessageListener( |
| 77 | + Consumer<KeyspaceEventMessageListener> preConditions) { |
| 78 | + |
| 79 | + TestKeyspaceEventMessageListener listener = |
| 80 | + new TestKeyspaceEventMessageListener(this.mockMessageListenerContainer); |
| 81 | + |
| 82 | + preConditions.accept(listener); |
| 83 | + |
| 84 | + return spy(listener); |
| 85 | + } |
| 86 | + |
| 87 | + @SuppressWarnings("all") |
| 88 | + private Properties singletonProperties(String propertyName, String propertyValue) { |
| 89 | + Properties properties = new Properties(); |
| 90 | + properties.setProperty(propertyName, propertyValue); |
| 91 | + return properties; |
| 92 | + } |
| 93 | + |
| 94 | + @Test // GH-2670 |
| 95 | + void handlesMessageWithChannelAndBody() { |
| 96 | + |
| 97 | + Message mockMessage = mockMessage("TestChannel", "TestBody"); |
| 98 | + |
| 99 | + KeyspaceEventMessageListener listener = newKeyspaceEventMessageListener(); |
| 100 | + |
| 101 | + listener.onMessage(mockMessage, null); |
| 102 | + |
| 103 | + verify(listener, times(1)).onMessage(eq(mockMessage), isNull()); |
| 104 | + verify(mockMessage, times(1)).getChannel(); |
| 105 | + verify(mockMessage, times(1)).getBody(); |
| 106 | + verify(listener, times(1)).doHandleMessage(eq(mockMessage)); |
| 107 | + verifyNoMoreInteractions(mockMessage, listener); |
| 108 | + } |
| 109 | + |
| 110 | + @Test // GH-2670 |
| 111 | + public void ignoreMessageWithNoBody() { |
| 112 | + |
| 113 | + Message mockMessage = mockMessage("TestChannel", null); |
| 114 | + |
| 115 | + KeyspaceEventMessageListener listener = newKeyspaceEventMessageListener(); |
| 116 | + |
| 117 | + listener.onMessage(mockMessage, null); |
| 118 | + |
| 119 | + verify(listener, times(1)).onMessage(eq(mockMessage), isNull()); |
| 120 | + verify(mockMessage, times(1)).getChannel(); |
| 121 | + verify(mockMessage, times(1)).getBody(); |
| 122 | + verify(listener, never()).doHandleMessage(any()); |
| 123 | + verifyNoMoreInteractions(mockMessage, listener); |
| 124 | + } |
| 125 | + |
| 126 | + @Test // GH-2670 |
| 127 | + public void ignoreMessageWithNoChannel() { |
| 128 | + |
| 129 | + Message mockMessage = mockMessage(null, "TestBody"); |
| 130 | + |
| 131 | + KeyspaceEventMessageListener listener = newKeyspaceEventMessageListener(); |
| 132 | + |
| 133 | + listener.onMessage(mockMessage, null); |
| 134 | + |
| 135 | + verify(listener, times(1)).onMessage(eq(mockMessage), isNull()); |
| 136 | + verify(mockMessage, times(1)).getChannel(); |
| 137 | + verify(listener, never()).doHandleMessage(any()); |
| 138 | + verifyNoMoreInteractions(mockMessage, listener); |
| 139 | + } |
| 140 | + |
| 141 | + @Test // GH-2670 |
| 142 | + public void doNotConfigureKeyspaceEventNotificationsWhenConfigParameterNotSpecified() { |
| 143 | + |
| 144 | + KeyspaceEventMessageListener listener = newKeyspaceEventMessageListener(it -> |
| 145 | + assertThat(it.getKeyspaceNotificationsConfigParameter()).isEmpty()); |
| 146 | + |
| 147 | + listener.init(); |
| 148 | + |
| 149 | + verify(listener, never()).configureKeyspaceEventNotifications(any()); |
| 150 | + } |
| 151 | + |
| 152 | + @Test // GH-2670 |
| 153 | + public void doNotConfigureKeyspaceEventNotificationsWhenContainerHasNoConnectionFactory() { |
| 154 | + |
| 155 | + KeyspaceEventMessageListener listener = newKeyspaceEventMessageListener(); |
| 156 | + |
| 157 | + listener.setKeyspaceNotificationsConfigParameter("EA"); |
| 158 | + listener.init(); |
| 159 | + |
| 160 | + assertThat(listener.getKeyspaceNotificationsConfigParameter()).isEqualTo("EA"); |
| 161 | + |
| 162 | + verify(listener, times(1)).configureKeyspaceEventNotifications(any()); |
| 163 | + verify(listener, never()).setKeyspaceEventNotifications(any(), any()); |
| 164 | + } |
| 165 | + |
| 166 | + @Test // GH-2670 |
| 167 | + public void doNotConfigureKeyspaceEventNotificationsWhenRedisServerSettingIsAlreadySet() { |
| 168 | + |
| 169 | + RedisConnectionFactory mockConnectionFactory = mock(RedisConnectionFactory.class); |
| 170 | + |
| 171 | + RedisConnection mockConnection = mock(RedisConnection.class); |
| 172 | + |
| 173 | + RedisServerCommands mockServerCommands = mock(RedisServerCommands.class); |
| 174 | + |
| 175 | + Properties config = singletonProperties(KeyspaceEventMessageListener.NOTIFY_KEYSPACE_EVENTS, "Em"); |
| 176 | + |
| 177 | + doReturn(mockConnectionFactory).when(this.mockMessageListenerContainer).getConnectionFactory(); |
| 178 | + doReturn(mockConnection).when(mockConnectionFactory).getConnection(); |
| 179 | + doReturn(mockServerCommands).when(mockConnection).serverCommands(); |
| 180 | + doReturn(config).when(mockServerCommands).getConfig(any()); |
| 181 | + |
| 182 | + KeyspaceEventMessageListener listener = newKeyspaceEventMessageListener(); |
| 183 | + |
| 184 | + listener.setKeyspaceNotificationsConfigParameter("EA"); |
| 185 | + listener.init(); |
| 186 | + |
| 187 | + assertThat(listener.getKeyspaceNotificationsConfigParameter()).isEqualTo("EA"); |
| 188 | + |
| 189 | + verify(listener, times(1)).configureKeyspaceEventNotifications(any()); |
| 190 | + verify(mockServerCommands, times(1)).getConfig(eq(KeyspaceEventMessageListener.NOTIFY_KEYSPACE_EVENTS)); |
| 191 | + verify(listener, never()).setKeyspaceEventNotifications(any(), any()); |
| 192 | + verify(mockConnection, times(1)).close(); |
| 193 | + } |
| 194 | + |
| 195 | + @Test // GH-2670 |
| 196 | + public void configuresKeyspaceEventNotificationsWhenRedisServerHasNoSettings() { |
| 197 | + |
| 198 | + RedisConnectionFactory mockConnectionFactory = mock(RedisConnectionFactory.class); |
| 199 | + |
| 200 | + RedisConnection mockConnection = mock(RedisConnection.class); |
| 201 | + |
| 202 | + RedisServerCommands mockServerCommands = mock(RedisServerCommands.class); |
| 203 | + |
| 204 | + doReturn(mockConnectionFactory).when(this.mockMessageListenerContainer).getConnectionFactory(); |
| 205 | + doReturn(mockConnection).when(mockConnectionFactory).getConnection(); |
| 206 | + doReturn(mockServerCommands).when(mockConnection).serverCommands(); |
| 207 | + doReturn(null).when(mockServerCommands).getConfig(any()); |
| 208 | + |
| 209 | + KeyspaceEventMessageListener listener = newKeyspaceEventMessageListener(); |
| 210 | + |
| 211 | + listener.setKeyspaceNotificationsConfigParameter("EA"); |
| 212 | + listener.init(); |
| 213 | + |
| 214 | + assertThat(listener.getKeyspaceNotificationsConfigParameter()).isEqualTo("EA"); |
| 215 | + |
| 216 | + verify(listener, times(1)).configureKeyspaceEventNotifications(any()); |
| 217 | + verify(mockServerCommands, times(1)) |
| 218 | + .getConfig(eq(KeyspaceEventMessageListener.NOTIFY_KEYSPACE_EVENTS)); |
| 219 | + verify(listener, times(1)) |
| 220 | + .setKeyspaceEventNotifications(eq(mockConnection), eq("EA")); |
| 221 | + verify(mockServerCommands, times(1)) |
| 222 | + .setConfig(eq(KeyspaceEventMessageListener.NOTIFY_KEYSPACE_EVENTS), eq("EA")); |
| 223 | + verify(mockConnection, times(1)).close(); |
| 224 | + } |
| 225 | + |
| 226 | + @Test // GH-2670 |
| 227 | + public void configuresKeyspaceEventNotificationsCorrectly() { |
| 228 | + |
| 229 | + RedisConnectionFactory mockConnectionFactory = mock(RedisConnectionFactory.class); |
| 230 | + |
| 231 | + RedisConnection mockConnection = mock(RedisConnection.class); |
| 232 | + |
| 233 | + RedisServerCommands mockServerCommands = mock(RedisServerCommands.class); |
| 234 | + |
| 235 | + Properties config = singletonProperties(KeyspaceEventMessageListener.NOTIFY_KEYSPACE_EVENTS, " "); |
| 236 | + |
| 237 | + doReturn(mockConnectionFactory).when(this.mockMessageListenerContainer).getConnectionFactory(); |
| 238 | + doReturn(mockConnection).when(mockConnectionFactory).getConnection(); |
| 239 | + doReturn(mockServerCommands).when(mockConnection).serverCommands(); |
| 240 | + doReturn(config).when(mockServerCommands).getConfig(any()); |
| 241 | + |
| 242 | + KeyspaceEventMessageListener listener = newKeyspaceEventMessageListener(); |
| 243 | + |
| 244 | + listener.setKeyspaceNotificationsConfigParameter("EA"); |
| 245 | + listener.init(); |
| 246 | + |
| 247 | + assertThat(listener.getKeyspaceNotificationsConfigParameter()).isEqualTo("EA"); |
| 248 | + |
| 249 | + verify(listener, times(1)).configureKeyspaceEventNotifications(any()); |
| 250 | + verify(mockServerCommands, times(1)) |
| 251 | + .getConfig(eq(KeyspaceEventMessageListener.NOTIFY_KEYSPACE_EVENTS)); |
| 252 | + verify(listener, times(1)) |
| 253 | + .setKeyspaceEventNotifications(eq(mockConnection), eq("EA")); |
| 254 | + verify(mockServerCommands, times(1)) |
| 255 | + .setConfig(eq(KeyspaceEventMessageListener.NOTIFY_KEYSPACE_EVENTS), eq("EA")); |
| 256 | + verify(mockConnection, times(1)).close(); |
| 257 | + } |
| 258 | + |
| 259 | + static class TestKeyspaceEventMessageListener extends KeyspaceEventMessageListener { |
| 260 | + |
| 261 | + TestKeyspaceEventMessageListener(RedisMessageListenerContainer messageListenerContainer) { |
| 262 | + super(messageListenerContainer); |
| 263 | + } |
| 264 | + |
| 265 | + @Override |
| 266 | + protected void doHandleMessage(Message message) { |
| 267 | + |
| 268 | + } |
| 269 | + } |
| 270 | +} |
0 commit comments