Search in sources :

Example 1 with MongoServiceInfo

use of org.springframework.cloud.service.common.MongoServiceInfo in project jhipster-sample-app-mongodb by jhipster.

the class CloudDatabaseConfiguration method mongobee.

@Bean
public Mongobee mongobee(MongoDbFactory mongoDbFactory, MongoTemplate mongoTemplate, Cloud cloud) {
    log.debug("Configuring Cloud Mongobee");
    List<ServiceInfo> matchingServiceInfos = cloud.getServiceInfos(MongoDbFactory.class);
    if (matchingServiceInfos.size() != 1) {
        throw new CloudException("No unique service matching MongoDbFactory found. Expected 1, found " + matchingServiceInfos.size());
    }
    MongoServiceInfo info = (MongoServiceInfo) matchingServiceInfos.get(0);
    Mongobee mongobee = new Mongobee(info.getUri());
    mongobee.setDbName(mongoDbFactory.getDb().getName());
    mongobee.setMongoTemplate(mongoTemplate);
    // package to scan for migrations
    mongobee.setChangeLogsScanPackage("io.github.jhipster.sample.config.dbmigrations");
    mongobee.setEnabled(true);
    return mongobee;
}
Also used : ServiceInfo(org.springframework.cloud.service.ServiceInfo) MongoServiceInfo(org.springframework.cloud.service.common.MongoServiceInfo) CloudException(org.springframework.cloud.CloudException) Mongobee(com.github.mongobee.Mongobee) MongoServiceInfo(org.springframework.cloud.service.common.MongoServiceInfo) LocalValidatorFactoryBean(org.springframework.validation.beanvalidation.LocalValidatorFactoryBean)

Example 2 with MongoServiceInfo

use of org.springframework.cloud.service.common.MongoServiceInfo in project spring-cloud-connectors by spring-cloud.

the class HerokuConnectorMongoServiceTest method mongoServiceCreation.

@Test
public void mongoServiceCreation() {
    for (String mongoEnv : new String[] { "MONGOLAB_URI", "MONGOHQ_URL", "MONGOSOUP_URL" }) {
        Map<String, String> env = new HashMap<String, String>();
        String mongoUrl = getMongoServiceUrl("db");
        env.put(mongoEnv, mongoUrl);
        when(mockEnvironment.getEnv()).thenReturn(env);
        List<ServiceInfo> serviceInfos = testCloudConnector.getServiceInfos();
        ServiceInfo serviceInfo = getServiceInfo(serviceInfos, mongoEnv.substring(0, mongoEnv.length() - 4));
        assertNotNull(serviceInfo);
        assertTrue(serviceInfo instanceof MongoServiceInfo);
        assertMongoServiceInfo((MongoServiceInfo) serviceInfo, "db");
    }
}
Also used : ServiceInfo(org.springframework.cloud.service.ServiceInfo) MongoServiceInfo(org.springframework.cloud.service.common.MongoServiceInfo) HashMap(java.util.HashMap) MongoServiceInfo(org.springframework.cloud.service.common.MongoServiceInfo) Test(org.junit.Test)

Example 3 with MongoServiceInfo

use of org.springframework.cloud.service.common.MongoServiceInfo 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 mongoClient = getMongoClientField(mongoDbFactory);
    List<ServerAddress> addresses = extractServerAddresses(mongoClient);
    assertEquals(3, addresses.size());
    MongoCredential credentials = mongoClient.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 4 with MongoServiceInfo

use of org.springframework.cloud.service.common.MongoServiceInfo in project spring-cloud-connectors by spring-cloud.

the class CloudTest method servicePropsOneServiceOfTheSameLabel.

@Test
public void servicePropsOneServiceOfTheSameLabel() {
    MysqlServiceInfo mysqlServiceInfo = createMysqlService("my-mysql");
    MongoServiceInfo mongoServiceInfo = createMongoService("my-mongo");
    CloudConnector stubCloudConnector = getTestCloudConnector(mysqlServiceInfo, mongoServiceInfo);
    Cloud testCloud = new Cloud(stubCloudConnector, serviceCreators);
    Properties cloudProperties = testCloud.getCloudProperties();
    assertRelationalProps("cloud.services.my-mysql", mysqlServiceInfo, cloudProperties);
    assertMongoProps("cloud.services.my-mongo", mongoServiceInfo, cloudProperties);
    assertRelationalProps("cloud.services.mysql", mysqlServiceInfo, cloudProperties);
    assertMongoProps("cloud.services.mongo", mongoServiceInfo, cloudProperties);
}
Also used : MysqlServiceInfo(org.springframework.cloud.service.common.MysqlServiceInfo) CloudTestUtil.getTestCloudConnector(org.springframework.cloud.CloudTestUtil.getTestCloudConnector) Properties(java.util.Properties) MongoServiceInfo(org.springframework.cloud.service.common.MongoServiceInfo) Test(org.junit.Test)

Example 5 with MongoServiceInfo

use of org.springframework.cloud.service.common.MongoServiceInfo 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 mongoClient = getMongoClientField(mongoDbFactory);
    MongoCredential credentials = mongoClient.getCredentialsList().get(0);
    List<ServerAddress> addresses = extractServerAddresses(mongoClient);
    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

MongoServiceInfo (org.springframework.cloud.service.common.MongoServiceInfo)8 Test (org.junit.Test)7 ServiceInfo (org.springframework.cloud.service.ServiceInfo)4 MongoClient (com.mongodb.MongoClient)2 MongoCredential (com.mongodb.MongoCredential)2 ServerAddress (com.mongodb.ServerAddress)2 Properties (java.util.Properties)2 CloudTestUtil.getTestCloudConnector (org.springframework.cloud.CloudTestUtil.getTestCloudConnector)2 MongoDbFactory (org.springframework.data.mongodb.MongoDbFactory)2 Mongobee (com.github.mongobee.Mongobee)1 HashMap (java.util.HashMap)1 CloudException (org.springframework.cloud.CloudException)1 MysqlServiceInfo (org.springframework.cloud.service.common.MysqlServiceInfo)1 LocalValidatorFactoryBean (org.springframework.validation.beanvalidation.LocalValidatorFactoryBean)1