|
16 | 16 |
|
17 | 17 | package org.springframework.boot.autoconfigure.amqp;
|
18 | 18 |
|
| 19 | +import com.rabbitmq.stream.Environment; |
| 20 | +import com.rabbitmq.stream.EnvironmentBuilder; |
| 21 | +import org.assertj.core.api.InstanceOfAssertFactories; |
19 | 22 | import org.junit.jupiter.api.Test;
|
20 |
| -import org.springframework.amqp.rabbit.annotation.EnableRabbit; |
| 23 | + |
21 | 24 | import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
22 | 25 | import org.springframework.amqp.rabbit.config.ContainerCustomizer;
|
| 26 | +import org.springframework.amqp.rabbit.listener.MessageListenerContainer; |
| 27 | +import org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory; |
23 | 28 | import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistry;
|
24 |
| -import org.springframework.beans.DirectFieldAccessor; |
25 | 29 | import org.springframework.boot.autoconfigure.AutoConfigurations;
|
26 | 30 | import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
27 | 31 | import org.springframework.context.annotation.Bean;
|
|
31 | 35 | import org.springframework.rabbit.stream.listener.StreamListenerContainer;
|
32 | 36 |
|
33 | 37 | import static org.assertj.core.api.Assertions.assertThat;
|
| 38 | +import static org.mockito.Mockito.mock; |
| 39 | +import static org.mockito.Mockito.verify; |
| 40 | +import static org.mockito.Mockito.verifyNoMoreInteractions; |
34 | 41 |
|
35 | 42 | /**
|
36 | 43 | * Tests for {@link RabbitStreamConfiguration}.
|
37 | 44 | *
|
38 | 45 | * @author Gary Russell
|
| 46 | + * @author Andy Wilkinson |
39 | 47 | */
|
40 | 48 | class RabbitStreamConfigurationTests {
|
41 | 49 |
|
42 | 50 | private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
43 | 51 | .withConfiguration(AutoConfigurations.of(RabbitAutoConfiguration.class));
|
44 | 52 |
|
45 | 53 | @Test
|
46 |
| - void testContainerType() { |
| 54 | + @SuppressWarnings("unchecked") |
| 55 | + void whenListenerTypeIsStreamThenStreamListenerContainerAndEnvironmentAreAutoConfigured() { |
47 | 56 | this.contextRunner.withUserConfiguration(TestConfiguration.class)
|
| 57 | + .withPropertyValues("spring.rabbitmq.listener.type:stream").run((context) -> { |
| 58 | + RabbitListenerEndpointRegistry registry = context.getBean(RabbitListenerEndpointRegistry.class); |
| 59 | + MessageListenerContainer listenerContainer = registry.getListenerContainer("test"); |
| 60 | + assertThat(listenerContainer).isInstanceOf(StreamListenerContainer.class); |
| 61 | + assertThat(listenerContainer).extracting("consumerCustomizer").isNotNull(); |
| 62 | + assertThat(context.getBean(StreamRabbitListenerContainerFactory.class)) |
| 63 | + .extracting("nativeListener", InstanceOfAssertFactories.BOOLEAN).isFalse(); |
| 64 | + verify(context.getBean(ContainerCustomizer.class)).configure(listenerContainer); |
| 65 | + assertThat(context).hasSingleBean(Environment.class); |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void whenNativeListenerIsEnabledThenContainerFactoryIsConfiguredToUseNativeListeners() { |
| 71 | + this.contextRunner |
48 | 72 | .withPropertyValues("spring.rabbitmq.listener.type:stream",
|
49 | 73 | "spring.rabbitmq.listener.stream.native-listener:true")
|
| 74 | + .run((context) -> assertThat(context.getBean(StreamRabbitListenerContainerFactory.class)) |
| 75 | + .extracting("nativeListener", InstanceOfAssertFactories.BOOLEAN).isTrue()); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + void whenCustomEnvironmenIsDefinedThenAutoConfiguredEnvironmentBacksOff() { |
| 80 | + this.contextRunner.withUserConfiguration(CustomEnvironmentConfiguration.class).run((context) -> { |
| 81 | + assertThat(context).hasSingleBean(Environment.class); |
| 82 | + assertThat(context.getBean(Environment.class)) |
| 83 | + .isSameAs(context.getBean(CustomEnvironmentConfiguration.class).environment); |
| 84 | + }); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void whenCustomMessageListenerContainerIsDefinedThenAutoConfiguredContainerBacksOff() { |
| 89 | + this.contextRunner.withUserConfiguration(CustomMessageListenerContainerFactoryConfiguration.class) |
50 | 90 | .run((context) -> {
|
51 |
| - RabbitListenerEndpointRegistry registry = context.getBean(RabbitListenerEndpointRegistry.class); |
52 |
| - assertThat(registry.getListenerContainer("test")).isInstanceOf(StreamListenerContainer.class); |
53 |
| - assertThat(new DirectFieldAccessor(registry.getListenerContainer("test")) |
54 |
| - .getPropertyValue("consumerCustomizer")).isNotNull(); |
55 |
| - assertThat(new DirectFieldAccessor(context.getBean(StreamRabbitListenerContainerFactory.class)) |
56 |
| - .getPropertyValue("nativeListener")).isEqualTo(Boolean.TRUE); |
57 |
| - assertThat(context.getBean(TestConfiguration.class).containerCustomizerCalled).isTrue(); |
| 91 | + assertThat(context).hasSingleBean(RabbitListenerContainerFactory.class); |
| 92 | + assertThat(context.getBean(RabbitListenerContainerFactory.class)).isSameAs(context.getBean( |
| 93 | + CustomMessageListenerContainerFactoryConfiguration.class).listenerContainerFactory); |
58 | 94 | });
|
59 | 95 | }
|
60 | 96 |
|
| 97 | + @Test |
| 98 | + void environmentUsesPropertyDefaultsByDefault() { |
| 99 | + EnvironmentBuilder builder = mock(EnvironmentBuilder.class); |
| 100 | + RabbitProperties properties = new RabbitProperties(); |
| 101 | + RabbitStreamConfiguration.configure(builder, properties); |
| 102 | + verify(builder).port(5552); |
| 103 | + verify(builder).host("localhost"); |
| 104 | + verify(builder).lazyInitialization(true); |
| 105 | + verify(builder).username("guest"); |
| 106 | + verify(builder).password("guest"); |
| 107 | + verifyNoMoreInteractions(builder); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + void whenStreamPortIsSetThenEnvironmentUsesCustomPort() { |
| 112 | + EnvironmentBuilder builder = mock(EnvironmentBuilder.class); |
| 113 | + RabbitProperties properties = new RabbitProperties(); |
| 114 | + properties.getStream().setPort(5553); |
| 115 | + RabbitStreamConfiguration.configure(builder, properties); |
| 116 | + verify(builder).port(5553); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + void whenStreamHostIsSetThenEnvironmentUsesCustomHost() { |
| 121 | + EnvironmentBuilder builder = mock(EnvironmentBuilder.class); |
| 122 | + RabbitProperties properties = new RabbitProperties(); |
| 123 | + properties.getStream().setHost("stream.rabbit.example.com"); |
| 124 | + RabbitStreamConfiguration.configure(builder, properties); |
| 125 | + verify(builder).host("stream.rabbit.example.com"); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + void whenStreamCredentialsAreNotSetThenEnvironmentUsesRabbitCredentials() { |
| 130 | + EnvironmentBuilder builder = mock(EnvironmentBuilder.class); |
| 131 | + RabbitProperties properties = new RabbitProperties(); |
| 132 | + properties.setUsername("alice"); |
| 133 | + properties.setPassword("secret"); |
| 134 | + RabbitStreamConfiguration.configure(builder, properties); |
| 135 | + verify(builder).username("alice"); |
| 136 | + verify(builder).password("secret"); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + void whenStreamCredentialsAreSetThenEnvironmentUsesStreamCredentials() { |
| 141 | + EnvironmentBuilder builder = mock(EnvironmentBuilder.class); |
| 142 | + RabbitProperties properties = new RabbitProperties(); |
| 143 | + properties.setUsername("alice"); |
| 144 | + properties.setPassword("secret"); |
| 145 | + properties.getStream().setUsername("bob"); |
| 146 | + properties.getStream().setPassword("confidential"); |
| 147 | + RabbitStreamConfiguration.configure(builder, properties); |
| 148 | + verify(builder).username("bob"); |
| 149 | + verify(builder).password("confidential"); |
| 150 | + } |
| 151 | + |
61 | 152 | @Configuration(proxyBeanMethods = false)
|
62 |
| - @EnableRabbit |
63 | 153 | static class TestConfiguration {
|
64 | 154 |
|
65 |
| - boolean containerCustomizerCalled; |
66 |
| - |
67 | 155 | @RabbitListener(id = "test", queues = "stream", autoStartup = "false")
|
68 | 156 | void listen(String in) {
|
69 | 157 | }
|
70 | 158 |
|
71 | 159 | @Bean
|
72 | 160 | ConsumerCustomizer consumerCustomizer() {
|
73 |
| - return (id, consumer) -> { |
74 |
| - }; |
| 161 | + return mock(ConsumerCustomizer.class); |
75 | 162 | }
|
76 | 163 |
|
77 | 164 | @Bean
|
| 165 | + @SuppressWarnings("unchecked") |
78 | 166 | ContainerCustomizer<StreamListenerContainer> containerCustomizer() {
|
79 |
| - return (container) -> this.containerCustomizerCalled = true; |
| 167 | + return mock(ContainerCustomizer.class); |
| 168 | + } |
| 169 | + |
| 170 | + } |
| 171 | + |
| 172 | + @Configuration(proxyBeanMethods = false) |
| 173 | + static class CustomEnvironmentConfiguration { |
| 174 | + |
| 175 | + private final Environment environment = Environment.builder().lazyInitialization(true).build(); |
| 176 | + |
| 177 | + @Bean |
| 178 | + Environment rabbitStreamEnvironment() { |
| 179 | + return this.environment; |
| 180 | + } |
| 181 | + |
| 182 | + } |
| 183 | + |
| 184 | + @Configuration(proxyBeanMethods = false) |
| 185 | + static class CustomMessageListenerContainerFactoryConfiguration { |
| 186 | + |
| 187 | + @SuppressWarnings("rawtypes") |
| 188 | + private final RabbitListenerContainerFactory listenerContainerFactory = mock( |
| 189 | + RabbitListenerContainerFactory.class); |
| 190 | + |
| 191 | + @Bean |
| 192 | + @SuppressWarnings("unchecked") |
| 193 | + RabbitListenerContainerFactory<MessageListenerContainer> rabbitListenerContainerFactory() { |
| 194 | + return this.listenerContainerFactory; |
80 | 195 | }
|
81 | 196 |
|
82 | 197 | }
|
|
0 commit comments