Merge pull request #41 from rgrassian-split/evalContextIterator

iterators to get all integer, string, boolean, or structure map entries
This commit is contained in:
Justin Abrahms 2022-08-03 15:54:05 -07:00 committed by GitHub
commit e7757eca85
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 70 additions and 4 deletions

View File

@ -39,6 +39,10 @@ public class EvaluationContext {
return objMapper.readValue(val, klass);
}
public Map<String, String> getStructureAttributes() {
return new HashMap<>(jsonAttributes);
}
public void addStringAttribute(String key, String value) {
stringAttributes.put(key, value);
}
@ -47,6 +51,10 @@ public class EvaluationContext {
return stringAttributes.get(key);
}
public Map<String, String> getStringAttributes() {
return new HashMap<>(stringAttributes);
}
public void addIntegerAttribute(String key, Integer value) {
integerAttributes.put(key, value);
}
@ -55,6 +63,10 @@ public class EvaluationContext {
return integerAttributes.get(key);
}
public Map<String, Integer> getIntegerAttributes() {
return new HashMap<>(integerAttributes);
}
public Boolean getBooleanAttribute(String key) {
return booleanAttributes.get(key);
}
@ -63,6 +75,10 @@ public class EvaluationContext {
booleanAttributes.put(key, b);
}
public Map<String, Boolean> getBooleanAttributes() {
return new HashMap<>(booleanAttributes);
}
public void addDatetimeAttribute(String key, ZonedDateTime value) {
this.stringAttributes.put(key, value.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
}

View File

@ -1,13 +1,19 @@
package dev.openfeature.javasdk;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import java.time.ZonedDateTime;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
public class EvalContextTest {
@Specification(number="3.1",
@Specification(number="3.1.1",
text="The `evaluation context` structure **MUST** define an optional `targeting key` field of " +
"type string, identifying the subject of the flag evaluation.")
@Test void requires_targeting_key() {
@ -16,7 +22,7 @@ public class EvalContextTest {
assertEquals("targeting-key", ec.getTargetingKey());
}
@Specification(number="3.2", text="The evaluation context MUST support the inclusion of " +
@Specification(number="3.1.2", text="The evaluation context MUST support the inclusion of " +
"custom fields, having keys of type `string`, and " +
"values of type `boolean | string | number | datetime | structure`.")
@Test void eval_context() {
@ -36,7 +42,7 @@ public class EvalContextTest {
assertEquals(dt, ec.getDatetimeAttribute("dt"));
}
@Specification(number="3.2", text="The evaluation context MUST support the inclusion of " +
@Specification(number="3.1.2", text="The evaluation context MUST support the inclusion of " +
"custom fields, having keys of type `string`, and " +
"values of type `boolean | string | number | datetime | structure`.")
@Test void eval_context__structure() {
@ -59,4 +65,48 @@ public class EvalContextTest {
assertEquals(2, nodeFromString.value);
assertEquals(4, nodeFromString.left.value);
}
@Specification(number="3.1.3", text="The evaluation context MUST support fetching the custom fields by key and " +
"also fetching all of the keys and values.")
@Test void fetch_all() {
EvaluationContext ec = new EvaluationContext();
ec.addStringAttribute("str", "test");
ec.addStringAttribute("str2", "test2");
ec.addBooleanAttribute("bool", true);
ec.addBooleanAttribute("bool2", false);
ec.addIntegerAttribute("int", 4);
ec.addIntegerAttribute("int2", 2);
ZonedDateTime dt = ZonedDateTime.now();
ec.addDatetimeAttribute("dt", dt);
Node<Integer> n1 = new Node<>();
n1.value = 4;
Node<Integer> n2 = new Node<>();
n2.value = 2;
n2.left = n1;
ec.addStructureAttribute("obj", n2);
Map<String, String> foundStr = ec.getStringAttributes();
assertEquals(ec.getStringAttribute("str"), foundStr.get("str"));
assertEquals(ec.getStringAttribute("str2"), foundStr.get("str2"));
Map<String, Boolean> foundBool = ec.getBooleanAttributes();
assertEquals(ec.getBooleanAttribute("bool"), foundBool.get("bool"));
assertEquals(ec.getBooleanAttribute("bool2"), foundBool.get("bool2"));
Map<String, Integer> foundInt = ec.getIntegerAttributes();
assertEquals(ec.getIntegerAttribute("int"), foundInt.get("int"));
assertEquals(ec.getIntegerAttribute("int2"), foundInt.get("int2"));
Map<String, String> foundObj = ec.getStructureAttributes();
try {
assertEquals(ec.getStructureAttribute("obj", Node.class), new ObjectMapper().readValue(foundObj.get("obj"), Node.class));
} catch (JsonProcessingException e) {
fail("Unexpected exception occurred: ", e);
}
}
}

View File

@ -411,7 +411,7 @@ public class HookSpecTest implements HookFixtures {
Client client = getClient(null);
client.getBooleanValue("key", false, new EvaluationContext(),
client.getBooleanValue("key", false, ctx,
FlagEvaluationOptions.builder()
.hook(hook2)
.hook(hook)