org.springframework.cloud.service.common.MongoServiceInfo Java Examples

The following examples show how to use org.springframework.cloud.service.common.MongoServiceInfo. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MongoServiceConnectorCreatorTest.java    From spring-cloud-connectors with Apache License 2.0 6 votes vote down vote up
@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"));
}
 
Example #2
Source File: HerokuConnectorMongoServiceTest.java    From spring-cloud-connectors with Apache License 2.0 5 votes vote down vote up
protected void assertMongoServiceInfo(MongoServiceInfo serviceInfo, String databaseName) {
	assertEquals(hostname, serviceInfo.getHost());
	assertEquals(port, serviceInfo.getPort());
	assertEquals(username, serviceInfo.getUserName());
	assertEquals(password, serviceInfo.getPassword());
	assertEquals(databaseName, serviceInfo.getPath());
}
 
Example #3
Source File: HerokuConnectorMongoServiceTest.java    From spring-cloud-connectors with Apache License 2.0 5 votes vote down vote up
@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");
	}
}
 
Example #4
Source File: CloudTest.java    From spring-cloud-connectors with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #5
Source File: CloudTest.java    From spring-cloud-connectors with Apache License 2.0 5 votes vote down vote up
@Test
public void servicePropsMongoMultipleHostsUriString() {
	String serviceId = "my-mongo-multiple-hosts-uri";
	MongoServiceInfo mongoServiceInfo = createMongoServiceWithMultipleHostsByUri(serviceId);
	CloudConnector stubCloudConnector = getTestCloudConnector(mongoServiceInfo);
	Cloud testCloud = new Cloud(stubCloudConnector, serviceCreators);

	Properties cloudProperties = testCloud.getCloudProperties();
	assertMongoPropsWithMultipleHostsByUri("cloud.services.my-mongo-multiple-hosts-uri", mongoServiceInfo, cloudProperties);
	assertMongoPropsWithMultipleHostsByUri("cloud.services.mongo", mongoServiceInfo, cloudProperties);
}
 
Example #6
Source File: CloudTest.java    From spring-cloud-connectors with Apache License 2.0 5 votes vote down vote up
private void assertMongoPropsWithMultipleHostsByUri(String leadKey, MongoServiceInfo serviceInfo, Properties cloudProperties) {
	assertEquals(serviceInfo.getId(), cloudProperties.get(leadKey + ".id"));
	assertEquals(serviceInfo.getUri(), cloudProperties.get(leadKey + ".connection.uri"));
	assertEquals(-1, cloudProperties.get(leadKey + ".connection.port"));

	assertNull(cloudProperties.get(leadKey + ".connection.host"));
	assertNull(cloudProperties.get(leadKey + ".connection.username"));
	assertNull(cloudProperties.get(leadKey + ".connection.password"));
}
 
Example #7
Source File: MongoServiceConnectorCreatorTest.java    From spring-cloud-connectors with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example #8
Source File: LocalConfigConnectorMongoServiceTest.java    From spring-cloud-connectors with Apache License 2.0 5 votes vote down vote up
@Test
public void serviceCreation() {
	List<ServiceInfo> services = connector.getServiceInfos();
	ServiceInfo service = getServiceInfo(services, "candygram");
	assertNotNull(service);
	assertTrue(service instanceof MongoServiceInfo);
	assertUriParameters((MongoServiceInfo) service);
}
 
Example #9
Source File: LocalConfigServiceOverrideTest.java    From spring-cloud-connectors with Apache License 2.0 5 votes vote down vote up
@Test
public void serviceOverride() {
	env.setSystemProperty("spring.cloud.candygram", "mongodb://youruser:[email protected]:4321/dbname");

	List<ServiceInfo> services = connector.getServiceInfos();
	ServiceInfo service = getServiceInfo(services, "candygram");
	assertNotNull(service);
	assertTrue(service instanceof MongoServiceInfo);
	MongoServiceInfo mongo = (MongoServiceInfo) service;
	assertEquals("youruser", mongo.getUserName());
	assertEquals(4321, mongo.getPort());
}
 
Example #10
Source File: CloudFoundryConnectorMongodbServiceTest.java    From spring-cloud-connectors with Apache License 2.0 5 votes vote down vote up
@Test
public void mongoServiceCreation() {
	when(mockEnvironment.getEnvValue("VCAP_SERVICES"))
			.thenReturn(getServicesPayload(
					getMongoServicePayload("mongo-1", hostname, port, username, password, "inventory-1", "db"),
					getMongoServicePayload("mongo-2", hostname, port, username, password, "inventory-2", "db")));

	List<ServiceInfo> serviceInfos = testCloudConnector.getServiceInfos();
	assertServiceFoundOfType(serviceInfos, "mongo-1", MongoServiceInfo.class);
	assertServiceFoundOfType(serviceInfos, "mongo-2", MongoServiceInfo.class);
}
 
Example #11
Source File: CloudFoundryConnectorMongodbServiceTest.java    From spring-cloud-connectors with Apache License 2.0 5 votes vote down vote up
@Test
public void mongoServiceCreationNoLabelNoTags() {
	when(mockEnvironment.getEnvValue("VCAP_SERVICES"))
			.thenReturn(getServicesPayload(
					getMongoServicePayloadNoLabelNoTags("mongo-1", hostname, port, username, password, "inventory-1", "db"),
					getMongoServicePayloadNoLabelNoTags("mongo-2", hostname, port, username, password, "inventory-2", "db")));

	List<ServiceInfo> serviceInfos = testCloudConnector.getServiceInfos();
	assertServiceFoundOfType(serviceInfos, "mongo-1", MongoServiceInfo.class);
	assertServiceFoundOfType(serviceInfos, "mongo-2", MongoServiceInfo.class);
}
 
Example #12
Source File: StubCloudConnectorTest.java    From spring-cloud-connectors with Apache License 2.0 4 votes vote down vote up
protected MongoServiceInfo createMongoService(String id) {
	return new MongoServiceInfo(id, "10.20.30.40", 1234, "username", "password", "db");
}
 
Example #13
Source File: MongoServiceInfoCreator.java    From spring-cloud-connectors with Apache License 2.0 4 votes vote down vote up
@Override
public MongoServiceInfo createServiceInfo(String id, String uri) {
	return new MongoServiceInfo(HerokuUtil.computeServiceName(id), uri);
}
 
Example #14
Source File: MongoServiceInfoCreator.java    From spring-cloud-connectors with Apache License 2.0 4 votes vote down vote up
public MongoServiceInfoCreator() {
	super(MongoServiceInfo.MONGODB_SCHEME);
}
 
Example #15
Source File: MongoServiceInfoCreator.java    From spring-cloud-connectors with Apache License 2.0 4 votes vote down vote up
@Override
public MongoServiceInfo createServiceInfo(String id, String uri) {
	return new MongoServiceInfo(id, uri);
}
 
Example #16
Source File: MongoServiceInfoCreator.java    From spring-cloud-connectors with Apache License 2.0 4 votes vote down vote up
public MongoServiceInfoCreator() {
	super(MongoServiceInfo.MONGODB_SCHEME);
}
 
Example #17
Source File: MongoServiceInfoCreator.java    From spring-cloud-connectors with Apache License 2.0 4 votes vote down vote up
public MongoServiceInfoCreator() {
	// the literal in the tag is CloudFoundry-specific
	super(new Tags("mongodb"), MongoServiceInfo.MONGODB_SCHEME);
}
 
Example #18
Source File: MongoDbFactoryFactoryTest.java    From spring-cloud-connectors with Apache License 2.0 4 votes vote down vote up
public MongoServiceInfo getTestServiceInfo(String id) {
	return new MongoServiceInfo(id, new UriInfo("mongodb", "host", 0, "username", "password", "db").getUriString());
}
 
Example #19
Source File: CloudTest.java    From spring-cloud-connectors with Apache License 2.0 4 votes vote down vote up
private void assertMongoProps(String leadKey, MongoServiceInfo serviceInfo, Properties cloudProperties) {
	assertBasicProps(leadKey, serviceInfo, cloudProperties);
}
 
Example #20
Source File: StubCloudConnectorTest.java    From spring-cloud-connectors with Apache License 2.0 4 votes vote down vote up
protected MongoServiceInfo createMongoServiceWithMultipleHostsByUri(String id) {
	return new MongoServiceInfo(id, "mongo://username:[email protected],10.20.30.41,10.20.30.42:1234/db");
}
 
Example #21
Source File: MongoDbFactoryCreator.java    From spring-cloud-connectors with Apache License 2.0 4 votes vote down vote up
private SimpleMongoDbFactory createMongoDbFactory(MongoServiceInfo serviceInfo, MongoClientOptions.Builder mongoOptionsToUse) throws UnknownHostException {
	MongoClientURI mongoClientURI = new MongoClientURI(serviceInfo.getUri(), mongoOptionsToUse);
	MongoClient mongo = new MongoClient(mongoClientURI);
	return new SimpleMongoDbFactory(mongo, mongoClientURI.getDatabase());
}
 
Example #22
Source File: MongoServiceInfoCreator.java    From spring-cloud-connectors with Apache License 2.0 3 votes vote down vote up
public MongoServiceInfo createServiceInfo(Map<String,Object> serviceData) {
	String id = getId(serviceData);

	String uri = getUriFromCredentials(getCredentials(serviceData));

	return new MongoServiceInfo(id, uri);
}