1
1
package dev .openfeature .javasdk ;
2
2
3
+ import com .fasterxml .jackson .core .JsonProcessingException ;
4
+ import com .fasterxml .jackson .databind .ObjectMapper ;
3
5
import org .junit .jupiter .api .Test ;
4
6
5
7
import java .time .ZonedDateTime ;
8
+ import java .util .Map ;
6
9
7
10
import static org .junit .jupiter .api .Assertions .assertEquals ;
11
+ import static org .junit .jupiter .api .Assertions .assertFalse ;
12
+ import static org .junit .jupiter .api .Assertions .assertTrue ;
13
+ import static org .junit .jupiter .api .Assertions .fail ;
8
14
9
15
public class EvalContextTest {
10
- @ Specification (number ="3.1" ,
16
+ @ Specification (number ="3.1.1 " ,
11
17
text ="The `evaluation context` structure **MUST** define an optional `targeting key` field of " +
12
18
"type string, identifying the subject of the flag evaluation." )
13
19
@ Test void requires_targeting_key () {
@@ -16,7 +22,7 @@ public class EvalContextTest {
16
22
assertEquals ("targeting-key" , ec .getTargetingKey ());
17
23
}
18
24
19
- @ Specification (number ="3.2" , text ="The evaluation context MUST support the inclusion of " +
25
+ @ Specification (number ="3.1. 2" , text ="The evaluation context MUST support the inclusion of " +
20
26
"custom fields, having keys of type `string`, and " +
21
27
"values of type `boolean | string | number | datetime | structure`." )
22
28
@ Test void eval_context () {
@@ -36,7 +42,7 @@ public class EvalContextTest {
36
42
assertEquals (dt , ec .getDatetimeAttribute ("dt" ));
37
43
}
38
44
39
- @ Specification (number ="3.2" , text ="The evaluation context MUST support the inclusion of " +
45
+ @ Specification (number ="3.1. 2" , text ="The evaluation context MUST support the inclusion of " +
40
46
"custom fields, having keys of type `string`, and " +
41
47
"values of type `boolean | string | number | datetime | structure`." )
42
48
@ Test void eval_context__structure () {
@@ -59,4 +65,48 @@ public class EvalContextTest {
59
65
assertEquals (2 , nodeFromString .value );
60
66
assertEquals (4 , nodeFromString .left .value );
61
67
}
68
+
69
+ @ Specification (number ="3.1.3" , text ="The evaluation context MUST support fetching the custom fields by key and " +
70
+ "also fetching all of the keys and values." )
71
+ @ Test void fetch_all () {
72
+ EvaluationContext ec = new EvaluationContext ();
73
+
74
+ ec .addStringAttribute ("str" , "test" );
75
+ ec .addStringAttribute ("str2" , "test2" );
76
+
77
+ ec .addBooleanAttribute ("bool" , true );
78
+ ec .addBooleanAttribute ("bool2" , false );
79
+
80
+ ec .addIntegerAttribute ("int" , 4 );
81
+ ec .addIntegerAttribute ("int2" , 2 );
82
+
83
+ ZonedDateTime dt = ZonedDateTime .now ();
84
+ ec .addDatetimeAttribute ("dt" , dt );
85
+
86
+ Node <Integer > n1 = new Node <>();
87
+ n1 .value = 4 ;
88
+ Node <Integer > n2 = new Node <>();
89
+ n2 .value = 2 ;
90
+ n2 .left = n1 ;
91
+ ec .addStructureAttribute ("obj" , n2 );
92
+
93
+ Map <String , String > foundStr = ec .getStringAttributes ();
94
+ assertEquals (ec .getStringAttribute ("str" ), foundStr .get ("str" ));
95
+ assertEquals (ec .getStringAttribute ("str2" ), foundStr .get ("str2" ));
96
+
97
+ Map <String , Boolean > foundBool = ec .getBooleanAttributes ();
98
+ assertEquals (ec .getBooleanAttribute ("bool" ), foundBool .get ("bool" ));
99
+ assertEquals (ec .getBooleanAttribute ("bool2" ), foundBool .get ("bool2" ));
100
+
101
+ Map <String , Integer > foundInt = ec .getIntegerAttributes ();
102
+ assertEquals (ec .getIntegerAttribute ("int" ), foundInt .get ("int" ));
103
+ assertEquals (ec .getIntegerAttribute ("int2" ), foundInt .get ("int2" ));
104
+
105
+ Map <String , String > foundObj = ec .getStructureAttributes ();
106
+ try {
107
+ assertEquals (ec .getStructureAttribute ("obj" , Node .class ), new ObjectMapper ().readValue (foundObj .get ("obj" ), Node .class ));
108
+ } catch (JsonProcessingException e ) {
109
+ fail ("Unexpected exception occurred: " , e );
110
+ }
111
+ }
62
112
}
0 commit comments