Prevent errors when only a JDBC URL is given in Cloud Foundry connectors.

This commit is contained in:
Scott Frederick
2015-08-21 12:00:01 -05:00
parent 6f1b17d12d
commit 86780fbc60
7 changed files with 68 additions and 6 deletions

View File

@@ -62,7 +62,13 @@ public abstract class RelationalServiceInfoCreator<SI extends RelationalServiceI
String database = (String) credentials.get("name");
uri = new UriInfo(getDefaultUriScheme(), host, port, username, password, database).toString();
if (host != null) {
uri = new UriInfo(getDefaultUriScheme(), host, port, username, password, database).toString();
}
}
if (uri == null) {
uri = jdbcUrl;
}
return createServiceInfo(id, uri, jdbcUrl);

View File

@@ -1,9 +1,9 @@
package org.springframework.cloud.cloudfoundry;
import org.mockito.internal.matchers.InstanceOf;
import org.springframework.cloud.service.ServiceInfo;
import org.springframework.cloud.service.common.RelationalServiceInfo;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
@@ -27,10 +27,16 @@ public abstract class AbstractCloudFoundryConnectorRelationalServiceTest extends
}
protected void assertJdbcUrlEqual(ServiceInfo serviceInfo, String scheme, String name) {
assertThat(serviceInfo, new InstanceOf(RelationalServiceInfo.class));
assertThat(serviceInfo, instanceOf(RelationalServiceInfo.class));
assertEquals(getJdbcUrl(scheme, name), ((RelationalServiceInfo) serviceInfo).getJdbcUrl());
}
protected void assertJdbcShemeSpecificPartEqual(ServiceInfo serviceInfo, String scheme, String name) {
assertThat(serviceInfo, instanceOf(RelationalServiceInfo.class));
String jdbcUrl = getJdbcUrl(scheme, name);
assertEquals(jdbcUrl.substring(5), ((RelationalServiceInfo) serviceInfo).getSchemeSpecificPart());
}
protected String getJdbcUrl(String databaseType, String name) {
// this should be cleaned up more broadly; pull into RelationalServiceInfo interface?
String jdbcUrlDatabaseType = databaseType;

View File

@@ -11,13 +11,13 @@ 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.service.UriBasedServiceInfo;
import org.springframework.cloud.util.EnvironmentAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
@@ -129,7 +129,7 @@ public abstract class AbstractCloudFoundryConnectorTest {
protected static void assertServiceFoundOfType(ServiceInfo serviceInfo, Class<? extends ServiceInfo> type) {
assertNotNull(serviceInfo);
assertThat(serviceInfo, new InstanceOf(type));
assertThat(serviceInfo, instanceOf(type));
}
protected static void assertServiceFoundOfType(List<ServiceInfo> serviceInfos, String serviceId, Class<? extends ServiceInfo> type) {
@@ -140,7 +140,7 @@ public abstract class AbstractCloudFoundryConnectorTest {
protected static void assertUriBasedServiceInfoFields(ServiceInfo serviceInfo,
String scheme, String host, int port,
String username, String password, String path) {
assertThat(serviceInfo, new InstanceOf(UriBasedServiceInfo.class));
assertThat(serviceInfo, instanceOf(UriBasedServiceInfo.class));
UriBasedServiceInfo info = (UriBasedServiceInfo) serviceInfo;

View File

@@ -127,6 +127,32 @@ public class CloudFoundryConnectorMysqlServiceTest extends AbstractCloudFoundryC
assertUriBasedServiceInfoFields(info2, MYSQL_SCHEME, hostname, port, username, password, name2);
}
@Test
public void mysqlServiceCreationWithJdbcUrlOnly() {
String name1 = "database-1";
String name2 = "database-2";
when(mockEnvironment.getEnvValue("VCAP_SERVICES"))
.thenReturn(getServicesPayload(
getMysqlServicePayloadWithJdbcUrlOnly("mysql-1", hostname, port, username, password, name1),
getMysqlServicePayloadWithJdbcUrlOnly("mysql-2", hostname, port, username, password, name2)));
List<ServiceInfo> serviceInfos = testCloudConnector.getServiceInfos();
ServiceInfo info1 = getServiceInfo(serviceInfos, "mysql-1");
ServiceInfo info2 = getServiceInfo(serviceInfos, "mysql-2");
assertServiceFoundOfType(info1, MysqlServiceInfo.class);
assertServiceFoundOfType(info2, MysqlServiceInfo.class);
assertJdbcUrlEqual(info1, MYSQL_SCHEME, name1);
assertJdbcUrlEqual(info2, MYSQL_SCHEME, name2);
assertUriBasedServiceInfoFields(info1, "jdbc", null, -1, null, null, null);
assertUriBasedServiceInfoFields(info2, "jdbc", null, -1, null, null, null);
assertJdbcShemeSpecificPartEqual(info1, MYSQL_SCHEME, name1);
assertJdbcShemeSpecificPartEqual(info2, MYSQL_SCHEME, name2);
}
private String getMysqlServicePayload(String serviceName,
String hostname, int port,
String user, String password, String name) {
@@ -161,4 +187,11 @@ public class CloudFoundryConnectorMysqlServiceTest extends AbstractCloudFoundryC
return getRelationalPayload("test-mysql-info-jdbc-url.json", serviceName,
hostname, port, user, password, name);
}
private String getMysqlServicePayloadWithJdbcUrlOnly(String serviceName,
String hostname, int port,
String user, String password, String name) {
return getRelationalPayload("test-mysql-info-jdbc-url-only.json", serviceName,
hostname, port, user, password, name);
}
}

View File

@@ -0,0 +1,6 @@
{
"name": "$serviceName",
"credentials": {
"jdbcUrl": "jdbc:mysql://$hostname:$port/$name?user=$user&password=$password"
}
}

View File

@@ -78,6 +78,11 @@ public abstract class UriBasedServiceInfo extends BaseServiceInfo {
return uriInfo.getScheme();
}
@ServiceProperty(category = "connection")
public String getSchemeSpecificPart() {
return uriInfo.getSchemeSpecificPart();
}
/**
* Validate the URI and clean it up by using defaults for any missing information, if possible.
*

View File

@@ -18,6 +18,7 @@ public class UriInfo {
private String password;
private String path;
private String query;
private String schemeSpecificPart;
private String uriString;
@@ -50,6 +51,7 @@ public class UriInfo {
this.port = uri.getPort();
this.path = parsePath(uri);
this.query = uri.getQuery();
this.schemeSpecificPart = uri.getSchemeSpecificPart();
String[] userinfo = parseUserinfo(uri);
this.userName = uriDecode(userinfo[0]);
@@ -84,6 +86,10 @@ public class UriInfo {
return query;
}
public String getSchemeSpecificPart() {
return schemeSpecificPart;
}
public URI getUri() {
try {
return new URI(uriString);