Add functionality to target and extract VCAP environment variable configuration for a specific CloudCache Service Instance in PCF.

Resolves gh-33.
This commit is contained in:
John Blum
2019-06-28 13:02:52 -07:00
parent 6e81c007e6
commit 621982f472
2 changed files with 196 additions and 14 deletions

View File

@@ -38,7 +38,9 @@ import org.springframework.geode.core.env.support.CloudCacheService;
import org.springframework.geode.core.env.support.Service;
import org.springframework.geode.core.env.support.User;
import org.springframework.geode.core.util.ObjectUtils;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
@@ -128,7 +130,7 @@ public class VcapPropertySource extends PropertySource<EnumerablePropertySource<
VCAP_PROPERTY_SOURCE_NAME));
}
private Predicate<Object> vcapServicePredicate;
private Predicate<String> vcapServicePredicate;
/**
* Constructs a new {@link PropertySource} from the existing, required {@link EnumerablePropertySource} instance
@@ -171,7 +173,24 @@ public class VcapPropertySource extends PropertySource<EnumerablePropertySource<
}
public Set<String> findAllVcapServicesProperties() {
return findAllPropertiesByNameMatching(VCAP_SERVICES_PROPERTIES_PREDICATE);
return findTargetVcapServiceProperties(VCAP_SERVICES_PROPERTIES_PREDICATE);
}
public Set<String> findTargetVcapServiceProperties(Predicate<String> vcapServicePropertiesPredicate) {
return findAllPropertiesByNameMatching(filterByVcapServicePropertiesPredicate(vcapServicePropertiesPredicate));
}
private Predicate<String> filterByVcapServicePropertiesPredicate(Predicate<String> vcapServicePropertiesPredicate) {
return isValid(vcapServicePropertiesPredicate)
? VCAP_SERVICES_PROPERTIES_PREDICATE.and(vcapServicePropertiesPredicate)
: VCAP_SERVICES_PROPERTIES_PREDICATE;
}
private boolean isValid(Predicate<String> vcapServicePropertiesPredicate) {
return vcapServicePropertiesPredicate != null
&& vcapServicePropertiesPredicate != VCAP_SERVICES_PROPERTIES_PREDICATE;
}
public Optional<CloudCacheService> findFirstCloudCacheService() {
@@ -208,11 +227,10 @@ public class VcapPropertySource extends PropertySource<EnumerablePropertySource<
public Optional<String> findFirstCloudCacheServiceName() {
Iterable<String> vcapServicesProperties = findAllVcapServicesProperties();
Iterable<String> vcapServicesProperties = findTargetVcapServiceProperties(getVcapServicePredicate());
Predicate<Object> vcapServicePredicate = resolveVcapServicePredicate();
return findAllPropertiesByValueMatching(vcapServicesProperties, vcapServicePredicate).stream()
return findAllPropertiesByValueMatching(vcapServicesProperties, CLOUD_CACHE_AND_GEMFIRE_SERVICE_PREDICATE)
.stream()
.filter(propertyName -> propertyName.endsWith(".tags"))
.map(propertyName -> propertyName.substring(VCAP_SERVICES_PROPERTY.length()))
.map(propertyName -> propertyName.substring(0, propertyName.indexOf(".")))
@@ -259,26 +277,41 @@ public class VcapPropertySource extends PropertySource<EnumerablePropertySource<
return Optional.empty();
}
protected Predicate<Object> resolveVcapServicePredicate() {
return this.vcapServicePredicate != null
? this.vcapServicePredicate
: CLOUD_CACHE_AND_GEMFIRE_SERVICE_PREDICATE;
}
@Nullable
@Override
public Object getProperty(String name) {
return getSource().getProperty(name);
}
@NonNull
protected Predicate<String> getVcapServicePredicate() {
return this.vcapServicePredicate != null
? this.vcapServicePredicate
: propertyName -> true;
}
@Override
@SuppressWarnings("all")
public Iterator<String> iterator() {
return Collections.unmodifiableList(Arrays.asList(getSource().getPropertyNames())).iterator();
}
public VcapPropertySource withVcapServicePredicate(Predicate<Object> vcapServicePredicate) {
@NonNull
public VcapPropertySource withVcapServiceName(@NonNull String serviceName) {
Assert.hasText(serviceName, "Service name is required");
String resolvedServiceName = StringUtils.trimAllWhitespace(serviceName);
Predicate<String> vcapServiceNamePredicate = propertyName ->
propertyName.startsWith(String.format("%1$s%2$s.", VCAP_SERVICES_PROPERTY, resolvedServiceName));
return withVcapServicePredicate(vcapServiceNamePredicate);
}
@NonNull
public VcapPropertySource withVcapServicePredicate(@Nullable Predicate<String> vcapServicePredicate) {
this.vcapServicePredicate = vcapServicePredicate;

View File

@@ -16,6 +16,7 @@
package org.springframework.geode.core.env;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
@@ -32,6 +33,7 @@ import java.util.Arrays;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.function.Predicate;
import org.junit.Test;
@@ -352,6 +354,62 @@ public class VcapPropertySourceUnitTests {
verify(mockPropertySource, times(1)).getPropertyNames();
}
@Test
public void findTargetVcapApplicationPropertiesIsSuccessful() {
EnumerablePropertySource mockPropertySource = mock(EnumerablePropertySource.class);
String[] propertyNames = {
"vcap.services.jblum-pcc.credentials.locators",
"vcap.services.jblum-pcc.credentials.users",
"vcap.application.host",
"vcap.services.test-pcc.credentials.locators",
"vcap.services.test-pcc.credentials.users",
"vcap.application.name",
"vcap.services.jblum-pcc.name",
"vcap.services.test-pcc.name",
"vcap.application.port",
"vcap.services.jblum-pcc.plan",
"vcap.application.space_name",
"vcap.services.test-pcc.plan",
"vcap.application.uris",
"vcap.services.jblum-pcc.tags",
"vcap.services.test-pcc.tags"
};
when(mockPropertySource.containsProperty(anyString())).thenAnswer(invocation ->
Arrays.asList(propertyNames).contains(invocation.getArgument(0, String.class)));
when(mockPropertySource.getName()).thenReturn("vcap");
when(mockPropertySource.getPropertyNames()).thenReturn(propertyNames);
VcapPropertySource propertySource = VcapPropertySource.from(mockPropertySource);
assertThat(propertySource).isNotNull();
assertThat(propertySource.getSource()).isEqualTo(mockPropertySource);
Predicate<String> testPccServicePredicate = propertyName -> propertyName.contains("test-pcc");
Set<String> testPccServicePropertyNames =
propertySource.findTargetVcapServiceProperties(testPccServicePredicate);
assertThat(testPccServicePropertyNames).isNotNull();
assertThat(testPccServicePropertyNames)
.describedAs("PCC Service Properties [%s]", testPccServicePropertyNames)
.hasSize(5);
assertThat(testPccServicePropertyNames)
.containsExactlyInAnyOrder("vcap.services.test-pcc.credentials.locators",
"vcap.services.test-pcc.credentials.users", "vcap.services.test-pcc.name",
"vcap.services.test-pcc.plan", "vcap.services.test-pcc.tags");
verify(mockPropertySource, times(1)).getName();
verify(mockPropertySource, times(1)).containsProperty(eq("vcap.application.name"));
verify(mockPropertySource, times(1)).containsProperty(eq("vcap.application.uris"));
verify(mockPropertySource, times(1)).getPropertyNames();
}
@Test
public void findFirstCloudCacheServiceNameReturnsOptionalOfServiceName() {
@@ -602,4 +660,95 @@ public class VcapPropertySourceUnitTests {
assertThat(majorTom.getPassword().orElse(null)).isEqualTo("s3cUr3");
assertThat(majorTom.getRole().map(User.Role::isClusterOperator).orElse(false)).isTrue();
}
@Test
@SuppressWarnings("unchecked")
public void withMockVcapServicePredicateConfiguresVcapServicePredicateReturnsThis() {
Properties properties = new Properties();
properties.setProperty("vcap.application.name", "withVcapServicePredicateConfiguresVcapServicePredicateReturnsThis");
properties.setProperty("vcap.application.uris", "{}");
Predicate<String> mockVcapServicePredicate = mock(Predicate.class);
VcapPropertySource propertySource = VcapPropertySource.from(properties);
assertThat(propertySource).isNotNull();
assertThat(propertySource.getVcapServicePredicate()).isNotEqualTo(mockVcapServicePredicate);
assertThat(propertySource.withVcapServicePredicate(mockVcapServicePredicate)).isEqualTo(propertySource);
assertThat(propertySource.getVcapServicePredicate()).isEqualTo(mockVcapServicePredicate);
}
@Test
public void withNullVcapServicePredicateAllowsAndSetsNullReturnsThis() {
Properties properties = new Properties();
properties.setProperty("vcap.application.name", "withNullVcapServicePredicateAllowsAndSetsNullReturnsThis");
properties.setProperty("vcap.application.uris", "{}");
VcapPropertySource propertySource = VcapPropertySource.from(properties);
assertThat(propertySource).isNotNull();
assertThat(propertySource.getVcapServicePredicate()).isNotNull();
assertThat(propertySource.withVcapServicePredicate(null)).isEqualTo(propertySource);
assertThat(propertySource.getVcapServicePredicate()).isNotNull();
}
private void testWithInvalidVcapServiceName(String serviceName) {
Properties properties = new Properties();
properties.setProperty("vcap.application.name", "testWithInvalidVcapServiceName");
properties.setProperty("vcap.application.uris", "{}");
VcapPropertySource propertySource = VcapPropertySource.from(properties);
assertThat(propertySource).isNotNull();
propertySource.withVcapServiceName(serviceName);
fail("Expected VcapPropertySource.withServiceName(%s) to fail", serviceName);
}
@Test(expected = IllegalArgumentException.class)
public void withBlankVcapServiceNameThrowsIllegalArgumentException() {
testWithInvalidVcapServiceName(" ");
}
@Test(expected = IllegalArgumentException.class)
public void withEmptyVcapServiceNameThrowsIllegalArgumentException() {
testWithInvalidVcapServiceName("");
}
@Test(expected = IllegalArgumentException.class)
public void withNullVcapServiceNameThrowsIllegalArgumentException() {
testWithInvalidVcapServiceName(null);
}
@Test
public void withValidVcapSeviceNameConfiguresVcapServicePredicateReturnsThis() {
Properties properties = new Properties();
properties.setProperty("vcap.application.name", "withValidVcapSeviceNameConfiguresVcapServicePredicateReturnsThis");
properties.setProperty("vcap.application.uris", "{}");
VcapPropertySource propertySource = VcapPropertySource.from(properties);
assertThat(propertySource).isNotNull();
assertThat(propertySource.withVcapServiceName("test-pcc")).isEqualTo(propertySource);
Predicate<String> vcapServicePredicate = propertySource.getVcapServicePredicate();
assertThat(vcapServicePredicate).isNotNull();
assertThat(vcapServicePredicate.test("vcap.services.test-pcc.name")).isTrue();
assertThat(vcapServicePredicate.test("vcap.services.test-pcc.tags")).isTrue();
assertThat(vcapServicePredicate.test("vcap.application.test-pcc.name")).isFalse();
assertThat(vcapServicePredicate.test("vcap.test-pcc.name")).isFalse();
assertThat(vcapServicePredicate.test("test-pcc.name")).isFalse();
assertThat(vcapServicePredicate.test("test-pcc")).isFalse();
assertThat(vcapServicePredicate.test("vcap.services.junk-pcc.name")).isFalse();
}
}