16
16
package io .pivotal .cfenv .core ;
17
17
18
18
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 ;
19
24
import java .util .ArrayList ;
25
+ import java .util .Collections ;
20
26
import java .util .List ;
21
27
import java .util .Map ;
22
28
29
+ import org .junit .Before ;
23
30
import org .junit .Test ;
24
31
32
+ import org .springframework .core .io .ClassPathResource ;
33
+
25
34
import io .pivotal .cfenv .core .test .CfEnvMock ;
26
35
27
36
import static org .assertj .core .api .Assertions .assertThat ;
32
41
* @author Paul Warren
33
42
* @author David Turanski
34
43
*/
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
+ }
36
76
37
77
@ Test
38
78
public void testCfApplicationValues () {
39
- mockVcapEnvVars ();
40
- CfEnv cfEnv = new CfEnv ();
41
79
CfApplication cfApplication = cfEnv .getApp ();
42
80
assertThat (cfApplication .getApplicationId ())
43
81
.isEqualTo ("fa05c1a9-0fc1-4fbd-bae1-139850dec7a3" );
@@ -62,9 +100,6 @@ public void testCfApplicationValues() {
62
100
63
101
@ Test
64
102
public void testCfService () {
65
- mockVcapEnvVars ();
66
- CfEnv cfEnv = new CfEnv ();
67
-
68
103
List <CfService > cfServices = cfEnv .findAllServices ();
69
104
assertThat (cfServices .size ()).isEqualTo (3 );
70
105
@@ -158,9 +193,6 @@ private void assertUriInfo(UriInfo uriInfo) {
158
193
159
194
@ Test
160
195
public void testFindServiceByName () {
161
- mockVcapEnvVars ();
162
- CfEnv cfEnv = new CfEnv ();
163
-
164
196
CfService cfService = cfEnv .findServiceByTag ("redis" );
165
197
assertThat (cfService .getLabel ()).isEqualTo ("p-redis" );
166
198
assertThat (cfService .getPlan ()).isEqualTo ("shared-vm" );
@@ -191,9 +223,6 @@ public void testFindServiceByName() {
191
223
192
224
@ Test
193
225
public void testFindServiceByLabel () {
194
- mockVcapEnvVars ();
195
- CfEnv cfEnv = new CfEnv ();
196
-
197
226
CfService cfService = cfEnv .findServiceByLabel ("p-redis" );
198
227
assertThat (cfService .getLabel ()).isEqualTo ("p-redis" );
199
228
assertThat (cfService .getPlan ()).isEqualTo ("shared-vm" );
@@ -226,9 +255,6 @@ public void testFindServiceByLabel() {
226
255
227
256
@ Test
228
257
public void testFindServiceByTag () {
229
- mockVcapEnvVars ();
230
- CfEnv cfEnv = new CfEnv ();
231
-
232
258
CfService cfService = cfEnv .findServiceByTag ("redis" );
233
259
assertThat (cfService .getLabel ()).isEqualTo ("p-redis" );
234
260
assertThat (cfService .getPlan ()).isEqualTo ("shared-vm" );
@@ -258,9 +284,6 @@ public void testFindServiceByTag() {
258
284
259
285
@ Test
260
286
public void testFindCredentialsByName () {
261
- mockVcapEnvVars ();
262
- CfEnv cfEnv = new CfEnv ();
263
-
264
287
CfCredentials cfCredentials = cfEnv .findCredentialsByName ("mysql" );
265
288
assertMySqlCredentials (cfCredentials );
266
289
@@ -289,9 +312,6 @@ public void testFindCredentialsByName() {
289
312
290
313
@ Test
291
314
public void testFindCredentialsByLabel () {
292
- mockVcapEnvVars ();
293
- CfEnv cfEnv = new CfEnv ();
294
-
295
315
CfCredentials cfCredentials = cfEnv .findCredentialsByLabel ("p-mysql" );
296
316
assertMySqlCredentials (cfCredentials );
297
317
@@ -319,9 +339,6 @@ public void testFindCredentialsByLabel() {
319
339
320
340
@ Test
321
341
public void testFindCredentialsByTag () {
322
- mockVcapEnvVars ();
323
- CfEnv cfEnv = new CfEnv ();
324
-
325
342
CfCredentials cfCredentials = cfEnv .findCredentialsByTag ("mysql" );
326
343
assertMySqlCredentials (cfCredentials );
327
344
@@ -350,8 +367,8 @@ public void testFindCredentialsByTag() {
350
367
351
368
@ Test
352
369
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
+
355
372
CfService cfService = cfEnv .findServiceByTag ("efs" );
356
373
// should not throw exception
357
374
cfService .existsByCredentialsContainsUriField ("foo" );
@@ -378,8 +395,8 @@ private void assertNfsVolumes(List<CfVolume> cfVolumes) {
378
395
379
396
@ Test
380
397
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
+
383
400
List <CfService > services = cfEnv .findAllServices ();
384
401
assertThat (services .size ()).isEqualTo (3 );
385
402
@@ -409,8 +426,16 @@ public void testMultipleMatchingServices() {
409
426
410
427
}
411
428
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
+ }
414
440
}
415
-
416
441
}
0 commit comments