Skip to content

Commit

Permalink
Merge branch 'wilkinsona-mongo-3-compatibility'
Browse files Browse the repository at this point in the history
  • Loading branch information
scottfrederick committed Jun 30, 2016
2 parents 1790aff + cdef9ed commit 8cf17aa
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import com.mongodb.MongoClient;
import com.mongodb.WriteConcern;

import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.test.util.ReflectionTestUtils;

import com.mongodb.MongoClient;
import com.mongodb.WriteConcern;

/**
*
* @author Ramnivas Laddad
*
*/
public class MongoDbFactoryCloudConfigTestHelper {

public static void assertConfigProperties(MongoDbFactory connector, Integer writeConcernW, Integer connectionsPerHost, Integer maxWaitTime) {
public static void assertConfigProperties(MongoDbFactory connector, String writeConcern, Integer connectionsPerHost, Integer maxWaitTime) {
if (connectionsPerHost == null) {
connectionsPerHost = 100; // default
}
Expand All @@ -25,11 +25,7 @@ public static void assertConfigProperties(MongoDbFactory connector, Integer writ
}
assertNotNull(connector);

WriteConcern writeConcern = (WriteConcern) ReflectionTestUtils.getField(connector, "writeConcern");
if (writeConcernW != null) {
assertNotNull(writeConcern);
assertEquals(writeConcernW.intValue(), writeConcern.getW());
}
assertEquals(ReflectionTestUtils.getField(connector, "writeConcern"), writeConcern == null ? null : WriteConcern.valueOf(writeConcern));

MongoClient mongoClient = (MongoClient) ReflectionTestUtils.getField(connector, "mongo");
assertEquals(connectionsPerHost.intValue(), mongoClient.getMongoClientOptions().getConnectionsPerHost());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void withConfigAllOptionsSpecifiedWriteConcernNone() {
getTestApplicationContext(MongoDbFactoryConfigWithServiceConfig.class, createService("my-service"));

MongoDbFactory connector = testContext.getBean("connectionPerHost50_MaxWait200_WriteConcernNone", getConnectorType());
MongoDbFactoryCloudConfigTestHelper.assertConfigProperties(connector, -1, 50, 200);
MongoDbFactoryCloudConfigTestHelper.assertConfigProperties(connector, "none", 50, 200);
}

@Test
Expand All @@ -41,7 +41,7 @@ public void withConfigAllOptionsSpecifiedWriteConcernSafe() {
getTestApplicationContext(MongoDbFactoryConfigWithServiceConfig.class, createService("my-service"));

MongoDbFactory connector = testContext.getBean("connectionPerHost50_MaxWait200_WriteConcernSafe", getConnectorType());
MongoDbFactoryCloudConfigTestHelper.assertConfigProperties(connector, 1, 50, 200);
MongoDbFactoryCloudConfigTestHelper.assertConfigProperties(connector, "safe", 50, 200);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ public void withConfigAllOptionsSpecifiedWriteConcernNone() {
ApplicationContext testContext = getTestApplicationContext("cloud-mongo-with-config.xml", createService("my-service"));

MongoDbFactory connector = testContext.getBean("service-connectionPerHost50-maxWait200-WriteConcernNone", getConnectorType());
MongoDbFactoryCloudConfigTestHelper.assertConfigProperties(connector, -1, 50, 200);
MongoDbFactoryCloudConfigTestHelper.assertConfigProperties(connector, "none", 50, 200);
}

@Test
public void withConfigAllOptionsSpecifiedWriteConcernSafe() {
ApplicationContext testContext = getTestApplicationContext("cloud-mongo-with-config.xml", createService("my-service"));

MongoDbFactory connector = testContext.getBean("service-maxWait200-connectionPerHost50-WriteConcernSafe", getConnectorType());
MongoDbFactoryCloudConfigTestHelper.assertConfigProperties(connector, 1, 50, 200);
MongoDbFactoryCloudConfigTestHelper.assertConfigProperties(connector, "safe", 50, 200);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.lang.reflect.Method;
import java.util.List;

import org.junit.Test;
import org.springframework.cloud.service.common.MongoServiceInfo;
import org.springframework.cloud.service.document.MongoDbFactoryCreator;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;

import com.mongodb.MongoClient;
Expand Down Expand Up @@ -49,7 +52,7 @@ public void cloudMongoCreationNoConfig() throws Exception {

MongoCredential credentials = mongo.getCredentialsList().get(0);

List<ServerAddress> addresses = mongo.getAllAddress();
List<ServerAddress> addresses = extractServerAddresses(mongo);
assertEquals(1, addresses.size());

ServerAddress address = addresses.get(0);
Expand Down Expand Up @@ -77,7 +80,7 @@ public void cloudMongoCreationWithMultipleHostsByUri() throws Exception {
MongoClient mongo = (MongoClient) ReflectionTestUtils.getField(mongoDbFactory, "mongo");
assertNotNull(mongo);

List<ServerAddress> addresses = mongo.getAllAddress();
List<ServerAddress> addresses = extractServerAddresses(mongo);
assertEquals(3, addresses.size());

MongoCredential credentials = mongo.getCredentialsList().get(0);
Expand All @@ -99,4 +102,19 @@ public void cloudMongoCreationWithMultipleHostsByUri() throws Exception {
assertEquals(TEST_HOST_2, address3.getHost());
assertEquals(TEST_PORT, address3.getPort());
}

@SuppressWarnings("unchecked")
private List<ServerAddress> extractServerAddresses(MongoClient client) {
if (ClassUtils.isPresent("com.mongodb.connection.Cluster",
getClass().getClassLoader())) {
Object cluster = ReflectionTestUtils.getField(client, "cluster");
Object clusterSettings = ReflectionTestUtils.getField(cluster, "settings");
Method getHostsMethod = ReflectionUtils.findMethod(clusterSettings.getClass(),
"getHosts");
return (List<ServerAddress>) ReflectionUtils.invokeMethod(getHostsMethod,
clusterSettings);
}
return client.getAllAddress();
}

}

0 comments on commit 8cf17aa

Please sign in to comment.