* fix: issue #859 Added failing tests

Signed-off-by: Adam Roberts <adam.roberts@collibra.com>

* fix: issue #859 Fixed the logic to allow the tests to pass by copying the passed in attributes

Signed-off-by: Adam Roberts <adam.roberts@collibra.com>

---------

Signed-off-by: Adam Roberts <adam.roberts@collibra.com>
This commit is contained in:
Adam Roberts 2024-03-22 14:15:16 -04:00 committed by GitHub
parent 675de140e4
commit d51cacbff6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 36 additions and 6 deletions

View File

@ -13,7 +13,7 @@ abstract class AbstractStructure implements Structure {
}
AbstractStructure(Map<String, Value> attributes) {
this.attributes = attributes;
this.attributes = new HashMap<>(attributes);
}
/**

View File

@ -52,9 +52,12 @@ public final class ImmutableContext implements EvaluationContext {
*/
public ImmutableContext(String targetingKey, Map<String, Value> attributes) {
if (targetingKey != null && !targetingKey.trim().isEmpty()) {
attributes.put(TARGETING_KEY, new Value(targetingKey));
final Map<String, Value> actualAttribs = new HashMap<>(attributes);
actualAttribs.put(TARGETING_KEY, new Value(targetingKey));
this.structure = new ImmutableStructure(actualAttribs);
} else {
this.structure = new ImmutableStructure(attributes);
}
this.structure = new ImmutableStructure(attributes);
}
/**

View File

@ -42,10 +42,10 @@ public class MutableContext implements EvaluationContext {
* @param attributes evaluation context attributes
*/
public MutableContext(String targetingKey, Map<String, Value> attributes) {
if (targetingKey != null && !targetingKey.trim().isEmpty()) {
attributes.put(TARGETING_KEY, new Value(targetingKey));
}
this.structure = new MutableStructure(attributes);
if (targetingKey != null && !targetingKey.trim().isEmpty()) {
this.structure.attributes.put(TARGETING_KEY, new Value(targetingKey));
}
}
// override @Delegate methods so that we can use "add" methods and still return MutableContext, not Structure

View File

@ -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.assertThrows;
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

View File

@ -3,7 +3,9 @@ package dev.openfeature.sdk;
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;
@ -12,6 +14,18 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
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() {