Skip to content

Commit 888d9c5

Browse files
committed
updated tests to use ImmutableContext implementation
Signed-off-by: thiyagu06 <[email protected]>
1 parent 6774992 commit 888d9c5

9 files changed

+160
-105
lines changed

src/main/java/dev/openfeature/sdk/ImmutableContext.java

+11
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,23 @@ public ImmutableContext() {
3030
this.targetingKey = "";
3131
}
3232

33+
/**
34+
* create an immutable context with an attributes provided.
35+
*
36+
* @param attributes evaluation context attributes
37+
*/
3338
public ImmutableContext(Map<String, Value> attributes) {
3439
HashMap<String, Value> copy = new HashMap<>(attributes);
3540
this.structure = new MutableStructure(copy);
3641
this.targetingKey = "";
3742
}
3843

44+
/**
45+
* create an immutable context with given targetingKey and attributes provided.
46+
*
47+
* @param targetingKey targeting key
48+
* @param attributes evaluation context attributes
49+
*/
3950
public ImmutableContext(String targetingKey, Map<String, Value> attributes) {
4051
this(attributes);
4152
this.targetingKey = targetingKey;

src/test/java/dev/openfeature/sdk/DeveloperExperienceTest.java

+10-8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import static org.mockito.Mockito.verify;
88

99
import java.util.Arrays;
10+
import java.util.HashMap;
11+
import java.util.List;
1012
import java.util.Map;
1113
import java.util.Optional;
1214

@@ -70,14 +72,14 @@ class DeveloperExperienceTest implements HookFixtures {
7072
OpenFeatureAPI api = OpenFeatureAPI.getInstance();
7173
api.setProvider(new NoOpProvider());
7274
Client client = api.getClient();
73-
74-
MutableContext ctx = new MutableContext()
75-
.add("int-val", 3)
76-
.add("double-val", 4.0)
77-
.add("str-val", "works")
78-
.add("bool-val", false)
79-
.add("value-val", Arrays.asList(new Value(2), new Value(4)));
80-
75+
Map<String, Value> attributes = new HashMap<>();
76+
List<Value> values = Arrays.asList(new Value(2), new Value(4));
77+
attributes.put("int-val", new Value(3));
78+
attributes.put("double-val", new Value(4.0));
79+
attributes.put("str-val", new Value("works"));
80+
attributes.put("bool-val", new Value(false));
81+
attributes.put("value-val", new Value(values));
82+
EvaluationContext ctx = new ImmutableContext(attributes);
8183
Boolean retval = client.getBooleanValue(flagKey, false, ctx);
8284
assertFalse(retval);
8385
}

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

+54-26
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,51 @@
88
import java.util.List;
99
import java.util.Map;
1010

11+
import io.cucumber.java.hu.Ha;
1112
import org.junit.jupiter.api.Test;
1213

1314
public class EvalContextTest {
1415
@Specification(number="3.1.1",
1516
text="The `evaluation context` structure **MUST** define an optional `targeting key` field of " +
1617
"type string, identifying the subject of the flag evaluation.")
1718
@Test void requires_targeting_key() {
18-
MutableContext ec = new MutableContext();
19-
ec.setTargetingKey("targeting-key");
19+
EvaluationContext ec = new ImmutableContext("targeting-key", new HashMap<>());
2020
assertEquals("targeting-key", ec.getTargetingKey());
2121
}
2222

2323
@Specification(number="3.1.2", text= "The evaluation context MUST support the inclusion of " +
2424
"custom fields, having keys of type `string`, and " +
2525
"values of type `boolean | string | number | datetime | structure`.")
2626
@Test void eval_context() {
27-
MutableContext ec = new MutableContext();
27+
Map<String, Value> attributes = new HashMap<>();
28+
Instant dt = Instant.now();
29+
attributes.put("str", new Value("test"));
30+
attributes.put("bool", new Value(true));
31+
attributes.put("int", new Value(4));
32+
attributes.put("dt", new Value(dt));
33+
EvaluationContext ec = new ImmutableContext(attributes);
2834

29-
ec.add("str", "test");
3035
assertEquals("test", ec.getValue("str").asString());
3136

32-
ec.add("bool", true);
3337
assertEquals(true, ec.getValue("bool").asBoolean());
3438

35-
ec.add("int", 4);
3639
assertEquals(4, ec.getValue("int").asInteger());
3740

38-
Instant dt = Instant.now();
39-
ec.add("dt", dt);
4041
assertEquals(dt, ec.getValue("dt").asInstant());
4142
}
4243

4344
@Specification(number="3.1.2", text="The evaluation context MUST support the inclusion of " +
4445
"custom fields, having keys of type `string`, and " +
4546
"values of type `boolean | string | number | datetime | structure`.")
4647
@Test void eval_context_structure_array() {
47-
MutableContext ec = new MutableContext();
48-
ec.add("obj", new MutableStructure().add("val1", 1).add("val2", "2"));
49-
ec.add("arr", new ArrayList<Value>(){{
48+
Map<String, Value> attributes = new HashMap<>();
49+
attributes.put("obj", new Value(new MutableStructure().add("val1", 1).add("val2", "2")));
50+
List<Value> values = new ArrayList<Value>(){{
5051
add(new Value("one"));
5152
add(new Value("two"));
52-
}});
53+
}};
54+
attributes.put("arr", new Value(values));
55+
EvaluationContext ec = new ImmutableContext(attributes);
5356

5457
Structure str = ec.getValue("obj").asStructure();
5558
assertEquals(1, str.getValue("val1").asInteger());
@@ -62,21 +65,18 @@ public class EvalContextTest {
6265

6366
@Specification(number="3.1.3", text="The evaluation context MUST support fetching the custom fields by key and also fetching all key value pairs.")
6467
@Test void fetch_all() {
65-
MutableContext ec = new MutableContext();
66-
67-
ec.add("str", "test");
68-
ec.add("str2", "test2");
69-
70-
ec.add("bool", true);
71-
ec.add("bool2", false);
72-
73-
ec.add("int", 4);
74-
ec.add("int2", 2);
75-
68+
Map<String, Value> attributes = new HashMap<>();
7669
Instant dt = Instant.now();
77-
ec.add("dt", dt);
78-
79-
ec.add("obj", new MutableStructure().add("val1", 1).add("val2", "2"));
70+
MutableStructure mutableStructure = new MutableStructure().add("val1", 1).add("val2", "2");
71+
attributes.put("str", new Value("test"));
72+
attributes.put("str2", new Value("test2"));
73+
attributes.put("bool", new Value(true));
74+
attributes.put("bool2", new Value(false));
75+
attributes.put("int", new Value(4));
76+
attributes.put("int2", new Value(2));
77+
attributes.put("dt", new Value(dt));
78+
attributes.put("obj", new Value(mutableStructure));
79+
EvaluationContext ec = new ImmutableContext(attributes);
8080

8181
Map<String, Value> foundStr = ec.asMap();
8282
assertEquals(ec.getValue("str").asString(), foundStr.get("str").asString());
@@ -106,6 +106,16 @@ public class EvalContextTest {
106106
assertEquals(3, ec.getValue("key").asInteger());
107107
}
108108

109+
@Test void unique_key_across_types_immutableContext() {
110+
HashMap<String, Value> attributes = new HashMap<>();
111+
attributes.put("key", new Value("val"));
112+
attributes.put("key", new Value("val2"));
113+
attributes.put("key", new Value(3));
114+
EvaluationContext ec = new ImmutableContext(attributes);
115+
assertEquals(null, ec.getValue("key").asString());
116+
assertEquals(3, ec.getValue("key").asInteger());
117+
}
118+
109119
@Test void can_chain_attribute_addition() {
110120
MutableContext ec = new MutableContext();
111121
MutableContext out = ec.add("str", "test")
@@ -132,6 +142,24 @@ public class EvalContextTest {
132142
assertEquals(null, ec.getValue("Instant").asString());
133143
}
134144

145+
@Test void Immutable_context_merge_targeting_key() {
146+
String key1 = "key1";
147+
EvaluationContext ctx1 = new ImmutableContext(key1, new HashMap<>());
148+
EvaluationContext ctx2 = new ImmutableContext(new HashMap<>());
149+
150+
EvaluationContext ctxMerged = ctx1.merge(ctx2);
151+
assertEquals(key1, ctxMerged.getTargetingKey());
152+
153+
String key2 = "key2";
154+
ctx2 = new ImmutableContext(key2, new HashMap<>());
155+
ctxMerged = ctx1.merge(ctx2);
156+
assertEquals(key2, ctxMerged.getTargetingKey());
157+
158+
ctx2 = new ImmutableContext(" ",new HashMap<>());
159+
ctxMerged = ctx1.merge(ctx2);
160+
assertEquals(key1, ctxMerged.getTargetingKey());
161+
}
162+
135163
@Test void merge_targeting_key() {
136164
String key1 = "key1";
137165
EvaluationContext ctx1 = new MutableContext(key1);

src/test/java/dev/openfeature/sdk/FlagEvaluationSpecTest.java

+35-29
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111
import static org.mockito.Mockito.times;
1212
import static org.mockito.Mockito.verify;
1313

14+
import java.util.HashMap;
1415
import java.util.List;
16+
import java.util.Map;
1517

18+
import io.cucumber.java.hu.Ha;
1619
import org.junit.jupiter.api.AfterEach;
1720
import org.junit.jupiter.api.Test;
1821

@@ -100,24 +103,24 @@ private Client _client() {
100103
String key = "key";
101104

102105
assertEquals(true, c.getBooleanValue(key, false));
103-
assertEquals(true, c.getBooleanValue(key, false, new MutableContext()));
104-
assertEquals(true, c.getBooleanValue(key, false, new MutableContext(), FlagEvaluationOptions.builder().build()));
106+
assertEquals(true, c.getBooleanValue(key, false, new ImmutableContext()));
107+
assertEquals(true, c.getBooleanValue(key, false, new ImmutableContext(), FlagEvaluationOptions.builder().build()));
105108

106109
assertEquals("gnirts-ym", c.getStringValue(key, "my-string"));
107-
assertEquals("gnirts-ym", c.getStringValue(key, "my-string", new MutableContext()));
108-
assertEquals("gnirts-ym", c.getStringValue(key, "my-string", new MutableContext(), FlagEvaluationOptions.builder().build()));
110+
assertEquals("gnirts-ym", c.getStringValue(key, "my-string", new ImmutableContext()));
111+
assertEquals("gnirts-ym", c.getStringValue(key, "my-string", new ImmutableContext(), FlagEvaluationOptions.builder().build()));
109112

110113
assertEquals(400, c.getIntegerValue(key, 4));
111-
assertEquals(400, c.getIntegerValue(key, 4, new MutableContext()));
112-
assertEquals(400, c.getIntegerValue(key, 4, new MutableContext(), FlagEvaluationOptions.builder().build()));
114+
assertEquals(400, c.getIntegerValue(key, 4, new ImmutableContext()));
115+
assertEquals(400, c.getIntegerValue(key, 4, new ImmutableContext(), FlagEvaluationOptions.builder().build()));
113116

114117
assertEquals(40.0, c.getDoubleValue(key, .4));
115-
assertEquals(40.0, c.getDoubleValue(key, .4, new MutableContext()));
116-
assertEquals(40.0, c.getDoubleValue(key, .4, new MutableContext(), FlagEvaluationOptions.builder().build()));
118+
assertEquals(40.0, c.getDoubleValue(key, .4, new ImmutableContext()));
119+
assertEquals(40.0, c.getDoubleValue(key, .4, new ImmutableContext(), FlagEvaluationOptions.builder().build()));
117120

118121
assertEquals(null, c.getObjectValue(key, new Value()));
119-
assertEquals(null, c.getObjectValue(key, new Value(), new MutableContext()));
120-
assertEquals(null, c.getObjectValue(key, new Value(), new MutableContext(), FlagEvaluationOptions.builder().build()));
122+
assertEquals(null, c.getObjectValue(key, new Value(), new ImmutableContext()));
123+
assertEquals(null, c.getObjectValue(key, new Value(), new ImmutableContext(), FlagEvaluationOptions.builder().build()));
121124
}
122125

123126
@Specification(number="1.4.1", text="The client MUST provide methods for detailed flag value evaluation with parameters flag key (string, required), default value (boolean | number | string | structure, required), evaluation context (optional), and evaluation options (optional), which returns an evaluation details structure.")
@@ -138,33 +141,33 @@ private Client _client() {
138141
.variant(null)
139142
.build();
140143
assertEquals(bd, c.getBooleanDetails(key, true));
141-
assertEquals(bd, c.getBooleanDetails(key, true, new MutableContext()));
142-
assertEquals(bd, c.getBooleanDetails(key, true, new MutableContext(), FlagEvaluationOptions.builder().build()));
144+
assertEquals(bd, c.getBooleanDetails(key, true, new ImmutableContext()));
145+
assertEquals(bd, c.getBooleanDetails(key, true, new ImmutableContext(), FlagEvaluationOptions.builder().build()));
143146

144147
FlagEvaluationDetails<String> sd = FlagEvaluationDetails.<String>builder()
145148
.flagKey(key)
146149
.value("tset")
147150
.variant(null)
148151
.build();
149152
assertEquals(sd, c.getStringDetails(key, "test"));
150-
assertEquals(sd, c.getStringDetails(key, "test", new MutableContext()));
151-
assertEquals(sd, c.getStringDetails(key, "test", new MutableContext(), FlagEvaluationOptions.builder().build()));
153+
assertEquals(sd, c.getStringDetails(key, "test", new ImmutableContext()));
154+
assertEquals(sd, c.getStringDetails(key, "test", new ImmutableContext(), FlagEvaluationOptions.builder().build()));
152155

153156
FlagEvaluationDetails<Integer> id = FlagEvaluationDetails.<Integer>builder()
154157
.flagKey(key)
155158
.value(400)
156159
.build();
157160
assertEquals(id, c.getIntegerDetails(key, 4));
158-
assertEquals(id, c.getIntegerDetails(key, 4, new MutableContext()));
159-
assertEquals(id, c.getIntegerDetails(key, 4, new MutableContext(), FlagEvaluationOptions.builder().build()));
161+
assertEquals(id, c.getIntegerDetails(key, 4, new ImmutableContext()));
162+
assertEquals(id, c.getIntegerDetails(key, 4, new ImmutableContext(), FlagEvaluationOptions.builder().build()));
160163

161164
FlagEvaluationDetails<Double> dd = FlagEvaluationDetails.<Double>builder()
162165
.flagKey(key)
163166
.value(40.0)
164167
.build();
165168
assertEquals(dd, c.getDoubleDetails(key, .4));
166-
assertEquals(dd, c.getDoubleDetails(key, .4, new MutableContext()));
167-
assertEquals(dd, c.getDoubleDetails(key, .4, new MutableContext(), FlagEvaluationOptions.builder().build()));
169+
assertEquals(dd, c.getDoubleDetails(key, .4, new ImmutableContext()));
170+
assertEquals(dd, c.getDoubleDetails(key, .4, new ImmutableContext(), FlagEvaluationOptions.builder().build()));
168171

169172
// TODO: Structure detail tests.
170173
}
@@ -233,22 +236,25 @@ private Client _client() {
233236
DoSomethingProvider provider = new DoSomethingProvider();
234237
api.setProvider(provider);
235238

236-
MutableContext apiCtx = new MutableContext();
237-
apiCtx.add("common", "1");
238-
apiCtx.add("common2", "1");
239-
apiCtx.add("api", "2");
239+
Map<String, Value> attributes = new HashMap<>();
240+
attributes.put("common", new Value("1"));
241+
attributes.put("common2", new Value("1"));
242+
attributes.put("api", new Value("2"));
243+
EvaluationContext apiCtx = new ImmutableContext(attributes);
244+
240245
api.setEvaluationContext(apiCtx);
241246

242247
Client c = api.getClient();
243-
MutableContext clientCtx = new MutableContext();
244-
clientCtx.add("common", "3");
245-
clientCtx.add("common2", "3");
246-
clientCtx.add("client", "4");
248+
Map<String, Value> attributes1 = new HashMap<>();
249+
attributes.put("common", new Value("3"));
250+
attributes.put("common2", new Value("3"));
251+
attributes.put("client", new Value("4"));
252+
attributes.put("common", new Value("5"));
253+
attributes.put("invocation", new Value("6"));
254+
EvaluationContext clientCtx = new ImmutableContext(attributes);
247255
c.setEvaluationContext(clientCtx);
248256

249-
MutableContext invocationCtx = new MutableContext();
250-
clientCtx.add("common", "5");
251-
clientCtx.add("invocation", "6");
257+
EvaluationContext invocationCtx = new ImmutableContext();
252258

253259
// dosomethingprovider inverts this value.
254260
assertTrue(c.getBooleanValue("key", false, invocationCtx));

src/test/java/dev/openfeature/sdk/HookContextTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class HookContextTest {
1515
FlagValueType.BOOLEAN,
1616
meta,
1717
meta,
18-
new MutableContext(),
18+
new ImmutableContext(),
1919
false
2020
);
2121

0 commit comments

Comments
 (0)