From ff2b601d18cf5f3779014599836c339551e21cc9 Mon Sep 17 00:00:00 2001 From: Scott Frederick Date: Fri, 21 Oct 2016 16:13:57 -0500 Subject: [PATCH] Handle missing or empty VCAP_SERVICES environment variable in CloudFoundryConnector without throwing NPE. --- .../cloudfoundry/CloudFoundryConnector.java | 20 ++++++------- .../CloudFoundryConnectorApplicationTest.java | 28 ++++++++++++++++++- .../service/PooledServiceConnectorConfig.java | 19 +++++++------ 3 files changed, 46 insertions(+), 21 deletions(-) diff --git a/spring-cloud-cloudfoundry-connector/src/main/java/org/springframework/cloud/cloudfoundry/CloudFoundryConnector.java b/spring-cloud-cloudfoundry-connector/src/main/java/org/springframework/cloud/cloudfoundry/CloudFoundryConnector.java index da515e7..caf0d14 100644 --- a/spring-cloud-cloudfoundry-connector/src/main/java/org/springframework/cloud/cloudfoundry/CloudFoundryConnector.java +++ b/spring-cloud-cloudfoundry-connector/src/main/java/org/springframework/cloud/cloudfoundry/CloudFoundryConnector.java @@ -24,8 +24,7 @@ public class CloudFoundryConnector extends AbstractCloudConnector * Returns a list whose element is a map with service attributes. *

@@ -67,15 +66,14 @@ public class CloudFoundryConnector extends AbstractCloudConnector>> rawServices = new HashMap>>(); - if (servicesString == null || servicesString.length() == 0) { - rawServices = new HashMap>>(); + if (servicesString != null && servicesString.length() > 0) { + try { + rawServices = objectMapper.readValue(servicesString, Map.class); + } catch (Exception e) { + throw new CloudException(e); + } } - try { - rawServices = objectMapper.readValue(servicesString, Map.class); - } catch (Exception e) { - throw new CloudException(e); - } - + List> flatServices = new ArrayList>(); for (Map.Entry>> entry : rawServices.entrySet()) { flatServices.addAll(entry.getValue()); diff --git a/spring-cloud-cloudfoundry-connector/src/test/java/org/springframework/cloud/cloudfoundry/CloudFoundryConnectorApplicationTest.java b/spring-cloud-cloudfoundry-connector/src/test/java/org/springframework/cloud/cloudfoundry/CloudFoundryConnectorApplicationTest.java index 93b8c74..4a80358 100644 --- a/spring-cloud-cloudfoundry-connector/src/test/java/org/springframework/cloud/cloudfoundry/CloudFoundryConnectorApplicationTest.java +++ b/spring-cloud-cloudfoundry-connector/src/test/java/org/springframework/cloud/cloudfoundry/CloudFoundryConnectorApplicationTest.java @@ -2,16 +2,21 @@ package org.springframework.cloud.cloudfoundry; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.when; import java.util.Arrays; +import java.util.List; import org.junit.Test; +import org.springframework.cloud.CloudException; +import org.springframework.cloud.service.ServiceInfo; /** * * @author Ramnivas Laddad + * @author Scott Frederick * */ public class CloudFoundryConnectorApplicationTest extends AbstractCloudFoundryConnectorTest { @@ -49,6 +54,27 @@ public class CloudFoundryConnectorApplicationTest extends AbstractCloudFoundryCo return payload; } - + + @Test + public void servicesInfosWithNullServices() { + when(mockEnvironment.getEnvValue("VCAP_SERVICES")).thenReturn(null); + List serviceInfos = testCloudConnector.getServiceInfos(); + assertNotNull(serviceInfos); + assertEquals(0, serviceInfos.size()); + } + + @Test + public void servicesInfosWithEmptyServices() { + when(mockEnvironment.getEnvValue("VCAP_SERVICES")).thenReturn(""); + List serviceInfos = testCloudConnector.getServiceInfos(); + assertNotNull(serviceInfos); + assertEquals(0, serviceInfos.size()); + } + + @Test(expected = CloudException.class) + public void servicesInfosWithNonJsonServices() { + when(mockEnvironment.getEnvValue("VCAP_SERVICES")).thenReturn("some value"); + testCloudConnector.getServiceInfos(); + } } diff --git a/spring-cloud-spring-service-connector/src/main/java/org/springframework/cloud/service/PooledServiceConnectorConfig.java b/spring-cloud-spring-service-connector/src/main/java/org/springframework/cloud/service/PooledServiceConnectorConfig.java index 60c1efa..5eaf491 100644 --- a/spring-cloud-spring-service-connector/src/main/java/org/springframework/cloud/service/PooledServiceConnectorConfig.java +++ b/spring-cloud-spring-service-connector/src/main/java/org/springframework/cloud/service/PooledServiceConnectorConfig.java @@ -8,7 +8,6 @@ import org.springframework.util.StringUtils; * @author Ramnivas Laddad * @author Mark Fisher * @author Thomas Risberg - * */ public class PooledServiceConnectorConfig implements ServiceConnectorConfig { private PoolConfig poolConfig; @@ -63,27 +62,29 @@ public class PooledServiceConnectorConfig implements ServiceConnectorConfig { } // For commons-pool2 + /** * @return property corresponding to commons-pool {@code maxTotal} */ - public int getMaxTotal() { - return maxPoolSize; - } + public int getMaxTotal() { + return maxPoolSize; + } - /** + /** * @return property corresponding to DBCP {@code maxWait} */ public int getMaxWait() { return maxWaitTime; } - // For commons-pool2 + // For commons-pool2 + /** * @return property corresponding to commons-pool {@code maxWaitMillis} */ - public int getMaxWaitMillis() { - return maxWaitTime; - } + public int getMaxWaitMillis() { + return maxWaitTime; + } private void determinePoolSizeRange(String poolSize) { if (StringUtils.hasText(poolSize)) {