@@ -21,7 +21,7 @@ class StableConfigSourceTest extends DDSpecification {
21
21
// How to check that the "file does not exist" error was logged?
22
22
}
23
23
24
- def " test valid file" () {
24
+ def " test empty file" () {
25
25
// test empty file
26
26
when :
27
27
Path filePath
@@ -42,28 +42,47 @@ class StableConfigSourceTest extends DDSpecification {
42
42
then :
43
43
config. getKeys(). size() == 0
44
44
config. getConfigId() == null
45
+ }
45
46
46
- // test populated file
47
+ def " test populated file" () {
47
48
when :
48
- def id = " 12345 "
49
- def key1 = " dd_first_key "
50
- def val1 = " dd_first_val "
51
- def key2 = " dd_second_key "
52
- def val2 = " dd_second_val "
53
- // Create the map that will be used to populate the config file
54
- Map< String , Object > data = new HashMap<> ()
55
- data . put( " config_id " , id )
56
- data . put( " apm_configuration_default " , new HashMap< String , Object > () {{
57
- put(key1, val1)
58
- put(key2, val2)
59
- }})
49
+ Path filePath
50
+ StableConfigSource stableCfg
51
+ try {
52
+ filePath = Files . createTempFile( " testFile_ " , " .yaml " )
53
+ } catch ( IOException e) {
54
+ println " Error creating file: ${ e.message } "
55
+ e . printStackTrace ()
56
+ return // or throw new RuntimeException("File creation failed ", e )
57
+ }
58
+ if (filePath == null ) {
59
+ return
60
+ }
60
61
61
62
DumperOptions options = new DumperOptions ()
62
63
options. setDefaultFlowStyle(DumperOptions.FlowStyle . BLOCK )
63
64
64
65
// Prepare to write the data map to the file in yaml format
65
66
Yaml yaml = new Yaml (options)
66
- String yamlString = yaml. dump(data)
67
+ String yamlString
68
+ if (corrupt == true ) {
69
+ yamlString = '''
70
+ abc: 123
71
+ def
72
+ ghi "jkl"
73
+ lmn: 456
74
+ '''
75
+ } else {
76
+ Map<String , Object > data = new HashMap<> ()
77
+ if (configId != null ) {
78
+ data. put(" config_id" , configId)
79
+ }
80
+ if (configs != null ) {
81
+ data. put(" apm_configuration_default" , configs)
82
+ }
83
+
84
+ yamlString = yaml. dump(data)
85
+ }
67
86
68
87
try {
69
88
StandardOpenOption [] openOpts = [StandardOpenOption . WRITE ] as StandardOpenOption []
@@ -74,12 +93,26 @@ class StableConfigSourceTest extends DDSpecification {
74
93
// fail fast?
75
94
}
76
95
96
+ stableCfg = new StableConfigSource (filePath. toString(), ConfigOrigin . USER_STABLE_CONFIG )
97
+
77
98
then :
78
- StableConfigSource config2 = new StableConfigSource (filePath. toString(), ConfigOrigin . USER_STABLE_CONFIG )
79
- config2. getConfigId() == id
80
- config2. getKeys(). size() == 2
81
- config2. get(key1) == val1
82
- config2. get(key2) == val2
99
+ stableCfg. getConfigId() == configId
100
+ if (configs == null ) {
101
+ stableCfg. getKeys(). size() == 0
102
+ } else {
103
+ stableCfg. getKeys(). size() == configs. size()
104
+ }
83
105
Files . delete(filePath)
106
+
107
+ where :
108
+ key_one = " key_one"
109
+ val_one = " val_one"
110
+ key_two = " key_two"
111
+ val_two = " val_2"
112
+ configId | configs | corrupt
113
+ null | null | true
114
+ " " | new HashMap<> () | false
115
+ " 12345" | new HashMap<> () << [" key_one" : " val_one" , " key_two" : " val_two" ] | false
116
+
84
117
}
85
118
}
0 commit comments