Commit a06ec4a8 authored by Madhura Bhave's avatar Madhura Bhave

Mongo Uri overrides host and port

Fixes gh-4017
parent ba1cf9a3
......@@ -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() {
......
......@@ -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();
......
......@@ -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<ServerAddress> 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<ServerAddress> 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<ServerAddress> 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<ServerAddress> allAddresses = extractServerAddresses(client);
assertThat(allAddresses).hasSize(1);
assertServerAddress(allAddresses.get(0), "localhost", 27017);
}
private List<ServerAddress> extractServerAddresses(MongoClient client) {
Cluster cluster = (Cluster) ReflectionTestUtils.getField(client, "cluster");
ClusterSettings clusterSettings = (ClusterSettings) ReflectionTestUtils
.getField(cluster, "settings");
List<ServerAddress> 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 {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment