1
1
/*
2
- * Copyright 2012-2023 the original author or authors.
2
+ * Copyright 2012-2024 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
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 ;
@@ -113,12 +114,14 @@ void whenCustomMessageListenerContainerFactoryIsDefinedThenAutoConfiguredContain
113
114
}
114
115
115
116
@ Test
116
- void environmentUsesPropertyDefaultsByDefault () {
117
+ void environmentUsesConnectionDetailsByDefault () {
117
118
EnvironmentBuilder builder = mock (EnvironmentBuilder .class );
118
119
RabbitProperties properties = new RabbitProperties ();
119
- RabbitStreamConfiguration .configure (builder , properties );
120
+ RabbitStreamConfiguration .configure (builder , properties ,
121
+ new TestRabbitConnectionDetails ("guest" , "guest" , "vhost" ));
120
122
then (builder ).should ().port (5552 );
121
123
then (builder ).should ().host ("localhost" );
124
+ then (builder ).should ().virtualHost ("vhost" );
122
125
then (builder ).should ().lazyInitialization (true );
123
126
then (builder ).should ().username ("guest" );
124
127
then (builder ).should ().password ("guest" );
@@ -130,7 +133,8 @@ void whenStreamPortIsSetThenEnvironmentUsesCustomPort() {
130
133
EnvironmentBuilder builder = mock (EnvironmentBuilder .class );
131
134
RabbitProperties properties = new RabbitProperties ();
132
135
properties .getStream ().setPort (5553 );
133
- RabbitStreamConfiguration .configure (builder , properties );
136
+ RabbitStreamConfiguration .configure (builder , properties ,
137
+ new TestRabbitConnectionDetails ("guest" , "guest" , "vhost" ));
134
138
then (builder ).should ().port (5553 );
135
139
}
136
140
@@ -139,7 +143,8 @@ void whenStreamHostIsSetThenEnvironmentUsesCustomHost() {
139
143
EnvironmentBuilder builder = mock (EnvironmentBuilder .class );
140
144
RabbitProperties properties = new RabbitProperties ();
141
145
properties .getStream ().setHost ("stream.rabbit.example.com" );
142
- RabbitStreamConfiguration .configure (builder , properties );
146
+ RabbitStreamConfiguration .configure (builder , properties ,
147
+ new TestRabbitConnectionDetails ("guest" , "guest" , "vhost" ));
143
148
then (builder ).should ().host ("stream.rabbit.example.com" );
144
149
}
145
150
@@ -148,28 +153,31 @@ void whenStreamVirtualHostIsSetThenEnvironmentUsesCustomVirtualHost() {
148
153
EnvironmentBuilder builder = mock (EnvironmentBuilder .class );
149
154
RabbitProperties properties = new RabbitProperties ();
150
155
properties .getStream ().setVirtualHost ("stream-virtual-host" );
151
- RabbitStreamConfiguration .configure (builder , properties );
156
+ RabbitStreamConfiguration .configure (builder , properties ,
157
+ new TestRabbitConnectionDetails ("guest" , "guest" , "vhost" ));
152
158
then (builder ).should ().virtualHost ("stream-virtual-host" );
153
159
}
154
160
155
161
@ Test
156
162
void whenStreamVirtualHostIsNotSetButDefaultVirtualHostIsSetThenEnvironmentUsesDefaultVirtualHost () {
157
163
EnvironmentBuilder builder = mock (EnvironmentBuilder .class );
158
164
RabbitProperties properties = new RabbitProperties ();
159
- properties .setVirtualHost ("default-virtual-host" );
160
- RabbitStreamConfiguration .configure (builder , properties );
165
+ properties .setVirtualHost ("properties-virtual-host" );
166
+ RabbitStreamConfiguration .configure (builder , properties ,
167
+ new TestRabbitConnectionDetails ("guest" , "guest" , "default-virtual-host" ));
161
168
then (builder ).should ().virtualHost ("default-virtual-host" );
162
169
}
163
170
164
171
@ Test
165
- void whenStreamCredentialsAreNotSetThenEnvironmentUsesRabbitCredentials () {
172
+ void whenStreamCredentialsAreNotSetThenEnvironmentUsesConnectionDetailsCredentials () {
166
173
EnvironmentBuilder builder = mock (EnvironmentBuilder .class );
167
174
RabbitProperties properties = new RabbitProperties ();
168
175
properties .setUsername ("alice" );
169
176
properties .setPassword ("secret" );
170
- RabbitStreamConfiguration .configure (builder , properties );
171
- then (builder ).should ().username ("alice" );
172
- then (builder ).should ().password ("secret" );
177
+ RabbitStreamConfiguration .configure (builder , properties ,
178
+ new TestRabbitConnectionDetails ("bob" , "password" , "vhost" ));
179
+ then (builder ).should ().username ("bob" );
180
+ then (builder ).should ().password ("password" );
173
181
}
174
182
175
183
@ Test
@@ -180,7 +188,8 @@ void whenStreamCredentialsAreSetThenEnvironmentUsesStreamCredentials() {
180
188
properties .setPassword ("secret" );
181
189
properties .getStream ().setUsername ("bob" );
182
190
properties .getStream ().setPassword ("confidential" );
183
- RabbitStreamConfiguration .configure (builder , properties );
191
+ RabbitStreamConfiguration .configure (builder , properties ,
192
+ new TestRabbitConnectionDetails ("charlotte" , "hidden" , "vhost" ));
184
193
then (builder ).should ().username ("bob" );
185
194
then (builder ).should ().password ("confidential" );
186
195
}
@@ -334,4 +343,40 @@ EnvironmentBuilderCustomizer customizerB() {
334
343
335
344
}
336
345
346
+ private static final class TestRabbitConnectionDetails implements RabbitConnectionDetails {
347
+
348
+ private final String username ;
349
+
350
+ private final String password ;
351
+
352
+ private final String virtualHost ;
353
+
354
+ private TestRabbitConnectionDetails (String username , String password , String virtualHost ) {
355
+ this .username = username ;
356
+ this .password = password ;
357
+ this .virtualHost = virtualHost ;
358
+ }
359
+
360
+ @ Override
361
+ public String getUsername () {
362
+ return this .username ;
363
+ }
364
+
365
+ @ Override
366
+ public String getPassword () {
367
+ return this .password ;
368
+ }
369
+
370
+ @ Override
371
+ public String getVirtualHost () {
372
+ return this .virtualHost ;
373
+ }
374
+
375
+ @ Override
376
+ public List <Address > getAddresses () {
377
+ throw new UnsupportedOperationException ();
378
+ }
379
+
380
+ }
381
+
337
382
}
0 commit comments