17
17
package org .springframework .boot .autoconfigure .amqp ;
18
18
19
19
import java .time .Duration ;
20
+ import java .util .List ;
20
21
22
+ import com .rabbitmq .stream .Address ;
21
23
import com .rabbitmq .stream .BackOffDelayPolicy ;
22
24
import com .rabbitmq .stream .Codec ;
23
25
import com .rabbitmq .stream .Environment ;
32
34
import org .springframework .amqp .rabbit .listener .RabbitListenerEndpointRegistry ;
33
35
import org .springframework .amqp .support .converter .MessageConverter ;
34
36
import org .springframework .boot .autoconfigure .AutoConfigurations ;
37
+ import org .springframework .boot .autoconfigure .amqp .RabbitStreamConfiguration .PropertiesRabbitStreamConnectionDetails ;
35
38
import org .springframework .boot .test .context .runner .ApplicationContextRunner ;
36
39
import org .springframework .context .annotation .Bean ;
37
40
import org .springframework .context .annotation .Configuration ;
43
46
import org .springframework .rabbit .stream .producer .ProducerCustomizer ;
44
47
import org .springframework .rabbit .stream .producer .RabbitStreamTemplate ;
45
48
import org .springframework .rabbit .stream .support .converter .StreamMessageConverter ;
49
+ import org .springframework .test .util .ReflectionTestUtils ;
46
50
47
51
import static org .assertj .core .api .Assertions .assertThat ;
48
52
import static org .mockito .BDDMockito .then ;
@@ -127,7 +131,7 @@ void whenCustomMessageListenerContainerFactoryIsDefinedThenAutoConfiguredContain
127
131
void environmentUsesPropertyDefaultsByDefault () {
128
132
EnvironmentBuilder builder = mock (EnvironmentBuilder .class );
129
133
RabbitProperties properties = new RabbitProperties ();
130
- RabbitStreamConfiguration .configure (builder , properties );
134
+ RabbitStreamConfiguration .configure (builder , properties , getRabbitConnectionDetails ( properties ) );
131
135
then (builder ).should ().port (5552 );
132
136
then (builder ).should ().host ("localhost" );
133
137
then (builder ).should ().lazyInitialization (true );
@@ -141,7 +145,7 @@ void whenStreamPortIsSetThenEnvironmentUsesCustomPort() {
141
145
EnvironmentBuilder builder = mock (EnvironmentBuilder .class );
142
146
RabbitProperties properties = new RabbitProperties ();
143
147
properties .getStream ().setPort (5553 );
144
- RabbitStreamConfiguration .configure (builder , properties );
148
+ RabbitStreamConfiguration .configure (builder , properties , getRabbitConnectionDetails ( properties ) );
145
149
then (builder ).should ().port (5553 );
146
150
}
147
151
@@ -150,7 +154,7 @@ void whenStreamHostIsSetThenEnvironmentUsesCustomHost() {
150
154
EnvironmentBuilder builder = mock (EnvironmentBuilder .class );
151
155
RabbitProperties properties = new RabbitProperties ();
152
156
properties .getStream ().setHost ("stream.rabbit.example.com" );
153
- RabbitStreamConfiguration .configure (builder , properties );
157
+ RabbitStreamConfiguration .configure (builder , properties , getRabbitConnectionDetails ( properties ) );
154
158
then (builder ).should ().host ("stream.rabbit.example.com" );
155
159
}
156
160
@@ -159,7 +163,7 @@ void whenStreamVirtualHostIsSetThenEnvironmentUsesCustomVirtualHost() {
159
163
EnvironmentBuilder builder = mock (EnvironmentBuilder .class );
160
164
RabbitProperties properties = new RabbitProperties ();
161
165
properties .getStream ().setVirtualHost ("stream-virtual-host" );
162
- RabbitStreamConfiguration .configure (builder , properties );
166
+ RabbitStreamConfiguration .configure (builder , properties , getRabbitConnectionDetails ( properties ) );
163
167
then (builder ).should ().virtualHost ("stream-virtual-host" );
164
168
}
165
169
@@ -168,7 +172,7 @@ void whenStreamVirtualHostIsNotSetButDefaultVirtualHostIsSetThenEnvironmentUsesD
168
172
EnvironmentBuilder builder = mock (EnvironmentBuilder .class );
169
173
RabbitProperties properties = new RabbitProperties ();
170
174
properties .setVirtualHost ("default-virtual-host" );
171
- RabbitStreamConfiguration .configure (builder , properties );
175
+ RabbitStreamConfiguration .configure (builder , properties , getRabbitConnectionDetails ( properties ) );
172
176
then (builder ).should ().virtualHost ("default-virtual-host" );
173
177
}
174
178
@@ -178,7 +182,7 @@ void whenStreamCredentialsAreNotSetThenEnvironmentUsesRabbitCredentials() {
178
182
RabbitProperties properties = new RabbitProperties ();
179
183
properties .setUsername ("alice" );
180
184
properties .setPassword ("secret" );
181
- RabbitStreamConfiguration .configure (builder , properties );
185
+ RabbitStreamConfiguration .configure (builder , properties , getRabbitConnectionDetails ( properties ) );
182
186
then (builder ).should ().username ("alice" );
183
187
then (builder ).should ().password ("secret" );
184
188
}
@@ -191,7 +195,7 @@ void whenStreamCredentialsAreSetThenEnvironmentUsesStreamCredentials() {
191
195
properties .setPassword ("secret" );
192
196
properties .getStream ().setUsername ("bob" );
193
197
properties .getStream ().setPassword ("confidential" );
194
- RabbitStreamConfiguration .configure (builder , properties );
198
+ RabbitStreamConfiguration .configure (builder , properties , getRabbitConnectionDetails ( properties ) );
195
199
then (builder ).should ().username ("bob" );
196
200
then (builder ).should ().password ("confidential" );
197
201
}
@@ -260,6 +264,22 @@ void environmentCreatedByBuilderCanBeCustomized() {
260
264
});
261
265
}
262
266
267
+ @ Test
268
+ @ SuppressWarnings ("unchecked" )
269
+ void connectionDetailsAreApplied () {
270
+ this .contextRunner .withPropertyValues ("spring.rabbitmq.stream.name:stream-test" )
271
+ .withUserConfiguration (CustomConnectionDetails .class )
272
+ .run ((context ) -> assertThat (context .getBean (Environment .class ))
273
+ .extracting ((environment ) -> (List <Address >) ReflectionTestUtils .getField (environment , "addresses" ))
274
+ .extracting ((address ) -> address .get (0 ))
275
+ .extracting ("host" , "port" )
276
+ .containsExactly ("rabbitmq" , 5555 ));
277
+ }
278
+
279
+ private RabbitStreamConnectionDetails getRabbitConnectionDetails (RabbitProperties properties ) {
280
+ return new PropertiesRabbitStreamConnectionDetails (properties .getStream ());
281
+ }
282
+
263
283
@ Configuration (proxyBeanMethods = false )
264
284
static class TestConfiguration {
265
285
@@ -345,4 +365,24 @@ EnvironmentBuilderCustomizer customizerB() {
345
365
346
366
}
347
367
368
+ @ Configuration (proxyBeanMethods = false )
369
+ static class CustomConnectionDetails {
370
+
371
+ @ Bean
372
+ RabbitStreamConnectionDetails customRabbitMqStreamConnectionDetails () {
373
+ return new RabbitStreamConnectionDetails () {
374
+ @ Override
375
+ public String getHost () {
376
+ return "rabbitmq" ;
377
+ }
378
+
379
+ @ Override
380
+ public int getPort () {
381
+ return 5555 ;
382
+ }
383
+ };
384
+ }
385
+
386
+ }
387
+
348
388
}
0 commit comments