|
18 | 18 |
|
19 | 19 | import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
20 | 20 | import static org.junit.jupiter.params.provider.Arguments.arguments; |
21 | | -import static org.mockito.Mockito.mock; |
22 | 21 | import static org.mockito.Mockito.spy; |
23 | 22 | import static org.mockito.Mockito.times; |
24 | 23 | import static org.mockito.Mockito.verify; |
25 | | -import static org.mockito.Mockito.when; |
26 | 24 |
|
27 | 25 | import java.util.stream.Stream; |
28 | 26 |
|
|
35 | 33 | import org.junit.jupiter.params.provider.Arguments; |
36 | 34 | import org.junit.jupiter.params.provider.MethodSource; |
37 | 35 |
|
38 | | -import org.springframework.beans.factory.config.ConfigurableBeanFactory; |
| 36 | +import org.springframework.beans.factory.annotation.Autowired; |
| 37 | +import org.springframework.context.annotation.Bean; |
| 38 | +import org.springframework.context.annotation.Configuration; |
39 | 39 | import org.springframework.lang.Nullable; |
40 | 40 | import org.springframework.pulsar.annotation.PulsarMessage; |
| 41 | +import org.springframework.pulsar.core.DefaultTopicResolverTests.TopicByAnnotatedMessageType.WithTopicExpression.WithTopicExpressionConfig; |
| 42 | +import org.springframework.test.annotation.DirtiesContext; |
| 43 | +import org.springframework.test.annotation.DirtiesContext.ClassMode; |
| 44 | +import org.springframework.test.context.ContextConfiguration; |
| 45 | +import org.springframework.test.context.TestPropertySource; |
| 46 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
41 | 47 |
|
42 | 48 | /** |
43 | 49 | * Unit tests for {@link DefaultTopicResolver}. |
@@ -138,8 +144,6 @@ static Stream<Arguments> resolveByMessageTypeProvider() { |
138 | 144 | @Nested |
139 | 145 | class TopicByAnnotatedMessageType { |
140 | 146 |
|
141 | | - private static final String bazTopicExpression = "#{someExpression}"; |
142 | | - |
143 | 147 | @Test |
144 | 148 | void customMappingTakesPrecedenceOverAnnotationMapping() { |
145 | 149 | assertThat(resolver.resolveTopic(null, Baz.class, () -> defaultTopic).value().orElse(null)) |
@@ -169,29 +173,63 @@ void annotatedMessageTypeWithTopicInfo() { |
169 | 173 | verify(resolver, times(1)).getAnnotatedTopicInfo(Baz.class); |
170 | 174 | } |
171 | 175 |
|
172 | | - @Test |
173 | | - void annotatedMessageTypeWithTopicExpressionIsResolved() { |
174 | | - var mockExpressionResolver = mock(ExpressionResolver.class); |
175 | | - when(mockExpressionResolver.resolveToString(bazTopicExpression)).thenReturn(Resolved.of(bazTopic)); |
176 | | - var expressionTopicResolver = new DefaultTopicResolver(mockExpressionResolver); |
177 | | - assertThat(expressionTopicResolver.resolveTopic(null, BazWithTopicExpression.class, |
178 | | - () -> defaultTopic).value().orElse(null)) |
179 | | - .isEqualTo(bazTopic); |
180 | | - verify(mockExpressionResolver, times(1)).resolveToString(bazTopicExpression); |
181 | | - } |
182 | | - |
183 | | - @Test |
184 | | - void deriveExpressionResolverFromBeanFactory() { |
185 | | - var mockBeanFactory = mock(ConfigurableBeanFactory.class); |
186 | | - var expressionTopicResolver = new DefaultTopicResolver(); |
187 | | - expressionTopicResolver.setBeanFactory(mockBeanFactory); |
188 | | - assertThat(expressionTopicResolver) |
189 | | - .extracting("expressionResolver") |
190 | | - .isNotNull(); |
191 | | - } |
| 176 | + /** |
| 177 | + * Lightweight integration tests for the expression resolver functionality in |
| 178 | + * {@link DefaultTopicResolver}. |
| 179 | + * <p> |
| 180 | + * Starts up a small Spring context which in turns provides the bean factory and |
| 181 | + * expression resolver to the topic resolver. |
| 182 | + */ |
| 183 | + @Nested |
| 184 | + @SpringJUnitConfig |
| 185 | + @DirtiesContext(classMode = ClassMode.AFTER_CLASS) |
| 186 | + @ContextConfiguration(classes = WithTopicExpressionConfig.class) |
| 187 | + @TestPropertySource(properties = { "app.customPropertyTopic = my-custom-property-topic" }) |
| 188 | + class WithTopicExpression { |
| 189 | + |
| 190 | + // @formatter:off |
| 191 | + @Test |
| 192 | + void propertyPlaceholderExpressionIsResolved(@Autowired DefaultTopicResolver topicResolver) { |
| 193 | + assertThat(topicResolver.resolveTopic(null, MsgTypeWithTopicPropertyExpression.class, () -> defaultTopic) |
| 194 | + .value().orElse(null)).isEqualTo("my-custom-property-topic"); |
| 195 | + |
| 196 | + } |
| 197 | + |
| 198 | + @Test |
| 199 | + void spelExpressionIsResolved(@Autowired DefaultTopicResolver topicResolver) { |
| 200 | + assertThat(topicResolver.resolveTopic(null, MsgTypeWithTopicSpELExpression.class, () -> defaultTopic) |
| 201 | + .value().orElse(null)).isEqualTo("my-custom-spel-topic"); |
| 202 | + } |
| 203 | + |
| 204 | + @Test |
| 205 | + void embeddedExpressionIsResolved(@Autowired DefaultTopicResolver topicResolver) { |
| 206 | + assertThat(topicResolver.resolveTopic(null, MsgTypeWithTopicEmbeddedExpression.class, () -> defaultTopic) |
| 207 | + .value().orElse(null)).isEqualTo("my-custom-property-topic".toUpperCase()); |
| 208 | + } |
| 209 | + // @formatter:on |
| 210 | + |
| 211 | + @Configuration(proxyBeanMethods = false) |
| 212 | + static class WithTopicExpressionConfig { |
| 213 | + |
| 214 | + @Bean |
| 215 | + DefaultTopicResolver defaultTopicResolver() { |
| 216 | + return new DefaultTopicResolver(); |
| 217 | + } |
| 218 | + |
| 219 | + } |
| 220 | + |
| 221 | + @PulsarMessage(topic = "${app.customPropertyTopic}") |
| 222 | + record MsgTypeWithTopicPropertyExpression(String value) { |
| 223 | + } |
| 224 | + |
| 225 | + @PulsarMessage(topic = "#{T(java.lang.String).valueOf('my-custom-spel-topic')}") |
| 226 | + record MsgTypeWithTopicSpELExpression(String value) { |
| 227 | + } |
| 228 | + |
| 229 | + @PulsarMessage(topic = "#{T(java.lang.String).valueOf('${app.customPropertyTopic}').toUpperCase()}") |
| 230 | + record MsgTypeWithTopicEmbeddedExpression(String value) { |
| 231 | + } |
192 | 232 |
|
193 | | - @PulsarMessage(topic = bazTopicExpression) |
194 | | - record BazWithTopicExpression(String value) { |
195 | 233 | } |
196 | 234 |
|
197 | 235 | } |
|
0 commit comments