Add support for multiple URI schemes for service type detection. Add verification of detected ServiceInfo type in CloudFoundryConnector tests.

This commit is contained in:
Scott Frederick
2014-10-31 15:33:51 -05:00
parent 1c826dad34
commit 20c7aba1f6
43 changed files with 243 additions and 204 deletions

View File

@@ -12,16 +12,15 @@ import org.springframework.cloud.service.common.AmqpServiceInfo;
public class AmqpServiceInfoCreator extends CloudFoundryServiceInfoCreator<AmqpServiceInfo> {
public AmqpServiceInfoCreator() {
super(new Tags("rabbitmq"), AmqpServiceInfo.URI_SCHEME);
super(new Tags("rabbitmq"), AmqpServiceInfo.AMQP_SCHEME, AmqpServiceInfo.AMQPS_SCHEME);
}
public AmqpServiceInfo createServiceInfo(Map<String,Object> serviceData) {
@SuppressWarnings("unchecked")
Map<String,Object> credentials = (Map<String, Object>) serviceData.get("credentials");
Map<String,Object> credentials = getCredentials(serviceData);
String id = (String) serviceData.get("name");
String uri = getStringFromCredentials(credentials, "uri", "url");
String uri = getUriFromCredentials(credentials);
return new AmqpServiceInfo(id, uri);
}

View File

@@ -12,15 +12,11 @@ import org.springframework.cloud.service.ServiceInfo;
public abstract class CloudFoundryServiceInfoCreator<SI extends ServiceInfo> implements ServiceInfoCreator<SI, Map<String, Object>> {
private Tags tags;
private String uriScheme;
private String[] uriSchemes;
public CloudFoundryServiceInfoCreator(Tags tags, String uriScheme) {
public CloudFoundryServiceInfoCreator(Tags tags, String... uriSchemes) {
this.tags = tags;
this.uriScheme = uriScheme;
}
public CloudFoundryServiceInfoCreator(Tags tags) {
this(tags, null);
this.uriSchemes = uriSchemes;
}
public boolean accept(Map<String, Object> serviceData) {
@@ -39,24 +35,30 @@ public abstract class CloudFoundryServiceInfoCreator<SI extends ServiceInfo> imp
}
protected boolean uriMatchesScheme(Map<String, Object> serviceData) {
if (uriScheme == null) {
if (uriSchemes == null) {
return false;
}
@SuppressWarnings("unchecked")
Map<String, String> credentials = (Map<String, String>) serviceData.get("credentials");
if (credentials != null) {
String uri = credentials.get("uri");
if (uri == null) {
uri = credentials.get("url");
}
if (uri != null) {
return uri.startsWith(uriScheme + "://");
String uri = getUriFromCredentials(getCredentials(serviceData));
if (uri != null) {
for (String uriScheme : uriSchemes) {
if (uri.startsWith(uriScheme + "://")) {
return true;
}
}
}
return false;
}
@SuppressWarnings("unchecked")
protected Map<String, Object> getCredentials(Map<String, Object> serviceData) {
return (Map<String, Object>) serviceData.get("credentials");
}
protected String getUriFromCredentials(Map<String, Object> credentials) {
return getStringFromCredentials(credentials, "uri", "url");
}
protected String getStringFromCredentials(Map<String, Object> credentials, String... keys) {
for (String key : keys) {
if (credentials.containsKey(key)) {
@@ -66,7 +68,21 @@ public abstract class CloudFoundryServiceInfoCreator<SI extends ServiceInfo> imp
return null;
}
public String getUriScheme() {
return uriScheme;
protected int getIntFromCredentials(Map<String, Object> credentials, String... keys) {
for (String key : keys) {
if (credentials.containsKey(key)) {
// allows the value to be quoted as a String or native integer type
return Integer.parseInt(credentials.get(key).toString());
}
}
return -1;
}
public String[] getUriSchemes() {
return uriSchemes;
}
public String getDefaultUriScheme() {
return uriSchemes[0];
}
}

View File

@@ -12,17 +12,14 @@ import org.springframework.cloud.service.common.MongoServiceInfo;
public class MongoServiceInfoCreator extends CloudFoundryServiceInfoCreator<MongoServiceInfo> {
public MongoServiceInfoCreator() {
// the literal in the tag is CloudFoundry-specific
super(new Tags("mongodb"), MongoServiceInfo.URI_SCHEME);
// the literal in the tag is CloudFoundry-specific
super(new Tags("mongodb"), MongoServiceInfo.MONGODB_SCHEME);
}
public MongoServiceInfo createServiceInfo(Map<String,Object> serviceData) {
@SuppressWarnings("unchecked")
Map<String,Object> credentials = (Map<String, Object>) serviceData.get("credentials");
String id = (String) serviceData.get("name");
String uri = getStringFromCredentials(credentials, "uri", "url");
String uri = getUriFromCredentials(getCredentials(serviceData));
return new MongoServiceInfo(id, uri);
}

View File

@@ -10,8 +10,8 @@ import org.springframework.cloud.service.common.MysqlServiceInfo;
public class MysqlServiceInfoCreator extends RelationalServiceInfoCreator<MysqlServiceInfo> {
public MysqlServiceInfoCreator() {
// the literal in the tag is CloudFoundry-specific
super(new Tags("mysql"), MysqlServiceInfo.URI_SCHEME);
// the literal in the tag is CloudFoundry-specific
super(new Tags("mysql"), MysqlServiceInfo.MYSQL_SCHEME);
}
@Override

View File

@@ -4,7 +4,7 @@ import org.springframework.cloud.service.common.OracleServiceInfo;
public class OracleServiceInfoCreator extends RelationalServiceInfoCreator<OracleServiceInfo> {
public OracleServiceInfoCreator() {
super(new Tags(), OracleServiceInfo.URI_SCHEME);
super(new Tags(), OracleServiceInfo.ORACLE_SCHEME);
}
@Override

View File

@@ -10,7 +10,7 @@ import org.springframework.cloud.service.common.PostgresqlServiceInfo;
public class PostgresqlServiceInfoCreator extends RelationalServiceInfoCreator<PostgresqlServiceInfo> {
public PostgresqlServiceInfoCreator() {
super(new Tags("postgresql"), PostgresqlServiceInfo.URI_SCHEME);
super(new Tags("postgresql"), PostgresqlServiceInfo.POSTGRES_SCHEME);
}
@Override

View File

@@ -12,21 +12,19 @@ import org.springframework.cloud.service.common.RedisServiceInfo;
public class RedisServiceInfoCreator extends CloudFoundryServiceInfoCreator<RedisServiceInfo> {
public RedisServiceInfoCreator() {
// the literal in the tag is CloudFoundry-specific
super(new Tags("redis"), RedisServiceInfo.URI_SCHEME);
// the literal in the tag is CloudFoundry-specific
super(new Tags("redis"), RedisServiceInfo.REDIS_SCHEME);
}
public RedisServiceInfo createServiceInfo(Map<String,Object> serviceData) {
@SuppressWarnings("unchecked")
Map<String, Object> credentials = (Map<String, Object>) serviceData.get("credentials");
String id = (String) serviceData.get("name");
String uri = getStringFromCredentials(credentials, "uri", "url");
Map<String, Object> credentials = getCredentials(serviceData);
String uri = getUriFromCredentials(credentials);
if (uri == null) {
String host = getStringFromCredentials(credentials, "hostname", "host");
Integer port = Integer.parseInt(credentials.get("port").toString());
Integer port = getIntFromCredentials(credentials, "port");
String password = (String) credentials.get("password");
return new RedisServiceInfo(id, host, port, password);

View File

@@ -12,30 +12,28 @@ import org.springframework.cloud.util.UriInfo;
*/
public abstract class RelationalServiceInfoCreator<SI extends RelationalServiceInfo> extends CloudFoundryServiceInfoCreator<SI> {
public RelationalServiceInfoCreator(Tags tags, String uriScheme) {
super(tags, uriScheme);
public RelationalServiceInfoCreator(Tags tags, String... uriSchemes) {
super(tags, uriSchemes);
}
public abstract SI createServiceInfo(String id, String uri);
public SI createServiceInfo(Map<String,Object> serviceData) {
@SuppressWarnings("unchecked")
Map<String,Object> credentials = (Map<String, Object>) serviceData.get("credentials");
public SI createServiceInfo(Map<String, Object> serviceData) {
String id = (String) serviceData.get("name");
String uri = getStringFromCredentials(credentials, "uri", "url");
Map<String,Object> credentials = getCredentials(serviceData);
String uri = getUriFromCredentials(credentials);
if (uri == null) {
String host = getStringFromCredentials(credentials, "hostname", "host");
int port = Integer.parseInt(credentials.get("port").toString()); // allows the port attribute to be quoted or plain
int port = getIntFromCredentials(credentials, "port");
String username = getStringFromCredentials(credentials, "user", "username");
String password = (String) credentials.get("password");
String database = (String) credentials.get("name");
uri = new UriInfo(getUriScheme(), host, port, username, password, database).toString();
uri = new UriInfo(getDefaultUriScheme(), host, port, username, password, database).toString();
}
return createServiceInfo(id, uri);

View File

@@ -15,26 +15,25 @@ public class SmtpServiceInfoCreator extends CloudFoundryServiceInfoCreator<SmtpS
private static final int DEFAULT_SMTP_PORT = 587;
public SmtpServiceInfoCreator() {
// the literal in the tag is CloudFoundry-specific
super(new Tags("smtp"), SmtpServiceInfo.URI_SCHEME);
// the literal in the tag is CloudFoundry-specific
super(new Tags("smtp"), SmtpServiceInfo.SMTP_SCHEME);
}
public SmtpServiceInfo createServiceInfo(Map<String,Object> serviceData) {
String id = (String) serviceData.get("name");
@SuppressWarnings("unchecked")
Map<String,Object> credentials = (Map<String, Object>) serviceData.get("credentials");
Map<String,Object> credentials = getCredentials(serviceData);
String host = (String) credentials.get("hostname");
int port = DEFAULT_SMTP_PORT;
if (credentials.containsKey("port")) {
port = Integer.parseInt(credentials.get("port").toString());
int port = getIntFromCredentials(credentials, "port");
if (port == -1) {
port = DEFAULT_SMTP_PORT;
}
String username = (String) credentials.get("username");
String password = (String) credentials.get("password");
String uri = new UriInfo(SmtpServiceInfo.URI_SCHEME, host, port, username, password).toString();
String uri = new UriInfo(SmtpServiceInfo.SMTP_SCHEME, host, port, username, password).toString();
return new SmtpServiceInfo(id, uri);
}

View File

@@ -11,11 +11,15 @@ import java.util.Scanner;
import org.junit.Before;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.internal.matchers.InstanceOf;
import org.springframework.cloud.service.ServiceInfo;
import org.springframework.cloud.util.EnvironmentAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
/**
* Base test class that provides setup and utility methods to generate test payload
*
@@ -120,5 +124,14 @@ public abstract class AbstractCloudFoundryConnectorTest {
private static String quote(String str) {
return "\"" + str + "\"";
}
protected static void assertServiceFoundOfType(ServiceInfo serviceInfo, Class<? extends ServiceInfo> type) {
assertNotNull(serviceInfo);
assertThat(serviceInfo, new InstanceOf(type));
}
protected static void assertServiceFoundOfType(List<ServiceInfo> serviceInfos, String serviceId, Class<? extends ServiceInfo> type) {
ServiceInfo serviceInfo = getServiceInfo(serviceInfos, serviceId);
assertServiceFoundOfType(serviceInfo, type);
}
}

View File

@@ -1,12 +1,12 @@
package org.springframework.cloud.cloudfoundry;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.when;
import java.util.List;
import org.junit.Test;
import org.springframework.cloud.service.ServiceInfo;
import org.springframework.cloud.service.common.AmqpServiceInfo;
/**
*
@@ -22,8 +22,8 @@ public class CloudFoundryConnectorAmqpServiceTest extends AbstractCloudFoundryCo
getRabbitServicePayloadWithTags("rabbit-2", hostname, port, username, password, "q-2", "vhost2")));
List<ServiceInfo> serviceInfos = testCloudConnector.getServiceInfos();
assertNotNull(getServiceInfo(serviceInfos, "rabbit-1"));
assertNotNull(getServiceInfo(serviceInfos, "rabbit-2"));
assertServiceFoundOfType(serviceInfos, "rabbit-1", AmqpServiceInfo.class);
assertServiceFoundOfType(serviceInfos, "rabbit-2", AmqpServiceInfo.class);
}
@Test
@@ -34,20 +34,32 @@ public class CloudFoundryConnectorAmqpServiceTest extends AbstractCloudFoundryCo
getRabbitServicePayloadWithoutTags("rabbit-2", hostname, port, username, password, "q-2", "vhost2")));
List<ServiceInfo> serviceInfos = testCloudConnector.getServiceInfos();
assertNotNull(getServiceInfo(serviceInfos, "rabbit-1"));
assertNotNull(getServiceInfo(serviceInfos, "rabbit-2"));
assertServiceFoundOfType(serviceInfos, "rabbit-1", AmqpServiceInfo.class);
assertServiceFoundOfType(serviceInfos, "rabbit-2", AmqpServiceInfo.class);
}
@Test
public void rabbitServiceCreationNoLabelNoTags() {
@Test
public void rabbitServiceCreationNoLabelNoTags() {
when(mockEnvironment.getEnvValue("VCAP_SERVICES"))
.thenReturn(getServicesPayload(
getRabbitServicePayloadNoLabelNoTags("rabbit-1", hostname, port, username, password, "q-1", "vhost1"),
getRabbitServicePayloadNoLabelNoTags("rabbit-2", hostname, port, username, password, "q-2", "vhost2")));
List<ServiceInfo> serviceInfos = testCloudConnector.getServiceInfos();
assertNotNull(getServiceInfo(serviceInfos, "rabbit-1"));
assertNotNull(getServiceInfo(serviceInfos, "rabbit-2"));
assertServiceFoundOfType(serviceInfos, "rabbit-1", AmqpServiceInfo.class);
assertServiceFoundOfType(serviceInfos, "rabbit-2", AmqpServiceInfo.class);
}
@Test
public void rabbitServiceCreationNoLabelNoTagsSecure() {
when(mockEnvironment.getEnvValue("VCAP_SERVICES"))
.thenReturn(getServicesPayload(
getRabbitServicePayloadNoLabelNoTagsSecure("rabbit-1", hostname, port, username, password, "q-1", "vhost1"),
getRabbitServicePayloadNoLabelNoTagsSecure("rabbit-2", hostname, port, username, password, "q-2", "vhost2")));
List<ServiceInfo> serviceInfos = testCloudConnector.getServiceInfos();
assertServiceFoundOfType(serviceInfos, "rabbit-1", AmqpServiceInfo.class);
assertServiceFoundOfType(serviceInfos, "rabbit-2", AmqpServiceInfo.class);
}
private String getRabbitServicePayloadWithoutTags(String serviceName,
@@ -66,6 +78,14 @@ public class CloudFoundryConnectorAmqpServiceTest extends AbstractCloudFoundryCo
hostname, port, user, password, name, vHost);
}
private String getRabbitServicePayloadNoLabelNoTagsSecure(String serviceName,
String hostname, int port,
String user, String password, String name,
String vHost) {
return getRabbitServicePayload("test-rabbit-info-no-label-no-tags-secure.json", serviceName,
hostname, port, user, password, name, vHost);
}
private String getRabbitServicePayloadWithTags(String serviceName,
String hostname, int port,
String user, String password, String name,

View File

@@ -1,68 +1,66 @@
package org.springframework.cloud.cloudfoundry;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.when;
import java.util.List;
import org.junit.Test;
import org.springframework.cloud.service.ServiceInfo;
import org.springframework.cloud.service.common.MongoServiceInfo;
/**
*
* @author Ramnivas Laddad
*
*/
public class CloudFoundryConnectorMongodbServiceTest extends AbstractCloudFoundryConnectorTest {
@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")));
.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();
assertNotNull(getServiceInfo(serviceInfos, "mongo-1"));
assertNotNull(getServiceInfo(serviceInfos, "mongo-2"));
assertServiceFoundOfType(serviceInfos, "mongo-1", MongoServiceInfo.class);
assertServiceFoundOfType(serviceInfos, "mongo-2", MongoServiceInfo.class);
}
@Test
public void mongoServiceCreationNoLabelNoTags() {
@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")));
.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);
}
List<ServiceInfo> serviceInfos = testCloudConnector.getServiceInfos();
assertNotNull(getServiceInfo(serviceInfos, "mongo-1"));
assertNotNull(getServiceInfo(serviceInfos, "mongo-2"));
}
private String getMongoServicePayload(String serviceName, String hostname, int port,
String username, String password, String db, String name) {
return getMongoServicePayload("test-mongodb-info.json",
serviceName, hostname, port, username, password, db, name);
return getMongoServicePayload("test-mongodb-info.json",
serviceName, hostname, port, username, password, db, name);
}
private String getMongoServicePayloadNoLabelNoTags(String serviceName,
String hostname, int port,
String username, String password, String db, String name) {
return getMongoServicePayload("test-mongodb-info-no-label-no-tags.json",
serviceName, hostname, port, username, password, db, name);
}
private String getMongoServicePayloadNoLabelNoTags(String serviceName,
String hostname, int port,
String username, String password, String db, String name) {
return getMongoServicePayload("test-mongodb-info-no-label-no-tags.json",
serviceName, hostname, port, username, password, db, name);
}
private String getMongoServicePayload(String payloadFile, String serviceName,
String hostname, int port,
String username, String password, String db, String name) {
String payload = readTestDataFile(payloadFile);
payload = payload.replace("$serviceName", serviceName);
payload = payload.replace("$hostname", hostname);
payload = payload.replace("$port", Integer.toString(port));
payload = payload.replace("$username", username);
payload = payload.replace("$password", password);
payload = payload.replace("$db", db);
payload = payload.replace("$name", name);
return payload;
}
private String getMongoServicePayload(String payloadFile, String serviceName,
String hostname, int port,
String username, String password, String db, String name) {
String payload = readTestDataFile(payloadFile);
payload = payload.replace("$serviceName", serviceName);
payload = payload.replace("$hostname", hostname);
payload = payload.replace("$port", Integer.toString(port));
payload = payload.replace("$username", username);
payload = payload.replace("$password", password);
payload = payload.replace("$db", db);
payload = payload.replace("$name", name);
return payload;
}
}

View File

@@ -1,12 +1,12 @@
package org.springframework.cloud.cloudfoundry;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.when;
import java.util.List;
import org.junit.Test;
import org.springframework.cloud.service.ServiceInfo;
import org.springframework.cloud.service.common.MonitoringServiceInfo;
/**
*
@@ -21,7 +21,7 @@ public class CloudFoundryConnectorMonitoringServiceTest extends AbstractCloudFou
.thenReturn(getServicesPayload(getMonitoringServicePayload("monitoring-1")));
List<ServiceInfo> serviceInfos = testCloudConnector.getServiceInfos();
assertNotNull(getServiceInfo(serviceInfos, "monitoring-1"));
assertServiceFoundOfType(serviceInfos, "monitoring-1", MonitoringServiceInfo.class);
}
private String getMonitoringServicePayload(String serviceName) {

View File

@@ -1,7 +1,6 @@
package org.springframework.cloud.cloudfoundry;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.when;
import java.util.List;
@@ -28,8 +27,9 @@ public class CloudFoundryConnectorMysqlServiceTest extends AbstractCloudFoundryC
MysqlServiceInfo info1 = (MysqlServiceInfo) getServiceInfo(serviceInfos, "mysql-1");
MysqlServiceInfo info2 = (MysqlServiceInfo) getServiceInfo(serviceInfos, "mysql-2");
assertNotNull(info1);
assertNotNull(info2);
assertServiceFoundOfType(info1, MysqlServiceInfo.class);
assertServiceFoundOfType(info2, MysqlServiceInfo.class);
assertEquals(getJdbcUrl("mysql", name1), info1.getJdbcUrl());
assertEquals(getJdbcUrl("mysql", name2), info2.getJdbcUrl());
}
@@ -46,8 +46,8 @@ public class CloudFoundryConnectorMysqlServiceTest extends AbstractCloudFoundryC
MysqlServiceInfo info1 = (MysqlServiceInfo) getServiceInfo(serviceInfos, "mysql-1");
MysqlServiceInfo info2 = (MysqlServiceInfo) getServiceInfo(serviceInfos, "mysql-2");
assertNotNull(info1);
assertNotNull(info2);
assertServiceFoundOfType(info1, MysqlServiceInfo.class);
assertServiceFoundOfType(info2, MysqlServiceInfo.class);
assertEquals(getJdbcUrl("mysql", name1), info1.getJdbcUrl());
assertEquals(getJdbcUrl("mysql", name2), info2.getJdbcUrl());
}
@@ -64,8 +64,8 @@ public class CloudFoundryConnectorMysqlServiceTest extends AbstractCloudFoundryC
MysqlServiceInfo info1 = (MysqlServiceInfo) getServiceInfo(serviceInfos, "mysql-1");
MysqlServiceInfo info2 = (MysqlServiceInfo) getServiceInfo(serviceInfos, "mysql-2");
assertNotNull(info1);
assertNotNull(info2);
assertServiceFoundOfType(info1, MysqlServiceInfo.class);
assertServiceFoundOfType(info2, MysqlServiceInfo.class);
assertEquals(getJdbcUrl("mysql", name1), info1.getJdbcUrl());
assertEquals(getJdbcUrl("mysql", name2), info2.getJdbcUrl());
}
@@ -82,8 +82,8 @@ public class CloudFoundryConnectorMysqlServiceTest extends AbstractCloudFoundryC
MysqlServiceInfo info1 = (MysqlServiceInfo) getServiceInfo(serviceInfos, "mysql-1");
MysqlServiceInfo info2 = (MysqlServiceInfo) getServiceInfo(serviceInfos, "mysql-2");
assertNotNull(info1);
assertNotNull(info2);
assertServiceFoundOfType(info1, MysqlServiceInfo.class);
assertServiceFoundOfType(info2, MysqlServiceInfo.class);
assertEquals(getJdbcUrl("mysql", name1), info1.getJdbcUrl());
assertEquals(getJdbcUrl("mysql", name2), info2.getJdbcUrl());
}

View File

@@ -27,7 +27,7 @@ public class CloudFoundryConnectorOracleServiceTest extends AbstractUserProvided
List<ServiceInfo> serviceInfos = testCloudConnector.getServiceInfos();
OracleServiceInfo info = (OracleServiceInfo) getServiceInfo(serviceInfos, SERVICE_NAME);
assertNotNull(info);
assertServiceFoundOfType(info, OracleServiceInfo.class);
assertEquals(getOracleJdbcUrl(INSTANCE_NAME), info.getJdbcUrl());
}

View File

@@ -1,7 +1,6 @@
package org.springframework.cloud.cloudfoundry;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.when;
import java.util.List;
@@ -27,8 +26,9 @@ public class CloudFoundryConnectorPostgresqlServiceTest extends AbstractCloudFou
List<ServiceInfo> serviceInfos = testCloudConnector.getServiceInfos();
PostgresqlServiceInfo info1 = (PostgresqlServiceInfo) getServiceInfo(serviceInfos, "postgresql-1");
PostgresqlServiceInfo info2 = (PostgresqlServiceInfo) getServiceInfo(serviceInfos, "postgresql-2");
assertNotNull(info1);
assertNotNull(info2);
assertServiceFoundOfType(info1, PostgresqlServiceInfo.class);
assertServiceFoundOfType(info2, PostgresqlServiceInfo.class);
assertEquals(getJdbcUrl("postgres", name1), info1.getJdbcUrl());
assertEquals(getJdbcUrl("postgres", name2), info2.getJdbcUrl());
}
@@ -45,8 +45,9 @@ public class CloudFoundryConnectorPostgresqlServiceTest extends AbstractCloudFou
List<ServiceInfo> serviceInfos = testCloudConnector.getServiceInfos();
PostgresqlServiceInfo info1 = (PostgresqlServiceInfo) getServiceInfo(serviceInfos, "postgresql-1");
PostgresqlServiceInfo info2 = (PostgresqlServiceInfo) getServiceInfo(serviceInfos, "postgresql-2");
assertNotNull(info1);
assertNotNull(info2);
assertServiceFoundOfType(info1, PostgresqlServiceInfo.class);
assertServiceFoundOfType(info2, PostgresqlServiceInfo.class);
assertEquals(getJdbcUrl("postgres", name1), info1.getJdbcUrl());
assertEquals(getJdbcUrl("postgres", name2), info2.getJdbcUrl());
}

View File

@@ -1,12 +1,12 @@
package org.springframework.cloud.cloudfoundry;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.when;
import java.util.List;
import org.junit.Test;
import org.springframework.cloud.service.ServiceInfo;
import org.springframework.cloud.service.common.RedisServiceInfo;
/**
*
@@ -22,8 +22,8 @@ public class CloudFoundryConnectorRedisServiceTest extends AbstractCloudFoundryC
getRedisServicePayload("redis-2", hostname, port, password, "redis-db")));
List<ServiceInfo> serviceInfos = testCloudConnector.getServiceInfos();
assertNotNull(getServiceInfo(serviceInfos, "redis-1"));
assertNotNull(getServiceInfo(serviceInfos, "redis-2"));
assertServiceFoundOfType(serviceInfos, "redis-1", RedisServiceInfo.class);
assertServiceFoundOfType(serviceInfos, "redis-2", RedisServiceInfo.class);
}
@Test
@@ -34,8 +34,8 @@ public class CloudFoundryConnectorRedisServiceTest extends AbstractCloudFoundryC
getRedisServicePayloadNoLabelNoTags("redis-2", hostname, port, password, "redis-db")));
List<ServiceInfo> serviceInfos = testCloudConnector.getServiceInfos();
assertNotNull(getServiceInfo(serviceInfos, "redis-1"));
assertNotNull(getServiceInfo(serviceInfos, "redis-2"));
assertServiceFoundOfType(serviceInfos, "redis-1", RedisServiceInfo.class);
assertServiceFoundOfType(serviceInfos, "redis-2", RedisServiceInfo.class);
}
private String getRedisServicePayload(String serviceName,

View File

@@ -28,7 +28,7 @@ public class CloudFoundryConnectorSmtpServiceTest extends AbstractCloudFoundryCo
assertEquals(hostname, smptServiceInfo.getHost());
assertEquals(587, smptServiceInfo.getPort());
assertEquals(username, smptServiceInfo.getUserName());
assertEquals(password, smptServiceInfo.getPassword());
assertEquals(password, smptServiceInfo.getPassword());
}
private String getSmtpServicePayload(String serviceName, String hostname,

View File

@@ -0,0 +1,6 @@
{
"name":"$serviceName",
"credentials":{
"uri": "amqps://$username:$password@$hostname/$virtualHost"
}
}

View File

@@ -7,6 +7,7 @@
"port": "$port",
"username": "$user",
"password": "$password",
"name": "$name"
"name": "$name",
"integer": 123
}
}

View File

@@ -2,24 +2,30 @@ package org.springframework.cloud.service;
import org.springframework.cloud.ServiceInfoCreator;
public abstract class UriBasedServiceInfoCreator<SI extends ServiceInfo> implements
ServiceInfoCreator<ServiceInfo, UriBasedServiceData> {
public abstract class UriBasedServiceInfoCreator<SI extends ServiceInfo>
implements ServiceInfoCreator<ServiceInfo, UriBasedServiceData> {
private final String uriScheme;
private final String[] uriSchemes;
public UriBasedServiceInfoCreator(String uriScheme) {
this.uriScheme = uriScheme;
}
public UriBasedServiceInfoCreator(String... uriSchemes) {
this.uriSchemes = uriSchemes;
}
@Override
public boolean accept(UriBasedServiceData serviceData) {
return serviceData.getUri().toString().startsWith(uriScheme + "://");
}
@Override
public boolean accept(UriBasedServiceData serviceData) {
String uriString = serviceData.getUri();
for (String uriScheme : uriSchemes) {
if (uriString.startsWith(uriScheme + "://")) {
return true;
}
}
return false;
}
public abstract SI createServiceInfo(String id, String uri);
public abstract SI createServiceInfo(String id, String uri);
@Override
public SI createServiceInfo(UriBasedServiceData serviceData) {
return createServiceInfo(serviceData.getKey(), serviceData.getUri());
}
@Override
public SI createServiceInfo(UriBasedServiceData serviceData) {
return createServiceInfo(serviceData.getKey(), serviceData.getUri());
}
}

View File

@@ -9,18 +9,20 @@ import org.springframework.cloud.util.UriInfo;
* Information to access RabbitMQ service.
*
* @author Ramnivas Laddad
* @author Scott Frederick
*
*/
@ServiceLabel("rabbitmq")
public class AmqpServiceInfo extends UriBasedServiceInfo {
public static final String URI_SCHEME = "amqp";
public static final String AMQP_SCHEME = "amqp";
public static final String AMQPS_SCHEME = "amqps";
public AmqpServiceInfo(String id, String host, int port, String username, String password, String virtualHost) {
super(id, URI_SCHEME, host, port, username, password, virtualHost);
super(id, AMQP_SCHEME, host, port, username, password, virtualHost);
}
public AmqpServiceInfo(String id, String uri) throws CloudException {
public AmqpServiceInfo(String id, String uri) throws CloudException {
super(id, uri);
}
@@ -32,36 +34,27 @@ public class AmqpServiceInfo extends UriBasedServiceInfo {
@Override
protected UriInfo validateAndCleanUriInfo(UriInfo uriInfo) {
if (uriInfo.getScheme() == null) {
throw new IllegalArgumentException("missing scheme in amqp URI: " + uriInfo);
throw new IllegalArgumentException("Missing scheme in amqp URI: " + uriInfo);
}
if (uriInfo.getHost() == null) {
throw new IllegalArgumentException("missing authority in amqp URI: " + uriInfo);
throw new IllegalArgumentException("Missing authority in amqp URI: " + uriInfo);
}
int port = uriInfo.getPort();
if (port == -1) {
port = 5672;
}
String userName = uriInfo.getUserName();
String password = uriInfo.getPassword();
if (userName == null || password == null) {
throw new IllegalArgumentException("missing userinfo in amqp URI: " + uriInfo);
if (uriInfo.getUserName() == null || uriInfo.getPassword() == null) {
throw new IllegalArgumentException("Missing userinfo in amqp URI: " + uriInfo);
}
String path = uriInfo.getPath();
if (path == null) {
// The RabbitMQ default vhost
path = "/";
throw new IllegalArgumentException("Missing virtual host in amqp URI: " + uriInfo);
} else {
// Check that the path only has a single segment. As we have an authority component
// in the URI, paths always begin with a slash.
if (path.indexOf('/') != -1) {
throw new IllegalArgumentException("multiple segments in path of amqp URI: " + uriInfo);
throw new IllegalArgumentException("Multiple segments in path of amqp URI: " + uriInfo);
}
}
return new UriInfo(uriInfo.getScheme(), uriInfo.getHost(), port, uriInfo.getUserName(), uriInfo.getPassword(), path);
return uriInfo;
}
}

View File

@@ -11,10 +11,10 @@ import org.springframework.cloud.service.ServiceInfo.ServiceLabel;
@ServiceLabel("mongo")
public class MongoServiceInfo extends UriBasedServiceInfo {
public static final String URI_SCHEME = "mongodb";
public static final String MONGODB_SCHEME = "mongodb";
public MongoServiceInfo(String id, String host, int port, String username, String password, String db) {
super(id, URI_SCHEME, host, port, username, password, db);
super(id, MONGODB_SCHEME, host, port, username, password, db);
}
public MongoServiceInfo(String id, String uri) {

View File

@@ -10,11 +10,11 @@ import org.springframework.cloud.service.ServiceInfo.ServiceLabel;
@ServiceLabel("mysql")
public class MysqlServiceInfo extends RelationalServiceInfo {
public static final String JDBC_URL_TYPE = "mysql";
private static final String JDBC_URL_TYPE = "mysql";
public static final String URI_SCHEME = JDBC_URL_TYPE;
public static final String MYSQL_SCHEME = JDBC_URL_TYPE;
public MysqlServiceInfo(String id, String url) {
super(id, url, URI_SCHEME);
super(id, url, MYSQL_SCHEME);
}
}

View File

@@ -5,9 +5,9 @@ import org.springframework.cloud.service.ServiceInfo;
@ServiceInfo.ServiceLabel("oracle")
public class OracleServiceInfo extends RelationalServiceInfo {
public static final String JDBC_URL_TYPE = "oracle";
private static final String JDBC_URL_TYPE = "oracle";
public static final String URI_SCHEME = JDBC_URL_TYPE;
public static final String ORACLE_SCHEME = JDBC_URL_TYPE;
public OracleServiceInfo(String id, String url) {
super(id, url, JDBC_URL_TYPE);

View File

@@ -11,9 +11,9 @@ import org.springframework.cloud.service.ServiceInfo.ServiceLabel;
@ServiceLabel("postgresql")
public class PostgresqlServiceInfo extends RelationalServiceInfo {
public static final String JDBC_URL_TYPE = "postgresql";
private static final String JDBC_URL_TYPE = "postgresql";
public static final String URI_SCHEME = "postgres";
public static final String POSTGRES_SCHEME = "postgres";
public PostgresqlServiceInfo(String id, String url) {
super(id, url, JDBC_URL_TYPE);

View File

@@ -11,10 +11,10 @@ import org.springframework.cloud.service.ServiceInfo.ServiceLabel;
@ServiceLabel("redis")
public class RedisServiceInfo extends UriBasedServiceInfo {
public static final String URI_SCHEME = "redis";
public static final String REDIS_SCHEME = "redis";
public RedisServiceInfo(String id, String host, int port, String password) {
super(id, URI_SCHEME, host, port, null, password, null);
super(id, REDIS_SCHEME, host, port, null, password, null);
}
public RedisServiceInfo(String id, String uri) {

View File

@@ -4,10 +4,10 @@ import org.springframework.cloud.service.UriBasedServiceInfo;
public class SmtpServiceInfo extends UriBasedServiceInfo {
public static final String URI_SCHEME = "smtp";
public static final String SMTP_SCHEME = "smtp";
public SmtpServiceInfo(String id, String host, int port, String username, String password) {
super(id, URI_SCHEME, host, port, username, password, "");
super(id, SMTP_SCHEME, host, port, username, password, "");
}
public SmtpServiceInfo(String id, String url) {

View File

@@ -10,7 +10,7 @@ import org.springframework.cloud.service.common.AmqpServiceInfo;
public class AmqpServiceInfoCreator extends HerokuServiceInfoCreator<AmqpServiceInfo> {
public AmqpServiceInfoCreator() {
super(AmqpServiceInfo.URI_SCHEME);
super(AmqpServiceInfo.AMQP_SCHEME, AmqpServiceInfo.AMQPS_SCHEME);
}
@Override

View File

@@ -10,8 +10,8 @@ import org.springframework.cloud.service.UriBasedServiceInfoCreator;
*/
public abstract class HerokuServiceInfoCreator<SI extends ServiceInfo> extends UriBasedServiceInfoCreator<SI> {
public HerokuServiceInfoCreator(String uriScheme) {
super(uriScheme);
public HerokuServiceInfoCreator(String... uriSchemes) {
super(uriSchemes);
}
/**

View File

@@ -10,7 +10,7 @@ import org.springframework.cloud.service.common.MongoServiceInfo;
public class MongoServiceInfoCreator extends HerokuServiceInfoCreator<MongoServiceInfo> {
public MongoServiceInfoCreator() {
super(MongoServiceInfo.URI_SCHEME);
super(MongoServiceInfo.MONGODB_SCHEME);
}
@Override

View File

@@ -10,7 +10,7 @@ import org.springframework.cloud.service.common.MysqlServiceInfo;
public class MysqlServiceInfoCreator extends RelationalServiceInfoCreator<MysqlServiceInfo> {
public MysqlServiceInfoCreator() {
super(MysqlServiceInfo.URI_SCHEME);
super(MysqlServiceInfo.MYSQL_SCHEME);
}
@Override

View File

@@ -10,7 +10,7 @@ import org.springframework.cloud.service.common.PostgresqlServiceInfo;
public class PostgresqlServiceInfoCreator extends RelationalServiceInfoCreator<PostgresqlServiceInfo> {
public PostgresqlServiceInfoCreator() {
super(PostgresqlServiceInfo.URI_SCHEME);
super(PostgresqlServiceInfo.POSTGRES_SCHEME);
}
@Override

View File

@@ -10,7 +10,7 @@ import org.springframework.cloud.service.common.RedisServiceInfo;
public class RedisServiceInfoCreator extends HerokuServiceInfoCreator<RedisServiceInfo> {
public RedisServiceInfoCreator() {
super(RedisServiceInfo.URI_SCHEME);
super(RedisServiceInfo.REDIS_SCHEME);
}
@Override

View File

@@ -19,7 +19,7 @@ import org.springframework.cloud.service.common.MysqlServiceInfo;
*/
public class HerokuConnectorMysqlServiceTest extends AbstractHerokuConnectorRelationalServiceTest {
public HerokuConnectorMysqlServiceTest() {
super(MysqlServiceInfo.URI_SCHEME);
super(MysqlServiceInfo.MYSQL_SCHEME);
}
@Test

View File

@@ -19,7 +19,7 @@ import org.springframework.cloud.service.common.PostgresqlServiceInfo;
*/
public class HerokuConnectorPostgresqlServiceTest extends AbstractHerokuConnectorRelationalServiceTest {
public HerokuConnectorPostgresqlServiceTest() {
super(PostgresqlServiceInfo.URI_SCHEME);
super(PostgresqlServiceInfo.POSTGRES_SCHEME);
}
@Test

View File

@@ -10,7 +10,7 @@ import org.springframework.cloud.service.common.AmqpServiceInfo;
public class AmqpServiceInfoCreator extends LocalConfigServiceInfoCreator<AmqpServiceInfo>{
public AmqpServiceInfoCreator() {
super(AmqpServiceInfo.URI_SCHEME);
super(AmqpServiceInfo.AMQP_SCHEME, AmqpServiceInfo.AMQPS_SCHEME);
}
@Override

View File

@@ -5,7 +5,7 @@ import org.springframework.cloud.service.UriBasedServiceInfoCreator;
public abstract class LocalConfigServiceInfoCreator<SI extends ServiceInfo> extends UriBasedServiceInfoCreator<SI> {
protected LocalConfigServiceInfoCreator(String uriScheme) {
super(uriScheme);
protected LocalConfigServiceInfoCreator(String... uriSchemes) {
super(uriSchemes);
}
}

View File

@@ -10,7 +10,7 @@ import org.springframework.cloud.service.common.MongoServiceInfo;
public class MongoServiceInfoCreator extends LocalConfigServiceInfoCreator<MongoServiceInfo>{
public MongoServiceInfoCreator() {
super(MongoServiceInfo.URI_SCHEME);
super(MongoServiceInfo.MONGODB_SCHEME);
}
@Override

View File

@@ -10,7 +10,7 @@ import org.springframework.cloud.service.common.MysqlServiceInfo;
public class MysqlServiceInfoCreator extends LocalConfigServiceInfoCreator<MysqlServiceInfo>{
public MysqlServiceInfoCreator() {
super(MysqlServiceInfo.URI_SCHEME);
super(MysqlServiceInfo.MYSQL_SCHEME);
}
@Override

View File

@@ -10,7 +10,7 @@ import org.springframework.cloud.service.common.PostgresqlServiceInfo;
public class PostgresqlServiceInfoCreator extends LocalConfigServiceInfoCreator<PostgresqlServiceInfo>{
public PostgresqlServiceInfoCreator() {
super(PostgresqlServiceInfo.URI_SCHEME);
super(PostgresqlServiceInfo.POSTGRES_SCHEME);
}
@Override

View File

@@ -10,7 +10,7 @@ import org.springframework.cloud.service.common.RedisServiceInfo;
public class RedisServiceInfoCreator extends LocalConfigServiceInfoCreator<RedisServiceInfo>{
public RedisServiceInfoCreator() {
super(RedisServiceInfo.URI_SCHEME);
super(RedisServiceInfo.REDIS_SCHEME);
}
@Override

View File

@@ -13,7 +13,7 @@ import org.springframework.cloud.service.common.AmqpServiceInfo;
public class RabbitServiceInfoTest {
@Test
public void uriBasedParsing() {
AmqpServiceInfo serviceInfo = new AmqpServiceInfo("id", "amqp://myuser:mypass@myhost:12345/myvhost");
AmqpServiceInfo serviceInfo = new AmqpServiceInfo("id", "amqp://myuser:mypass@myhost:12345/myvhost");
assertEquals("myhost", serviceInfo.getHost());
assertEquals(12345, serviceInfo.getPort());
@@ -27,6 +27,7 @@ public class RabbitServiceInfoTest {
new AmqpServiceInfo("id", "://myuser:mypass@:12345/myvhost");
}
@Test
public void amqpsSchemeAccepted() {
AmqpServiceInfo serviceInfo = new AmqpServiceInfo("id", "amqps://myuser:mypass@myhost:12345/myvhost");
assertEquals("amqps", serviceInfo.getScheme());
@@ -37,12 +38,6 @@ public class RabbitServiceInfoTest {
new AmqpServiceInfo("id", "amqp://myuser:mypass@:12345/myvhost");
}
@Test
public void missingPort() {
AmqpServiceInfo serviceInfo = new AmqpServiceInfo("id", "amqp://myuser:mypass@myhost/myvhost");
assertEquals(5672, serviceInfo.getPort()); // the default port is 5672
}
@Test(expected=IllegalArgumentException.class)
public void badUserInfo() {
new AmqpServiceInfo("id", "amqp://myuser@myhost/myvhost");
@@ -53,10 +48,9 @@ public class RabbitServiceInfoTest {
new AmqpServiceInfo("id", "amqp://myhost:12345/myvhost");
}
@Test
@Test(expected=IllegalArgumentException.class)
public void missingVirtualHost() {
AmqpServiceInfo serviceInfo = new AmqpServiceInfo("id", "amqp://myuser:mypass@myhost:12345");
assertEquals("/", serviceInfo.getVirtualHost());
new AmqpServiceInfo("id", "amqp://myuser:mypass@myhost:12345");
}
@Test(expected=IllegalArgumentException.class)