1
1
package dev .openfeature .contrib .providers .flagd .resolver .process .targeting ;
2
2
3
3
import static org .junit .jupiter .api .Assertions .assertEquals ;
4
+ import static org .junit .jupiter .api .Assertions .assertTrue ;
4
5
5
6
import java .util .HashMap ;
6
7
import java .util .Map ;
8
+ import java .time .Instant ;
7
9
8
10
import org .junit .jupiter .api .BeforeAll ;
9
11
import org .junit .jupiter .api .Test ;
@@ -33,6 +35,45 @@ void flagKeyPresent() throws TargetingRuleException {
33
35
assertEquals (true , evalVariant );
34
36
}
35
37
38
+ @ Test
39
+ void timestampPresent () throws TargetingRuleException {
40
+ // given
41
+
42
+ // rule asserting $flagd.timestamp is a number (i.e., a Unix timestamp)
43
+ final String targetingRule = "{\" var\" :[\" $flagd.timestamp\" ]}" ;
44
+
45
+ // when
46
+ Object timestampString = OPERATOR .apply ("some-key" , targetingRule , new ImmutableContext ());
47
+
48
+ long timestamp = (long ) Double .parseDouble (timestampString .toString ());
49
+
50
+ // generating current unix timestamp & 5 minute threshold
51
+ long currentTimestamp = Instant .now ().getEpochSecond ();
52
+ long thresholdPast = currentTimestamp - (5 );
53
+ long thresholdFuture = currentTimestamp + (5 );
54
+
55
+ // checks if the timestamp is within 5 minutes of the current time
56
+ assertTrue (timestamp >= thresholdPast && timestamp <= thresholdFuture );
57
+ }
58
+
59
+ @ Test
60
+ void testFlagPropertiesConstructor () {
61
+ // Given
62
+ Map <String , Object > flagdProperties = new HashMap <>();
63
+ flagdProperties .put (Operator .FLAG_KEY , "some-key" );
64
+ flagdProperties .put (Operator .TIME_STAMP , 1634000000L );
65
+
66
+ Map <String , Object > dataMap = new HashMap <>();
67
+ dataMap .put (Operator .FLAGD_PROPS_KEY , flagdProperties );
68
+
69
+ // When
70
+ Operator .FlagProperties flagProperties = new Operator .FlagProperties (dataMap );
71
+
72
+ // Then
73
+ assertEquals ("some-key" , flagProperties .getFlagKey ());
74
+ assertEquals (1634000000L , flagProperties .getTimestamp ());
75
+ }
76
+
36
77
@ Test
37
78
void fractionalTestA () throws TargetingRuleException {
38
79
// given
@@ -64,7 +105,6 @@ void fractionalTestA() throws TargetingRuleException {
64
105
Map <String , Value > ctxData = new HashMap <>();
65
106
ctxData .
put (
"email" ,
new Value (
"[email protected] " ));
66
107
67
-
68
108
// when
69
109
Object evalVariant = OPERATOR .apply ("headerColor" , targetingRule , new ImmutableContext (ctxData ));
70
110
@@ -103,7 +143,6 @@ void fractionalTestB() throws TargetingRuleException {
103
143
Map <String , Value > ctxData = new HashMap <>();
104
144
ctxData .
put (
"email" ,
new Value (
"[email protected] " ));
105
145
106
-
107
146
// when
108
147
Object evalVariant = OPERATOR .apply ("headerColor" , targetingRule , new ImmutableContext (ctxData ));
109
148
@@ -142,7 +181,6 @@ void fractionalTestC() throws TargetingRuleException {
142
181
Map <String , Value > ctxData = new HashMap <>();
143
182
ctxData .
put (
"email" ,
new Value (
"[email protected] " ));
144
183
145
-
146
184
// when
147
185
Object evalVariant = OPERATOR .apply ("headerColor" , targetingRule , new ImmutableContext (ctxData ));
148
186
@@ -166,7 +204,6 @@ void stringCompStartsWith() throws TargetingRuleException {
166
204
Map <String , Value > ctxData = new HashMap <>();
167
205
ctxData .
put (
"email" ,
new Value (
"[email protected] " ));
168
206
169
-
170
207
// when
171
208
Object evalVariant = OPERATOR .apply ("adminRule" , targetingRule , new ImmutableContext (ctxData ));
172
209
@@ -190,7 +227,6 @@ void stringCompEndsWith() throws TargetingRuleException {
190
227
Map <String , Value > ctxData = new HashMap <>();
191
228
ctxData .
put (
"email" ,
new Value (
"[email protected] " ));
192
229
193
-
194
230
// when
195
231
Object evalVariant = OPERATOR .apply ("isFaas" , targetingRule , new ImmutableContext (ctxData ));
196
232
@@ -215,7 +251,6 @@ void semVerA() throws TargetingRuleException {
215
251
Map <String , Value > ctxData = new HashMap <>();
216
252
ctxData .put ("version" , new Value ("1.1.0" ));
217
253
218
-
219
254
// when
220
255
Object evalVariant = OPERATOR .apply ("versionFlag" , targetingRule , new ImmutableContext (ctxData ));
221
256
0 commit comments