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