Skip to content

Commit bfba968

Browse files
thiyagu06toddbaert
authored andcommitted
added few more tests for verifying immutable context.
Signed-off-by: thiyagu06 <[email protected]>
1 parent ec8c66d commit bfba968

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/main/java/dev/openfeature/sdk/ImmutableContext.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ public void setTargetingKey(String targetingKey) {
6464
}
6565

6666
/**
67-
* Merges this EvaluationContext object with the passed EvaluationContext, with the passed EvaluationContext overriding in
68-
* case of conflict.
67+
* Merges this EvaluationContext object with the passed EvaluationContext, overriding in case of conflict.
6968
*
7069
* @param overridingContext overriding context
7170
* @return resulting merged context

src/test/java/dev/openfeature/sdk/ImmutableContextTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.HashMap;
77

88
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
910
import static org.junit.jupiter.api.Assertions.assertThrows;
1011

1112
public class ImmutableContextTest {
@@ -27,4 +28,40 @@ void shouldCreateCopyOfAttributesForImmutableContext() {
2728
attributes.put("key3", new Value("val3"));
2829
assertArrayEquals(ctx.keySet().toArray(), new Object[]{"key1", "key2"});
2930
}
31+
32+
@DisplayName("targeting key should be changed from the overriding context")
33+
@Test
34+
void shouldChangeTargetingKeyFromOverridingContext() {
35+
HashMap<String, Value> attributes = new HashMap<>();
36+
attributes.put("key1", new Value("val1"));
37+
attributes.put("key2", new Value("val2"));
38+
EvaluationContext ctx = new ImmutableContext("targeting key", attributes);
39+
EvaluationContext overriding = new ImmutableContext("overriding_key");
40+
EvaluationContext merge = ctx.merge(overriding);
41+
assertEquals("overriding_key", merge.getTargetingKey());
42+
}
43+
44+
@DisplayName("targeting key should not changed from the overriding context if missing")
45+
@Test
46+
void shouldRetainTargetingKeyWhenOverridingContextTargetingKeyValueIsEmpty() {
47+
HashMap<String, Value> attributes = new HashMap<>();
48+
attributes.put("key1", new Value("val1"));
49+
attributes.put("key2", new Value("val2"));
50+
EvaluationContext ctx = new ImmutableContext("targeting_key", attributes);
51+
EvaluationContext overriding = new ImmutableContext("");
52+
EvaluationContext merge = ctx.merge(overriding);
53+
assertEquals("targeting_key", merge.getTargetingKey());
54+
}
55+
56+
@DisplayName("Merge should retain all the attributes from the existing context when overriding context is null")
57+
@Test
58+
void mergeShouldReturnAllTheValuesFromTheContextWhenOverridingContextIsNull() {
59+
HashMap<String, Value> attributes = new HashMap<>();
60+
attributes.put("key1", new Value("val1"));
61+
attributes.put("key2", new Value("val2"));
62+
EvaluationContext ctx = new ImmutableContext("targeting_key", attributes);
63+
EvaluationContext merge = ctx.merge(null);
64+
assertEquals("targeting_key", merge.getTargetingKey());
65+
assertArrayEquals(merge.keySet().toArray(), new Object[]{"key1", "key2"});
66+
}
3067
}

0 commit comments

Comments
 (0)