3333import org .springframework .amqp .rabbit .listener .RabbitListenerEndpointRegistry ;
3434import org .springframework .amqp .support .converter .MessageConverter ;
3535import org .springframework .boot .autoconfigure .AutoConfigurations ;
36+ import org .springframework .boot .autoconfigure .ssl .SslAutoConfiguration ;
37+ import org .springframework .boot .ssl .NoSuchSslBundleException ;
38+ import org .springframework .boot .ssl .SslBundles ;
3639import org .springframework .boot .test .context .runner .ApplicationContextRunner ;
3740import org .springframework .context .annotation .Bean ;
3841import org .springframework .context .annotation .Configuration ;
5053import org .springframework .rabbit .stream .support .converter .StreamMessageConverter ;
5154
5255import static org .assertj .core .api .Assertions .assertThat ;
56+ import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
57+ import static org .mockito .BDDMockito .given ;
5358import static org .mockito .BDDMockito .then ;
5459import static org .mockito .Mockito .mock ;
60+ import static org .mockito .Mockito .never ;
5561
5662/**
5763 * Tests for {@link RabbitStreamConfiguration}.
6066 * @author Andy Wilkinson
6167 * @author Eddú Meléndez
6268 * @author Moritz Halbritter
69+ * @author Jay Choi
6370 */
6471class RabbitStreamConfigurationTests {
6572
6673 private final ApplicationContextRunner contextRunner = new ApplicationContextRunner ()
67- .withConfiguration (AutoConfigurations .of (RabbitAutoConfiguration .class ));
74+ .withConfiguration (AutoConfigurations .of (RabbitAutoConfiguration .class , SslAutoConfiguration . class ));
6875
6976 @ Test
7077 @ SuppressWarnings ("unchecked" )
@@ -146,7 +153,7 @@ void environmentUsesConnectionDetailsByDefault() {
146153 EnvironmentBuilder builder = mock (EnvironmentBuilder .class );
147154 RabbitProperties properties = new RabbitProperties ();
148155 RabbitStreamConfiguration .configure (builder , properties ,
149- new TestRabbitConnectionDetails ("guest" , "guest" , "vhost" ));
156+ new TestRabbitConnectionDetails ("guest" , "guest" , "vhost" ), null );
150157 then (builder ).should ().port (5552 );
151158 then (builder ).should ().host ("localhost" );
152159 then (builder ).should ().virtualHost ("vhost" );
@@ -162,7 +169,7 @@ void whenStreamPortIsSetThenEnvironmentUsesCustomPort() {
162169 RabbitProperties properties = new RabbitProperties ();
163170 properties .getStream ().setPort (5553 );
164171 RabbitStreamConfiguration .configure (builder , properties ,
165- new TestRabbitConnectionDetails ("guest" , "guest" , "vhost" ));
172+ new TestRabbitConnectionDetails ("guest" , "guest" , "vhost" ), null );
166173 then (builder ).should ().port (5553 );
167174 }
168175
@@ -172,7 +179,7 @@ void whenStreamHostIsSetThenEnvironmentUsesCustomHost() {
172179 RabbitProperties properties = new RabbitProperties ();
173180 properties .getStream ().setHost ("stream.rabbit.example.com" );
174181 RabbitStreamConfiguration .configure (builder , properties ,
175- new TestRabbitConnectionDetails ("guest" , "guest" , "vhost" ));
182+ new TestRabbitConnectionDetails ("guest" , "guest" , "vhost" ), null );
176183 then (builder ).should ().host ("stream.rabbit.example.com" );
177184 }
178185
@@ -182,7 +189,7 @@ void whenStreamVirtualHostIsSetThenEnvironmentUsesCustomVirtualHost() {
182189 RabbitProperties properties = new RabbitProperties ();
183190 properties .getStream ().setVirtualHost ("stream-virtual-host" );
184191 RabbitStreamConfiguration .configure (builder , properties ,
185- new TestRabbitConnectionDetails ("guest" , "guest" , "vhost" ));
192+ new TestRabbitConnectionDetails ("guest" , "guest" , "vhost" ), null );
186193 then (builder ).should ().virtualHost ("stream-virtual-host" );
187194 }
188195
@@ -192,7 +199,7 @@ void whenStreamVirtualHostIsNotSetButDefaultVirtualHostIsSetThenEnvironmentUsesD
192199 RabbitProperties properties = new RabbitProperties ();
193200 properties .setVirtualHost ("properties-virtual-host" );
194201 RabbitStreamConfiguration .configure (builder , properties ,
195- new TestRabbitConnectionDetails ("guest" , "guest" , "default-virtual-host" ));
202+ new TestRabbitConnectionDetails ("guest" , "guest" , "default-virtual-host" ), null );
196203 then (builder ).should ().virtualHost ("default-virtual-host" );
197204 }
198205
@@ -203,7 +210,7 @@ void whenStreamCredentialsAreNotSetThenEnvironmentUsesConnectionDetailsCredentia
203210 properties .setUsername ("alice" );
204211 properties .setPassword ("secret" );
205212 RabbitStreamConfiguration .configure (builder , properties ,
206- new TestRabbitConnectionDetails ("bob" , "password" , "vhost" ));
213+ new TestRabbitConnectionDetails ("bob" , "password" , "vhost" ), null );
207214 then (builder ).should ().username ("bob" );
208215 then (builder ).should ().password ("password" );
209216 }
@@ -217,7 +224,7 @@ void whenStreamCredentialsAreSetThenEnvironmentUsesStreamCredentials() {
217224 properties .getStream ().setUsername ("bob" );
218225 properties .getStream ().setPassword ("confidential" );
219226 RabbitStreamConfiguration .configure (builder , properties ,
220- new TestRabbitConnectionDetails ("charlotte" , "hidden" , "vhost" ));
227+ new TestRabbitConnectionDetails ("charlotte" , "hidden" , "vhost" ), null );
221228 then (builder ).should ().username ("bob" );
222229 then (builder ).should ().password ("confidential" );
223230 }
@@ -297,6 +304,71 @@ void environmentCreatedByBuilderCanBeCustomized() {
297304 });
298305 }
299306
307+ @ Test
308+ void whenStreamSslIsNotConfiguredThenTlsIsNotUsed () {
309+ EnvironmentBuilder builder = mock (EnvironmentBuilder .class );
310+ RabbitProperties properties = new RabbitProperties ();
311+ RabbitStreamConfiguration .configure (builder , properties ,
312+ new TestRabbitConnectionDetails ("guest" , "guest" , "vhost" ), null );
313+ then (builder ).should (never ()).tls ();
314+ }
315+
316+ @ Test
317+ void whenStreamSslIsEnabledThenTlsIsUsed () {
318+ EnvironmentBuilder builder = mock (EnvironmentBuilder .class );
319+ RabbitProperties properties = new RabbitProperties ();
320+ properties .getStream ().getSsl ().setEnabled (true );
321+ RabbitStreamConfiguration .configure (builder , properties ,
322+ new TestRabbitConnectionDetails ("guest" , "guest" , "vhost" ), null );
323+ then (builder ).should ().tls ();
324+ }
325+
326+ @ Test
327+ void whenStreamSslBundleIsConfiguredThenTlsIsUsed () {
328+ this .contextRunner .withPropertyValues ("spring.rabbitmq.stream.ssl.bundle=test-bundle" ,
329+ "spring.ssl.bundle.jks.test-bundle.keystore.location=classpath:org/springframework/boot/amqp/autoconfigure/test.jks" ,
330+ "spring.ssl.bundle.jks.test-bundle.keystore.password=secret" )
331+ .run ((context ) -> {
332+ assertThat (context ).hasNotFailed ();
333+ assertThat (context ).hasSingleBean (Environment .class );
334+ });
335+ }
336+
337+ @ Test
338+ void whenStreamSslIsDisabledThenTlsIsNotUsed () {
339+ EnvironmentBuilder builder = mock (EnvironmentBuilder .class );
340+ RabbitProperties properties = new RabbitProperties ();
341+ properties .getStream ().getSsl ().setEnabled (false );
342+ RabbitStreamConfiguration .configure (builder , properties ,
343+ new TestRabbitConnectionDetails ("guest" , "guest" , "vhost" ), null );
344+ then (builder ).should (never ()).tls ();
345+ }
346+
347+ @ Test
348+ void whenStreamSslIsDisabledWithBundleThenTlsIsNotUsed () {
349+ EnvironmentBuilder builder = mock (EnvironmentBuilder .class );
350+ RabbitProperties properties = new RabbitProperties ();
351+ properties .getStream ().getSsl ().setEnabled (false );
352+ properties .getStream ().getSsl ().setBundle ("some-bundle" );
353+ RabbitStreamConfiguration .configure (builder , properties ,
354+ new TestRabbitConnectionDetails ("guest" , "guest" , "vhost" ), null );
355+ then (builder ).should (never ()).tls ();
356+ }
357+
358+ @ Test
359+ void whenStreamSslBundleIsInvalidThenFails () {
360+ EnvironmentBuilder builder = mock (EnvironmentBuilder .class );
361+ SslBundles sslBundles = mock (SslBundles .class );
362+ given (sslBundles .getBundle ("invalid-bundle" )).willThrow (
363+ new NoSuchSslBundleException ("invalid-bundle" , "SSL bundle name 'invalid-bundle' cannot be found" ));
364+ RabbitProperties properties = new RabbitProperties ();
365+ properties .getStream ().getSsl ().setBundle ("invalid-bundle" );
366+ assertThatExceptionOfType (NoSuchSslBundleException .class )
367+ .isThrownBy (() -> RabbitStreamConfiguration .configure (builder , properties ,
368+ new TestRabbitConnectionDetails ("guest" , "guest" , "vhost" ), sslBundles ))
369+ .withMessageContaining ("invalid-bundle" );
370+ }
371+
300372 @ Configuration (proxyBeanMethods = false )
301373 static class TestConfiguration {
302374
0 commit comments