17
17
package org .springframework .boot .autoconfigure .amqp ;
18
18
19
19
import java .time .Duration ;
20
+ import java .util .List ;
20
21
21
22
import com .rabbitmq .stream .BackOffDelayPolicy ;
22
23
import com .rabbitmq .stream .Codec ;
@@ -124,12 +125,14 @@ void whenCustomMessageListenerContainerFactoryIsDefinedThenAutoConfiguredContain
124
125
}
125
126
126
127
@ Test
127
- void environmentUsesPropertyDefaultsByDefault () {
128
+ void environmentUsesConnectionDetailsByDefault () {
128
129
EnvironmentBuilder builder = mock (EnvironmentBuilder .class );
129
130
RabbitProperties properties = new RabbitProperties ();
130
- RabbitStreamConfiguration .configure (builder , properties );
131
+ RabbitStreamConfiguration .configure (builder , properties ,
132
+ new TestRabbitConnectionDetails ("guest" , "guest" , "vhost" ));
131
133
then (builder ).should ().port (5552 );
132
134
then (builder ).should ().host ("localhost" );
135
+ then (builder ).should ().virtualHost ("vhost" );
133
136
then (builder ).should ().lazyInitialization (true );
134
137
then (builder ).should ().username ("guest" );
135
138
then (builder ).should ().password ("guest" );
@@ -141,7 +144,8 @@ void whenStreamPortIsSetThenEnvironmentUsesCustomPort() {
141
144
EnvironmentBuilder builder = mock (EnvironmentBuilder .class );
142
145
RabbitProperties properties = new RabbitProperties ();
143
146
properties .getStream ().setPort (5553 );
144
- RabbitStreamConfiguration .configure (builder , properties );
147
+ RabbitStreamConfiguration .configure (builder , properties ,
148
+ new TestRabbitConnectionDetails ("guest" , "guest" , "vhost" ));
145
149
then (builder ).should ().port (5553 );
146
150
}
147
151
@@ -150,7 +154,8 @@ 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 ,
158
+ new TestRabbitConnectionDetails ("guest" , "guest" , "vhost" ));
154
159
then (builder ).should ().host ("stream.rabbit.example.com" );
155
160
}
156
161
@@ -159,28 +164,31 @@ void whenStreamVirtualHostIsSetThenEnvironmentUsesCustomVirtualHost() {
159
164
EnvironmentBuilder builder = mock (EnvironmentBuilder .class );
160
165
RabbitProperties properties = new RabbitProperties ();
161
166
properties .getStream ().setVirtualHost ("stream-virtual-host" );
162
- RabbitStreamConfiguration .configure (builder , properties );
167
+ RabbitStreamConfiguration .configure (builder , properties ,
168
+ new TestRabbitConnectionDetails ("guest" , "guest" , "vhost" ));
163
169
then (builder ).should ().virtualHost ("stream-virtual-host" );
164
170
}
165
171
166
172
@ Test
167
173
void whenStreamVirtualHostIsNotSetButDefaultVirtualHostIsSetThenEnvironmentUsesDefaultVirtualHost () {
168
174
EnvironmentBuilder builder = mock (EnvironmentBuilder .class );
169
175
RabbitProperties properties = new RabbitProperties ();
170
- properties .setVirtualHost ("default-virtual-host" );
171
- RabbitStreamConfiguration .configure (builder , properties );
176
+ properties .setVirtualHost ("properties-virtual-host" );
177
+ RabbitStreamConfiguration .configure (builder , properties ,
178
+ new TestRabbitConnectionDetails ("guest" , "guest" , "default-virtual-host" ));
172
179
then (builder ).should ().virtualHost ("default-virtual-host" );
173
180
}
174
181
175
182
@ Test
176
- void whenStreamCredentialsAreNotSetThenEnvironmentUsesRabbitCredentials () {
183
+ void whenStreamCredentialsAreNotSetThenEnvironmentUsesConnectionDetailsCredentials () {
177
184
EnvironmentBuilder builder = mock (EnvironmentBuilder .class );
178
185
RabbitProperties properties = new RabbitProperties ();
179
186
properties .setUsername ("alice" );
180
187
properties .setPassword ("secret" );
181
- RabbitStreamConfiguration .configure (builder , properties );
182
- then (builder ).should ().username ("alice" );
183
- then (builder ).should ().password ("secret" );
188
+ RabbitStreamConfiguration .configure (builder , properties ,
189
+ new TestRabbitConnectionDetails ("bob" , "password" , "vhost" ));
190
+ then (builder ).should ().username ("bob" );
191
+ then (builder ).should ().password ("password" );
184
192
}
185
193
186
194
@ Test
@@ -191,7 +199,8 @@ void whenStreamCredentialsAreSetThenEnvironmentUsesStreamCredentials() {
191
199
properties .setPassword ("secret" );
192
200
properties .getStream ().setUsername ("bob" );
193
201
properties .getStream ().setPassword ("confidential" );
194
- RabbitStreamConfiguration .configure (builder , properties );
202
+ RabbitStreamConfiguration .configure (builder , properties ,
203
+ new TestRabbitConnectionDetails ("charlotte" , "hidden" , "vhost" ));
195
204
then (builder ).should ().username ("bob" );
196
205
then (builder ).should ().password ("confidential" );
197
206
}
@@ -345,4 +354,40 @@ EnvironmentBuilderCustomizer customizerB() {
345
354
346
355
}
347
356
357
+ private static final class TestRabbitConnectionDetails implements RabbitConnectionDetails {
358
+
359
+ private final String username ;
360
+
361
+ private final String password ;
362
+
363
+ private final String virtualHost ;
364
+
365
+ private TestRabbitConnectionDetails (String username , String password , String virtualHost ) {
366
+ this .username = username ;
367
+ this .password = password ;
368
+ this .virtualHost = virtualHost ;
369
+ }
370
+
371
+ @ Override
372
+ public String getUsername () {
373
+ return this .username ;
374
+ }
375
+
376
+ @ Override
377
+ public String getPassword () {
378
+ return this .password ;
379
+ }
380
+
381
+ @ Override
382
+ public String getVirtualHost () {
383
+ return this .virtualHost ;
384
+ }
385
+
386
+ @ Override
387
+ public List <Address > getAddresses () {
388
+ throw new UnsupportedOperationException ();
389
+ }
390
+
391
+ }
392
+
348
393
}
0 commit comments