Polish "Allow customization of MongoClientSettings.Builder"
Closes gh-9341
This commit is contained in:
@@ -31,8 +31,8 @@ public interface MongoClientSettingsBuilderCustomizer {
|
||||
|
||||
/**
|
||||
* Customize the {@link Builder}.
|
||||
* @param settingsBuilder the builder to customize
|
||||
* @param clientSettingsBuilder the builder to customize
|
||||
*/
|
||||
void customize(Builder settingsBuilder);
|
||||
void customize(Builder clientSettingsBuilder);
|
||||
|
||||
}
|
||||
|
||||
@@ -54,7 +54,8 @@ public class ReactiveMongoClientFactory {
|
||||
List<MongoClientSettingsBuilderCustomizer> builderCustomizers) {
|
||||
this.properties = properties;
|
||||
this.environment = environment;
|
||||
this.builderCustomizers = builderCustomizers;
|
||||
this.builderCustomizers = (builderCustomizers != null ? builderCustomizers
|
||||
: Collections.emptyList());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -157,10 +158,8 @@ public class ReactiveMongoClientFactory {
|
||||
}
|
||||
|
||||
private void customize(MongoClientSettings.Builder builder) {
|
||||
if (this.builderCustomizers != null) {
|
||||
for (MongoClientSettingsBuilderCustomizer customizer : this.builderCustomizers) {
|
||||
customizer.customize(builder);
|
||||
}
|
||||
for (MongoClientSettingsBuilderCustomizer customizer : this.builderCustomizers) {
|
||||
customizer.customize(builder);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,6 @@ import java.util.concurrent.TimeUnit;
|
||||
|
||||
import com.mongodb.ReadPreference;
|
||||
import com.mongodb.async.client.MongoClientSettings;
|
||||
import com.mongodb.async.client.MongoClientSettings.Builder;
|
||||
import com.mongodb.connection.SocketSettings;
|
||||
import com.mongodb.connection.StreamFactory;
|
||||
import com.mongodb.connection.StreamFactoryFactory;
|
||||
@@ -38,7 +37,6 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Tests for {@link MongoReactiveAutoConfiguration}.
|
||||
@@ -108,21 +106,6 @@ public class MongoReactiveAutoConfigurationTests {
|
||||
.isSameAs(this.context.getBean("myStreamFactoryFactory"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customizerGetsInvoked() {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
TestPropertyValues.of(
|
||||
"spring.data.mongodb.uri:mongodb://localhost/test").applyTo(this.context);
|
||||
this.context.register(PropertyPlaceholderAutoConfiguration.class,
|
||||
MongoReactiveAutoConfiguration.class, MockCustomizerConfig.class);
|
||||
this.context.refresh();
|
||||
assertThat(this.context.getBeanNamesForType(MongoClient.class).length)
|
||||
.isEqualTo(1);
|
||||
MongoClientSettingsBuilderCustomizer customizer = this.context
|
||||
.getBean(MongoClientSettingsBuilderCustomizer.class);
|
||||
verify(customizer).customize(any(Builder.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customizerOverridesAutoConfig() {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
@@ -171,27 +154,13 @@ public class MongoReactiveAutoConfigurationTests {
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class MockCustomizerConfig {
|
||||
|
||||
@Bean
|
||||
public MongoClientSettingsBuilderCustomizer customizer() {
|
||||
return mock(MongoClientSettingsBuilderCustomizer.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class SimpleCustomizerConfig {
|
||||
|
||||
@Bean
|
||||
public MongoClientSettingsBuilderCustomizer customizer() {
|
||||
return new MongoClientSettingsBuilderCustomizer() {
|
||||
@Override
|
||||
public void customize(MongoClientSettings.Builder settingsBuilder) {
|
||||
settingsBuilder.applicationName("overridden-name");
|
||||
}
|
||||
};
|
||||
return clientSettingsBuilder ->
|
||||
clientSettingsBuilder.applicationName("overridden-name");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.springframework.boot.autoconfigure.mongo;
|
||||
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import com.mongodb.MongoCredential;
|
||||
@@ -32,6 +33,9 @@ import org.springframework.core.env.Environment;
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Tests for {@link ReactiveMongoClientFactory}.
|
||||
@@ -148,14 +152,24 @@ public class ReactiveMongoClientFactoryTests {
|
||||
assertServerAddress(allAddresses.get(0), "localhost", 4000);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customizerIsInvoked() {
|
||||
MongoProperties properties = new MongoProperties();
|
||||
MongoClientSettingsBuilderCustomizer customizer = mock(
|
||||
MongoClientSettingsBuilderCustomizer.class);
|
||||
createMongoClient(properties, this.environment, customizer);
|
||||
verify(customizer).customize(any(MongoClientSettings.Builder.class));
|
||||
}
|
||||
|
||||
private MongoClient createMongoClient(MongoProperties properties) {
|
||||
return createMongoClient(properties, this.environment);
|
||||
}
|
||||
|
||||
private MongoClient createMongoClient(MongoProperties properties,
|
||||
Environment environment) {
|
||||
return new ReactiveMongoClientFactory(properties, environment, null)
|
||||
.createMongoClient(null);
|
||||
Environment environment,
|
||||
MongoClientSettingsBuilderCustomizer... customizers) {
|
||||
return new ReactiveMongoClientFactory(properties, environment,
|
||||
Arrays.asList(customizers)).createMongoClient(null);
|
||||
}
|
||||
|
||||
private List<ServerAddress> extractServerAddresses(MongoClient client) {
|
||||
|
||||
Reference in New Issue
Block a user