Skip to content

Commit 7fc10c2

Browse files
fix: issue #859 Added failing tests
1 parent e1e15f4 commit 7fc10c2

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

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

+13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package dev.openfeature.sdk;
22

3+
import java.util.Collections;
4+
import java.util.Map;
35
import org.junit.jupiter.api.DisplayName;
46
import org.junit.jupiter.api.Test;
57

@@ -12,6 +14,17 @@
1214
import static org.junit.jupiter.api.Assertions.assertTrue;
1315

1416
class ImmutableContextTest {
17+
@DisplayName("attributes unable to allow mutation should not affect the immutable context")
18+
@Test
19+
void shouldNotAttemptToModifyAttributesForImmutableContext() {
20+
final Map<String, Value> attributes = new HashMap<>();
21+
attributes.put("key1", new Value("val1"));
22+
attributes.put("key2", new Value("val2"));
23+
// should check the usage of Map.of() which is a more likely use case, but that API isn't available in Java 8
24+
EvaluationContext ctx = new ImmutableContext("targeting key", Collections.unmodifiableMap(attributes));
25+
attributes.put("key3", new Value("val3"));
26+
assertArrayEquals(new Object[]{"key1", "key2", TARGETING_KEY}, ctx.keySet().toArray());
27+
}
1528

1629
@DisplayName("attributes mutation should not affect the immutable context")
1730
@Test
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package dev.openfeature.sdk;
2+
3+
import java.util.Collections;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
import org.junit.jupiter.api.DisplayName;
7+
import org.junit.jupiter.api.Test;
8+
9+
10+
import static dev.openfeature.sdk.EvaluationContext.TARGETING_KEY;
11+
import static org.junit.jupiter.api.Assertions.*;
12+
13+
class MutableContextTest {
14+
@DisplayName("attributes unable to allow mutation should not affect the Mutable context")
15+
@Test
16+
void shouldNotAttemptToModifyAttributesForMutableContext() {
17+
final Map<String, Value> attributes = new HashMap<>();
18+
attributes.put("key1", new Value("val1"));
19+
attributes.put("key2", new Value("val2"));
20+
// should check the usage of Map.of() which is a more likely use case, but that API isn't available in Java 8
21+
EvaluationContext ctx = new MutableContext("targeting key", Collections.unmodifiableMap(attributes));
22+
attributes.put("key3", new Value("val3"));
23+
assertArrayEquals(new Object[]{"key1", "key2", TARGETING_KEY}, ctx.keySet().toArray());
24+
}
25+
}

0 commit comments

Comments
 (0)