-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathImmutableStructureTest.java
132 lines (117 loc) · 4.94 KB
/
ImmutableStructureTest.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
package dev.openfeature.sdk;
import org.junit.jupiter.api.Test;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.*;
class ImmutableStructureTest {
@Test void noArgShouldContainEmptyAttributes() {
ImmutableStructure structure = new ImmutableStructure();
assertEquals(0, structure.asMap().keySet().size());
}
@Test void mapArgShouldContainNewMap() {
String KEY = "key";
Map<String, Value> map = new HashMap<String, Value>() {
{
put(KEY, new Value(KEY));
}
};
ImmutableStructure structure = new ImmutableStructure(map);
assertEquals(KEY, structure.asMap().get(KEY).asString());
assertNotSame(structure.asMap(), map); // should be a copy
}
@Test void MutatingGetValueShouldNotChangeOriginalValue() {
String KEY = "key";
List<Value> lists = new ArrayList<>();
lists.add(new Value(KEY));
Map<String, Value> map = new HashMap<String, Value>() {
{
put(KEY, new Value(lists));
}
};
ImmutableStructure structure = new ImmutableStructure(map);
List<Value> values = structure.getValue(KEY).asList();
values.add(new Value("dummyValue"));
lists.add(new Value("dummy"));
assertEquals(1, structure.getValue(KEY).asList().size());
assertNotSame(structure.asMap(), map); // should be a copy
}
@Test void MutatingGetInstantValueShouldNotChangeOriginalValue() {
String KEY = "key";
Instant now = Instant.now().truncatedTo(ChronoUnit.MILLIS);
Map<String, Value> map = new HashMap<String, Value>() {
{
put(KEY, new Value(now));
}
};
ImmutableStructure structure = new ImmutableStructure(map);
//mutate the original value
Instant tomorrow = now.plus(1, ChronoUnit.DAYS);
//mutate the getValue
structure.getValue(KEY).asInstant().plus(1, ChronoUnit.DAYS);
assertNotEquals(tomorrow, structure.getValue(KEY).asInstant());
assertEquals(now, structure.getValue(KEY).asInstant());
}
@Test void MutatingGetStructureValueShouldNotChangeOriginalValue() {
String KEY = "key";
List<Value> lists = new ArrayList<>();
lists.add(new Value("dummy_list_1"));
MutableStructure mutableStructure = new MutableStructure().add("key1","val1").add("list", lists);
Map<String, Value> map = new HashMap<String, Value>() {
{
put(KEY, new Value(mutableStructure));
}
};
ImmutableStructure structure = new ImmutableStructure(map);
//mutate the original structure
mutableStructure.add("key2", "val2");
//mutate the return value
structure.getValue(KEY).asStructure().asMap().put("key3", new Value("val3"));
assertEquals(2, structure.getValue(KEY).asStructure().asMap().size());
assertArrayEquals(new Object[]{"key1", "list"}, structure.getValue(KEY).asStructure().keySet().toArray());
assertTrue(structure.getValue(KEY).asStructure() instanceof ImmutableStructure);
//mutate list value
lists.add(new Value("dummy_list_2"));
//mutate the return list value
structure.getValue(KEY).asStructure().asMap().get("list").asList().add(new Value("dummy_list_3"));
assertEquals(1, structure.getValue(KEY).asStructure().asMap().get("list").asList().size());
assertEquals("dummy_list_1", structure.getValue(KEY).asStructure().asMap().get("list").asList().get(0).asString());
}
@Test
void ModifyingTheValuesReturnByTheKeySetMethodShouldNotModifyTheUnderlyingImmutableStructure() {
Map<String, Value> map = new HashMap<String, Value>() {
{
put("key", new Value(10));
put("key1", new Value(20));
}
};
ImmutableStructure structure = new ImmutableStructure(map);
Set<String> keys = structure.keySet();
keys.remove("key1");
assertEquals(2, structure.keySet().size());
}
@Test
void GettingAMissingValueShouldReturnNull() {
ImmutableStructure structure = new ImmutableStructure();
Object value = structure.getValue("missing");
assertNull(value);
}
@Test void objectMapTest() {
Map<String, Value> attrs = new HashMap<>();
attrs.put("test", new Value(45));
ImmutableStructure structure = new ImmutableStructure(attrs);
Map<String, Integer> expected = new HashMap<>();
expected.put("test", 45);
assertEquals(expected, structure.asObjectMap());
}
@Test
void constructorHandlesNullValue() {
HashMap<String, Value> attrs = new HashMap<>();
attrs.put("null", null);
new ImmutableStructure(attrs);
}
}