Skip to content

Commit bd408ce

Browse files
authored
CfEnv: allow passing in values of VCAP_SERVICES & VCAP_APPLICATION (#186)
This change makes it easier to test components using CfEnv to parse a given VCAP_SERVICES string.
1 parent 48f2ab5 commit bd408ce

File tree

2 files changed

+66
-39
lines changed

2 files changed

+66
-39
lines changed

java-cfenv/src/main/java/io/pivotal/cfenv/core/CfEnv.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,35 +39,37 @@ public class CfEnv {
3939
private CfApplication cfApplication;
4040

4141
public CfEnv() {
42+
this(System.getenv(VCAP_APPLICATION), System.getenv(VCAP_SERVICES));
43+
}
44+
45+
public CfEnv(String vcapApplicationJson, String vcapServicesJson) {
4246
/* TODO pick small json parser and package as a shadowed jar */
4347
ObjectMapper objectMapper = new ObjectMapper();
44-
parseVcapServices(objectMapper);
45-
parseVcapApplication(objectMapper);
48+
parseVcapServices(objectMapper, vcapServicesJson);
49+
parseVcapApplication(objectMapper, vcapApplicationJson);
4650
}
4751

48-
private void parseVcapApplication(ObjectMapper objectMapper) {
52+
private void parseVcapApplication(ObjectMapper objectMapper, String vcapApplicationJson) {
4953
try {
50-
String vcapApplicationJson = System.getenv(VCAP_APPLICATION);
5154
if (vcapApplicationJson != null && vcapApplicationJson.length() > 0) {
5255
Map<String, Object> applicationData = objectMapper.readValue(vcapApplicationJson, Map.class);
5356
this.cfApplication = new CfApplication(applicationData);
5457
}
5558
} catch (Exception e) {
56-
throw new IllegalStateException("Could not access/parse " + VCAP_APPLICATION + "environment variable.", e);
59+
throw new IllegalStateException("Could not parse " + VCAP_APPLICATION + "environment variable.", e);
5760
}
5861
}
5962

60-
private void parseVcapServices(ObjectMapper objectMapper) {
63+
private void parseVcapServices(ObjectMapper objectMapper, String vcapServicesJson) {
6164
try {
62-
String vcapServicesJson = System.getenv(VCAP_SERVICES);
6365
if (vcapServicesJson != null && vcapServicesJson.length() > 0) {
6466
Map<String, List<Map<String, Object>>> rawServicesMap = objectMapper.readValue(vcapServicesJson, Map.class);
6567
rawServicesMap.values().stream()
6668
.flatMap(Collection::stream)
6769
.forEach(serviceData -> cfServices.add(new CfService(serviceData)));
6870
}
6971
} catch (Exception e) {
70-
throw new IllegalStateException("Could not access/parse " + VCAP_SERVICES + " environment variable.", e);
72+
throw new IllegalStateException("Could not parse " + VCAP_SERVICES + " environment variable.", e);
7173
}
7274
}
7375

java-cfenv/src/test/java/io/pivotal/cfenv/core/CfEnvTests.java

Lines changed: 56 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,21 @@
1616
package io.pivotal.cfenv.core;
1717

1818
import java.io.File;
19+
import java.io.IOException;
20+
import java.nio.file.FileSystem;
21+
import java.nio.file.FileSystems;
22+
import java.nio.file.Files;
23+
import java.nio.file.Paths;
1924
import java.util.ArrayList;
25+
import java.util.Collections;
2026
import java.util.List;
2127
import java.util.Map;
2228

29+
import org.junit.Before;
2330
import org.junit.Test;
2431

32+
import org.springframework.core.io.ClassPathResource;
33+
2534
import io.pivotal.cfenv.core.test.CfEnvMock;
2635

2736
import static org.assertj.core.api.Assertions.assertThat;
@@ -32,12 +41,41 @@
3241
* @author Paul Warren
3342
* @author David Turanski
3443
*/
35-
public class CfEnvTests {
44+
public abstract class CfEnvTests {
45+
46+
public static final String DEFAULT_VCAP_SERVICES = "vcap-services.json";
47+
public static final String DEFAULT_VCAP_APPLICATION = "test/vcap-application.json";
48+
protected CfEnv cfEnv;
49+
50+
@Before
51+
public void beforeEach() {
52+
setupTest(DEFAULT_VCAP_SERVICES, DEFAULT_VCAP_APPLICATION);
53+
}
54+
55+
protected abstract void setupTest(String vcapServicesResource, String vcapApplicationResource);
56+
57+
public static class CfEnvWithParametersTests extends CfEnvTests {
58+
@Override
59+
protected void setupTest(String vcapServicesResource, String vcapApplicationResource) {
60+
String vcapServicesJson = readResource(vcapServicesResource);
61+
String vcapApplicationJson = readResource(vcapApplicationResource);
62+
cfEnv = new CfEnv(vcapApplicationJson, vcapServicesJson);
63+
}
64+
}
65+
66+
public static class CfEnvDefaultConstructorTests extends CfEnvTests {
67+
@Override
68+
protected void setupTest(String vcapServicesResource, String vcapApplicationResource) {
69+
CfEnvMock.configure()
70+
.vcapServicesResource(vcapServicesResource)
71+
.vcapApplicationResource(vcapApplicationResource)
72+
.mock();
73+
cfEnv = new CfEnv();
74+
}
75+
}
3676

3777
@Test
3878
public void testCfApplicationValues() {
39-
mockVcapEnvVars();
40-
CfEnv cfEnv = new CfEnv();
4179
CfApplication cfApplication = cfEnv.getApp();
4280
assertThat(cfApplication.getApplicationId())
4381
.isEqualTo("fa05c1a9-0fc1-4fbd-bae1-139850dec7a3");
@@ -62,9 +100,6 @@ public void testCfApplicationValues() {
62100

63101
@Test
64102
public void testCfService() {
65-
mockVcapEnvVars();
66-
CfEnv cfEnv = new CfEnv();
67-
68103
List<CfService> cfServices = cfEnv.findAllServices();
69104
assertThat(cfServices.size()).isEqualTo(3);
70105

@@ -158,9 +193,6 @@ private void assertUriInfo(UriInfo uriInfo) {
158193

159194
@Test
160195
public void testFindServiceByName() {
161-
mockVcapEnvVars();
162-
CfEnv cfEnv = new CfEnv();
163-
164196
CfService cfService = cfEnv.findServiceByTag("redis");
165197
assertThat(cfService.getLabel()).isEqualTo("p-redis");
166198
assertThat(cfService.getPlan()).isEqualTo("shared-vm");
@@ -191,9 +223,6 @@ public void testFindServiceByName() {
191223

192224
@Test
193225
public void testFindServiceByLabel() {
194-
mockVcapEnvVars();
195-
CfEnv cfEnv = new CfEnv();
196-
197226
CfService cfService = cfEnv.findServiceByLabel("p-redis");
198227
assertThat(cfService.getLabel()).isEqualTo("p-redis");
199228
assertThat(cfService.getPlan()).isEqualTo("shared-vm");
@@ -226,9 +255,6 @@ public void testFindServiceByLabel() {
226255

227256
@Test
228257
public void testFindServiceByTag() {
229-
mockVcapEnvVars();
230-
CfEnv cfEnv = new CfEnv();
231-
232258
CfService cfService = cfEnv.findServiceByTag("redis");
233259
assertThat(cfService.getLabel()).isEqualTo("p-redis");
234260
assertThat(cfService.getPlan()).isEqualTo("shared-vm");
@@ -258,9 +284,6 @@ public void testFindServiceByTag() {
258284

259285
@Test
260286
public void testFindCredentialsByName() {
261-
mockVcapEnvVars();
262-
CfEnv cfEnv = new CfEnv();
263-
264287
CfCredentials cfCredentials = cfEnv.findCredentialsByName("mysql");
265288
assertMySqlCredentials(cfCredentials);
266289

@@ -289,9 +312,6 @@ public void testFindCredentialsByName() {
289312

290313
@Test
291314
public void testFindCredentialsByLabel() {
292-
mockVcapEnvVars();
293-
CfEnv cfEnv = new CfEnv();
294-
295315
CfCredentials cfCredentials = cfEnv.findCredentialsByLabel("p-mysql");
296316
assertMySqlCredentials(cfCredentials);
297317

@@ -319,9 +339,6 @@ public void testFindCredentialsByLabel() {
319339

320340
@Test
321341
public void testFindCredentialsByTag() {
322-
mockVcapEnvVars();
323-
CfEnv cfEnv = new CfEnv();
324-
325342
CfCredentials cfCredentials = cfEnv.findCredentialsByTag("mysql");
326343
assertMySqlCredentials(cfCredentials);
327344

@@ -350,8 +367,8 @@ public void testFindCredentialsByTag() {
350367

351368
@Test
352369
public void testNullCredentials() {
353-
CfEnvMock.configure().vcapServicesResource("vcap-null-credentials.json").mock();
354-
CfEnv cfEnv = new CfEnv();
370+
setupTest("vcap-null-credentials.json", DEFAULT_VCAP_APPLICATION);
371+
355372
CfService cfService = cfEnv.findServiceByTag("efs");
356373
// should not throw exception
357374
cfService.existsByCredentialsContainsUriField("foo");
@@ -378,8 +395,8 @@ private void assertNfsVolumes(List<CfVolume> cfVolumes) {
378395

379396
@Test
380397
public void testMultipleMatchingServices() {
381-
CfEnvMock.configure().vcapServicesResource("vcap-services-multiple-mysql.json").mock();
382-
CfEnv cfEnv = new CfEnv();
398+
setupTest("vcap-services-multiple-mysql.json", DEFAULT_VCAP_APPLICATION);
399+
383400
List<CfService> services = cfEnv.findAllServices();
384401
assertThat(services.size()).isEqualTo(3);
385402

@@ -409,8 +426,16 @@ public void testMultipleMatchingServices() {
409426

410427
}
411428

412-
private void mockVcapEnvVars() {
413-
CfEnvMock.configure().vcapServicesResource("vcap-services.json").mock();
429+
protected String readResource(String resource) {
430+
ClassPathResource classPathResource = new ClassPathResource(resource);
431+
try (FileSystem ignored = FileSystems.newFileSystem(
432+
classPathResource.getURI(),
433+
Collections.emptyMap(),
434+
ClassLoader.getSystemClassLoader())) {
435+
byte[] resourceBytes = Files.readAllBytes(Paths.get(classPathResource.getURI()));
436+
return new String(resourceBytes);
437+
} catch (IOException e) {
438+
throw new RuntimeException(e);
439+
}
414440
}
415-
416441
}

0 commit comments

Comments
 (0)