Skip to content

Commit

Permalink
Fix AssertJ conflicts in new Appender tests
Browse files Browse the repository at this point in the history
  • Loading branch information
artembilan committed Jul 18, 2019
1 parent 98cb7a1 commit 586fa83
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DefaultSaslConfig;
import com.rabbitmq.client.JDKSaslConfig;
import com.rabbitmq.client.SaslConfig;
import com.rabbitmq.client.impl.CRDemoMechanism;

/**
Expand Down Expand Up @@ -196,26 +197,34 @@ public void testSaslConfig() {
Logger logger = LogManager.getLogger("sasl");
AmqpAppender appender = (AmqpAppender) TestUtils.getPropertyValue(logger, "context.configuration.appenders",
Map.class).get("sasl1");
assertThat(TestUtils.getPropertyValue(appender, "manager.connectionFactory.rabbitConnectionFactory",
ConnectionFactory.class).getSaslConfig())
.isInstanceOf(DefaultSaslConfig.class)
.hasFieldOrPropertyWithValue("mechanism", "PLAIN");
SaslConfig saslConfig =
TestUtils.getPropertyValue(appender, "manager.connectionFactory.rabbitConnectionFactory",
ConnectionFactory.class)
.getSaslConfig();
assertThat(saslConfig, instanceOf(DefaultSaslConfig.class));
assertEquals("PLAIN", TestUtils.getPropertyValue(saslConfig, "mechanism"));
appender = (AmqpAppender) TestUtils.getPropertyValue(logger, "context.configuration.appenders",
Map.class).get("sasl2");
assertThat(TestUtils.getPropertyValue(appender, "manager.connectionFactory.rabbitConnectionFactory",
ConnectionFactory.class).getSaslConfig())
.isInstanceOf(DefaultSaslConfig.class)
.hasFieldOrPropertyWithValue("mechanism", "EXTERNAL");
saslConfig =
TestUtils.getPropertyValue(appender, "manager.connectionFactory.rabbitConnectionFactory",
ConnectionFactory.class)
.getSaslConfig();
assertThat(saslConfig, instanceOf(DefaultSaslConfig.class));
assertEquals("EXTERNAL", TestUtils.getPropertyValue(saslConfig, "mechanism"));
appender = (AmqpAppender) TestUtils.getPropertyValue(logger, "context.configuration.appenders",
Map.class).get("sasl3");
assertThat(TestUtils.getPropertyValue(appender, "manager.connectionFactory.rabbitConnectionFactory",
ConnectionFactory.class).getSaslConfig())
.isInstanceOf(JDKSaslConfig.class);
saslConfig =
TestUtils.getPropertyValue(appender, "manager.connectionFactory.rabbitConnectionFactory",
ConnectionFactory.class)
.getSaslConfig();
assertThat(saslConfig, instanceOf(JDKSaslConfig.class));
appender = (AmqpAppender) TestUtils.getPropertyValue(logger, "context.configuration.appenders",
Map.class).get("sasl4");
assertThat(TestUtils.getPropertyValue(appender, "manager.connectionFactory.rabbitConnectionFactory",
ConnectionFactory.class).getSaslConfig())
.isInstanceOf(CRDemoMechanism.CRDemoSaslConfig.class);
saslConfig =
TestUtils.getPropertyValue(appender, "manager.connectionFactory.rabbitConnectionFactory",
ConnectionFactory.class)
.getSaslConfig();
assertThat(saslConfig, instanceOf(CRDemoMechanism.CRDemoSaslConfig.class));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@

package org.springframework.amqp.rabbit.logback;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyString;
Expand All @@ -35,6 +38,7 @@

import org.springframework.amqp.UncategorizedAmqpException;
import org.springframework.amqp.rabbit.connection.RabbitConnectionFactoryBean;
import org.springframework.amqp.utils.test.TestUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.test.util.ReflectionTestUtils;

Expand All @@ -47,6 +51,7 @@
*
* @author Stephen Oakey
* @author Artem Bilan
* @author Gary Russell
*
* @since 2.0
*/
Expand Down Expand Up @@ -214,30 +219,34 @@ public void testSasl() {
verify(bean).setUseSSL(eq(true));
ArgumentCaptor<SaslConfig> captor = ArgumentCaptor.forClass(SaslConfig.class);
verify(bean).setSaslConfig(captor.capture());
assertThat(captor.getValue())
.isInstanceOf(DefaultSaslConfig.class)
.hasFieldOrPropertyWithValue("mechanism", "PLAIN");
SaslConfig saslConfig = captor.getValue();
assertThat(saslConfig, instanceOf(DefaultSaslConfig.class));
assertEquals("PLAIN", TestUtils.getPropertyValue(saslConfig, "mechanism"));
appender.setSaslConfig("DefaultSaslConfig.EXTERNAL");
appender.configureRabbitConnectionFactory(bean);
verify(bean, times(2)).setSaslConfig(captor.capture());
assertThat(captor.getValue())
.isInstanceOf(DefaultSaslConfig.class)
.hasFieldOrPropertyWithValue("mechanism", "EXTERNAL");
saslConfig = captor.getValue();
assertThat(saslConfig, instanceOf(DefaultSaslConfig.class));
assertEquals("EXTERNAL", TestUtils.getPropertyValue(saslConfig, "mechanism"));
appender.setSaslConfig("JDKSaslConfig");
appender.configureRabbitConnectionFactory(bean);
verify(bean, times(3)).setSaslConfig(captor.capture());
assertThat(captor.getValue())
.isInstanceOf(JDKSaslConfig.class);
assertThat(captor.getValue(), instanceOf(JDKSaslConfig.class));
appender.setSaslConfig("CRDemoSaslConfig");
appender.configureRabbitConnectionFactory(bean);
verify(bean, times(4)).setSaslConfig(captor.capture());
assertThat(captor.getValue())
.isInstanceOf(CRDemoMechanism.CRDemoSaslConfig.class);
assertThat(captor.getValue(), instanceOf(CRDemoMechanism.CRDemoSaslConfig.class));
appender.setSaslConfig("junk");
assertThatThrownBy(() -> appender.configureRabbitConnectionFactory(bean))
.isInstanceOf(UncategorizedAmqpException.class)
.hasCauseInstanceOf(IllegalStateException.class)
.withFailMessage("Unrecognized SaslConfig: junk");


try {
appender.configureRabbitConnectionFactory(bean);
}
catch (Exception e) {
assertThat(e, instanceOf(UncategorizedAmqpException.class));
assertThat(e.getCause(), instanceOf(IllegalStateException.class));
assertThat(e.getMessage(), containsString("Unrecognized SaslConfig: junk"));
}
}


Expand Down

0 comments on commit 586fa83

Please sign in to comment.