forked from open-feature/java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStructureTest.java
117 lines (105 loc) · 4.61 KB
/
StructureTest.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
package dev.openfeature.sdk;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Test;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static dev.openfeature.sdk.Structure.mapToStructure;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class StructureTest {
@Test public void noArgShouldContainEmptyAttributes() {
MutableStructure structure = new MutableStructure();
assertEquals(0, structure.asMap().keySet().size());
}
@Test public void mapArgShouldContainNewMap() {
String KEY = "key";
Map<String, Value> map = new HashMap<String, Value>() {
{
put(KEY, new Value(KEY));
}
};
MutableStructure structure = new MutableStructure(map);
assertEquals(KEY, structure.asMap().get(KEY).asString());
assertNotSame(structure.asMap(), map); // should be a copy
}
@Test public void addAndGetAddAndReturnValues() {
String BOOL_KEY = "bool";
String STRING_KEY = "string";
String INT_KEY = "int";
String DOUBLE_KEY = "double";
String DATE_KEY = "date";
String STRUCT_KEY = "struct";
String LIST_KEY = "list";
String VALUE_KEY = "value";
boolean BOOL_VAL = true;
String STRING_VAL = "val";
int INT_VAL = 13;
double DOUBLE_VAL = .5;
Instant DATE_VAL = Instant.now();
MutableStructure STRUCT_VAL = new MutableStructure();
List<Value> LIST_VAL = new ArrayList<>();
Value VALUE_VAL = new Value();
MutableStructure structure = new MutableStructure();
structure.add(BOOL_KEY, BOOL_VAL);
structure.add(STRING_KEY, STRING_VAL);
structure.add(INT_KEY, INT_VAL);
structure.add(DOUBLE_KEY, DOUBLE_VAL);
structure.add(DATE_KEY, DATE_VAL);
structure.add(STRUCT_KEY, STRUCT_VAL);
structure.add(LIST_KEY, LIST_VAL);
structure.add(VALUE_KEY, VALUE_VAL);
assertEquals(BOOL_VAL, structure.getValue(BOOL_KEY).asBoolean());
assertEquals(STRING_VAL, structure.getValue(STRING_KEY).asString());
assertEquals(INT_VAL, structure.getValue(INT_KEY).asInteger());
assertEquals(DOUBLE_VAL, structure.getValue(DOUBLE_KEY).asDouble());
assertEquals(DATE_VAL, structure.getValue(DATE_KEY).asInstant());
assertEquals(STRUCT_VAL, structure.getValue(STRUCT_KEY).asStructure());
assertEquals(LIST_VAL, structure.getValue(LIST_KEY).asList());
assertTrue(structure.getValue(VALUE_KEY).isNull());
}
@SneakyThrows
@Test
void mapToStructureTest() {
Map<String, Object> map = new HashMap<>();
map.put("String", "str");
map.put("Boolean", true);
map.put("Integer", 1);
map.put("Double", 1.1);
map.put("List", Collections.singletonList(new Value(1)));
map.put("Value", new Value((true)));
map.put("Instant", Instant.ofEpochSecond(0));
map.put("Map", new HashMap<>());
map.put("nullKey", null);
ImmutableContext immutableContext = new ImmutableContext();
map.put("ImmutableContext", immutableContext);
Structure res = mapToStructure(map);
assertEquals(new Value("str"), res.getValue("String"));
assertEquals(new Value(true), res.getValue("Boolean"));
assertEquals(new Value(1), res.getValue("Integer"));
assertEquals(new Value(1.1), res.getValue("Double"));
assertEquals(new Value(Collections.singletonList(new Value(1))), res.getValue("List"));
assertEquals(new Value(true), res.getValue("Value"));
assertEquals(new Value(Instant.ofEpochSecond(0)), res.getValue("Instant"));
assertEquals(new HashMap<>(), res.getValue("Map").asStructure().asMap());
assertEquals(new Value(immutableContext), res.getValue("ImmutableContext"));
assertEquals(new Value(), res.getValue("nullKey"));
}
@Test
void asObjectHandlesNullValue() {
Map<String, Value> map = new HashMap<>();
map.put("null", new Value((String)null));
ImmutableStructure structure = new ImmutableStructure(map);
assertNull(structure.asObjectMap().get("null"));
}
@Test
void convertValueHandlesNullValue() {
ImmutableStructure structure = new ImmutableStructure();
assertNull(structure.convertValue(new Value((String)null)));
}
}