Skip to content

Commit

Permalink
fix: issue open-feature#859 Added failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ARobertsCollibra committed Mar 21, 2024
1 parent e1e15f4 commit 7fc10c2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/test/java/dev/openfeature/sdk/ImmutableContextTest.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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<String, Value> 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
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/dev/openfeature/sdk/MutableContextTest.java
Original file line number Diff line number Diff line change
@@ -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<String, Value> 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());
}
}

0 comments on commit 7fc10c2

Please sign in to comment.