1
1
package test
2
2
3
3
import (
4
+ "fmt"
5
+ "strconv"
4
6
"testing"
5
7
6
8
"github.com/flanksource/konfig-manager/pkg"
@@ -20,8 +22,14 @@ var tests = map[string]testInput{
20
22
data : "fixtures/spring.yml" ,
21
23
verifications : map [string ]string {
22
24
"config-key" : "some-value" ,
25
+ "config-key-quotes" : "some-other-value" ,
23
26
"spring.datasource.maxActive" : "40" ,
27
+ "null-string-key" : "null" ,
28
+ "undefined-string-key" : "undefined" ,
29
+ "bool-string-key" : "true" ,
30
+ "int-string-key" : "11" ,
24
31
},
32
+ applications : []string {"spring" },
25
33
},
26
34
"testHierarchyMergeWithInputFile" : {
27
35
config : "fixtures/spring-config.yml" ,
@@ -30,6 +38,7 @@ var tests = map[string]testInput{
30
38
"config-key" : "some-value" ,
31
39
"spring.datasource.maxActive" : "40" ,
32
40
},
41
+ applications : []string {"spring" },
33
42
},
34
43
"testReadFromConfigMapCreatedWithFile" : {
35
44
config : "fixtures/fileProperties-config.yml" ,
@@ -39,6 +48,7 @@ var tests = map[string]testInput{
39
48
"new-key" : "diff-value" ,
40
49
"logging.level.org.springframework.web" : "INFO" ,
41
50
},
51
+ applications : []string {"spring" },
42
52
},
43
53
"testSecretValues" : {
44
54
config : "fixtures/secret-config.yml" ,
@@ -47,6 +57,7 @@ var tests = map[string]testInput{
47
57
"secret-key" : "some-value" ,
48
58
"logging.level.org.springframework.web" : "INFO" ,
49
59
},
60
+ applications : []string {"spring" },
50
61
},
51
62
"testMultipleApplications" : {
52
63
config : "fixtures/multi-application-config.yml" ,
@@ -56,9 +67,55 @@ var tests = map[string]testInput{
56
67
"secret-key" : "some-value" ,
57
68
"new-key" : "diff-value" ,
58
69
},
70
+ applications : []string {"spring" , "quarkus" },
59
71
},
60
72
}
61
73
74
+ func TestGenerateJs (t * testing.T ) {
75
+ for name , test := range tests {
76
+ t .Run (name , func (t * testing.T ) {
77
+ resources , err := pkg .ReadResources (test .data )
78
+ if err != nil {
79
+ t .Error (err )
80
+ }
81
+
82
+ for _ , name := range test .applications {
83
+ hierarchy , err := pkg .GetHierarchy (test .config , name )
84
+ if err != nil {
85
+ t .Error (err )
86
+ }
87
+
88
+ file := hierarchy .GenerateJsPropertiesFile (resources )
89
+
90
+ p := properties .MustLoadString (file )
91
+ // check property key and values
92
+
93
+ for key , value := range test .verifications {
94
+ var transformedValue string
95
+ if _ , err := strconv .Atoi (value ); err == nil {
96
+ transformedValue = fmt .Sprintf ("%v;" , value )
97
+ } else if _ , err := strconv .ParseBool (value ); err == nil {
98
+ transformedValue = fmt .Sprintf ("%v;" , value )
99
+ } else if value == "null" || value == "undefined" {
100
+ transformedValue = fmt .Sprintf ("%v;" , value )
101
+ } else {
102
+ transformedValue = fmt .Sprintf ("\" %v\" ;" , value )
103
+ }
104
+
105
+ transformedKey := fmt .Sprintf ("window['__%v__']" , key )
106
+ propVal , exists := p .Get (transformedKey )
107
+ if ! exists {
108
+ t .Errorf ("property not found: %s" , key )
109
+ }
110
+ if propVal != transformedValue {
111
+ t .Errorf ("%s: expected %s got %s" , key , propVal , transformedValue )
112
+ }
113
+ }
114
+ }
115
+ })
116
+ }
117
+ }
118
+
62
119
func TestGenerate (t * testing.T ) {
63
120
for name , test := range tests {
64
121
t .Run (name , func (t * testing.T ) {
0 commit comments