Search in sources :

Example 6 with MongoDbFactory

use of org.springframework.data.mongodb.MongoDbFactory in project spring-cloud-connectors by spring-cloud.

the class MongoServiceConnectorCreatorTest method cloudMongoCreationWithMultipleHostsByUri.

@Test
public void cloudMongoCreationWithMultipleHostsByUri() throws Exception {
    String uri = String.format("%s://%s:%s@%s:%s/%s", MONGODB_SCHEME, TEST_USERNAME, TEST_PASSWORD, StringUtils.arrayToDelimitedString(TEST_HOSTS, ","), TEST_PORT, TEST_DB);
    MongoServiceInfo serviceInfo = new MongoServiceInfo("id", uri);
    MongoDbFactory mongoDbFactory = testCreator.create(serviceInfo, null);
    assertNotNull(mongoDbFactory);
    MongoClient mongo = (MongoClient) ReflectionTestUtils.getField(mongoDbFactory, "mongo");
    assertNotNull(mongo);
    List<ServerAddress> addresses = extractServerAddresses(mongo);
    assertEquals(3, addresses.size());
    MongoCredential credentials = mongo.getCredentialsList().get(0);
    assertEquals(TEST_USERNAME, credentials.getUserName());
    assertNotNull(credentials.getPassword());
    // Don't do connector.getDatabase().getName() as that will try to initiate the connection
    assertEquals(TEST_DB, ReflectionTestUtils.getField(mongoDbFactory, "databaseName"));
    ServerAddress address1 = addresses.get(0);
    assertEquals(TEST_HOST, address1.getHost());
    assertEquals(TEST_PORT_DEFAULT, address1.getPort());
    ServerAddress address2 = addresses.get(1);
    assertEquals(TEST_HOST_1, address2.getHost());
    assertEquals(TEST_PORT_DEFAULT, address2.getPort());
    ServerAddress address3 = addresses.get(2);
    assertEquals(TEST_HOST_2, address3.getHost());
    assertEquals(TEST_PORT, address3.getPort());
}
Also used : MongoClient(com.mongodb.MongoClient) MongoDbFactory(org.springframework.data.mongodb.MongoDbFactory) MongoCredential(com.mongodb.MongoCredential) ServerAddress(com.mongodb.ServerAddress) MongoServiceInfo(org.springframework.cloud.service.common.MongoServiceInfo) Test(org.junit.Test)

Example 7 with MongoDbFactory

use of org.springframework.data.mongodb.MongoDbFactory in project spring-cloud-connectors by spring-cloud.

the class MongoDbFactoryXmlConfigTest method withConfigAllOptionsSpecifiedWriteConcernSafe.

@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, "safe", 50, 200);
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) MongoDbFactory(org.springframework.data.mongodb.MongoDbFactory) Test(org.junit.Test)

Example 8 with MongoDbFactory

use of org.springframework.data.mongodb.MongoDbFactory in project spring-cloud-connectors by spring-cloud.

the class MongoDbFactoryXmlConfigTest method withConfigAllOptionsSpecifiedWriteConcernNone.

@Test
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, "none", 50, 200);
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) MongoDbFactory(org.springframework.data.mongodb.MongoDbFactory) Test(org.junit.Test)

Example 9 with MongoDbFactory

use of org.springframework.data.mongodb.MongoDbFactory in project spring-cloud-connectors by spring-cloud.

the class MongoDbFactoryXmlConfigTest method withConfigOnlyMaxWaitSpecified.

@Test
public void withConfigOnlyMaxWaitSpecified() {
    ApplicationContext testContext = getTestApplicationContext("cloud-mongo-with-config.xml", createService("my-service"));
    MongoDbFactory connector = testContext.getBean("service-maxWait200-connectionPerHostUnspecified-WriteConcernUnspecified", getConnectorType());
    MongoDbFactoryCloudConfigTestHelper.assertConfigProperties(connector, null, null, 200);
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) MongoDbFactory(org.springframework.data.mongodb.MongoDbFactory) Test(org.junit.Test)

Example 10 with MongoDbFactory

use of org.springframework.data.mongodb.MongoDbFactory in project spring-cloud-connectors by spring-cloud.

the class MongoServiceConnectorCreatorTest method cloudMongoCreationNoConfig.

@Test
public void cloudMongoCreationNoConfig() throws Exception {
    MongoServiceInfo serviceInfo = new MongoServiceInfo("id", TEST_HOST, TEST_PORT, TEST_USERNAME, TEST_PASSWORD, TEST_DB);
    MongoDbFactory mongoDbFactory = testCreator.create(serviceInfo, null);
    assertNotNull(mongoDbFactory);
    MongoClient mongo = (MongoClient) ReflectionTestUtils.getField(mongoDbFactory, "mongo");
    assertNotNull(mongo);
    MongoCredential credentials = mongo.getCredentialsList().get(0);
    List<ServerAddress> addresses = extractServerAddresses(mongo);
    assertEquals(1, addresses.size());
    ServerAddress address = addresses.get(0);
    assertEquals(serviceInfo.getHost(), address.getHost());
    assertEquals(serviceInfo.getPort(), address.getPort());
    assertEquals(serviceInfo.getUserName(), credentials.getUserName());
    assertNotNull(credentials.getPassword());
    // Don't do connector.getDatabase().getName() as that will try to initiate the connection
    assertEquals(serviceInfo.getDatabase(), ReflectionTestUtils.getField(mongoDbFactory, "databaseName"));
}
Also used : MongoClient(com.mongodb.MongoClient) MongoDbFactory(org.springframework.data.mongodb.MongoDbFactory) MongoCredential(com.mongodb.MongoCredential) ServerAddress(com.mongodb.ServerAddress) MongoServiceInfo(org.springframework.cloud.service.common.MongoServiceInfo) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)12 MongoDbFactory (org.springframework.data.mongodb.MongoDbFactory)12 ApplicationContext (org.springframework.context.ApplicationContext)10 MongoClient (com.mongodb.MongoClient)2 MongoCredential (com.mongodb.MongoCredential)2 ServerAddress (com.mongodb.ServerAddress)2 MongoServiceInfo (org.springframework.cloud.service.common.MongoServiceInfo)2