Skip to content

Commit 54ce066

Browse files
committed
add MutableContextTest for test coverage
Signed-off-by: liran2000 <[email protected]>
1 parent 2ad71d1 commit 54ce066

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package dev.openfeature.sdk;
2+
3+
import org.junit.jupiter.api.DisplayName;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.util.HashMap;
7+
8+
import static dev.openfeature.sdk.EvaluationContext.TARGETING_KEY;
9+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
10+
import static org.junit.jupiter.api.Assertions.assertEquals;
11+
import static org.junit.jupiter.api.Assertions.assertTrue;
12+
13+
class MutableContextTest {
14+
15+
@DisplayName("targeting key should be changed from the overriding context")
16+
@Test
17+
void shouldChangeTargetingKeyFromOverridingContext() {
18+
HashMap<String, Value> attributes = new HashMap<>();
19+
attributes.put("key1", new Value("val1"));
20+
attributes.put("key2", new Value("val2"));
21+
EvaluationContext ctx = new MutableContext("targeting key", attributes);
22+
EvaluationContext overriding = new MutableContext("overriding_key");
23+
EvaluationContext merge = ctx.merge(overriding);
24+
assertEquals("overriding_key", merge.getTargetingKey());
25+
}
26+
27+
@DisplayName("targeting key should not changed from the overriding context if missing")
28+
@Test
29+
void shouldRetainTargetingKeyWhenOverridingContextTargetingKeyValueIsEmpty() {
30+
HashMap<String, Value> attributes = new HashMap<>();
31+
attributes.put("key1", new Value("val1"));
32+
attributes.put("key2", new Value("val2"));
33+
EvaluationContext ctx = new MutableContext("targeting_key", attributes);
34+
EvaluationContext overriding = new MutableContext("");
35+
EvaluationContext merge = ctx.merge(overriding);
36+
assertEquals("targeting_key", merge.getTargetingKey());
37+
}
38+
@DisplayName("missing targeting key should return null")
39+
@Test
40+
void missingTargetingKeyShould() {
41+
EvaluationContext ctx = new MutableContext();
42+
assertEquals(null, ctx.getTargetingKey());
43+
}
44+
45+
@DisplayName("Merge should retain all the attributes from the existing context when overriding context is null")
46+
@Test
47+
void mergeShouldReturnAllTheValuesFromTheContextWhenOverridingContextIsNull() {
48+
HashMap<String, Value> attributes = new HashMap<>();
49+
attributes.put("key1", new Value("val1"));
50+
attributes.put("key2", new Value("val2"));
51+
EvaluationContext ctx = new MutableContext("targeting_key", attributes);
52+
EvaluationContext merge = ctx.merge(null);
53+
assertEquals("targeting_key", merge.getTargetingKey());
54+
assertArrayEquals(new Object[]{"key1", "key2", TARGETING_KEY}, merge.keySet().toArray());
55+
}
56+
57+
@DisplayName("Merge should retain subkeys from the existing context when the overriding context has the same targeting key")
58+
@Test
59+
void mergeShouldRetainItsSubkeysWhenOverridingContextHasTheSameKey() {
60+
HashMap<String, Value> attributes = new HashMap<>();
61+
HashMap<String, Value> overridingAttributes = new HashMap<>();
62+
HashMap<String, Value> key1Attributes = new HashMap<>();
63+
HashMap<String, Value> ovKey1Attributes = new HashMap<>();
64+
65+
key1Attributes.put("key1_1", new Value("val1_1"));
66+
attributes.put("key1", new Value(new ImmutableStructure(key1Attributes)));
67+
attributes.put("key2", new Value("val2"));
68+
ovKey1Attributes.put("overriding_key1_1", new Value("overriding_val_1_1"));
69+
overridingAttributes.put("key1", new Value(new ImmutableStructure(ovKey1Attributes)));
70+
71+
EvaluationContext ctx = new MutableContext("targeting_key", attributes);
72+
EvaluationContext overriding = new MutableContext("targeting_key", overridingAttributes);
73+
EvaluationContext merge = ctx.merge(overriding);
74+
assertEquals("targeting_key", merge.getTargetingKey());
75+
assertArrayEquals(new Object[]{"key1", "key2", TARGETING_KEY}, merge.keySet().toArray());
76+
77+
Value key1 = merge.getValue("key1");
78+
assertTrue(key1.isStructure());
79+
80+
Structure value = key1.asStructure();
81+
assertArrayEquals(new Object[]{"key1_1","overriding_key1_1"}, value.keySet().toArray());
82+
}
83+
84+
@DisplayName("Merge should retain subkeys from the existing context when the overriding context doesn't have targeting key")
85+
@Test
86+
void mergeShouldRetainItsSubkeysWhenOverridingContextHasNoTargetingKey() {
87+
HashMap<String, Value> attributes = new HashMap<>();
88+
HashMap<String, Value> key1Attributes = new HashMap<>();
89+
90+
key1Attributes.put("key1_1", new Value("val1_1"));
91+
attributes.put("key1", new Value(new ImmutableStructure(key1Attributes)));
92+
attributes.put("key2", new Value("val2"));
93+
94+
EvaluationContext ctx = new MutableContext(attributes);
95+
EvaluationContext overriding = new MutableContext();
96+
EvaluationContext merge = ctx.merge(overriding);
97+
assertArrayEquals(new Object[]{"key1", "key2"}, merge.keySet().toArray());
98+
99+
Value key1 = merge.getValue("key1");
100+
assertTrue(key1.isStructure());
101+
102+
Structure value = key1.asStructure();
103+
assertArrayEquals(new Object[]{"key1_1"}, value.keySet().toArray());
104+
}
105+
}

0 commit comments

Comments
 (0)