Fix MongoPropertiesTests following upgrade to Mongo 3.2 (6f5bd2e)

See gh-3011
pull/6046/merge
Andy Wilkinson 9 years ago
parent 306a82e652
commit 8c9c649805

@ -22,12 +22,15 @@ import java.util.List;
import com.mongodb.MongoClient; import com.mongodb.MongoClient;
import com.mongodb.MongoCredential; import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress; import com.mongodb.ServerAddress;
import com.mongodb.connection.Cluster;
import com.mongodb.connection.ClusterSettings;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.util.EnvironmentTestUtils; import org.springframework.boot.test.util.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -55,7 +58,7 @@ public class MongoPropertiesTests {
MongoProperties properties = new MongoProperties(); MongoProperties properties = new MongoProperties();
properties.setPort(12345); properties.setPort(12345);
MongoClient client = properties.createMongoClient(null, null); MongoClient client = properties.createMongoClient(null, null);
List<ServerAddress> allAddresses = client.getAllAddress(); List<ServerAddress> allAddresses = extractServerAddresses(client);
assertThat(allAddresses).hasSize(1); assertThat(allAddresses).hasSize(1);
assertServerAddress(allAddresses.get(0), "localhost", 12345); assertServerAddress(allAddresses.get(0), "localhost", 12345);
} }
@ -65,7 +68,7 @@ public class MongoPropertiesTests {
MongoProperties properties = new MongoProperties(); MongoProperties properties = new MongoProperties();
properties.setHost("mongo.example.com"); properties.setHost("mongo.example.com");
MongoClient client = properties.createMongoClient(null, null); MongoClient client = properties.createMongoClient(null, null);
List<ServerAddress> allAddresses = client.getAllAddress(); List<ServerAddress> allAddresses = extractServerAddresses(client);
assertThat(allAddresses).hasSize(1); assertThat(allAddresses).hasSize(1);
assertServerAddress(allAddresses.get(0), "mongo.example.com", 27017); assertServerAddress(allAddresses.get(0), "mongo.example.com", 27017);
} }
@ -108,7 +111,7 @@ public class MongoPropertiesTests {
properties.setUri("mongodb://user:secret@mongo1.example.com:12345," properties.setUri("mongodb://user:secret@mongo1.example.com:12345,"
+ "mongo2.example.com:23456/test"); + "mongo2.example.com:23456/test");
MongoClient client = properties.createMongoClient(null, null); MongoClient client = properties.createMongoClient(null, null);
List<ServerAddress> allAddresses = client.getAllAddress(); List<ServerAddress> allAddresses = extractServerAddresses(client);
assertThat(allAddresses).hasSize(2); assertThat(allAddresses).hasSize(2);
assertServerAddress(allAddresses.get(0), "mongo1.example.com", 12345); assertServerAddress(allAddresses.get(0), "mongo1.example.com", 12345);
assertServerAddress(allAddresses.get(1), "mongo2.example.com", 23456); assertServerAddress(allAddresses.get(1), "mongo2.example.com", 23456);
@ -117,6 +120,14 @@ public class MongoPropertiesTests {
assertMongoCredential(credentialsList.get(0), "user", "secret", "test"); 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, private void assertServerAddress(ServerAddress serverAddress, String expectedHost,
int expectedPort) { int expectedPort) {
assertThat(serverAddress.getHost()).isEqualTo(expectedHost); assertThat(serverAddress.getHost()).isEqualTo(expectedHost);

Loading…
Cancel
Save