Skip to content

Commit a60d530

Browse files
use copy of map rather than iterator, tests
Signed-off-by: Robert Grassian <robert.grassian@split.io>
1 parent 3fe2b7a commit a60d530

File tree

2 files changed

+54
-12
lines changed

2 files changed

+54
-12
lines changed

src/main/java/dev/openfeature/javasdk/EvaluationContext.java

+8-9
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import java.time.ZonedDateTime;
77
import java.time.format.DateTimeFormatter;
88
import java.util.HashMap;
9-
import java.util.Iterator;
109
import java.util.Map;
1110

1211
@ToString @EqualsAndHashCode
@@ -40,8 +39,8 @@ public <T> T getStructureAttribute(String key, Class<T> klass) {
4039
return objMapper.readValue(val, klass);
4140
}
4241

43-
public Iterator<Map.Entry<String, String>> getStructureAttributes() {
44-
return jsonAttributes.entrySet().iterator();
42+
public Map<String, String> getStructureAttributes() {
43+
return new HashMap<>(jsonAttributes);
4544
}
4645

4746
public void addStringAttribute(String key, String value) {
@@ -52,8 +51,8 @@ public String getStringAttribute(String key) {
5251
return stringAttributes.get(key);
5352
}
5453

55-
public Iterator<Map.Entry<String, String>> getStringAttributes() {
56-
return stringAttributes.entrySet().iterator();
54+
public Map<String, String> getStringAttributes() {
55+
return new HashMap<>(stringAttributes);
5756
}
5857

5958
public void addIntegerAttribute(String key, Integer value) {
@@ -64,8 +63,8 @@ public Integer getIntegerAttribute(String key) {
6463
return integerAttributes.get(key);
6564
}
6665

67-
public Iterator<Map.Entry<String, Integer>> getIntegerAttributes() {
68-
return integerAttributes.entrySet().iterator();
66+
public Map<String, Integer> getIntegerAttributes() {
67+
return new HashMap<>(integerAttributes);
6968
}
7069

7170
public Boolean getBooleanAttribute(String key) {
@@ -76,8 +75,8 @@ public void addBooleanAttribute(String key, Boolean b) {
7675
booleanAttributes.put(key, b);
7776
}
7877

79-
public Iterator<Map.Entry<String, Boolean>> getBooleanAttributes() {
80-
return booleanAttributes.entrySet().iterator();
78+
public Map<String, Boolean> getBooleanAttributes() {
79+
return new HashMap<>(booleanAttributes);
8180
}
8281

8382
public void addDatetimeAttribute(String key, ZonedDateTime value) {

src/test/java/dev/openfeature/javasdk/EvalContextTest.java

+46-3
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
import org.junit.jupiter.api.Test;
44

55
import java.time.ZonedDateTime;
6+
import java.util.Map;
67

78
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
import static org.junit.jupiter.api.Assertions.assertFalse;
10+
import static org.junit.jupiter.api.Assertions.assertTrue;
811

912
public class EvalContextTest {
10-
@Specification(number="3.1",
13+
@Specification(number="3.1.1",
1114
text="The `evaluation context` structure **MUST** define an optional `targeting key` field of " +
1215
"type string, identifying the subject of the flag evaluation.")
1316
@Test void requires_targeting_key() {
@@ -16,7 +19,7 @@ public class EvalContextTest {
1619
assertEquals("targeting-key", ec.getTargetingKey());
1720
}
1821

19-
@Specification(number="3.2", text="The evaluation context MUST support the inclusion of " +
22+
@Specification(number="3.1.2", text="The evaluation context MUST support the inclusion of " +
2023
"custom fields, having keys of type `string`, and " +
2124
"values of type `boolean | string | number | datetime | structure`.")
2225
@Test void eval_context() {
@@ -36,7 +39,7 @@ public class EvalContextTest {
3639
assertEquals(dt, ec.getDatetimeAttribute("dt"));
3740
}
3841

39-
@Specification(number="3.2", text="The evaluation context MUST support the inclusion of " +
42+
@Specification(number="3.1.2", text="The evaluation context MUST support the inclusion of " +
4043
"custom fields, having keys of type `string`, and " +
4144
"values of type `boolean | string | number | datetime | structure`.")
4245
@Test void eval_context__structure() {
@@ -59,4 +62,44 @@ public class EvalContextTest {
5962
assertEquals(2, nodeFromString.value);
6063
assertEquals(4, nodeFromString.left.value);
6164
}
65+
66+
@Specification(number="3.1.3", text="The evaluation context MUST support fetching the custom fields by key and " +
67+
"also fetching all of the keys and values.")
68+
@Test void fetch_all() {
69+
EvaluationContext ec = new EvaluationContext();
70+
71+
ec.addStringAttribute("str", "test");
72+
ec.addStringAttribute("str2", "test2");
73+
74+
ec.addBooleanAttribute("bool", true);
75+
ec.addBooleanAttribute("bool2", false);
76+
77+
ec.addIntegerAttribute("int", 4);
78+
ec.addIntegerAttribute("int2", 2);
79+
80+
ZonedDateTime dt = ZonedDateTime.now();
81+
ec.addDatetimeAttribute("dt", dt);
82+
83+
Node<Integer> n1 = new Node<>();
84+
n1.value = 4;
85+
Node<Integer> n2 = new Node<>();
86+
n2.value = 2;
87+
n2.left = n1;
88+
ec.addStructureAttribute("obj", n2);
89+
90+
Map<String, String> foundStr = ec.getStringAttributes();
91+
assertEquals("test", foundStr.get("str"));
92+
assertEquals("test2", foundStr.get("str2"));
93+
94+
Map<String, Boolean> foundBool = ec.getBooleanAttributes();
95+
assertTrue(foundBool.get("bool"));
96+
assertFalse(foundBool.get("bool2"));
97+
98+
Map<String, Integer> foundInt = ec.getIntegerAttributes();
99+
assertEquals(4, foundInt.get("int"));
100+
assertEquals(2, foundInt.get("int2"));
101+
102+
Map<String, String> foundObj = ec.getStructureAttributes();
103+
assertEquals("{\"left\":{\"left\":null,\"right\":null,\"value\":4},\"right\":null,\"value\":2}", foundObj.get("obj"));
104+
}
62105
}

0 commit comments

Comments
 (0)