|
|
|
|
@ -25,6 +25,7 @@ import com.rabbitmq.stream.Codec;
@@ -25,6 +25,7 @@ import com.rabbitmq.stream.Codec;
|
|
|
|
|
import com.rabbitmq.stream.Environment; |
|
|
|
|
import com.rabbitmq.stream.EnvironmentBuilder; |
|
|
|
|
import org.assertj.core.api.InstanceOfAssertFactories; |
|
|
|
|
import org.jspecify.annotations.Nullable; |
|
|
|
|
import org.junit.jupiter.api.Test; |
|
|
|
|
|
|
|
|
|
import org.springframework.amqp.core.MessageListenerContainer; |
|
|
|
|
@ -35,6 +36,10 @@ import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistry;
@@ -35,6 +36,10 @@ import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistry;
|
|
|
|
|
import org.springframework.amqp.support.converter.MessageConverter; |
|
|
|
|
import org.springframework.boot.amqp.autoconfigure.RabbitStreamConfiguration.PropertiesRabbitStreamConnectionDetails; |
|
|
|
|
import org.springframework.boot.autoconfigure.AutoConfigurations; |
|
|
|
|
import org.springframework.boot.autoconfigure.ssl.SslAutoConfiguration; |
|
|
|
|
import org.springframework.boot.ssl.NoSuchSslBundleException; |
|
|
|
|
import org.springframework.boot.ssl.SslBundle; |
|
|
|
|
import org.springframework.boot.ssl.SslBundles; |
|
|
|
|
import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
|
|
|
|
import org.springframework.context.annotation.Bean; |
|
|
|
|
import org.springframework.context.annotation.Configuration; |
|
|
|
|
@ -53,8 +58,11 @@ import org.springframework.rabbit.stream.support.converter.StreamMessageConverte
@@ -53,8 +58,11 @@ import org.springframework.rabbit.stream.support.converter.StreamMessageConverte
|
|
|
|
|
import org.springframework.test.util.ReflectionTestUtils; |
|
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
|
|
|
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
|
|
|
|
import static org.mockito.BDDMockito.given; |
|
|
|
|
import static org.mockito.BDDMockito.then; |
|
|
|
|
import static org.mockito.Mockito.mock; |
|
|
|
|
import static org.mockito.Mockito.never; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Tests for {@link RabbitStreamConfiguration}. |
|
|
|
|
@ -63,11 +71,12 @@ import static org.mockito.Mockito.mock;
@@ -63,11 +71,12 @@ import static org.mockito.Mockito.mock;
|
|
|
|
|
* @author Andy Wilkinson |
|
|
|
|
* @author Eddú Meléndez |
|
|
|
|
* @author Moritz Halbritter |
|
|
|
|
* @author Jay Choi |
|
|
|
|
*/ |
|
|
|
|
class RabbitStreamConfigurationTests { |
|
|
|
|
|
|
|
|
|
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() |
|
|
|
|
.withConfiguration(AutoConfigurations.of(RabbitAutoConfiguration.class)); |
|
|
|
|
.withConfiguration(AutoConfigurations.of(RabbitAutoConfiguration.class, SslAutoConfiguration.class)); |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
|
@ -150,7 +159,7 @@ class RabbitStreamConfigurationTests {
@@ -150,7 +159,7 @@ class RabbitStreamConfigurationTests {
|
|
|
|
|
RabbitProperties properties = new RabbitProperties(); |
|
|
|
|
RabbitStreamConfiguration.configure(builder, properties, |
|
|
|
|
new TestRabbitConnectionDetails("guest", "guest", "vhost"), |
|
|
|
|
getRabbitStreamConnectionDetails(properties)); |
|
|
|
|
getRabbitStreamConnectionDetails(properties, null)); |
|
|
|
|
then(builder).should().port(5552); |
|
|
|
|
then(builder).should().host("localhost"); |
|
|
|
|
then(builder).should().virtualHost("vhost"); |
|
|
|
|
@ -167,7 +176,7 @@ class RabbitStreamConfigurationTests {
@@ -167,7 +176,7 @@ class RabbitStreamConfigurationTests {
|
|
|
|
|
properties.getStream().setPort(5553); |
|
|
|
|
RabbitStreamConfiguration.configure(builder, properties, |
|
|
|
|
new TestRabbitConnectionDetails("guest", "guest", "vhost"), |
|
|
|
|
getRabbitStreamConnectionDetails(properties)); |
|
|
|
|
getRabbitStreamConnectionDetails(properties, null)); |
|
|
|
|
then(builder).should().port(5553); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -178,7 +187,7 @@ class RabbitStreamConfigurationTests {
@@ -178,7 +187,7 @@ class RabbitStreamConfigurationTests {
|
|
|
|
|
properties.getStream().setHost("stream.rabbit.example.com"); |
|
|
|
|
RabbitStreamConfiguration.configure(builder, properties, |
|
|
|
|
new TestRabbitConnectionDetails("guest", "guest", "vhost"), |
|
|
|
|
getRabbitStreamConnectionDetails(properties)); |
|
|
|
|
getRabbitStreamConnectionDetails(properties, null)); |
|
|
|
|
then(builder).should().host("stream.rabbit.example.com"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -189,7 +198,7 @@ class RabbitStreamConfigurationTests {
@@ -189,7 +198,7 @@ class RabbitStreamConfigurationTests {
|
|
|
|
|
properties.getStream().setVirtualHost("stream-virtual-host"); |
|
|
|
|
RabbitStreamConfiguration.configure(builder, properties, |
|
|
|
|
new TestRabbitConnectionDetails("guest", "guest", "vhost"), |
|
|
|
|
getRabbitStreamConnectionDetails(properties)); |
|
|
|
|
getRabbitStreamConnectionDetails(properties, null)); |
|
|
|
|
then(builder).should().virtualHost("stream-virtual-host"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -200,7 +209,7 @@ class RabbitStreamConfigurationTests {
@@ -200,7 +209,7 @@ class RabbitStreamConfigurationTests {
|
|
|
|
|
properties.setVirtualHost("default-virtual-host"); |
|
|
|
|
RabbitStreamConfiguration.configure(builder, properties, |
|
|
|
|
new TestRabbitConnectionDetails("guest", "guest", "default-virtual-host"), |
|
|
|
|
getRabbitStreamConnectionDetails(properties)); |
|
|
|
|
getRabbitStreamConnectionDetails(properties, null)); |
|
|
|
|
then(builder).should().virtualHost("default-virtual-host"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -212,7 +221,7 @@ class RabbitStreamConfigurationTests {
@@ -212,7 +221,7 @@ class RabbitStreamConfigurationTests {
|
|
|
|
|
properties.setPassword("secret"); |
|
|
|
|
RabbitStreamConfiguration.configure(builder, properties, |
|
|
|
|
new TestRabbitConnectionDetails("bob", "password", "vhost"), |
|
|
|
|
getRabbitStreamConnectionDetails(properties)); |
|
|
|
|
getRabbitStreamConnectionDetails(properties, null)); |
|
|
|
|
then(builder).should().username("bob"); |
|
|
|
|
then(builder).should().password("password"); |
|
|
|
|
} |
|
|
|
|
@ -227,7 +236,7 @@ class RabbitStreamConfigurationTests {
@@ -227,7 +236,7 @@ class RabbitStreamConfigurationTests {
|
|
|
|
|
properties.getStream().setPassword("confidential"); |
|
|
|
|
RabbitStreamConfiguration.configure(builder, properties, |
|
|
|
|
new TestRabbitConnectionDetails("guest", "guest", "vhost"), |
|
|
|
|
getRabbitStreamConnectionDetails(properties)); |
|
|
|
|
getRabbitStreamConnectionDetails(properties, null)); |
|
|
|
|
then(builder).should().username("bob"); |
|
|
|
|
then(builder).should().password("confidential"); |
|
|
|
|
} |
|
|
|
|
@ -319,8 +328,92 @@ class RabbitStreamConfigurationTests {
@@ -319,8 +328,92 @@ class RabbitStreamConfigurationTests {
|
|
|
|
|
.containsExactly("rabbitmq", 5555)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private RabbitStreamConnectionDetails getRabbitStreamConnectionDetails(RabbitProperties properties) { |
|
|
|
|
return new PropertiesRabbitStreamConnectionDetails(properties.getStream()); |
|
|
|
|
@Test |
|
|
|
|
void whenStreamSslIsNotConfiguredThenTlsIsNotUsed() { |
|
|
|
|
EnvironmentBuilder builder = mock(EnvironmentBuilder.class); |
|
|
|
|
RabbitProperties properties = new RabbitProperties(); |
|
|
|
|
RabbitStreamConfiguration.configure(builder, properties, |
|
|
|
|
new TestRabbitConnectionDetails("guest", "guest", "vhost"), |
|
|
|
|
getRabbitStreamConnectionDetails(properties, null)); |
|
|
|
|
then(builder).should(never()).tls(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void whenStreamSslIsEnabledThenTlsIsUsed() { |
|
|
|
|
EnvironmentBuilder builder = mock(EnvironmentBuilder.class); |
|
|
|
|
RabbitProperties properties = new RabbitProperties(); |
|
|
|
|
properties.getStream().getSsl().setEnabled(true); |
|
|
|
|
RabbitStreamConfiguration.configure(builder, properties, |
|
|
|
|
new TestRabbitConnectionDetails("guest", "guest", "vhost"), |
|
|
|
|
getRabbitStreamConnectionDetails(properties, null)); |
|
|
|
|
then(builder).should().tls(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void whenStreamSslBundleIsConfiguredThenTlsIsUsed() { |
|
|
|
|
this.contextRunner.withPropertyValues("spring.rabbitmq.stream.ssl.bundle=test-bundle", |
|
|
|
|
"spring.ssl.bundle.jks.test-bundle.keystore.location=classpath:org/springframework/boot/amqp/autoconfigure/test.jks", |
|
|
|
|
"spring.ssl.bundle.jks.test-bundle.keystore.password=secret") |
|
|
|
|
.run((context) -> { |
|
|
|
|
assertThat(context).hasNotFailed(); |
|
|
|
|
assertThat(context).hasSingleBean(Environment.class); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void whenStreamSslIsDisabledThenTlsIsNotUsed() { |
|
|
|
|
EnvironmentBuilder builder = mock(EnvironmentBuilder.class); |
|
|
|
|
RabbitProperties properties = new RabbitProperties(); |
|
|
|
|
properties.getStream().getSsl().setEnabled(false); |
|
|
|
|
RabbitStreamConfiguration.configure(builder, properties, |
|
|
|
|
new TestRabbitConnectionDetails("guest", "guest", "vhost"), |
|
|
|
|
getRabbitStreamConnectionDetails(properties, null)); |
|
|
|
|
then(builder).should(never()).tls(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void whenStreamSslIsDisabledWithBundleThenTlsIsStillUsed() { |
|
|
|
|
EnvironmentBuilder builder = mock(EnvironmentBuilder.class); |
|
|
|
|
SslBundles sslBundles = mock(SslBundles.class); |
|
|
|
|
RabbitProperties properties = new RabbitProperties(); |
|
|
|
|
properties.getStream().getSsl().setEnabled(false); |
|
|
|
|
properties.getStream().getSsl().setBundle("some-bundle"); |
|
|
|
|
RabbitStreamConfiguration.configure(builder, properties, |
|
|
|
|
new TestRabbitConnectionDetails("guest", "guest", "vhost"), |
|
|
|
|
getRabbitStreamConnectionDetails(properties, sslBundles)); |
|
|
|
|
then(builder).should().tls(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void whenConnectionDetailsSslBundleIsProvidedThenTlsIsUsedWithoutProperties() { |
|
|
|
|
this.contextRunner.withUserConfiguration(CustomConnectionDetails.class) |
|
|
|
|
.withPropertyValues( |
|
|
|
|
"spring.ssl.bundle.jks.test-bundle.keystore.location=classpath:org/springframework/boot/amqp/autoconfigure/test.jks", |
|
|
|
|
"spring.ssl.bundle.jks.test-bundle.keystore.password=secret") |
|
|
|
|
.run((context) -> { |
|
|
|
|
assertThat(context).hasNotFailed(); |
|
|
|
|
assertThat(context).hasSingleBean(Environment.class); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void whenStreamSslBundleIsInvalidThenFails() { |
|
|
|
|
EnvironmentBuilder builder = mock(EnvironmentBuilder.class); |
|
|
|
|
SslBundles sslBundles = mock(SslBundles.class); |
|
|
|
|
given(sslBundles.getBundle("invalid-bundle")).willThrow( |
|
|
|
|
new NoSuchSslBundleException("invalid-bundle", "SSL bundle name 'invalid-bundle' cannot be found")); |
|
|
|
|
RabbitProperties properties = new RabbitProperties(); |
|
|
|
|
properties.getStream().getSsl().setBundle("invalid-bundle"); |
|
|
|
|
assertThatExceptionOfType(NoSuchSslBundleException.class) |
|
|
|
|
.isThrownBy(() -> RabbitStreamConfiguration.configure(builder, properties, |
|
|
|
|
new TestRabbitConnectionDetails("guest", "guest", "vhost"), |
|
|
|
|
getRabbitStreamConnectionDetails(properties, sslBundles))) |
|
|
|
|
.withMessageContaining("invalid-bundle"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private RabbitStreamConnectionDetails getRabbitStreamConnectionDetails(RabbitProperties properties, |
|
|
|
|
@Nullable SslBundles sslBundles) { |
|
|
|
|
return new PropertiesRabbitStreamConnectionDetails(properties.getStream(), sslBundles); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Configuration(proxyBeanMethods = false) |
|
|
|
|
@ -448,7 +541,7 @@ class RabbitStreamConfigurationTests {
@@ -448,7 +541,7 @@ class RabbitStreamConfigurationTests {
|
|
|
|
|
static class CustomConnectionDetails { |
|
|
|
|
|
|
|
|
|
@Bean |
|
|
|
|
RabbitStreamConnectionDetails customRabbitMqStreamConnectionDetails() { |
|
|
|
|
RabbitStreamConnectionDetails customRabbitMqStreamConnectionDetails(SslBundles sslBundles) { |
|
|
|
|
return new RabbitStreamConnectionDetails() { |
|
|
|
|
@Override |
|
|
|
|
public String getHost() { |
|
|
|
|
@ -459,6 +552,14 @@ class RabbitStreamConfigurationTests {
@@ -459,6 +552,14 @@ class RabbitStreamConfigurationTests {
|
|
|
|
|
public int getPort() { |
|
|
|
|
return 5555; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public @Nullable SslBundle getSslBundle() { |
|
|
|
|
if (sslBundles.getBundleNames().contains("test-bundle")) { |
|
|
|
|
return sslBundles.getBundle("test-bundle"); |
|
|
|
|
} |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|