This repository was archived by the owner on Oct 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmappingsv2_test.go
130 lines (113 loc) · 3.46 KB
/
mappingsv2_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* © Copyright IBM Corp. 2018
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package IBMCloudEnv
import (
"github.com/tidwall/gjson"
"os"
"testing"
)
const jsonObjectV2 = `{
"service1": [
{
"name": "service1-name1",
"credentials": {
"username": "service1-username1"
}
},
{
"name": "service1-name2",
"credentials": {
"username": "service1-username2"
}
}
],
"user-provided": [
{
"name": "service2-name1",
"credentials":{
"username": "service2-username1"
}
}
]
}`
const vcap_applicationV2 = `{"application_name": "test-application"}`
const var_stringV2 = `test-12345`
const credentialsV2 = `{"credentials": {
"username": "env-var-json-username"
}}`
func setEnvVariableV2() {
os.Setenv("VCAP_APPLICATION", vcap_applicationV2)
os.Setenv("VCAP_SERVICES", jsonObjectV2)
os.Setenv("ENV_VAR_STRING", var_stringV2)
os.Setenv("ENV_VAR_JSON", credentialsV2)
Initialize("/invalid-file-name")
Initialize("server/config/v2/mappings.json")
}
func TestPlainTextFileV2(t *testing.T) {
setEnvVariableV2()
result := GetDictionary("var1").Get("file_var1")
if result.String() != "plain-text-string" {
t.Errorf("can't read " + result.String() + " text from GetDictionary()")
}
}
func TestJsonFileAndPathV2(t *testing.T) {
setEnvVariableV2()
pre_json := GetDictionary("var2").Get("file_var2").String()
testString := gjson.Parse(pre_json).Get("level2").String()
if testString != "12345" {
t.Errorf("Got: \t%s\n Wanted: \t%s\n", testString, "12345")
}
}
func TestCFServiceCredentialsWithServiceInstanceV2(t *testing.T) {
setEnvVariableV2()
pre_json := GetDictionary("var1").Get("cf_var1").String()
testString := gjson.Parse(pre_json).Get("username").String()
if testString != "service1-username1" {
t.Errorf("Got: \t%s\n Wanted: \t%s\n", testString, "service1-username1")
}
}
func TestReadVcapsWithJsonPathV2(t *testing.T) {
setEnvVariableV2()
result1 := GetDictionary("var2").Get("cf_var2").String()
if result1 != "service1-username1" {
t.Errorf("can't read " + result1 + " text from GetDictionary()")
}
result2 := GetDictionary("var3").Get("cf_var3").String()
if result2 != "test-application" {
t.Errorf("can't read " + result2 + " text from GetDictionary()")
}
}
func TestSimpleStringFromEnvVarV2(t *testing.T) {
setEnvVariableV2()
result := GetDictionary("env_var1").Get("value")
if result.String() != "test-12345" {
t.Errorf("can't read " + result.String() + " text from GetDictionary()")
}
}
func TestJsonFromEnvVarV2(t *testing.T) {
setEnvVariableV2()
testString := GetDictionary("env_var2").Get("credentials").Get("username").String()
if testString != "env-var-json-username" {
t.Errorf("Got: \t%s\n Wanted: \t%s\n", testString, "env-var-json-username")
}
}
func TestJsonPathFromEnvVarV2(t *testing.T) {
setEnvVariableV2()
testString := GetDictionary("env_var3").Get("value").String()
if testString != "env-var-json-username" {
t.Errorf("Got: \t%s\n Wanted: \t%s\n", testString, "env-var-json-username")
}
}