Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: missing targeting key should return null #849

Merged
merged 3 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/main/java/dev/openfeature/sdk/ImmutableContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public ImmutableContext(String targetingKey, Map<String, Value> attributes) {
*/
@Override
public String getTargetingKey() {
return this.getValue(TARGETING_KEY).asString();
Value value = this.getValue(TARGETING_KEY);
return value == null ? null : value.asString();
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/dev/openfeature/sdk/MutableContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ public void setTargetingKey(String targetingKey) {
*/
@Override
public String getTargetingKey() {
return this.getValue(TARGETING_KEY).asString();
Value value = this.getValue(TARGETING_KEY);
return value == null ? null : value.asString();
}

/**
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/dev/openfeature/sdk/ImmutableContextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ void shouldRetainTargetingKeyWhenOverridingContextTargetingKeyValueIsEmpty() {
EvaluationContext merge = ctx.merge(overriding);
assertEquals("targeting_key", merge.getTargetingKey());
}
@DisplayName("missing targeting key should return null")
@Test
void missingTargetingKeyShould() {
EvaluationContext ctx = new ImmutableContext();
assertEquals(null, ctx.getTargetingKey());
}

@DisplayName("Merge should retain all the attributes from the existing context when overriding context is null")
@Test
Expand Down
105 changes: 105 additions & 0 deletions src/test/java/dev/openfeature/sdk/MutableContextTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package dev.openfeature.sdk;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.HashMap;

import static dev.openfeature.sdk.EvaluationContext.TARGETING_KEY;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

class MutableContextTest {

@DisplayName("targeting key should be changed from the overriding context")
@Test
void shouldChangeTargetingKeyFromOverridingContext() {
HashMap<String, Value> attributes = new HashMap<>();
attributes.put("key1", new Value("val1"));
attributes.put("key2", new Value("val2"));
EvaluationContext ctx = new MutableContext("targeting key", attributes);
EvaluationContext overriding = new MutableContext("overriding_key");
EvaluationContext merge = ctx.merge(overriding);
assertEquals("overriding_key", merge.getTargetingKey());
}

@DisplayName("targeting key should not changed from the overriding context if missing")
@Test
void shouldRetainTargetingKeyWhenOverridingContextTargetingKeyValueIsEmpty() {
HashMap<String, Value> attributes = new HashMap<>();
attributes.put("key1", new Value("val1"));
attributes.put("key2", new Value("val2"));
EvaluationContext ctx = new MutableContext("targeting_key", attributes);
EvaluationContext overriding = new MutableContext("");
EvaluationContext merge = ctx.merge(overriding);
assertEquals("targeting_key", merge.getTargetingKey());
}
@DisplayName("missing targeting key should return null")
@Test
void missingTargetingKeyShould() {
EvaluationContext ctx = new MutableContext();
assertEquals(null, ctx.getTargetingKey());
}

@DisplayName("Merge should retain all the attributes from the existing context when overriding context is null")
@Test
void mergeShouldReturnAllTheValuesFromTheContextWhenOverridingContextIsNull() {
HashMap<String, Value> attributes = new HashMap<>();
attributes.put("key1", new Value("val1"));
attributes.put("key2", new Value("val2"));
EvaluationContext ctx = new MutableContext("targeting_key", attributes);
EvaluationContext merge = ctx.merge(null);
assertEquals("targeting_key", merge.getTargetingKey());
assertArrayEquals(new Object[]{"key1", "key2", TARGETING_KEY}, merge.keySet().toArray());
}

@DisplayName("Merge should retain subkeys from the existing context when the overriding context has the same targeting key")
@Test
void mergeShouldRetainItsSubkeysWhenOverridingContextHasTheSameKey() {
HashMap<String, Value> attributes = new HashMap<>();
HashMap<String, Value> overridingAttributes = new HashMap<>();
HashMap<String, Value> key1Attributes = new HashMap<>();
HashMap<String, Value> ovKey1Attributes = new HashMap<>();

key1Attributes.put("key1_1", new Value("val1_1"));
attributes.put("key1", new Value(new ImmutableStructure(key1Attributes)));
attributes.put("key2", new Value("val2"));
ovKey1Attributes.put("overriding_key1_1", new Value("overriding_val_1_1"));
overridingAttributes.put("key1", new Value(new ImmutableStructure(ovKey1Attributes)));

EvaluationContext ctx = new MutableContext("targeting_key", attributes);
EvaluationContext overriding = new MutableContext("targeting_key", overridingAttributes);
EvaluationContext merge = ctx.merge(overriding);
assertEquals("targeting_key", merge.getTargetingKey());
assertArrayEquals(new Object[]{"key1", "key2", TARGETING_KEY}, merge.keySet().toArray());

Value key1 = merge.getValue("key1");
assertTrue(key1.isStructure());

Structure value = key1.asStructure();
assertArrayEquals(new Object[]{"key1_1","overriding_key1_1"}, value.keySet().toArray());
}

@DisplayName("Merge should retain subkeys from the existing context when the overriding context doesn't have targeting key")
@Test
void mergeShouldRetainItsSubkeysWhenOverridingContextHasNoTargetingKey() {
HashMap<String, Value> attributes = new HashMap<>();
HashMap<String, Value> key1Attributes = new HashMap<>();

key1Attributes.put("key1_1", new Value("val1_1"));
attributes.put("key1", new Value(new ImmutableStructure(key1Attributes)));
attributes.put("key2", new Value("val2"));

EvaluationContext ctx = new MutableContext(attributes);
EvaluationContext overriding = new MutableContext();
EvaluationContext merge = ctx.merge(overriding);
assertArrayEquals(new Object[]{"key1", "key2"}, merge.keySet().toArray());

Value key1 = merge.getValue("key1");
assertTrue(key1.isStructure());

Structure value = key1.asStructure();
assertArrayEquals(new Object[]{"key1_1"}, value.keySet().toArray());
}
}
Loading