Handle missing or empty VCAP_SERVICES environment variable in CloudFoundryConnector without throwing NPE.

This commit is contained in:
Scott Frederick
2016-10-21 16:13:57 -05:00
parent 2d763feb82
commit ff2b601d18
3 changed files with 46 additions and 21 deletions

View File

@@ -24,8 +24,7 @@ public class CloudFoundryConnector extends AbstractCloudConnector<Map<String,Obj
private ObjectMapper objectMapper = new ObjectMapper();
private EnvironmentAccessor environment = new EnvironmentAccessor();
private ApplicationInstanceInfoCreator applicationInstanceInfoCreator
= new ApplicationInstanceInfoCreator();
private ApplicationInstanceInfoCreator applicationInstanceInfoCreator = new ApplicationInstanceInfoCreator();
@SuppressWarnings({ "unchecked", "rawtypes" })
public CloudFoundryConnector() {
@@ -56,7 +55,7 @@ public class CloudFoundryConnector extends AbstractCloudConnector<Map<String,Obj
/**
* Return object representation of the VCAP_SERIVCES environment variable
* Return object representation of the VCAP_SERVICES environment variable
* <p>
* Returns a list whose element is a map with service attributes.
* </p>
@@ -67,15 +66,14 @@ public class CloudFoundryConnector extends AbstractCloudConnector<Map<String,Obj
String servicesString = environment.getEnvValue("VCAP_SERVICES");
Map<String, List<Map<String,Object>>> rawServices = new HashMap<String, List<Map<String,Object>>>();
if (servicesString == null || servicesString.length() == 0) {
rawServices = new HashMap<String, List<Map<String,Object>>>();
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<Map<String,Object>> flatServices = new ArrayList<Map<String,Object>>();
for (Map.Entry<String, List<Map<String,Object>>> entry : rawServices.entrySet()) {
flatServices.addAll(entry.getValue());

View File

@@ -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<ServiceInfo> serviceInfos = testCloudConnector.getServiceInfos();
assertNotNull(serviceInfos);
assertEquals(0, serviceInfos.size());
}
@Test
public void servicesInfosWithEmptyServices() {
when(mockEnvironment.getEnvValue("VCAP_SERVICES")).thenReturn("");
List<ServiceInfo> 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();
}
}

View File

@@ -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)) {