Skip to content

Commit

Permalink
Fix MongoPropertiesTests following upgrade to Mongo 3.2 (6f5bd2e)
Browse files Browse the repository at this point in the history
  • Loading branch information
wilkinsona committed May 26, 2016
1 parent 306a82e commit 8c9c649
Showing 1 changed file with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.connection.Cluster;
import com.mongodb.connection.ClusterSettings;
import org.junit.Test;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.util.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.util.ReflectionTestUtils;

import static org.assertj.core.api.Assertions.assertThat;

Expand Down Expand Up @@ -55,7 +58,7 @@ public void portCanBeCustomized() throws UnknownHostException {
MongoProperties properties = new MongoProperties();
properties.setPort(12345);
MongoClient client = properties.createMongoClient(null, null);
List<ServerAddress> allAddresses = client.getAllAddress();
List<ServerAddress> allAddresses = extractServerAddresses(client);
assertThat(allAddresses).hasSize(1);
assertServerAddress(allAddresses.get(0), "localhost", 12345);
}
Expand All @@ -65,7 +68,7 @@ public void hostCanBeCustomized() throws UnknownHostException {
MongoProperties properties = new MongoProperties();
properties.setHost("mongo.example.com");
MongoClient client = properties.createMongoClient(null, null);
List<ServerAddress> allAddresses = client.getAllAddress();
List<ServerAddress> allAddresses = extractServerAddresses(client);
assertThat(allAddresses).hasSize(1);
assertServerAddress(allAddresses.get(0), "mongo.example.com", 27017);
}
Expand Down Expand Up @@ -108,7 +111,7 @@ public void uriCanBeCustomized() throws UnknownHostException {
properties.setUri("mongodb://user:[email protected]:12345,"
+ "mongo2.example.com:23456/test");
MongoClient client = properties.createMongoClient(null, null);
List<ServerAddress> allAddresses = client.getAllAddress();
List<ServerAddress> allAddresses = extractServerAddresses(client);
assertThat(allAddresses).hasSize(2);
assertServerAddress(allAddresses.get(0), "mongo1.example.com", 12345);
assertServerAddress(allAddresses.get(1), "mongo2.example.com", 23456);
Expand All @@ -117,6 +120,14 @@ public void uriCanBeCustomized() throws UnknownHostException {
assertMongoCredential(credentialsList.get(0), "user", "secret", "test");
}

private List<ServerAddress> extractServerAddresses(MongoClient client) {
Cluster cluster = (Cluster) ReflectionTestUtils.getField(client, "cluster");
ClusterSettings clusterSettings = (ClusterSettings) ReflectionTestUtils
.getField(cluster, "settings");
List<ServerAddress> allAddresses = clusterSettings.getHosts();
return allAddresses;
}

private void assertServerAddress(ServerAddress serverAddress, String expectedHost,
int expectedPort) {
assertThat(serverAddress.getHost()).isEqualTo(expectedHost);
Expand Down

0 comments on commit 8c9c649

Please sign in to comment.