Skip to content

Commit

Permalink
fix: issue open-feature#859 Added failing tests
Browse files Browse the repository at this point in the history
Signed-off-by: Adam Roberts <adam.roberts@collibra.com>
  • Loading branch information
ARobertsCollibra authored and toddbaert committed Mar 22, 2024
1 parent 48a196c commit ce44071
Show file tree
Hide file tree
Showing 2 changed files with 27 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
14 changes: 14 additions & 0 deletions src/test/java/dev/openfeature/sdk/MutableContextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import static dev.openfeature.sdk.EvaluationContext.TARGETING_KEY;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
Expand All @@ -12,6 +14,18 @@

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());
}

@DisplayName("targeting key should be changed from the overriding context")
@Test
void shouldChangeTargetingKeyFromOverridingContext() {
Expand Down

0 comments on commit ce44071

Please sign in to comment.