From 7fc10c2842ca74843bde8eb34dcc1fd225becc06 Mon Sep 17 00:00:00 2001 From: Adam Roberts Date: Thu, 21 Mar 2024 13:03:07 -0400 Subject: [PATCH] fix: issue #859 Added failing tests --- .../openfeature/sdk/ImmutableContextTest.java | 13 ++++++++++ .../openfeature/sdk/MutableContextTest.java | 25 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 src/test/java/dev/openfeature/sdk/MutableContextTest.java diff --git a/src/test/java/dev/openfeature/sdk/ImmutableContextTest.java b/src/test/java/dev/openfeature/sdk/ImmutableContextTest.java index 7cfedec99..4e27eb774 100644 --- a/src/test/java/dev/openfeature/sdk/ImmutableContextTest.java +++ b/src/test/java/dev/openfeature/sdk/ImmutableContextTest.java @@ -1,5 +1,7 @@ package dev.openfeature.sdk; +import java.util.Collections; +import java.util.Map; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -12,6 +14,17 @@ import static org.junit.jupiter.api.Assertions.assertTrue; class ImmutableContextTest { + @DisplayName("attributes unable to allow mutation should not affect the immutable context") + @Test + void shouldNotAttemptToModifyAttributesForImmutableContext() { + final Map attributes = new HashMap<>(); + attributes.put("key1", new Value("val1")); + attributes.put("key2", new Value("val2")); + // should check the usage of Map.of() which is a more likely use case, but that API isn't available in Java 8 + EvaluationContext ctx = new ImmutableContext("targeting key", Collections.unmodifiableMap(attributes)); + attributes.put("key3", new Value("val3")); + assertArrayEquals(new Object[]{"key1", "key2", TARGETING_KEY}, ctx.keySet().toArray()); + } @DisplayName("attributes mutation should not affect the immutable context") @Test diff --git a/src/test/java/dev/openfeature/sdk/MutableContextTest.java b/src/test/java/dev/openfeature/sdk/MutableContextTest.java new file mode 100644 index 000000000..4a728f3bd --- /dev/null +++ b/src/test/java/dev/openfeature/sdk/MutableContextTest.java @@ -0,0 +1,25 @@ +package dev.openfeature.sdk; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + + +import static dev.openfeature.sdk.EvaluationContext.TARGETING_KEY; +import static org.junit.jupiter.api.Assertions.*; + +class MutableContextTest { + @DisplayName("attributes unable to allow mutation should not affect the Mutable context") + @Test + void shouldNotAttemptToModifyAttributesForMutableContext() { + final Map attributes = new HashMap<>(); + attributes.put("key1", new Value("val1")); + attributes.put("key2", new Value("val2")); + // should check the usage of Map.of() which is a more likely use case, but that API isn't available in Java 8 + EvaluationContext ctx = new MutableContext("targeting key", Collections.unmodifiableMap(attributes)); + attributes.put("key3", new Value("val3")); + assertArrayEquals(new Object[]{"key1", "key2", TARGETING_KEY}, ctx.keySet().toArray()); + } +}