@ -21,6 +21,7 @@ import java.util.concurrent.atomic.AtomicReference;
@@ -21,6 +21,7 @@ import java.util.concurrent.atomic.AtomicReference;
import com.mongodb.ConnectionString ;
import com.mongodb.MongoClientSettings ;
import com.mongodb.MongoCredential ;
import com.mongodb.ReadPreference ;
import com.mongodb.connection.AsynchronousSocketChannelStreamFactoryFactory ;
import com.mongodb.connection.SslSettings ;
@ -121,6 +122,95 @@ class MongoReactiveAutoConfigurationTests {
@@ -121,6 +122,95 @@ class MongoReactiveAutoConfigurationTests {
} ) ;
}
@Test
void doesNotConfigureCredentialsWithoutUsername ( ) {
this . contextRunner
. withPropertyValues ( "spring.data.mongodb.password=secret" ,
"spring.data.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" )
. run ( ( context ) - > {
MongoCredential credential = getSettings ( context ) . getCredential ( ) ;
assertThat ( credential . getUserName ( ) ) . isEqualTo ( "user" ) ;
assertThat ( credential . getPassword ( ) ) . isEqualTo ( "secret" . toCharArray ( ) ) ;
assertThat ( credential . getSource ( ) ) . isEqualTo ( "test" ) ;
} ) ;
}
@Test
void configuresCredentialsFromPropertiesWithDatabase ( ) {
this . contextRunner
. withPropertyValues ( "spring.data.mongodb.username=user" , "spring.data.mongodb.password=secret" ,
"spring.data.mongodb.database=mydb" )
. run ( ( context ) - > {
MongoCredential credential = getSettings ( context ) . getCredential ( ) ;
assertThat ( credential . getUserName ( ) ) . isEqualTo ( "user" ) ;
assertThat ( credential . getPassword ( ) ) . isEqualTo ( "secret" . toCharArray ( ) ) ;
assertThat ( credential . getSource ( ) ) . isEqualTo ( "mydb" ) ;
} ) ;
}
@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" )
. run ( ( context ) - > {
MongoCredential credential = getSettings ( context ) . getCredential ( ) ;
assertThat ( credential . getUserName ( ) ) . isEqualTo ( "user" ) ;
assertThat ( credential . getPassword ( ) ) . isEqualTo ( "secret" . toCharArray ( ) ) ;
assertThat ( credential . getSource ( ) ) . isEqualTo ( "authdb" ) ;
} ) ;
}
@Test
void doesNotConfigureCredentialsWithoutUsernameInUri ( ) {
this . contextRunner . withPropertyValues ( "spring.data.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" ) ;
} ) ;
}
@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" )
. run ( ( context ) - > {
MongoCredential credential = getSettings ( context ) . getCredential ( ) ;
assertThat ( credential . getUserName ( ) ) . isEqualTo ( "user" ) ;
assertThat ( credential . getPassword ( ) ) . isEqualTo ( "secret" . toCharArray ( ) ) ;
assertThat ( credential . getSource ( ) ) . isEqualTo ( "mydb" ) ;
} ) ;
}
@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" )
. run ( ( context ) - > {
MongoCredential credential = getSettings ( context ) . getCredential ( ) ;
assertThat ( credential . getUserName ( ) ) . isEqualTo ( "user" ) ;
assertThat ( credential . getPassword ( ) ) . isEqualTo ( "secret" . toCharArray ( ) ) ;
assertThat ( credential . getSource ( ) ) . isEqualTo ( "authdb" ) ;
} ) ;
}
@Test
void nettyStreamFactoryFactoryIsConfiguredAutomatically ( ) {
AtomicReference < EventLoopGroup > eventLoopGroupReference = new AtomicReference < > ( ) ;