-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonObjTest.java
51 lines (40 loc) · 1.36 KB
/
JsonObjTest.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
package in.eko.exprutils;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class JsonObjTest {
String obj = "{'a':1, 'b':{'c':2, 'd':{'e': 3}}}";
@Test
void get() {
// Test simple (non-nested) value retrieve
assertEquals("1",
JsonObj.get(obj, "a", "0").toString()
);
// Test nested (1-level deep) value retrieve
assertEquals("2",
JsonObj.get(obj, "b.c", "0").toString()
);
// Test nested (2-level deep) value retrieve
assertEquals("3",
JsonObj.get(obj, "b.d.e", "0").toString()
);
// Test default value return when key does not exist
assertEquals("-1",
JsonObj.get(obj, "b.Z.e", "-1").toString()
);
}
@Test
void set() {
// Test simple (non-nested) value set
assertEquals("{\"a\":0}",
JsonObj.set("{}", "a", 0).toString()
);
// Test nested (1-level deep) value set
assertEquals("{\"a\":{\"hello\":\"world\"}}",
JsonObj.set("{}", "a.hello", "world").toString()
);
// Test nested (2-level deep) value set
assertEquals("{\"a\":{\"hello\":{\"world\":\"again\"}},\"b\":5}",
JsonObj.set("{\"b\":5}", "a.hello.world", "again").toString()
);
}
}