-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfigurationTest.java
171 lines (138 loc) · 6.11 KB
/
ConfigurationTest.java
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package io.github.tofpu.dynamicconfiguration;
import io.github.tofpu.dynamicconfiguration.service.ConfigType;
import io.github.tofpu.dynamicconfiguration.service.DynamicConfigHandler;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.time.Instant;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
// todo: figure out a way to write & fetch nested keys and values
public class ConfigurationTest {
private final File runtimeDirectory = new File("test-resources/runtime");
private final File defaultDirectory = new File("test-resources/default/configuration");
private DynamicConfigHandler service;
@BeforeEach
void setUp() {
cleanRuntime();
service = new DynamicConfigHandler(runtimeDirectory);
service.load(ConfigTypes.values());
}
@SuppressWarnings("all")
private void cleanRuntime() {
File[] files = runtimeDirectory.listFiles();
if (files != null && files.length != 0) {
Arrays.stream(files).forEach(File::delete);
}
runtimeDirectory.delete();
}
@AfterEach
void tearDown() {
service.unload();
cleanRuntime();
}
@Test
void basic_test() {
Configuration config = service.on(ConfigTypes.CONFIG);
config.setString("daddy", "tofpu");
config.setString("mommy", "also tofpu");
config.setString("friendly.friend", "and also tofpu");
Assertions.assertEquals("tofpu", config.getString("daddy", null));
Assertions.assertEquals("also tofpu", config.getString("mommy", null));
Assertions.assertNull(config.getString("unknown", null));
Assertions.assertEquals("world", config.getString("hello", "world"));
Assertions.assertEquals("and also tofpu", config.getString("friendly.friend", null));
}
@Test
void nesting_test() {
Configuration config = service.on(ConfigTypes.CONFIG);
Assertions.assertEquals(123, config.path("hello")
.setString("friend", "hi!")
.setInt("id", 123)
.getInt("id", -1));
config.set("nested.with.periods", true);
Assertions.assertEquals("hi!", config.path("hello").getString("friend", null));
Assertions.assertEquals(123, config.path("hello").getInt("id", -1));
Assertions.assertNull(config.path("hello").getString("unknown", null));
Assertions.assertTrue(config.getAs(Boolean.class, "nested.with.periods", null));
}
@Test
void double_nesting_test() {
Configuration config = service.on(ConfigTypes.CONFIG);
final Map<String, Object> newYorkMap = new HashMap<>();
newYorkMap.put("id", 1);
newYorkMap.put("employees_size", 10);
newYorkMap.put("director", "Tofpu");
final Map<String, Object> newMexicoMap = new HashMap<>();
newMexicoMap.put("id", 2);
newMexicoMap.put("employees_size", 20);
newMexicoMap.put("director", "AnotherTofpu");
Date creationDate = Date.from(Instant.now());
Date latestUpdate = Date.from(Instant.now().minus(200, TimeUnitHelper.toChronoUnit(TimeUnit.DAYS)));
config.path("department")
.setString("creationDate", creationDate.toString())
.setString("latestUpdate", latestUpdate.toString())
.path("list")
.set("New York", newYorkMap)
.set("New Mexico", newMexicoMap)
.previous()
.set("count", 20);
Assertions.assertEquals(4, config.getAs(Map.class, "department", null).size());
Assertions.assertEquals(2, config.path("department").getAs(Map.class, "list", null).size());
Assertions.assertEquals(creationDate.toString(),
config.path("department").getString("creationDate", null));
Assertions.assertEquals(latestUpdate.toString(),
config.path("department").getString("latestUpdate", null));
Assertions.assertEquals(MapUtil.of("New York", newYorkMap, "New Mexico", newMexicoMap),
config.path("department").getAs(Map.class, "list", null));
Assertions.assertEquals(newYorkMap,
config.path("department").path("list").getAs(Map.class, "New York", null));
Assertions.assertEquals(newMexicoMap,
config.path("department").path("list").getAs(Map.class, "New Mexico", null));
Assertions.assertEquals(20, config.path("department").getInt("count", -1));
}
@Test
void test_config_types() {
int i = 0;
for (ConfigType value : ConfigTypes.values()) {
LoadableConfiguration config = (LoadableConfiguration) service.on(value);
config.setInt("index", i);
Assertions.assertEquals(i, config.getInt("index", -1));
Assertions.assertEquals("none", config.getString("hat", "none"));
Assertions.assertNull(config.getAs(String.class, "unknown", null));
i++;
}
}
@Test
void load_test() {
LoadableConfiguration config = (LoadableConfiguration) service.on(ConfigTypes.CONFIG);
config.load(new File(defaultDirectory, "config.yml"));
Assertions.assertEquals("yes", config.getString("test", null));
// Assertions.assertEquals("two", config.getString("default.one", null));
// Assertions.assertEquals("four", config.getString("default.three", null));
Assertions.assertNull(config.getString("unknown", null));
}
@Test
void failure_test() {
Configuration config = service.on(ConfigTypes.CONFIG);
config.setString("test", "hello world!");
Assertions.assertThrows(IllegalStateException.class, () -> config.getInt("test", -1));
}
public enum ConfigTypes implements ConfigType {
CONFIG("config"),
MESSAGE("messages");
private final String identifier;
ConfigTypes(String identifier) {
this.identifier = identifier;
}
@Override
public String identifier() {
return identifier;
}
}
}