From 64599261a5746b36f231f9502d11fef2f0c9624e Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 26 Nov 2014 17:34:21 +0000 Subject: [PATCH] Allow mongo port, host, and credentials to be configured individually MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously, the host had to have a custom value for the configuration of the port or credentials (username and password) to take effect. This meant, for example, that you couldn’t just set the port or just set the username and password while using the default host. This commit allows the port or username and password to be configured without also configuring the host. The default host (localhost) and port (27017) are retained. Fixes gh-2008 --- .../autoconfigure/mongo/MongoProperties.java | 27 +++++-- .../mongo/MongoPropertiesTests.java | 70 +++++++++++++++++++ 2 files changed, 90 insertions(+), 7 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoProperties.java index 5556e06b4d..b49d57c249 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoProperties.java @@ -35,13 +35,16 @@ import com.mongodb.ServerAddress; * @author Dave Syer * @author Phillip Webb * @author Josh Long + * @author Andy Wilkinson */ @ConfigurationProperties(prefix = "spring.data.mongodb") public class MongoProperties { + private static final int DEFAULT_PORT = 27017; + private String host; - private int port = 27017; + private Integer port = null; private String uri = "mongodb://localhost/test"; @@ -102,11 +105,11 @@ public class MongoProperties { this.uri = uri; } - public int getPort() { + public Integer getPort() { return this.port; } - public void setPort(int port) { + public void setPort(Integer port) { this.port = port; } @@ -128,17 +131,19 @@ public class MongoProperties { public MongoClient createMongoClient(MongoClientOptions options) throws UnknownHostException { try { - if (this.host != null) { + if (customAddress() || customCredentials()) { if (options == null) { options = MongoClientOptions.builder().build(); } List credentials = null; - if (this.password != null && this.username != null) { + if (customCredentials()) { credentials = Arrays.asList(MongoCredential.createMongoCRCredential( this.username, getMongoClientDatabase(), this.password)); } - return new MongoClient(Arrays.asList(new ServerAddress(this.host, - this.port)), credentials, options); + String host = this.host == null ? "localhost" : this.host; + int port = this.port == null ? DEFAULT_PORT : this.port; + return new MongoClient(Arrays.asList(new ServerAddress(host, port)), + credentials, options); } // The options and credentials are in the URI return new MongoClient(new MongoClientURI(this.uri, builder(options))); @@ -148,6 +153,14 @@ public class MongoProperties { } } + private boolean customAddress() { + return this.host != null || this.port != null; + } + + private boolean customCredentials() { + return this.username != null && this.password != null; + } + private Builder builder(MongoClientOptions options) { Builder builder = MongoClientOptions.builder(); if (options != null) { diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoPropertiesTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoPropertiesTests.java index 60105a2a52..986b0d2db9 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoPropertiesTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoPropertiesTests.java @@ -16,19 +16,29 @@ package org.springframework.boot.autoconfigure.mongo; +import java.net.UnknownHostException; +import java.util.List; + import org.junit.Test; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Configuration; +import com.mongodb.MongoClient; +import com.mongodb.MongoCredential; +import com.mongodb.ServerAddress; + import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasSize; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; /** * Tests for {@link MongoProperties}. * * @author Phillip Webb + * @author Andy Wilkinson */ public class MongoPropertiesTests { @@ -43,6 +53,66 @@ public class MongoPropertiesTests { assertThat(properties.getPassword(), equalTo("word".toCharArray())); } + @Test + public void portCanBeCustomized() throws UnknownHostException { + MongoProperties properties = new MongoProperties(); + properties.setPort(12345); + MongoClient client = properties.createMongoClient(null); + List allAddresses = client.getAllAddress(); + assertThat(allAddresses, hasSize(1)); + assertServerAddress(allAddresses.get(0), "localhost", 12345); + } + + @Test + public void hostCanBeCustomized() throws UnknownHostException { + MongoProperties properties = new MongoProperties(); + properties.setHost("mongo.example.com"); + MongoClient client = properties.createMongoClient(null); + List allAddresses = client.getAllAddress(); + assertThat(allAddresses, hasSize(1)); + assertServerAddress(allAddresses.get(0), "mongo.example.com", 27017); + } + + @Test + public void credentialsCanBeCustomized() throws UnknownHostException { + MongoProperties properties = new MongoProperties(); + properties.setUsername("user"); + properties.setPassword("secret".toCharArray()); + + MongoClient client = properties.createMongoClient(null); + + assertMongoCredential(client.getCredentialsList().get(0), "user", "secret"); + } + + @Test + public void uriCanBeCustomized() throws UnknownHostException { + MongoProperties properties = new MongoProperties(); + properties + .setUri("mongodb://user:secret@mongo1.example.com:12345,mongo2.example.com:23456/test"); + MongoClient client = properties.createMongoClient(null); + + List allAddresses = client.getAllAddress(); + assertEquals(2, allAddresses.size()); + assertServerAddress(allAddresses.get(0), "mongo1.example.com", 12345); + assertServerAddress(allAddresses.get(1), "mongo2.example.com", 23456); + + List credentialsList = client.getCredentialsList(); + assertEquals(1, credentialsList.size()); + assertMongoCredential(credentialsList.get(0), "user", "secret"); + } + + private void assertServerAddress(ServerAddress serverAddress, String expectedHost, + int expectedPort) { + assertThat(serverAddress.getHost(), equalTo(expectedHost)); + assertThat(serverAddress.getPort(), equalTo(expectedPort)); + } + + private void assertMongoCredential(MongoCredential credentials, + String expectedUsername, String expectedPassword) { + assertThat(credentials.getUserName(), equalTo(expectedUsername)); + assertThat(credentials.getPassword(), equalTo(expectedPassword.toCharArray())); + } + @Configuration @EnableConfigurationProperties(MongoProperties.class) static class Conf {