Commit 7b5df365 authored by Stephane Nicoll's avatar Stephane Nicoll

Enable SSL from MongoClientOptions

Closes gh-5099
parent fa0a137c
......@@ -39,6 +39,7 @@ import org.springframework.core.env.Environment;
* @author Josh Long
* @author Andy Wilkinson
* @author Eddú Meléndez
* @author Stephane Nicoll
*/
@ConfigurationProperties(prefix = "spring.data.mongodb")
public class MongoProperties {
......@@ -257,6 +258,7 @@ public class MongoProperties {
builder.description(options.getDescription());
builder.maxWaitTime(options.getMaxWaitTime());
builder.readPreference(options.getReadPreference());
builder.sslEnabled(options.isSslEnabled());
builder.socketFactory(options.getSocketFactory());
builder.socketKeepAlive(options.isSocketKeepAlive());
builder.socketTimeout(options.getSocketTimeout());
......
......@@ -16,7 +16,10 @@
package org.springframework.boot.autoconfigure.mongo;
import javax.net.SocketFactory;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import org.junit.After;
import org.junit.Test;
......@@ -28,11 +31,13 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link MongoAutoConfiguration}.
*
* @author Dave Syer
* @author Stephane Nicoll
*/
public class MongoAutoConfigurationTests {
......@@ -78,6 +83,20 @@ public class MongoAutoConfigurationTests {
.isEqualTo(300);
}
@Test
public void optionsSslConfig() {
this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context,
"spring.data.mongodb.uri:mongodb://localhost/test");
this.context.register(SslOptionsConfig.class,
PropertyPlaceholderAutoConfiguration.class, MongoAutoConfiguration.class);
this.context.refresh();
MongoClient mongo = this.context.getBean(MongoClient.class);
MongoClientOptions options = mongo.getMongoClientOptions();
assertThat(options.isSslEnabled()).isTrue();
assertThat(options.getSocketFactory()).isSameAs(this.context.getBean("mySocketFactory"));
}
@Configuration
protected static class OptionsConfig {
......@@ -88,4 +107,19 @@ public class MongoAutoConfigurationTests {
}
@Configuration
protected static class SslOptionsConfig {
@Bean
public MongoClientOptions mongoClientOptions() {
return MongoClientOptions.builder().sslEnabled(true).socketFactory(mySocketFactory()).build();
}
@Bean
public SocketFactory mySocketFactory() {
return mock(SocketFactory.class);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment