This commit is contained in:
Stephane Nicoll 2018-01-08 10:10:50 +01:00
parent f527c4b88c
commit 06572b0101
2 changed files with 61 additions and 100 deletions

View File

@ -20,12 +20,10 @@ import javax.net.SocketFactory;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import org.junit.After;
import org.junit.Test;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -40,58 +38,44 @@ import static org.mockito.Mockito.mock;
*/
public class MongoAutoConfigurationTests {
private AnnotationConfigApplicationContext context;
@After
public void close() {
if (this.context != null) {
this.context.close();
}
}
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(MongoAutoConfiguration.class));
@Test
public void clientExists() {
this.context = new AnnotationConfigApplicationContext(
PropertyPlaceholderAutoConfiguration.class, MongoAutoConfiguration.class);
assertThat(this.context.getBeanNamesForType(MongoClient.class)).hasSize(1);
this.contextRunner.run((context) ->
assertThat(context).hasSingleBean(MongoClient.class));
}
@Test
public void optionsAdded() {
this.context = new AnnotationConfigApplicationContext();
TestPropertyValues.of("spring.data.mongodb.host:localhost").applyTo(this.context);
this.context.register(OptionsConfig.class,
PropertyPlaceholderAutoConfiguration.class, MongoAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBean(MongoClient.class).getMongoClientOptions()
.getSocketTimeout()).isEqualTo(300);
this.contextRunner.withPropertyValues("spring.data.mongodb.host:localhost")
.withUserConfiguration(OptionsConfig.class).run((context) ->
assertThat(context.getBean(MongoClient.class).getMongoClientOptions()
.getSocketTimeout()).isEqualTo(300));
}
@Test
public void optionsAddedButNoHost() {
this.context = new AnnotationConfigApplicationContext();
TestPropertyValues.of("spring.data.mongodb.uri:mongodb://localhost/test")
.applyTo(this.context);
this.context.register(OptionsConfig.class,
PropertyPlaceholderAutoConfiguration.class, MongoAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBean(MongoClient.class).getMongoClientOptions()
.getSocketTimeout()).isEqualTo(300);
this.contextRunner
.withPropertyValues("spring.data.mongodb.uri:mongodb://localhost/test")
.withUserConfiguration(OptionsConfig.class).run((context) ->
assertThat(context.getBean(MongoClient.class).getMongoClientOptions()
.getSocketTimeout()).isEqualTo(300));
}
@Test
public void optionsSslConfig() {
this.context = new AnnotationConfigApplicationContext();
TestPropertyValues.of("spring.data.mongodb.uri:mongodb://localhost/test")
.applyTo(this.context);
this.context.register(SslOptionsConfig.class,
PropertyPlaceholderAutoConfiguration.class, MongoAutoConfiguration.class);
this.context.refresh();
MongoClient mongo = this.context.getBean(MongoClient.class);
MongoClientOptions options = mongo.getMongoClientOptions();
assertThat(options.isSslEnabled()).isTrue();
assertThat(options.getSocketFactory())
.isSameAs(this.context.getBean("mySocketFactory"));
this.contextRunner
.withPropertyValues("spring.data.mongodb.uri:mongodb://localhost/test")
.withUserConfiguration(SslOptionsConfig.class).run((context) -> {
assertThat(context).hasSingleBean(MongoClient.class);
MongoClient mongo = context.getBean(MongoClient.class);
MongoClientOptions options = mongo.getMongoClientOptions();
assertThat(options.isSslEnabled()).isTrue();
assertThat(options.getSocketFactory())
.isSameAs(context.getBean("mySocketFactory"));
});
}
@Configuration

View File

@ -24,12 +24,10 @@ import com.mongodb.connection.SocketSettings;
import com.mongodb.connection.StreamFactory;
import com.mongodb.connection.StreamFactoryFactory;
import com.mongodb.reactivestreams.client.MongoClient;
import org.junit.After;
import org.junit.Test;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -42,83 +40,62 @@ import static org.mockito.Mockito.mock;
* Tests for {@link MongoReactiveAutoConfiguration}.
*
* @author Mark Paluch
* @author Stephane Nicoll
*/
public class MongoReactiveAutoConfigurationTests {
private AnnotationConfigApplicationContext context;
@After
public void close() {
if (this.context != null) {
this.context.close();
}
}
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(
MongoReactiveAutoConfiguration.class));
@Test
public void clientExists() {
this.context = new AnnotationConfigApplicationContext(
PropertyPlaceholderAutoConfiguration.class,
MongoReactiveAutoConfiguration.class);
assertThat(this.context.getBeanNamesForType(MongoClient.class).length)
.isEqualTo(1);
this.contextRunner.run((context) ->
assertThat(context).hasSingleBean(MongoClient.class));
}
@Test
public void optionsAdded() {
this.context = new AnnotationConfigApplicationContext();
TestPropertyValues.of("spring.data.mongodb.host:localhost").applyTo(this.context);
this.context.register(OptionsConfig.class,
PropertyPlaceholderAutoConfiguration.class,
MongoReactiveAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBean(MongoClient.class).getSettings()
.getSocketSettings().getReadTimeout(TimeUnit.SECONDS)).isEqualTo(300);
this.contextRunner.withPropertyValues("spring.data.mongodb.host:localhost")
.withUserConfiguration(OptionsConfig.class).run((context) ->
assertThat(context.getBean(MongoClient.class).getSettings()
.getSocketSettings().getReadTimeout(TimeUnit.SECONDS))
.isEqualTo(300));
}
@Test
public void optionsAddedButNoHost() {
this.context = new AnnotationConfigApplicationContext();
TestPropertyValues.of("spring.data.mongodb.uri:mongodb://localhost/test")
.applyTo(this.context);
this.context.register(OptionsConfig.class,
PropertyPlaceholderAutoConfiguration.class,
MongoReactiveAutoConfiguration.class);
this.context.refresh();
assertThat(
this.context.getBean(MongoClient.class).getSettings().getReadPreference())
.isEqualTo(ReadPreference.nearest());
this.contextRunner
.withPropertyValues("spring.data.mongodb.uri:mongodb://localhost/test")
.withUserConfiguration(OptionsConfig.class).run((context) ->
assertThat(context.getBean(MongoClient.class).getSettings()
.getReadPreference()).isEqualTo(ReadPreference.nearest()));
}
@Test
public void optionsSslConfig() {
this.context = new AnnotationConfigApplicationContext();
TestPropertyValues.of("spring.data.mongodb.uri:mongodb://localhost/test")
.applyTo(this.context);
this.context.register(SslOptionsConfig.class,
PropertyPlaceholderAutoConfiguration.class,
MongoReactiveAutoConfiguration.class);
this.context.refresh();
MongoClient mongo = this.context.getBean(MongoClient.class);
MongoClientSettings settings = mongo.getSettings();
assertThat(settings.getApplicationName()).isEqualTo("test-config");
assertThat(settings.getStreamFactoryFactory())
.isSameAs(this.context.getBean("myStreamFactoryFactory"));
this.contextRunner
.withPropertyValues("spring.data.mongodb.uri:mongodb://localhost/test")
.withUserConfiguration(SslOptionsConfig.class).run((context) -> {
assertThat(context).hasSingleBean(MongoClient.class);
MongoClient mongo = context.getBean(MongoClient.class);
MongoClientSettings settings = mongo.getSettings();
assertThat(settings.getApplicationName()).isEqualTo("test-config");
assertThat(settings.getStreamFactoryFactory())
.isSameAs(context.getBean("myStreamFactoryFactory"));
});
}
@Test
public void customizerOverridesAutoConfig() {
this.context = new AnnotationConfigApplicationContext();
TestPropertyValues
.of("spring.data.mongodb.uri:mongodb://localhost/test?appname=auto-config")
.applyTo(this.context);
this.context.register(PropertyPlaceholderAutoConfiguration.class,
MongoReactiveAutoConfiguration.class, SimpleCustomizerConfig.class);
this.context.refresh();
assertThat(this.context.getBeanNamesForType(MongoClient.class).length)
.isEqualTo(1);
MongoClient client = this.context.getBean(MongoClient.class);
assertThat(client.getSettings().getApplicationName())
.isEqualTo("overridden-name");
this.contextRunner
.withPropertyValues("spring.data.mongodb.uri:mongodb://localhost/test?appname=auto-config")
.withUserConfiguration(SimpleCustomizerConfig.class).run((context) -> {
assertThat(context).hasSingleBean(MongoClient.class);
MongoClient client = context.getBean(MongoClient.class);
assertThat(client.getSettings().getApplicationName())
.isEqualTo("overridden-name");
});
}
@Configuration