Merge branch '2.7.x'
This commit is contained in:
		
						commit
						5702f9ebc3
					
				|  | @ -59,8 +59,7 @@ public class MongoProperties { | ||||||
| 	private Integer port = null; | 	private Integer port = null; | ||||||
| 
 | 
 | ||||||
| 	/** | 	/** | ||||||
| 	 * Mongo database URI. Cannot be set with host, port, credentials and replica set | 	 * Mongo database URI. Overrides host, port, username, password, and database. | ||||||
| 	 * name. |  | ||||||
| 	 */ | 	 */ | ||||||
| 	private String uri; | 	private String uri; | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -25,7 +25,6 @@ import com.mongodb.ServerAddress; | ||||||
| 
 | 
 | ||||||
| import org.springframework.core.Ordered; | import org.springframework.core.Ordered; | ||||||
| import org.springframework.core.env.Environment; | import org.springframework.core.env.Environment; | ||||||
| import org.springframework.util.Assert; |  | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * A {@link MongoClientSettingsBuilderCustomizer} that applies properties from a |  * A {@link MongoClientSettingsBuilderCustomizer} that applies properties from a | ||||||
|  | @ -49,20 +48,12 @@ public class MongoPropertiesClientSettingsBuilderCustomizer implements MongoClie | ||||||
| 
 | 
 | ||||||
| 	@Override | 	@Override | ||||||
| 	public void customize(MongoClientSettings.Builder settingsBuilder) { | 	public void customize(MongoClientSettings.Builder settingsBuilder) { | ||||||
| 		validateConfiguration(); |  | ||||||
| 		applyUuidRepresentation(settingsBuilder); | 		applyUuidRepresentation(settingsBuilder); | ||||||
| 		applyHostAndPort(settingsBuilder); | 		applyHostAndPort(settingsBuilder); | ||||||
| 		applyCredentials(settingsBuilder); | 		applyCredentials(settingsBuilder); | ||||||
| 		applyReplicaSet(settingsBuilder); | 		applyReplicaSet(settingsBuilder); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	private void validateConfiguration() { |  | ||||||
| 		if (hasCustomAddress() || hasCustomCredentials() || hasReplicaSet()) { |  | ||||||
| 			Assert.state(this.properties.getUri() == null, |  | ||||||
| 					"Invalid mongo configuration, either uri or host/port/credentials/replicaSet must be specified"); |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	private void applyUuidRepresentation(MongoClientSettings.Builder settingsBuilder) { | 	private void applyUuidRepresentation(MongoClientSettings.Builder settingsBuilder) { | ||||||
| 		settingsBuilder.uuidRepresentation(this.properties.getUuidRepresentation()); | 		settingsBuilder.uuidRepresentation(this.properties.getUuidRepresentation()); | ||||||
| 	} | 	} | ||||||
|  | @ -72,20 +63,23 @@ public class MongoPropertiesClientSettingsBuilderCustomizer implements MongoClie | ||||||
| 			settings.applyConnectionString(new ConnectionString("mongodb://localhost:" + getEmbeddedPort())); | 			settings.applyConnectionString(new ConnectionString("mongodb://localhost:" + getEmbeddedPort())); | ||||||
| 			return; | 			return; | ||||||
| 		} | 		} | ||||||
| 
 | 		if (this.properties.getUri() != null) { | ||||||
| 		if (hasCustomAddress()) { | 			settings.applyConnectionString(new ConnectionString(this.properties.getUri())); | ||||||
|  | 			return; | ||||||
|  | 		} | ||||||
|  | 		if (this.properties.getHost() != null || this.properties.getPort() != null) { | ||||||
| 			String host = getOrDefault(this.properties.getHost(), "localhost"); | 			String host = getOrDefault(this.properties.getHost(), "localhost"); | ||||||
| 			int port = getOrDefault(this.properties.getPort(), MongoProperties.DEFAULT_PORT); | 			int port = getOrDefault(this.properties.getPort(), MongoProperties.DEFAULT_PORT); | ||||||
| 			ServerAddress serverAddress = new ServerAddress(host, port); | 			ServerAddress serverAddress = new ServerAddress(host, port); | ||||||
| 			settings.applyToClusterSettings((cluster) -> cluster.hosts(Collections.singletonList(serverAddress))); | 			settings.applyToClusterSettings((cluster) -> cluster.hosts(Collections.singletonList(serverAddress))); | ||||||
| 			return; | 			return; | ||||||
| 		} | 		} | ||||||
| 
 | 		settings.applyConnectionString(new ConnectionString(MongoProperties.DEFAULT_URI)); | ||||||
| 		settings.applyConnectionString(new ConnectionString(this.properties.determineUri())); |  | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	private void applyCredentials(MongoClientSettings.Builder builder) { | 	private void applyCredentials(MongoClientSettings.Builder builder) { | ||||||
| 		if (hasCustomCredentials()) { | 		if (this.properties.getUri() == null && this.properties.getUsername() != null | ||||||
|  | 				&& this.properties.getPassword() != null) { | ||||||
| 			String database = (this.properties.getAuthenticationDatabase() != null) | 			String database = (this.properties.getAuthenticationDatabase() != null) | ||||||
| 					? this.properties.getAuthenticationDatabase() : this.properties.getMongoClientDatabase(); | 					? this.properties.getAuthenticationDatabase() : this.properties.getMongoClientDatabase(); | ||||||
| 			builder.credential((MongoCredential.createCredential(this.properties.getUsername(), database, | 			builder.credential((MongoCredential.createCredential(this.properties.getUsername(), database, | ||||||
|  | @ -94,7 +88,7 @@ public class MongoPropertiesClientSettingsBuilderCustomizer implements MongoClie | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	private void applyReplicaSet(MongoClientSettings.Builder builder) { | 	private void applyReplicaSet(MongoClientSettings.Builder builder) { | ||||||
| 		if (hasReplicaSet()) { | 		if (this.properties.getReplicaSetName() != null) { | ||||||
| 			builder.applyToClusterSettings( | 			builder.applyToClusterSettings( | ||||||
| 					(cluster) -> cluster.requiredReplicaSetName(this.properties.getReplicaSetName())); | 					(cluster) -> cluster.requiredReplicaSetName(this.properties.getReplicaSetName())); | ||||||
| 		} | 		} | ||||||
|  | @ -114,18 +108,6 @@ public class MongoPropertiesClientSettingsBuilderCustomizer implements MongoClie | ||||||
| 		return null; | 		return null; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	private boolean hasCustomCredentials() { |  | ||||||
| 		return this.properties.getUsername() != null && this.properties.getPassword() != null; |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	private boolean hasCustomAddress() { |  | ||||||
| 		return this.properties.getHost() != null || this.properties.getPort() != null; |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	private boolean hasReplicaSet() { |  | ||||||
| 		return this.properties.getReplicaSetName() != null; |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	@Override | 	@Override | ||||||
| 	public int getOrder() { | 	public int getOrder() { | ||||||
| 		return this.order; | 		return this.order; | ||||||
|  |  | ||||||
|  | @ -27,7 +27,6 @@ import org.junit.jupiter.api.Test; | ||||||
| import org.springframework.mock.env.MockEnvironment; | import org.springframework.mock.env.MockEnvironment; | ||||||
| 
 | 
 | ||||||
| import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||||
| import static org.assertj.core.api.Assertions.assertThatIllegalStateException; |  | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * Tests for {@link MongoPropertiesClientSettingsBuilderCustomizer}. |  * Tests for {@link MongoPropertiesClientSettingsBuilderCustomizer}. | ||||||
|  | @ -153,29 +152,34 @@ class MongoPropertiesClientSettingsBuilderCustomizerTests { | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	@Test | 	@Test | ||||||
| 	void uriCannotBeSetWithCredentials() { | 	void uriOverridesUsernameAndPassword() { | ||||||
| 		this.properties.setUri("mongodb://127.0.0.1:1234/mydb"); | 		this.properties.setUri("mongodb://127.0.0.1:1234/mydb"); | ||||||
| 		this.properties.setUsername("user"); | 		this.properties.setUsername("user"); | ||||||
| 		this.properties.setPassword("secret".toCharArray()); | 		this.properties.setPassword("secret".toCharArray()); | ||||||
| 		assertThatIllegalStateException().isThrownBy(this::customizeSettings).withMessageContaining( | 		MongoClientSettings settings = customizeSettings(); | ||||||
| 				"Invalid mongo configuration, either uri or host/port/credentials/replicaSet must be specified"); | 		assertThat(settings.getCredential()).isNull(); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	@Test | 	@Test | ||||||
| 	void uriCannotBeSetWithReplicaSetName() { | 	void uriOverridesDatabase() { | ||||||
| 		this.properties.setUri("mongodb://127.0.0.1:1234/mydb"); | 		this.properties.setUri("mongodb://secret:password@127.0.0.1:1234/mydb"); | ||||||
| 		this.properties.setReplicaSetName("test"); | 		this.properties.setDatabase("test"); | ||||||
| 		assertThatIllegalStateException().isThrownBy(this::customizeSettings).withMessageContaining( | 		MongoClientSettings settings = customizeSettings(); | ||||||
| 				"Invalid mongo configuration, either uri or host/port/credentials/replicaSet must be specified"); | 		List<ServerAddress> allAddresses = getAllAddresses(settings); | ||||||
|  | 		assertThat(allAddresses).hasSize(1); | ||||||
|  | 		assertServerAddress(allAddresses.get(0), "127.0.0.1", 1234); | ||||||
|  | 		assertThat(settings.getCredential().getSource()).isEqualTo("mydb"); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	@Test | 	@Test | ||||||
| 	void uriCannotBeSetWithHostPort() { | 	void uriOverridesHostAndPort() { | ||||||
| 		this.properties.setUri("mongodb://127.0.0.1:1234/mydb"); | 		this.properties.setUri("mongodb://127.0.0.1:1234/mydb"); | ||||||
| 		this.properties.setHost("localhost"); | 		this.properties.setHost("localhost"); | ||||||
| 		this.properties.setPort(4567); | 		this.properties.setPort(4567); | ||||||
| 		assertThatIllegalStateException().isThrownBy(this::customizeSettings).withMessageContaining( | 		MongoClientSettings settings = customizeSettings(); | ||||||
| 				"Invalid mongo configuration, either uri or host/port/credentials/replicaSet must be specified"); | 		List<ServerAddress> addresses = getAllAddresses(settings); | ||||||
|  | 		assertThat(addresses.get(0).getHost()).isEqualTo("127.0.0.1"); | ||||||
|  | 		assertThat(addresses.get(0).getPort()).isEqualTo(1234); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	@Test | 	@Test | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue