forked from spring-projects/spring-boot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix MongoPropertiesTests following upgrade to Mongo 3.2 (6f5bd2e)
- Loading branch information
1 parent
306a82e
commit 8c9c649
Showing
1 changed file
with
14 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -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); | ||
} | ||
|
@@ -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); | ||
} | ||
|
@@ -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); | ||
|
@@ -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); | ||
|