From a06ec4a8779cd5f08dac80b2558e0bdc2a25a560 Mon Sep 17 00:00:00 2001 From: Madhura Bhave Date: Mon, 9 Oct 2017 19:31:30 -0700 Subject: [PATCH] Mongo Uri overrides host and port Fixes gh-4017 --- .../mongo/MongoClientFactory.java | 12 ++-- .../mongo/MongoClientFactoryTests.java | 24 ------- .../mongo/MongoPropertiesTests.java | 67 +++++++++++++++++++ 3 files changed, 72 insertions(+), 31 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoClientFactory.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoClientFactory.java index c102d3d4116..f630f564e88 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoClientFactory.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoClientFactory.java @@ -89,12 +89,11 @@ public class MongoClientFactory { } private MongoClient createNetworkMongoClient(MongoClientOptions options) { - + if (this.properties.getUri() != null) { + return new MongoClient( + new MongoClientURI(this.properties.getUri(), builder(options))); + } if (hasCustomAddress() || hasCustomCredentials()) { - if (this.properties.getUri() != null) { - throw new IllegalStateException("Invalid mongo configuration, " - + "either uri or host/port/credentials must be specified"); - } if (options == null) { options = MongoClientOptions.builder().build(); } @@ -115,9 +114,8 @@ public class MongoClientFactory { Collections.singletonList(new ServerAddress(host, port)), credentials, options); } - // The options and credentials are in the URI return new MongoClient( - new MongoClientURI(this.properties.determineUri(), builder(options))); + new MongoClientURI(MongoProperties.DEFAULT_URI, builder(options))); } private boolean hasCustomAddress() { diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoClientFactoryTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoClientFactoryTests.java index c628ee19a42..dd437ddca60 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoClientFactoryTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoClientFactoryTests.java @@ -117,30 +117,6 @@ public class MongoClientFactoryTests { assertMongoCredential(credentialsList.get(0), "user", "secret", "test"); } - @Test - public void uriCannotBeSetWithCredentials() { - MongoProperties properties = new MongoProperties(); - properties.setUri("mongodb://127.0.0.1:1234/mydb"); - properties.setUsername("user"); - properties.setPassword("secret".toCharArray()); - this.thrown.expect(IllegalStateException.class); - this.thrown.expectMessage("Invalid mongo configuration, " - + "either uri or host/port/credentials must be specified"); - createMongoClient(properties); - } - - @Test - public void uriCannotBeSetWithHostPort() { - MongoProperties properties = new MongoProperties(); - properties.setUri("mongodb://127.0.0.1:1234/mydb"); - properties.setHost("localhost"); - properties.setPort(4567); - this.thrown.expect(IllegalStateException.class); - this.thrown.expectMessage("Invalid mongo configuration, " - + "either uri or host/port/credentials must be specified"); - createMongoClient(properties); - } - @Test public void uriIsIgnoredInEmbeddedMode() { MongoProperties properties = new MongoProperties(); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoPropertiesTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoPropertiesTests.java index 39b3b9a47df..7fea2f3d04c 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoPropertiesTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoPropertiesTests.java @@ -16,14 +16,21 @@ package org.springframework.boot.autoconfigure.mongo; +import java.net.UnknownHostException; +import java.util.List; + import com.mongodb.MongoClient; import com.mongodb.MongoClientOptions; +import com.mongodb.ServerAddress; +import com.mongodb.connection.Cluster; +import com.mongodb.connection.ClusterSettings; import org.junit.Test; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Configuration; +import org.springframework.test.util.ReflectionTestUtils; import static org.assertj.core.api.Assertions.assertThat; @@ -106,6 +113,66 @@ public class MongoPropertiesTests { .isEqualTo(options.getRequiredReplicaSetName()); } + @Test + public void uriOverridesHostAndPort() throws UnknownHostException { + MongoProperties properties = new MongoProperties(); + properties.setHost("localhost"); + properties.setPort(27017); + properties.setUri("mongodb://mongo1.example.com:12345"); + MongoClient client = new MongoClientFactory(properties, null) + .createMongoClient(null); + List allAddresses = extractServerAddresses(client); + assertThat(allAddresses).hasSize(1); + assertServerAddress(allAddresses.get(0), "mongo1.example.com", 12345); + } + + @Test + public void onlyHostAndPortSetShouldUseThat() throws UnknownHostException { + MongoProperties properties = new MongoProperties(); + properties.setHost("localhost"); + properties.setPort(27017); + MongoClient client = new MongoClientFactory(properties, null) + .createMongoClient(null); + List allAddresses = extractServerAddresses(client); + assertThat(allAddresses).hasSize(1); + assertServerAddress(allAddresses.get(0), "localhost", 27017); + } + + @Test + public void onlyUriSetShouldUseThat() throws UnknownHostException { + MongoProperties properties = new MongoProperties(); + properties.setUri("mongodb://mongo1.example.com:12345"); + MongoClient client = new MongoClientFactory(properties, null) + .createMongoClient(null); + List allAddresses = extractServerAddresses(client); + assertThat(allAddresses).hasSize(1); + assertServerAddress(allAddresses.get(0), "mongo1.example.com", 12345); + } + + @Test + public void noCustomAddressAndNoUriUsesDefaultUri() throws UnknownHostException { + MongoProperties properties = new MongoProperties(); + MongoClient client = new MongoClientFactory(properties, null) + .createMongoClient(null); + List allAddresses = extractServerAddresses(client); + assertThat(allAddresses).hasSize(1); + assertServerAddress(allAddresses.get(0), "localhost", 27017); + } + + private List extractServerAddresses(MongoClient client) { + Cluster cluster = (Cluster) ReflectionTestUtils.getField(client, "cluster"); + ClusterSettings clusterSettings = (ClusterSettings) ReflectionTestUtils + .getField(cluster, "settings"); + List allAddresses = clusterSettings.getHosts(); + return allAddresses; + } + + private void assertServerAddress(ServerAddress serverAddress, String expectedHost, + int expectedPort) { + assertThat(serverAddress.getHost()).isEqualTo(expectedHost); + assertThat(serverAddress.getPort()).isEqualTo(expectedPort); + } + @Configuration @EnableConfigurationProperties(MongoProperties.class) static class Config {