From 90369996a9410bb52e5b0c381c5d32d2845b901c Mon Sep 17 00:00:00 2001 From: Johnny Lim Date: Tue, 19 Dec 2017 07:41:32 +0900 Subject: [PATCH 1/2] Apply customize() to createCredentialNetworkMongoClient() See gh-11376 --- .../mongo/ReactiveMongoClientFactory.java | 9 ++++++--- .../mongo/ReactiveMongoClientFactoryTests.java | 10 ++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/ReactiveMongoClientFactory.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/ReactiveMongoClientFactory.java index 3a9e4e1db5..b8912d61ad 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/ReactiveMongoClientFactory.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/ReactiveMongoClientFactory.java @@ -92,6 +92,10 @@ public class ReactiveMongoClientFactory { ClusterSettings clusterSettings = ClusterSettings.builder() .hosts(Collections.singletonList(new ServerAddress(host, port))).build(); builder.clusterSettings(clusterSettings); + return createMongoClient(builder); + } + + private MongoClient createMongoClient(Builder builder) { customize(builder); return MongoClients.create(builder.build()); } @@ -102,7 +106,7 @@ public class ReactiveMongoClientFactory { } ConnectionString connectionString = new ConnectionString( this.properties.determineUri()); - return MongoClients.create(createBuilder(settings, connectionString).build()); + return createMongoClient(createBuilder(settings, connectionString)); } private MongoClient createCredentialNetworkMongoClient(MongoClientSettings settings) { @@ -117,7 +121,7 @@ public class ReactiveMongoClientFactory { ServerAddress serverAddress = new ServerAddress(host, port); builder.clusterSettings(ClusterSettings.builder() .hosts(Collections.singletonList(serverAddress)).build()); - return MongoClients.create(builder.build()); + return createMongoClient(builder); } private void applyCredentials(Builder builder) { @@ -155,7 +159,6 @@ public class ReactiveMongoClientFactory { if (connection.getApplicationName() != null) { builder.applicationName(connection.getApplicationName()); } - customize(builder); return builder; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/ReactiveMongoClientFactoryTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/ReactiveMongoClientFactoryTests.java index 33962435dd..48eb1144a5 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/ReactiveMongoClientFactoryTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/ReactiveMongoClientFactoryTests.java @@ -160,6 +160,16 @@ public class ReactiveMongoClientFactoryTests { verify(customizer).customize(any(MongoClientSettings.Builder.class)); } + @Test + public void customizerIsInvokedWhenHostIsSet() { + MongoProperties properties = new MongoProperties(); + properties.setHost("localhost"); + MongoClientSettingsBuilderCustomizer customizer = mock( + MongoClientSettingsBuilderCustomizer.class); + createMongoClient(properties, this.environment, customizer); + verify(customizer).customize(any(MongoClientSettings.Builder.class)); + } + @Test public void customizerIsInvokedForEmbeddedMongo() { MongoProperties properties = new MongoProperties(); From c709ff8e3b03af11acd6e6f31d961a5091ed8778 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Thu, 28 Dec 2017 12:39:54 +0100 Subject: [PATCH 2/2] Polish "Apply customize() to createCredentialNetworkMongoClient()" Closes gh-11376 --- .../mongo/ReactiveMongoClientFactory.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/ReactiveMongoClientFactory.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/ReactiveMongoClientFactory.java index b8912d61ad..61d3c6eb0d 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/ReactiveMongoClientFactory.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/ReactiveMongoClientFactory.java @@ -95,11 +95,6 @@ public class ReactiveMongoClientFactory { return createMongoClient(builder); } - private MongoClient createMongoClient(Builder builder) { - customize(builder); - return MongoClients.create(builder.build()); - } - private MongoClient createNetworkMongoClient(MongoClientSettings settings) { if (hasCustomAddress() || hasCustomCredentials()) { return createCredentialNetworkMongoClient(settings); @@ -138,6 +133,11 @@ public class ReactiveMongoClientFactory { return (value == null ? defaultValue : value); } + private MongoClient createMongoClient(Builder builder) { + customize(builder); + return MongoClients.create(builder.build()); + } + private Builder createBuilder(MongoClientSettings settings, ConnectionString connection) { Builder builder = builder(settings);