mirror of
https://github.com/spring-projects/spring-boot.git
synced 2026-10-09 10:59:11 +00:00
Rename MongoDB properties that don't require Spring Data MongoDB
Previously, all configuration properties for MongoDB used the prefix spring.data.mongodb irrespective of whether or not they required Spring Data MongoDB. This commit renames the properties that do not require Spring Data MongoDB to use the prefix spring.mongodb. Closes gh-34954
This commit is contained in:
+20
-24
@@ -130,14 +130,13 @@ Otherwise a javadoc:{url-mongodb-driver-core-javadoc}/com.mongodb.MongoClientSet
|
||||
In either case, you can declare one or more javadoc:org.springframework.boot.autoconfigure.mongo.MongoClientSettingsBuilderCustomizer[] beans to fine-tune the javadoc:{url-mongodb-driver-core-javadoc}/com.mongodb.MongoClientSettings[] configuration.
|
||||
Each will be called in order with the javadoc:{url-mongodb-driver-core-javadoc}/com.mongodb.MongoClientSettings$Builder[] that is used to build the javadoc:{url-mongodb-driver-core-javadoc}/com.mongodb.MongoClientSettings[].
|
||||
|
||||
You can set the configprop:spring.data.mongodb.uri[] property to change the URL and configure additional settings such as the _replica set_, as shown in the following example:
|
||||
You can set the configprop:spring.mongodb.uri[] property to change the URL and configure additional settings such as the _replica set_, as shown in the following example:
|
||||
|
||||
[configprops,yaml]
|
||||
----
|
||||
spring:
|
||||
data:
|
||||
mongodb:
|
||||
uri: "mongodb://user:secret@mongoserver1.example.com:27017,mongoserver2.example.com:23456/test"
|
||||
mongodb:
|
||||
uri: "mongodb://user:secret@mongoserver1.example.com:27017,mongoserver2.example.com:23456/test"
|
||||
----
|
||||
|
||||
Alternatively, you can specify connection details using discrete properties.
|
||||
@@ -146,15 +145,14 @@ For example, you might declare the following settings in your `application.prope
|
||||
[configprops,yaml]
|
||||
----
|
||||
spring:
|
||||
data:
|
||||
mongodb:
|
||||
host: "mongoserver1.example.com"
|
||||
port: 27017
|
||||
additional-hosts:
|
||||
- "mongoserver2.example.com:23456"
|
||||
database: "test"
|
||||
username: "user"
|
||||
password: "secret"
|
||||
mongodb:
|
||||
host: "mongoserver1.example.com"
|
||||
port: 27017
|
||||
additional-hosts:
|
||||
- "mongoserver2.example.com:23456"
|
||||
database: "test"
|
||||
username: "user"
|
||||
password: "secret"
|
||||
----
|
||||
|
||||
The auto-configured javadoc:{url-mongodb-driver-sync-javadoc}/com.mongodb.client.MongoClient[] can be configured to use SSL for communication with the server by setting the properties as shown in this example:
|
||||
@@ -162,11 +160,10 @@ The auto-configured javadoc:{url-mongodb-driver-sync-javadoc}/com.mongodb.client
|
||||
[configprops,yaml]
|
||||
----
|
||||
spring:
|
||||
data:
|
||||
mongodb:
|
||||
uri: "mongodb://user:secret@mongoserver1.example.com:27017,mongoserver2.example.com:23456/test"
|
||||
ssl:
|
||||
enabled: true
|
||||
mongodb:
|
||||
uri: "mongodb://user:secret@mongoserver1.example.com:27017,mongoserver2.example.com:23456/test"
|
||||
ssl:
|
||||
enabled: true
|
||||
----
|
||||
|
||||
Custom SSL trust material can be configured in an xref:features/ssl.adoc[SSL bundle] and applied to the javadoc:{url-mongodb-driver-sync-javadoc}/com.mongodb.client.MongoClient[] as shown in this example:
|
||||
@@ -174,17 +171,16 @@ Custom SSL trust material can be configured in an xref:features/ssl.adoc[SSL bun
|
||||
[configprops,yaml]
|
||||
----
|
||||
spring:
|
||||
data:
|
||||
mongodb:
|
||||
uri: "mongodb://user:secret@mongoserver1.example.com:27017,mongoserver2.example.com:23456/test"
|
||||
ssl:
|
||||
bundle: "example"
|
||||
mongodb:
|
||||
uri: "mongodb://user:secret@mongoserver1.example.com:27017,mongoserver2.example.com:23456/test"
|
||||
ssl:
|
||||
bundle: "example"
|
||||
----
|
||||
|
||||
|
||||
[TIP]
|
||||
====
|
||||
If `spring.data.mongodb.port` is not specified, the default of `27017` is used.
|
||||
If configprop:spring.mongodb.port[] is not specified, the default of `27017` is used.
|
||||
You could delete this line from the example shown earlier.
|
||||
|
||||
You can also specify the port as part of the host address by using the `host:port` syntax.
|
||||
|
||||
+2
-2
@@ -33,8 +33,8 @@ public class MyContainersConfiguration {
|
||||
@Bean
|
||||
public DynamicPropertyRegistrar mongoDbProperties(MongoDBContainer container) {
|
||||
return (properties) -> {
|
||||
properties.add("spring.data.mongodb.host", container::getHost);
|
||||
properties.add("spring.data.mongodb.port", container::getFirstMappedPort);
|
||||
properties.add("spring.mongodb.host", container::getHost);
|
||||
properties.add("spring.mongodb.port", container::getFirstMappedPort);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -32,8 +32,8 @@ class MyContainersConfiguration {
|
||||
@Bean
|
||||
fun mongoDbProperties(container: MongoDBContainer): DynamicPropertyRegistrar {
|
||||
return DynamicPropertyRegistrar { properties ->
|
||||
properties.add("spring.data.mongodb.host") { container.host }
|
||||
properties.add("spring.data.mongodb.port") { container.firstMappedPort }
|
||||
properties.add("spring.mongodb.host") { container.host }
|
||||
properties.add("spring.mongodb.port") { container.firstMappedPort }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+94
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
* Copyright 2012-present the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.data.mongodb.autoconfigure;
|
||||
|
||||
import org.jspecify.annotations.Nullable;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* Configuration properties for Spring Data MongoDB.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @since 4.0.0
|
||||
*/
|
||||
@ConfigurationProperties("spring.data.mongodb")
|
||||
public class DataMongoProperties {
|
||||
|
||||
/**
|
||||
* Whether to enable auto-index creation.
|
||||
*/
|
||||
private @Nullable Boolean autoIndexCreation;
|
||||
|
||||
/**
|
||||
* Fully qualified name of the FieldNamingStrategy to use.
|
||||
*/
|
||||
private @Nullable Class<?> fieldNamingStrategy;
|
||||
|
||||
private final Gridfs gridfs = new Gridfs();
|
||||
|
||||
public @Nullable Boolean isAutoIndexCreation() {
|
||||
return this.autoIndexCreation;
|
||||
}
|
||||
|
||||
public void setAutoIndexCreation(@Nullable Boolean autoIndexCreation) {
|
||||
this.autoIndexCreation = autoIndexCreation;
|
||||
}
|
||||
|
||||
public @Nullable Class<?> getFieldNamingStrategy() {
|
||||
return this.fieldNamingStrategy;
|
||||
}
|
||||
|
||||
public void setFieldNamingStrategy(@Nullable Class<?> fieldNamingStrategy) {
|
||||
this.fieldNamingStrategy = fieldNamingStrategy;
|
||||
}
|
||||
|
||||
public Gridfs getGridfs() {
|
||||
return this.gridfs;
|
||||
}
|
||||
|
||||
public static class Gridfs {
|
||||
|
||||
/**
|
||||
* GridFS database name.
|
||||
*/
|
||||
private @Nullable String database;
|
||||
|
||||
/**
|
||||
* GridFS bucket name.
|
||||
*/
|
||||
private @Nullable String bucket;
|
||||
|
||||
public @Nullable String getDatabase() {
|
||||
return this.database;
|
||||
}
|
||||
|
||||
public void setDatabase(@Nullable String database) {
|
||||
this.database = database;
|
||||
}
|
||||
|
||||
public @Nullable String getBucket() {
|
||||
return this.bucket;
|
||||
}
|
||||
|
||||
public void setBucket(@Nullable String bucket) {
|
||||
this.bucket = bucket;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+4
-4
@@ -35,13 +35,13 @@ import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
|
||||
|
||||
/**
|
||||
* {@link EnableAutoConfiguration Auto-configuration} for Spring Data's mongo support.
|
||||
* {@link EnableAutoConfiguration Auto-configuration} for Spring Data's MongoDB support.
|
||||
* <p>
|
||||
* Registers a {@link MongoTemplate} and {@link GridFsTemplate} beans if no other beans of
|
||||
* the same type are configured.
|
||||
* <p>
|
||||
* Honors the {@literal spring.data.mongodb.database} property if set, otherwise connects
|
||||
* to the {@literal test} database.
|
||||
* Honors the {@literal spring.mongodb.database} property if set, otherwise connects to
|
||||
* the {@literal test} database.
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @author Oliver Gierke
|
||||
@@ -54,7 +54,7 @@ import org.springframework.data.mongodb.gridfs.GridFsTemplate;
|
||||
*/
|
||||
@AutoConfiguration(after = MongoAutoConfiguration.class)
|
||||
@ConditionalOnClass({ MongoClient.class, MongoTemplate.class })
|
||||
@EnableConfigurationProperties(MongoProperties.class)
|
||||
@EnableConfigurationProperties({ MongoProperties.class, DataMongoProperties.class })
|
||||
@Import({ MongoDataConfiguration.class, MongoDatabaseFactoryConfiguration.class,
|
||||
MongoDatabaseFactoryDependentConfiguration.class })
|
||||
public final class MongoDataAutoConfiguration {
|
||||
|
||||
+1
-2
@@ -23,7 +23,6 @@ import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.domain.EntityScanner;
|
||||
import org.springframework.boot.context.properties.PropertyMapper;
|
||||
import org.springframework.boot.mongodb.autoconfigure.MongoProperties;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@@ -57,7 +56,7 @@ class MongoDataConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
MongoMappingContext mongoMappingContext(MongoProperties properties, MongoCustomConversions conversions,
|
||||
MongoMappingContext mongoMappingContext(DataMongoProperties properties, MongoCustomConversions conversions,
|
||||
MongoManagedTypes managedTypes) {
|
||||
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
|
||||
MongoMappingContext context = new MongoMappingContext();
|
||||
|
||||
+4
-5
@@ -23,8 +23,7 @@ import org.jspecify.annotations.Nullable;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.mongodb.autoconfigure.MongoProperties;
|
||||
import org.springframework.boot.mongodb.autoconfigure.MongoProperties.Gridfs;
|
||||
import org.springframework.boot.data.mongodb.autoconfigure.DataMongoProperties.Gridfs;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
@@ -57,7 +56,7 @@ class MongoDatabaseFactoryDependentConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(GridFsOperations.class)
|
||||
GridFsTemplate gridFsTemplate(MongoProperties properties, MongoDatabaseFactory factory,
|
||||
GridFsTemplate gridFsTemplate(DataMongoProperties properties, MongoDatabaseFactory factory,
|
||||
MongoTemplate mongoTemplate) {
|
||||
return new GridFsTemplate(new GridFsMongoDatabaseFactory(factory, properties), mongoTemplate.getConverter(),
|
||||
properties.getGridfs().getBucket());
|
||||
@@ -71,9 +70,9 @@ class MongoDatabaseFactoryDependentConfiguration {
|
||||
|
||||
private final MongoDatabaseFactory mongoDatabaseFactory;
|
||||
|
||||
private final MongoProperties properties;
|
||||
private final DataMongoProperties properties;
|
||||
|
||||
GridFsMongoDatabaseFactory(MongoDatabaseFactory mongoDatabaseFactory, MongoProperties properties) {
|
||||
GridFsMongoDatabaseFactory(MongoDatabaseFactory mongoDatabaseFactory, DataMongoProperties properties) {
|
||||
Assert.notNull(mongoDatabaseFactory, "'mongoDatabaseFactory' must not be null");
|
||||
Assert.notNull(properties, "'properties' must not be null");
|
||||
this.mongoDatabaseFactory = mongoDatabaseFactory;
|
||||
|
||||
+11
-16
@@ -33,9 +33,9 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.boot.data.mongodb.autoconfigure.DataMongoProperties.Gridfs;
|
||||
import org.springframework.boot.mongodb.autoconfigure.MongoConnectionDetails;
|
||||
import org.springframework.boot.mongodb.autoconfigure.MongoProperties;
|
||||
import org.springframework.boot.mongodb.autoconfigure.MongoProperties.Gridfs;
|
||||
import org.springframework.boot.mongodb.autoconfigure.MongoReactiveAutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Import;
|
||||
@@ -72,21 +72,15 @@ import org.springframework.util.StringUtils;
|
||||
@AutoConfiguration(after = MongoReactiveAutoConfiguration.class)
|
||||
@ConditionalOnClass({ MongoClient.class, ReactiveMongoTemplate.class })
|
||||
@ConditionalOnBean(MongoClient.class)
|
||||
@EnableConfigurationProperties(MongoProperties.class)
|
||||
@EnableConfigurationProperties({ MongoProperties.class, DataMongoProperties.class })
|
||||
@Import(MongoDataConfiguration.class)
|
||||
public final class MongoReactiveDataAutoConfiguration {
|
||||
|
||||
private final MongoProperties properties;
|
||||
|
||||
MongoReactiveDataAutoConfiguration(MongoProperties properties) {
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(ReactiveMongoDatabaseFactory.class)
|
||||
SimpleReactiveMongoDatabaseFactory reactiveMongoDatabaseFactory(MongoClient mongo,
|
||||
SimpleReactiveMongoDatabaseFactory reactiveMongoDatabaseFactory(MongoProperties properties, MongoClient mongo,
|
||||
MongoConnectionDetails connectionDetails) {
|
||||
String database = this.properties.getDatabase();
|
||||
String database = properties.getDatabase();
|
||||
if (database == null) {
|
||||
database = connectionDetails.getConnectionString().getDatabase();
|
||||
}
|
||||
@@ -109,11 +103,12 @@ public final class MongoReactiveDataAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(ReactiveGridFsOperations.class)
|
||||
ReactiveGridFsTemplate reactiveGridFsTemplate(ReactiveMongoDatabaseFactory reactiveMongoDatabaseFactory,
|
||||
MappingMongoConverter mappingMongoConverter, DataBufferFactory dataBufferFactory) {
|
||||
ReactiveGridFsTemplate reactiveGridFsTemplate(DataMongoProperties dataProperties,
|
||||
ReactiveMongoDatabaseFactory reactiveMongoDatabaseFactory, MappingMongoConverter mappingMongoConverter,
|
||||
DataBufferFactory dataBufferFactory) {
|
||||
return new ReactiveGridFsTemplate(dataBufferFactory,
|
||||
new GridFsReactiveMongoDatabaseFactory(reactiveMongoDatabaseFactory, this.properties),
|
||||
mappingMongoConverter, this.properties.getGridfs().getBucket());
|
||||
new GridFsReactiveMongoDatabaseFactory(reactiveMongoDatabaseFactory, dataProperties),
|
||||
mappingMongoConverter, dataProperties.getGridfs().getBucket());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -124,9 +119,9 @@ public final class MongoReactiveDataAutoConfiguration {
|
||||
|
||||
private final ReactiveMongoDatabaseFactory delegate;
|
||||
|
||||
private final MongoProperties properties;
|
||||
private final DataMongoProperties properties;
|
||||
|
||||
GridFsReactiveMongoDatabaseFactory(ReactiveMongoDatabaseFactory delegate, MongoProperties properties) {
|
||||
GridFsReactiveMongoDatabaseFactory(ReactiveMongoDatabaseFactory delegate, DataMongoProperties properties) {
|
||||
this.delegate = delegate;
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
+14
-16
@@ -5,6 +5,20 @@
|
||||
"type": "java.lang.Boolean",
|
||||
"description": "Whether to enable MongoDB health check.",
|
||||
"defaultValue": true
|
||||
},
|
||||
{
|
||||
"name": "spring.data.mongodb.grid-fs-database",
|
||||
"type": "java.lang.String",
|
||||
"deprecation": {
|
||||
"replacement": "spring.data.mongodb.gridfs.database",
|
||||
"level": "error"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "spring.data.mongodb.repositories.type",
|
||||
"type": "org.springframework.boot.autoconfigure.data.RepositoryType",
|
||||
"description": "Type of Mongo repositories to enable.",
|
||||
"defaultValue": "auto"
|
||||
}
|
||||
],
|
||||
"hints": [
|
||||
@@ -18,22 +32,6 @@
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "spring.data.mongodb.protocol",
|
||||
"values": [
|
||||
{
|
||||
"value": "mongodb"
|
||||
},
|
||||
{
|
||||
"value": "mongodb+srv"
|
||||
}
|
||||
],
|
||||
"providers": [
|
||||
{
|
||||
"name": "any"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright 2012-present the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.data.mongodb.autoconfigure;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link DataMongoProperties}.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
class DataMongoPropertiesTests {
|
||||
|
||||
@Test
|
||||
void canBindAutoIndexCreation() {
|
||||
new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(MongoDataAutoConfiguration.class))
|
||||
.withPropertyValues("spring.data.mongodb.auto-index-creation=true")
|
||||
.run((context) -> {
|
||||
DataMongoProperties properties = context.getBean(DataMongoProperties.class);
|
||||
assertThat(properties.isAutoIndexCreation()).isTrue();
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
+10
-12
@@ -240,7 +240,7 @@ class MongoDataAutoConfigurationTests {
|
||||
|
||||
@Test
|
||||
void databasePropertyIsUsed() {
|
||||
this.contextRunner.withPropertyValues("spring.data.mongodb.database=mydb").run((context) -> {
|
||||
this.contextRunner.withPropertyValues("spring.mongodb.database=mydb").run((context) -> {
|
||||
MongoDatabaseFactory factory = context.getBean(MongoDatabaseFactory.class);
|
||||
assertThat(factory).isInstanceOf(SimpleMongoClientDatabaseFactory.class);
|
||||
assertThat(factory.getMongoDatabase().getName()).isEqualTo("mydb");
|
||||
@@ -249,19 +249,18 @@ class MongoDataAutoConfigurationTests {
|
||||
|
||||
@Test
|
||||
void databaseInUriPropertyIsUsed() {
|
||||
this.contextRunner.withPropertyValues("spring.data.mongodb.uri=mongodb://mongo.example.com/mydb")
|
||||
.run((context) -> {
|
||||
MongoDatabaseFactory factory = context.getBean(MongoDatabaseFactory.class);
|
||||
assertThat(factory).isInstanceOf(SimpleMongoClientDatabaseFactory.class);
|
||||
assertThat(factory.getMongoDatabase().getName()).isEqualTo("mydb");
|
||||
});
|
||||
this.contextRunner.withPropertyValues("spring.mongodb.uri=mongodb://mongo.example.com/mydb").run((context) -> {
|
||||
MongoDatabaseFactory factory = context.getBean(MongoDatabaseFactory.class);
|
||||
assertThat(factory).isInstanceOf(SimpleMongoClientDatabaseFactory.class);
|
||||
assertThat(factory.getMongoDatabase().getName()).isEqualTo("mydb");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void databasePropertyOverridesUriProperty() {
|
||||
this.contextRunner
|
||||
.withPropertyValues("spring.data.mongodb.uri=mongodb://mongo.example.com/notused",
|
||||
"spring.data.mongodb.database=mydb")
|
||||
.withPropertyValues("spring.mongodb.uri=mongodb://mongo.example.com/notused",
|
||||
"spring.mongodb.database=mydb")
|
||||
.run((context) -> {
|
||||
MongoDatabaseFactory factory = context.getBean(MongoDatabaseFactory.class);
|
||||
assertThat(factory).isInstanceOf(SimpleMongoClientDatabaseFactory.class);
|
||||
@@ -272,8 +271,7 @@ class MongoDataAutoConfigurationTests {
|
||||
@Test
|
||||
void databasePropertyIsUsedWhenNoDatabaseInUri() {
|
||||
this.contextRunner
|
||||
.withPropertyValues("spring.data.mongodb.uri=mongodb://mongo.example.com/",
|
||||
"spring.data.mongodb.database=mydb")
|
||||
.withPropertyValues("spring.mongodb.uri=mongodb://mongo.example.com/", "spring.mongodb.database=mydb")
|
||||
.run((context) -> {
|
||||
MongoDatabaseFactory factory = context.getBean(MongoDatabaseFactory.class);
|
||||
assertThat(factory).isInstanceOf(SimpleMongoClientDatabaseFactory.class);
|
||||
@@ -283,7 +281,7 @@ class MongoDataAutoConfigurationTests {
|
||||
|
||||
@Test
|
||||
void contextFailsWhenDatabaseNotSet() {
|
||||
this.contextRunner.withPropertyValues("spring.data.mongodb.uri=mongodb://mongo.example.com/")
|
||||
this.contextRunner.withPropertyValues("spring.mongodb.uri=mongodb://mongo.example.com/")
|
||||
.run((context) -> assertThat(context).getFailure().hasMessageContaining("Database name must not be empty"));
|
||||
}
|
||||
|
||||
|
||||
+10
-12
@@ -100,7 +100,7 @@ class MongoReactiveDataAutoConfigurationTests {
|
||||
|
||||
@Test
|
||||
void databasePropertyIsUsed() {
|
||||
this.contextRunner.withPropertyValues("spring.data.mongodb.database=mydb").run((context) -> {
|
||||
this.contextRunner.withPropertyValues("spring.mongodb.database=mydb").run((context) -> {
|
||||
ReactiveMongoDatabaseFactory factory = context.getBean(ReactiveMongoDatabaseFactory.class);
|
||||
assertThat(factory).isInstanceOf(SimpleReactiveMongoDatabaseFactory.class);
|
||||
assertThat(factory.getMongoDatabase().block().getName()).isEqualTo("mydb");
|
||||
@@ -109,19 +109,18 @@ class MongoReactiveDataAutoConfigurationTests {
|
||||
|
||||
@Test
|
||||
void databaseInUriPropertyIsUsed() {
|
||||
this.contextRunner.withPropertyValues("spring.data.mongodb.uri=mongodb://mongo.example.com/mydb")
|
||||
.run((context) -> {
|
||||
ReactiveMongoDatabaseFactory factory = context.getBean(ReactiveMongoDatabaseFactory.class);
|
||||
assertThat(factory).isInstanceOf(SimpleReactiveMongoDatabaseFactory.class);
|
||||
assertThat(factory.getMongoDatabase().block().getName()).isEqualTo("mydb");
|
||||
});
|
||||
this.contextRunner.withPropertyValues("spring.mongodb.uri=mongodb://mongo.example.com/mydb").run((context) -> {
|
||||
ReactiveMongoDatabaseFactory factory = context.getBean(ReactiveMongoDatabaseFactory.class);
|
||||
assertThat(factory).isInstanceOf(SimpleReactiveMongoDatabaseFactory.class);
|
||||
assertThat(factory.getMongoDatabase().block().getName()).isEqualTo("mydb");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void databasePropertyOverridesUriProperty() {
|
||||
this.contextRunner
|
||||
.withPropertyValues("spring.data.mongodb.uri=mongodb://mongo.example.com/notused",
|
||||
"spring.data.mongodb.database=mydb")
|
||||
.withPropertyValues("spring.mongodb.uri=mongodb://mongo.example.com/notused",
|
||||
"spring.mongodb.database=mydb")
|
||||
.run((context) -> {
|
||||
ReactiveMongoDatabaseFactory factory = context.getBean(ReactiveMongoDatabaseFactory.class);
|
||||
assertThat(factory).isInstanceOf(SimpleReactiveMongoDatabaseFactory.class);
|
||||
@@ -132,8 +131,7 @@ class MongoReactiveDataAutoConfigurationTests {
|
||||
@Test
|
||||
void databasePropertyIsUsedWhenNoDatabaseInUri() {
|
||||
this.contextRunner
|
||||
.withPropertyValues("spring.data.mongodb.uri=mongodb://mongo.example.com/",
|
||||
"spring.data.mongodb.database=mydb")
|
||||
.withPropertyValues("spring.mongodb.uri=mongodb://mongo.example.com/", "spring.mongodb.database=mydb")
|
||||
.run((context) -> {
|
||||
ReactiveMongoDatabaseFactory factory = context.getBean(ReactiveMongoDatabaseFactory.class);
|
||||
assertThat(factory).isInstanceOf(SimpleReactiveMongoDatabaseFactory.class);
|
||||
@@ -143,7 +141,7 @@ class MongoReactiveDataAutoConfigurationTests {
|
||||
|
||||
@Test
|
||||
void contextFailsWhenDatabaseNotSet() {
|
||||
this.contextRunner.withPropertyValues("spring.data.mongodb.uri=mongodb://mongo.example.com/")
|
||||
this.contextRunner.withPropertyValues("spring.mongodb.uri=mongodb://mongo.example.com/")
|
||||
.run((context) -> assertThat(context).getFailure().hasMessageContaining("Database name must not be empty"));
|
||||
}
|
||||
|
||||
|
||||
+1
-63
@@ -39,7 +39,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
* @author Safeer Ansari
|
||||
* @since 4.0.0
|
||||
*/
|
||||
@ConfigurationProperties("spring.data.mongodb")
|
||||
@ConfigurationProperties("spring.mongodb")
|
||||
public class MongoProperties {
|
||||
|
||||
/**
|
||||
@@ -89,8 +89,6 @@ public class MongoProperties {
|
||||
*/
|
||||
private @Nullable String authenticationDatabase;
|
||||
|
||||
private final Gridfs gridfs = new Gridfs();
|
||||
|
||||
/**
|
||||
* Login user of the mongo server. Ignored if 'uri' is set.
|
||||
*/
|
||||
@@ -106,11 +104,6 @@ public class MongoProperties {
|
||||
*/
|
||||
private @Nullable String replicaSetName;
|
||||
|
||||
/**
|
||||
* Fully qualified name of the FieldNamingStrategy to use.
|
||||
*/
|
||||
private @Nullable Class<?> fieldNamingStrategy;
|
||||
|
||||
/**
|
||||
* Representation to use when converting a UUID to a BSON binary value.
|
||||
*/
|
||||
@@ -118,11 +111,6 @@ public class MongoProperties {
|
||||
|
||||
private final Ssl ssl = new Ssl();
|
||||
|
||||
/**
|
||||
* Whether to enable auto-index creation.
|
||||
*/
|
||||
private @Nullable Boolean autoIndexCreation;
|
||||
|
||||
public void setProtocol(String protocol) {
|
||||
this.protocol = protocol;
|
||||
}
|
||||
@@ -179,14 +167,6 @@ public class MongoProperties {
|
||||
this.replicaSetName = replicaSetName;
|
||||
}
|
||||
|
||||
public @Nullable Class<?> getFieldNamingStrategy() {
|
||||
return this.fieldNamingStrategy;
|
||||
}
|
||||
|
||||
public void setFieldNamingStrategy(@Nullable Class<?> fieldNamingStrategy) {
|
||||
this.fieldNamingStrategy = fieldNamingStrategy;
|
||||
}
|
||||
|
||||
public UuidRepresentation getUuidRepresentation() {
|
||||
return this.uuidRepresentation;
|
||||
}
|
||||
@@ -215,10 +195,6 @@ public class MongoProperties {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public Gridfs getGridfs() {
|
||||
return this.gridfs;
|
||||
}
|
||||
|
||||
public @Nullable String getMongoClientDatabase() {
|
||||
if (this.database != null) {
|
||||
return this.database;
|
||||
@@ -226,14 +202,6 @@ public class MongoProperties {
|
||||
return new ConnectionString(determineUri()).getDatabase();
|
||||
}
|
||||
|
||||
public @Nullable Boolean isAutoIndexCreation() {
|
||||
return this.autoIndexCreation;
|
||||
}
|
||||
|
||||
public void setAutoIndexCreation(@Nullable Boolean autoIndexCreation) {
|
||||
this.autoIndexCreation = autoIndexCreation;
|
||||
}
|
||||
|
||||
public @Nullable List<String> getAdditionalHosts() {
|
||||
return this.additionalHosts;
|
||||
}
|
||||
@@ -246,36 +214,6 @@ public class MongoProperties {
|
||||
return this.ssl;
|
||||
}
|
||||
|
||||
public static class Gridfs {
|
||||
|
||||
/**
|
||||
* GridFS database name.
|
||||
*/
|
||||
private @Nullable String database;
|
||||
|
||||
/**
|
||||
* GridFS bucket name.
|
||||
*/
|
||||
private @Nullable String bucket;
|
||||
|
||||
public @Nullable String getDatabase() {
|
||||
return this.database;
|
||||
}
|
||||
|
||||
public void setDatabase(@Nullable String database) {
|
||||
this.database = database;
|
||||
}
|
||||
|
||||
public @Nullable String getBucket() {
|
||||
return this.bucket;
|
||||
}
|
||||
|
||||
public void setBucket(@Nullable String bucket) {
|
||||
this.bucket = bucket;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class Ssl {
|
||||
|
||||
/**
|
||||
|
||||
+116
-8
@@ -12,22 +12,130 @@
|
||||
"defaultValue": true
|
||||
},
|
||||
{
|
||||
"name": "spring.data.mongodb.grid-fs-database",
|
||||
"type": "java.lang.String",
|
||||
"name": "spring.data.mongodb.additional-hosts",
|
||||
"deprecation": {
|
||||
"replacement": "spring.data.mongodb.gridfs.database",
|
||||
"level": "error"
|
||||
"level": "error",
|
||||
"replacement": "spring.mongodb.additional-hosts",
|
||||
"since": "4.0.0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "spring.data.mongodb.repositories.type",
|
||||
"type": "org.springframework.boot.autoconfigure.data.RepositoryType",
|
||||
"description": "Type of Mongo repositories to enable.",
|
||||
"defaultValue": "auto"
|
||||
"name": "spring.data.mongodb.authentication-database",
|
||||
"deprecation": {
|
||||
"level": "error",
|
||||
"replacement": "spring.mongodb.authentication-database",
|
||||
"since": "4.0.0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "spring.data.mongodb.database",
|
||||
"deprecation": {
|
||||
"level": "error",
|
||||
"replacement": "spring.mongodb.database",
|
||||
"since": "4.0.0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "spring.data.mongodb.host",
|
||||
"deprecation": {
|
||||
"level": "error",
|
||||
"replacement": "spring.mongodb.host",
|
||||
"since": "4.0.0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "spring.data.mongodb.password",
|
||||
"deprecation": {
|
||||
"level": "error",
|
||||
"replacement": "spring.mongodb.password",
|
||||
"since": "4.0.0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "spring.data.mongodb.port",
|
||||
"deprecation": {
|
||||
"level": "error",
|
||||
"replacement": "spring.mongodb.port",
|
||||
"since": "4.0.0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "spring.data.mongodb.protocol",
|
||||
"deprecation": {
|
||||
"level": "error",
|
||||
"replacement": "spring.mongodb.protocol",
|
||||
"since": "4.0.0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "spring.data.mongodb.replica-set-name",
|
||||
"deprecation": {
|
||||
"level": "error",
|
||||
"replacement": "spring.mongodb.replica-set-name",
|
||||
"since": "4.0.0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "spring.data.mongodb.ssl.bundle",
|
||||
"deprecation": {
|
||||
"level": "error",
|
||||
"replacement": "spring.mongodb.ssl.bundle",
|
||||
"since": "4.0.0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "spring.data.mongodb.ssl.enabled",
|
||||
"deprecation": {
|
||||
"level": "error",
|
||||
"replacement": "spring.mongodb.ssl.enabled",
|
||||
"since": "4.0.0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "spring.data.mongodb.uri",
|
||||
"deprecation": {
|
||||
"level": "error",
|
||||
"replacement": "spring.mongodb.uri",
|
||||
"since": "4.0.0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "spring.data.mongodb.username",
|
||||
"deprecation": {
|
||||
"level": "error",
|
||||
"replacement": "spring.mongodb.username",
|
||||
"since": "4.0.0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "spring.data.mongodb.uuid-representation",
|
||||
"deprecation": {
|
||||
"level": "error",
|
||||
"replacement": "spring.mongodb.uuid-representation",
|
||||
"since": "4.0.0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "spring.mongodb.uri",
|
||||
"defaultValue": "mongodb://localhost/test"
|
||||
}
|
||||
],
|
||||
"hints": [
|
||||
{
|
||||
"name": "spring.mongodb.protocol",
|
||||
"values": [
|
||||
{
|
||||
"value": "mongodb"
|
||||
},
|
||||
{
|
||||
"value": "mongodb+srv"
|
||||
}
|
||||
],
|
||||
"providers": [
|
||||
{
|
||||
"name": "any"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
+23
-27
@@ -79,7 +79,7 @@ class MongoAutoConfigurationTests {
|
||||
|
||||
@Test
|
||||
void configuresSslWhenEnabled() {
|
||||
this.contextRunner.withPropertyValues("spring.data.mongodb.ssl.enabled=true").run((context) -> {
|
||||
this.contextRunner.withPropertyValues("spring.mongodb.ssl.enabled=true").run((context) -> {
|
||||
SslSettings sslSettings = getSettings(context).getSslSettings();
|
||||
assertThat(sslSettings.isEnabled()).isTrue();
|
||||
assertThat(sslSettings.getContext()).isNotNull();
|
||||
@@ -90,7 +90,7 @@ class MongoAutoConfigurationTests {
|
||||
@WithPackageResources("test.jks")
|
||||
void configuresSslWithBundle() {
|
||||
this.contextRunner
|
||||
.withPropertyValues("spring.data.mongodb.ssl.bundle=test-bundle",
|
||||
.withPropertyValues("spring.mongodb.ssl.bundle=test-bundle",
|
||||
"spring.ssl.bundle.jks.test-bundle.keystore.location=classpath:test.jks",
|
||||
"spring.ssl.bundle.jks.test-bundle.keystore.password=secret",
|
||||
"spring.ssl.bundle.jks.test-bundle.key.password=password")
|
||||
@@ -103,7 +103,7 @@ class MongoAutoConfigurationTests {
|
||||
|
||||
@Test
|
||||
void configuresProtocol() {
|
||||
this.contextRunner.withPropertyValues("spring.data.mongodb.protocol=mongodb+srv").run((context) -> {
|
||||
this.contextRunner.withPropertyValues("spring.mongodb.protocol=mongodb+srv").run((context) -> {
|
||||
MongoClientSettings settings = getSettings(context);
|
||||
assertThat(settings.getClusterSettings().getMode()).isEqualTo(ClusterConnectionMode.MULTIPLE);
|
||||
});
|
||||
@@ -120,7 +120,7 @@ class MongoAutoConfigurationTests {
|
||||
@Test
|
||||
void configuresWithoutSslWhenDisabledWithBundle() {
|
||||
this.contextRunner
|
||||
.withPropertyValues("spring.data.mongodb.ssl.enabled=false", "spring.data.mongodb.ssl.bundle=test-bundle")
|
||||
.withPropertyValues("spring.mongodb.ssl.enabled=false", "spring.mongodb.ssl.bundle=test-bundle")
|
||||
.run((context) -> {
|
||||
SslSettings sslSettings = getSettings(context).getSslSettings();
|
||||
assertThat(sslSettings.isEnabled()).isFalse();
|
||||
@@ -130,15 +130,13 @@ class MongoAutoConfigurationTests {
|
||||
@Test
|
||||
void doesNotConfigureCredentialsWithoutUsername() {
|
||||
this.contextRunner
|
||||
.withPropertyValues("spring.data.mongodb.password=secret",
|
||||
"spring.data.mongodb.authentication-database=authdb")
|
||||
.withPropertyValues("spring.mongodb.password=secret", "spring.mongodb.authentication-database=authdb")
|
||||
.run((context) -> assertThat(getSettings(context).getCredential()).isNull());
|
||||
}
|
||||
|
||||
@Test
|
||||
void configuresCredentialsFromPropertiesWithDefaultDatabase() {
|
||||
this.contextRunner
|
||||
.withPropertyValues("spring.data.mongodb.username=user", "spring.data.mongodb.password=secret")
|
||||
this.contextRunner.withPropertyValues("spring.mongodb.username=user", "spring.mongodb.password=secret")
|
||||
.run((context) -> {
|
||||
MongoCredential credential = getSettings(context).getCredential();
|
||||
assertThat(credential.getUserName()).isEqualTo("user");
|
||||
@@ -150,8 +148,8 @@ class MongoAutoConfigurationTests {
|
||||
@Test
|
||||
void configuresCredentialsFromPropertiesWithDatabase() {
|
||||
this.contextRunner
|
||||
.withPropertyValues("spring.data.mongodb.username=user", "spring.data.mongodb.password=secret",
|
||||
"spring.data.mongodb.database=mydb")
|
||||
.withPropertyValues("spring.mongodb.username=user", "spring.mongodb.password=secret",
|
||||
"spring.mongodb.database=mydb")
|
||||
.run((context) -> {
|
||||
MongoCredential credential = getSettings(context).getCredential();
|
||||
assertThat(credential.getUserName()).isEqualTo("user");
|
||||
@@ -163,8 +161,8 @@ class MongoAutoConfigurationTests {
|
||||
@Test
|
||||
void configuresCredentialsFromPropertiesWithAuthDatabase() {
|
||||
this.contextRunner
|
||||
.withPropertyValues("spring.data.mongodb.username=user", "spring.data.mongodb.password=secret",
|
||||
"spring.data.mongodb.database=mydb", "spring.data.mongodb.authentication-database=authdb")
|
||||
.withPropertyValues("spring.mongodb.username=user", "spring.mongodb.password=secret",
|
||||
"spring.mongodb.database=mydb", "spring.mongodb.authentication-database=authdb")
|
||||
.run((context) -> {
|
||||
MongoCredential credential = getSettings(context).getCredential();
|
||||
assertThat(credential.getUserName()).isEqualTo("user");
|
||||
@@ -175,8 +173,7 @@ class MongoAutoConfigurationTests {
|
||||
|
||||
@Test
|
||||
void configuresCredentialsFromPropertiesWithSpecialCharacters() {
|
||||
this.contextRunner
|
||||
.withPropertyValues("spring.data.mongodb.username=us:er", "spring.data.mongodb.password=sec@ret")
|
||||
this.contextRunner.withPropertyValues("spring.mongodb.username=us:er", "spring.mongodb.password=sec@ret")
|
||||
.run((context) -> {
|
||||
MongoCredential credential = getSettings(context).getCredential();
|
||||
assertThat(credential.getUserName()).isEqualTo("us:er");
|
||||
@@ -187,26 +184,25 @@ class MongoAutoConfigurationTests {
|
||||
|
||||
@Test
|
||||
void doesNotConfigureCredentialsWithoutUsernameInUri() {
|
||||
this.contextRunner.withPropertyValues("spring.data.mongodb.uri=mongodb://localhost/mydb?authSource=authdb")
|
||||
this.contextRunner.withPropertyValues("spring.mongodb.uri=mongodb://localhost/mydb?authSource=authdb")
|
||||
.run((context) -> assertThat(getSettings(context).getCredential()).isNull());
|
||||
}
|
||||
|
||||
@Test
|
||||
void configuresCredentialsFromUriPropertyWithDefaultDatabase() {
|
||||
this.contextRunner.withPropertyValues("spring.data.mongodb.uri=mongodb://user:secret@localhost/")
|
||||
.run((context) -> {
|
||||
MongoCredential credential = getSettings(context).getCredential();
|
||||
assertThat(credential.getUserName()).isEqualTo("user");
|
||||
assertThat(credential.getPassword()).isEqualTo("secret".toCharArray());
|
||||
assertThat(credential.getSource()).isEqualTo("admin");
|
||||
});
|
||||
this.contextRunner.withPropertyValues("spring.mongodb.uri=mongodb://user:secret@localhost/").run((context) -> {
|
||||
MongoCredential credential = getSettings(context).getCredential();
|
||||
assertThat(credential.getUserName()).isEqualTo("user");
|
||||
assertThat(credential.getPassword()).isEqualTo("secret".toCharArray());
|
||||
assertThat(credential.getSource()).isEqualTo("admin");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void configuresCredentialsFromUriPropertyWithDatabase() {
|
||||
this.contextRunner
|
||||
.withPropertyValues("spring.data.mongodb.uri=mongodb://user:secret@localhost/mydb",
|
||||
"spring.data.mongodb.database=notused", "spring.data.mongodb.authentication-database=notused")
|
||||
.withPropertyValues("spring.mongodb.uri=mongodb://user:secret@localhost/mydb",
|
||||
"spring.mongodb.database=notused", "spring.mongodb.authentication-database=notused")
|
||||
.run((context) -> {
|
||||
MongoCredential credential = getSettings(context).getCredential();
|
||||
assertThat(credential.getUserName()).isEqualTo("user");
|
||||
@@ -218,8 +214,8 @@ class MongoAutoConfigurationTests {
|
||||
@Test
|
||||
void configuresCredentialsFromUriPropertyWithAuthDatabase() {
|
||||
this.contextRunner
|
||||
.withPropertyValues("spring.data.mongodb.uri=mongodb://user:secret@localhost/mydb?authSource=authdb",
|
||||
"spring.data.mongodb.database=notused", "spring.data.mongodb.authentication-database=notused")
|
||||
.withPropertyValues("spring.mongodb.uri=mongodb://user:secret@localhost/mydb?authSource=authdb",
|
||||
"spring.mongodb.database=notused", "spring.mongodb.authentication-database=notused")
|
||||
.run((context) -> {
|
||||
MongoCredential credential = getSettings(context).getCredential();
|
||||
assertThat(credential.getUserName()).isEqualTo("user");
|
||||
@@ -236,7 +232,7 @@ class MongoAutoConfigurationTests {
|
||||
|
||||
@Test
|
||||
void customizerOverridesAutoConfig() {
|
||||
this.contextRunner.withPropertyValues("spring.data.mongodb.uri:mongodb://localhost/test?appname=auto-config")
|
||||
this.contextRunner.withPropertyValues("spring.mongodb.uri:mongodb://localhost/test?appname=auto-config")
|
||||
.withUserConfiguration(SimpleCustomizerConfig.class)
|
||||
.run((context) -> assertThat(getSettings(context).getApplicationName()).isEqualTo("overridden-name"));
|
||||
}
|
||||
|
||||
-23
@@ -24,8 +24,6 @@ import com.mongodb.MongoClientSettings;
|
||||
import org.junit.jupiter.api.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 static org.assertj.core.api.Assertions.assertThat;
|
||||
@@ -47,17 +45,6 @@ import static org.mockito.Mockito.mock;
|
||||
*/
|
||||
abstract class MongoClientFactorySupportTests<T> {
|
||||
|
||||
@Test
|
||||
void canBindCharArrayPassword() {
|
||||
// gh-1572
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
TestPropertyValues.of("spring.data.mongodb.password:word").applyTo(context);
|
||||
context.register(Config.class);
|
||||
context.refresh();
|
||||
MongoProperties properties = context.getBean(MongoProperties.class);
|
||||
assertThat(properties.getPassword()).isEqualTo("word".toCharArray());
|
||||
}
|
||||
|
||||
@Test
|
||||
void allMongoClientSettingsCanBeSet() {
|
||||
MongoClientSettings.Builder builder = MongoClientSettings.builder();
|
||||
@@ -101,16 +88,6 @@ abstract class MongoClientFactorySupportTests<T> {
|
||||
then(customizer).should().customize(any(MongoClientSettings.Builder.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void canBindAutoIndexCreation() {
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
TestPropertyValues.of("spring.data.mongodb.autoIndexCreation:true").applyTo(context);
|
||||
context.register(Config.class);
|
||||
context.refresh();
|
||||
MongoProperties properties = context.getBean(MongoProperties.class);
|
||||
assertThat(properties.isAutoIndexCreation()).isTrue();
|
||||
}
|
||||
|
||||
protected T createMongoClient(MongoClientSettings settings) {
|
||||
return createMongoClient(null, settings);
|
||||
}
|
||||
|
||||
+12
@@ -19,6 +19,8 @@ package org.springframework.boot.mongodb.autoconfigure;
|
||||
import org.bson.UuidRepresentation;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.data.mongodb.config.MongoConfigurationSupport;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@@ -37,6 +39,16 @@ class MongoPropertiesTests {
|
||||
assertThat(springBootDefault).isEqualTo(springDataDefault);
|
||||
}
|
||||
|
||||
@Test
|
||||
void canBindCharArrayPassword() {
|
||||
new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(MongoAutoConfiguration.class))
|
||||
.withPropertyValues("spring.mongodb.password:word")
|
||||
.run((context) -> {
|
||||
MongoProperties properties = context.getBean(MongoProperties.class);
|
||||
assertThat(properties.getPassword()).isEqualTo("word".toCharArray());
|
||||
});
|
||||
}
|
||||
|
||||
private UuidRepresentation springDataDefaultUuidRepresentation() {
|
||||
return new MongoConfigurationSupport() {
|
||||
|
||||
|
||||
+24
-27
@@ -59,7 +59,7 @@ class MongoReactiveAutoConfigurationTests {
|
||||
|
||||
@Test
|
||||
void settingsAdded() {
|
||||
this.contextRunner.withPropertyValues("spring.data.mongodb.host:localhost")
|
||||
this.contextRunner.withPropertyValues("spring.mongodb.host:localhost")
|
||||
.withUserConfiguration(SettingsConfig.class)
|
||||
.run((context) -> assertThat(getSettings(context).getSocketSettings().getReadTimeout(TimeUnit.SECONDS))
|
||||
.isEqualTo(300));
|
||||
@@ -67,14 +67,14 @@ class MongoReactiveAutoConfigurationTests {
|
||||
|
||||
@Test
|
||||
void settingsAddedButNoHost() {
|
||||
this.contextRunner.withPropertyValues("spring.data.mongodb.uri:mongodb://localhost/test")
|
||||
this.contextRunner.withPropertyValues("spring.mongodb.uri:mongodb://localhost/test")
|
||||
.withUserConfiguration(SettingsConfig.class)
|
||||
.run((context) -> assertThat(getSettings(context).getReadPreference()).isEqualTo(ReadPreference.nearest()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void settingsSslConfig() {
|
||||
this.contextRunner.withPropertyValues("spring.data.mongodb.uri:mongodb://localhost/test")
|
||||
this.contextRunner.withPropertyValues("spring.mongodb.uri:mongodb://localhost/test")
|
||||
.withUserConfiguration(SslSettingsConfig.class)
|
||||
.run((context) -> {
|
||||
assertThat(context).hasSingleBean(MongoClient.class);
|
||||
@@ -86,7 +86,7 @@ class MongoReactiveAutoConfigurationTests {
|
||||
|
||||
@Test
|
||||
void configuresSslWhenEnabled() {
|
||||
this.contextRunner.withPropertyValues("spring.data.mongodb.ssl.enabled=true").run((context) -> {
|
||||
this.contextRunner.withPropertyValues("spring.mongodb.ssl.enabled=true").run((context) -> {
|
||||
SslSettings sslSettings = getSettings(context).getSslSettings();
|
||||
assertThat(sslSettings.isEnabled()).isTrue();
|
||||
assertThat(sslSettings.getContext()).isNotNull();
|
||||
@@ -97,7 +97,7 @@ class MongoReactiveAutoConfigurationTests {
|
||||
@WithPackageResources("test.jks")
|
||||
void configuresSslWithBundle() {
|
||||
this.contextRunner
|
||||
.withPropertyValues("spring.data.mongodb.ssl.bundle=test-bundle",
|
||||
.withPropertyValues("spring.mongodb.ssl.bundle=test-bundle",
|
||||
"spring.ssl.bundle.jks.test-bundle.keystore.location=classpath:test.jks",
|
||||
"spring.ssl.bundle.jks.test-bundle.keystore.password=secret",
|
||||
"spring.ssl.bundle.jks.test-bundle.key.password=password")
|
||||
@@ -111,7 +111,7 @@ class MongoReactiveAutoConfigurationTests {
|
||||
@Test
|
||||
void configuresWithoutSslWhenDisabledWithBundle() {
|
||||
this.contextRunner
|
||||
.withPropertyValues("spring.data.mongodb.ssl.enabled=false", "spring.data.mongodb.ssl.bundle=test-bundle")
|
||||
.withPropertyValues("spring.mongodb.ssl.enabled=false", "spring.mongodb.ssl.bundle=test-bundle")
|
||||
.run((context) -> {
|
||||
SslSettings sslSettings = getSettings(context).getSslSettings();
|
||||
assertThat(sslSettings.isEnabled()).isFalse();
|
||||
@@ -121,15 +121,13 @@ class MongoReactiveAutoConfigurationTests {
|
||||
@Test
|
||||
void doesNotConfigureCredentialsWithoutUsername() {
|
||||
this.contextRunner
|
||||
.withPropertyValues("spring.data.mongodb.password=secret",
|
||||
"spring.data.mongodb.authentication-database=authdb")
|
||||
.withPropertyValues("spring.mongodb.password=secret", "spring.mongodb.authentication-database=authdb")
|
||||
.run((context) -> assertThat(getSettings(context).getCredential()).isNull());
|
||||
}
|
||||
|
||||
@Test
|
||||
void configuresCredentialsFromPropertiesWithDefaultDatabase() {
|
||||
this.contextRunner
|
||||
.withPropertyValues("spring.data.mongodb.username=user", "spring.data.mongodb.password=secret")
|
||||
this.contextRunner.withPropertyValues("spring.mongodb.username=user", "spring.mongodb.password=secret")
|
||||
.run((context) -> {
|
||||
MongoCredential credential = getSettings(context).getCredential();
|
||||
assertThat(credential.getUserName()).isEqualTo("user");
|
||||
@@ -141,8 +139,8 @@ class MongoReactiveAutoConfigurationTests {
|
||||
@Test
|
||||
void configuresCredentialsFromPropertiesWithDatabase() {
|
||||
this.contextRunner
|
||||
.withPropertyValues("spring.data.mongodb.username=user", "spring.data.mongodb.password=secret",
|
||||
"spring.data.mongodb.database=mydb")
|
||||
.withPropertyValues("spring.mongodb.username=user", "spring.mongodb.password=secret",
|
||||
"spring.mongodb.database=mydb")
|
||||
.run((context) -> {
|
||||
MongoCredential credential = getSettings(context).getCredential();
|
||||
assertThat(credential.getUserName()).isEqualTo("user");
|
||||
@@ -154,8 +152,8 @@ class MongoReactiveAutoConfigurationTests {
|
||||
@Test
|
||||
void configuresCredentialsFromPropertiesWithAuthDatabase() {
|
||||
this.contextRunner
|
||||
.withPropertyValues("spring.data.mongodb.username=user", "spring.data.mongodb.password=secret",
|
||||
"spring.data.mongodb.database=mydb", "spring.data.mongodb.authentication-database=authdb")
|
||||
.withPropertyValues("spring.mongodb.username=user", "spring.mongodb.password=secret",
|
||||
"spring.mongodb.database=mydb", "spring.mongodb.authentication-database=authdb")
|
||||
.run((context) -> {
|
||||
MongoCredential credential = getSettings(context).getCredential();
|
||||
assertThat(credential.getUserName()).isEqualTo("user");
|
||||
@@ -166,26 +164,25 @@ class MongoReactiveAutoConfigurationTests {
|
||||
|
||||
@Test
|
||||
void doesNotConfigureCredentialsWithoutUsernameInUri() {
|
||||
this.contextRunner.withPropertyValues("spring.data.mongodb.uri=mongodb://localhost/mydb?authSource=authdb")
|
||||
this.contextRunner.withPropertyValues("spring.mongodb.uri=mongodb://localhost/mydb?authSource=authdb")
|
||||
.run((context) -> assertThat(getSettings(context).getCredential()).isNull());
|
||||
}
|
||||
|
||||
@Test
|
||||
void configuresCredentialsFromUriPropertyWithDefaultDatabase() {
|
||||
this.contextRunner.withPropertyValues("spring.data.mongodb.uri=mongodb://user:secret@localhost/")
|
||||
.run((context) -> {
|
||||
MongoCredential credential = getSettings(context).getCredential();
|
||||
assertThat(credential.getUserName()).isEqualTo("user");
|
||||
assertThat(credential.getPassword()).isEqualTo("secret".toCharArray());
|
||||
assertThat(credential.getSource()).isEqualTo("admin");
|
||||
});
|
||||
this.contextRunner.withPropertyValues("spring.mongodb.uri=mongodb://user:secret@localhost/").run((context) -> {
|
||||
MongoCredential credential = getSettings(context).getCredential();
|
||||
assertThat(credential.getUserName()).isEqualTo("user");
|
||||
assertThat(credential.getPassword()).isEqualTo("secret".toCharArray());
|
||||
assertThat(credential.getSource()).isEqualTo("admin");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void configuresCredentialsFromUriPropertyWithDatabase() {
|
||||
this.contextRunner
|
||||
.withPropertyValues("spring.data.mongodb.uri=mongodb://user:secret@localhost/mydb",
|
||||
"spring.data.mongodb.database=notused", "spring.data.mongodb.authentication-database=notused")
|
||||
.withPropertyValues("spring.mongodb.uri=mongodb://user:secret@localhost/mydb",
|
||||
"spring.mongodb.database=notused", "spring.mongodb.authentication-database=notused")
|
||||
.run((context) -> {
|
||||
MongoCredential credential = getSettings(context).getCredential();
|
||||
assertThat(credential.getUserName()).isEqualTo("user");
|
||||
@@ -197,8 +194,8 @@ class MongoReactiveAutoConfigurationTests {
|
||||
@Test
|
||||
void configuresCredentialsFromUriPropertyWithAuthDatabase() {
|
||||
this.contextRunner
|
||||
.withPropertyValues("spring.data.mongodb.uri=mongodb://user:secret@localhost/mydb?authSource=authdb",
|
||||
"spring.data.mongodb.database=notused", "spring.data.mongodb.authentication-database=notused")
|
||||
.withPropertyValues("spring.mongodb.uri=mongodb://user:secret@localhost/mydb?authSource=authdb",
|
||||
"spring.mongodb.database=notused", "spring.mongodb.authentication-database=notused")
|
||||
.run((context) -> {
|
||||
MongoCredential credential = getSettings(context).getCredential();
|
||||
assertThat(credential.getUserName()).isEqualTo("user");
|
||||
@@ -224,7 +221,7 @@ class MongoReactiveAutoConfigurationTests {
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
void customizerWithTransportSettingsOverridesAutoConfig() {
|
||||
this.contextRunner.withPropertyValues("spring.data.mongodb.uri:mongodb://localhost/test?appname=auto-config")
|
||||
this.contextRunner.withPropertyValues("spring.mongodb.uri:mongodb://localhost/test?appname=auto-config")
|
||||
.withUserConfiguration(SimpleTransportSettingsCustomizerConfig.class)
|
||||
.run((context) -> {
|
||||
assertThat(context).hasSingleBean(MongoClient.class);
|
||||
|
||||
+5
-5
@@ -62,14 +62,14 @@ class MongoReactiveSessionAutoConfigurationTests extends AbstractReactiveSession
|
||||
|
||||
@Test
|
||||
void defaultConfig() {
|
||||
this.contextRunner.withPropertyValues("spring.data.mongodb.uri=" + mongoDb.getReplicaSetUrl())
|
||||
this.contextRunner.withPropertyValues("spring.mongodb.uri=" + mongoDb.getReplicaSetUrl())
|
||||
.run(validateSpringSessionUsesMongo("sessions"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void defaultConfigWithCustomTimeout() {
|
||||
this.contextRunner
|
||||
.withPropertyValues("spring.session.timeout=1m", "spring.data.mongodb.uri=" + mongoDb.getReplicaSetUrl())
|
||||
.withPropertyValues("spring.session.timeout=1m", "spring.mongodb.uri=" + mongoDb.getReplicaSetUrl())
|
||||
.run((context) -> {
|
||||
ReactiveMongoSessionRepository repository = validateSessionRepository(context,
|
||||
ReactiveMongoSessionRepository.class);
|
||||
@@ -81,7 +81,7 @@ class MongoReactiveSessionAutoConfigurationTests extends AbstractReactiveSession
|
||||
void defaultConfigWithCustomSessionTimeout() {
|
||||
this.contextRunner
|
||||
.withPropertyValues("server.reactive.session.timeout=1m",
|
||||
"spring.data.mongodb.uri=" + mongoDb.getReplicaSetUrl())
|
||||
"spring.mongodb.uri=" + mongoDb.getReplicaSetUrl())
|
||||
.run((context) -> {
|
||||
ReactiveMongoSessionRepository repository = validateSessionRepository(context,
|
||||
ReactiveMongoSessionRepository.class);
|
||||
@@ -93,7 +93,7 @@ class MongoReactiveSessionAutoConfigurationTests extends AbstractReactiveSession
|
||||
void mongoSessionStoreWithCustomizations() {
|
||||
this.contextRunner
|
||||
.withPropertyValues("spring.session.mongodb.collection-name=foo",
|
||||
"spring.data.mongodb.uri=" + mongoDb.getReplicaSetUrl())
|
||||
"spring.mongodb.uri=" + mongoDb.getReplicaSetUrl())
|
||||
.run(validateSpringSessionUsesMongo("foo"));
|
||||
}
|
||||
|
||||
@@ -106,7 +106,7 @@ class MongoReactiveSessionAutoConfigurationTests extends AbstractReactiveSession
|
||||
"server.reactive.session.cookie.path:/example", "server.reactive.session.cookie.max-age:60",
|
||||
"server.reactive.session.cookie.http-only:false", "server.reactive.session.cookie.secure:false",
|
||||
"server.reactive.session.cookie.same-site:strict",
|
||||
"spring.data.mongodb.uri=" + mongoDb.getReplicaSetUrl())
|
||||
"spring.mongodb.uri=" + mongoDb.getReplicaSetUrl())
|
||||
.run(assertExchangeWithSession((exchange) -> {
|
||||
List<ResponseCookie> cookies = exchange.getResponse().getCookies().get("JSESSIONID");
|
||||
assertThat(cookies).isNotEmpty();
|
||||
|
||||
+1
-1
@@ -57,7 +57,7 @@ class MongoSessionAutoConfigurationTests extends AbstractSessionAutoConfiguratio
|
||||
this.contextRunner = new WebApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(MongoAutoConfiguration.class, MongoDataAutoConfiguration.class,
|
||||
SessionAutoConfiguration.class, MongoSessionAutoConfiguration.class))
|
||||
.withPropertyValues("spring.data.mongodb.uri=" + mongoDb.getReplicaSetUrl());
|
||||
.withPropertyValues("spring.mongodb.uri=" + mongoDb.getReplicaSetUrl());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user