diff --git a/spring-cloud-heroku-connector/src/main/java/org/springframework/cloud/heroku/HerokuConnector.java b/spring-cloud-heroku-connector/src/main/java/org/springframework/cloud/heroku/HerokuConnector.java index 55d1cba6..6dd5a054 100644 --- a/spring-cloud-heroku-connector/src/main/java/org/springframework/cloud/heroku/HerokuConnector.java +++ b/spring-cloud-heroku-connector/src/main/java/org/springframework/cloud/heroku/HerokuConnector.java @@ -18,22 +18,21 @@ /** * Implementation of CloudConnector for Heroku - * + *

* Currently support Postgres (default provided), Mysql (Cleardb), MongoDb (MongoLab, MongoHQ, MongoSoup), * Redis (RedisToGo, RedisCloud, OpenRedis, RedisGreen), and AMQP (CloudAmqp). * * @author Ramnivas Laddad - * */ public class HerokuConnector extends AbstractCloudConnector { private EnvironmentAccessor environment = new EnvironmentAccessor(); private ApplicationInstanceInfoCreator applicationInstanceInfoCreator - = new ApplicationInstanceInfoCreator(environment); + = new ApplicationInstanceInfoCreator(environment); private List serviceEnvPrefixes; - @SuppressWarnings({ "unchecked", "rawtypes" }) + @SuppressWarnings({"unchecked", "rawtypes"}) public HerokuConnector() { super((Class) HerokuServiceInfoCreator.class); } @@ -60,15 +59,15 @@ void setCloudEnvironment(EnvironmentAccessor environment) { @Override protected void registerServiceInfoCreator(ServiceInfoCreator serviceInfoCreator) { - super.registerServiceInfoCreator(serviceInfoCreator); - HerokuServiceInfoCreator herokuServiceInfoCreator = (HerokuServiceInfoCreator)serviceInfoCreator; - String[] envPrefixes = herokuServiceInfoCreator.getEnvPrefixes(); - - // need to do this since this method gets called during construction and we cannot initialize serviceEnvPrefixes before this - if (serviceEnvPrefixes == null) { - serviceEnvPrefixes = new ArrayList(); - } - serviceEnvPrefixes.addAll(Arrays.asList(envPrefixes)); + super.registerServiceInfoCreator(serviceInfoCreator); + HerokuServiceInfoCreator herokuServiceInfoCreator = (HerokuServiceInfoCreator) serviceInfoCreator; + String[] envPrefixes = herokuServiceInfoCreator.getEnvPrefixes(); + + // need to do this since this method gets called during construction and we cannot initialize serviceEnvPrefixes before this + if (serviceEnvPrefixes == null) { + serviceEnvPrefixes = new ArrayList(); + } + serviceEnvPrefixes.addAll(Arrays.asList(envPrefixes)); } /** @@ -76,26 +75,27 @@ protected void registerServiceInfoCreator(ServiceInfoCreator * Returns map whose key is the env key and value is the associated url *

+ * * @return information about services bound to the app */ protected List getServicesData() { List serviceData = new ArrayList(); - Map env = environment.getEnv(); + Map env = environment.getEnv(); for (Map.Entry envEntry : env.entrySet()) { - for (String envPrefix : serviceEnvPrefixes) { - if (envEntry.getKey().startsWith(envPrefix)) { - serviceData.add(new UriBasedServiceData(envEntry.getKey(), envEntry.getValue())); - } - } + for (String envPrefix : serviceEnvPrefixes) { + if (envEntry.getKey().startsWith(envPrefix)) { + serviceData.add(new UriBasedServiceData(envEntry.getKey(), envEntry.getValue())); + } + } } return serviceData; } @Override - protected FallbackServiceInfoCreator getFallbackServiceInfoCreator() { + protected FallbackServiceInfoCreator getFallbackServiceInfoCreator() { return new FallbackBaseServiceInfoCreator(); } } \ No newline at end of file diff --git a/spring-cloud-heroku-connector/src/main/java/org/springframework/cloud/heroku/PostgresqlServiceInfoCreator.java b/spring-cloud-heroku-connector/src/main/java/org/springframework/cloud/heroku/PostgresqlServiceInfoCreator.java index e6d5ab2b..013e9a4f 100644 --- a/spring-cloud-heroku-connector/src/main/java/org/springframework/cloud/heroku/PostgresqlServiceInfoCreator.java +++ b/spring-cloud-heroku-connector/src/main/java/org/springframework/cloud/heroku/PostgresqlServiceInfoCreator.java @@ -20,6 +20,6 @@ public PostgresqlServiceInfo createServiceInfo(String id, String uri) { @Override public String[] getEnvPrefixes() { - return new String[]{"HEROKU_POSTGRESQL_"}; + return new String[]{"DATABASE_URL", "HEROKU_POSTGRESQL_"}; } } diff --git a/spring-cloud-heroku-connector/src/test/java/org/springframework/cloud/heroku/HerokuConnectorPostgresqlServiceTest.java b/spring-cloud-heroku-connector/src/test/java/org/springframework/cloud/heroku/HerokuConnectorPostgresqlServiceTest.java index 969f6f6e..d75d3b31 100644 --- a/spring-cloud-heroku-connector/src/test/java/org/springframework/cloud/heroku/HerokuConnectorPostgresqlServiceTest.java +++ b/spring-cloud-heroku-connector/src/test/java/org/springframework/cloud/heroku/HerokuConnectorPostgresqlServiceTest.java @@ -21,14 +21,23 @@ public HerokuConnectorPostgresqlServiceTest() { } @Test - public void postgresqlServiceCreation() { + public void postgresqlServiceCreationPrimary() { + assertPostgresServiceCreated("DATABASE_URL", "DATABASE"); + } + + @Test + public void postgresqlServiceCreationSecondary() { + assertPostgresServiceCreated("HEROKU_POSTGRESQL_YELLOW_URL", "HEROKU_POSTGRESQL_YELLOW"); + } + + private void assertPostgresServiceCreated(String envVarName, String serviceInstanceName) { Map env = new HashMap(); String postgresUrl = getRelationalServiceUrl("db"); - env.put("HEROKU_POSTGRESQL_YELLOW_URL", postgresUrl); + env.put(envVarName, postgresUrl); when(mockEnvironment.getEnv()).thenReturn(env); List serviceInfos = testCloudConnector.getServiceInfos(); - ServiceInfo serviceInfo = getServiceInfo(serviceInfos, "HEROKU_POSTGRESQL_YELLOW"); + ServiceInfo serviceInfo = getServiceInfo(serviceInfos, serviceInstanceName); assertNotNull(serviceInfo); assertTrue(serviceInfo instanceof PostgresqlServiceInfo); assertReleationServiceInfo((PostgresqlServiceInfo) serviceInfo, "db");