4
4
5
5
import java .util .HashMap ;
6
6
import java .util .Map ;
7
+ import java .util .function .Consumer ;
7
8
9
+ import org .awaitility .Awaitility ;
8
10
import org .junit .jupiter .api .BeforeEach ;
9
11
import org .junit .jupiter .api .Order ;
10
12
import org .junit .jupiter .api .parallel .Isolated ;
11
13
12
14
import dev .openfeature .sdk .Client ;
13
15
import dev .openfeature .sdk .EvaluationContext ;
16
+ import dev .openfeature .sdk .EventDetails ;
14
17
import dev .openfeature .sdk .FeatureProvider ;
15
18
import dev .openfeature .sdk .FlagEvaluationDetails ;
16
19
import dev .openfeature .sdk .ImmutableContext ;
25
28
import io .cucumber .java .en .Then ;
26
29
import io .cucumber .java .en .When ;
27
30
31
+ import java .time .Duration ;
32
+
28
33
/**
29
34
* Common test suite used by both RPC and in-process flagd providers.
30
35
*/
@@ -63,6 +68,12 @@ public class StepDefinitions {
63
68
64
69
private EvaluationContext customEvaluatorContext ;
65
70
71
+ private boolean isChangeHandlerRun = false ;
72
+ private boolean isReadyHandlerRun = false ;
73
+
74
+ private Consumer <EventDetails > changeHandler ;
75
+ private Consumer <EventDetails > readyHandler ;
76
+
66
77
/**
67
78
* Injects the client to use for this test.
68
79
* Tests run one at a time, but just in case, a lock is used to make sure the
@@ -110,6 +121,7 @@ public void the_resolved_boolean_value_should_be_true(String expected) {
110
121
}
111
122
112
123
// string value
124
+
113
125
@ When ("a string flag with key {string} is evaluated with default value {string}" )
114
126
public void a_string_flag_with_key_is_evaluated_with_default_value (String flagKey , String defaultValue ) {
115
127
this .stringFlagKey = flagKey ;
@@ -371,7 +383,119 @@ public void a_context_containing_a_key_with_value(String key, String value) {
371
383
372
384
@ Then ("the returned value should be {string}" )
373
385
public void the_returned_value_should_be (String expected ) {
374
- String value = client .getStringValue (this .stringFlagKey , this .stringFlagDefaultValue , this .customEvaluatorContext );
386
+ String value = client .getStringValue (this .stringFlagKey , this .stringFlagDefaultValue ,
387
+ this .customEvaluatorContext );
388
+ assertEquals (expected , value );
389
+ }
390
+
391
+ /*
392
+ * Events
393
+ */
394
+
395
+ // Flag change event
396
+ @ When ("a PROVIDER_CONFIGURATION_CHANGED handler is added" )
397
+ public void a_provider_configuration_changed_handler_is_added () {
398
+ this .changeHandler = (EventDetails details ) -> {
399
+ this .isChangeHandlerRun = true ;
400
+ };
401
+ client .onProviderConfigurationChanged (this .changeHandler );
402
+
403
+ }
404
+
405
+ @ When ("a flag with key {string} is modified" )
406
+ public void a_flag_with_key_is_modified (String flagKey ) {
407
+ // This happens automatically
408
+ }
409
+
410
+ @ Then ("the PROVIDER_CONFIGURATION_CHANGED handler must run" )
411
+ public void the_provider_configuration_changed_handler_must_run () {
412
+ Awaitility .await ()
413
+ .atMost (Duration .ofSeconds (2 ))
414
+ .until (() -> {
415
+ return this .isChangeHandlerRun ;
416
+ });
417
+ }
418
+
419
+ @ Then ("the event details must indicate {string} was altered" )
420
+ public void the_event_details_must_indicate_was_altered (String flagKey ) {
421
+ // TODO: In-process-provider doesnt support flag change list.
422
+ }
423
+
424
+ // Provider ready event
425
+ @ When ("a PROVIDER_READY handler is added" )
426
+ public void a_provider_ready_handler_is_added () {
427
+ this .readyHandler = (EventDetails details ) -> {
428
+ this .isReadyHandlerRun = true ;
429
+ };
430
+ client .onProviderReady (this .readyHandler );
431
+ }
432
+
433
+ @ Then ("the PROVIDER_READY handler must run" )
434
+ public void the_provider_ready_handler_must_run () {
435
+ Awaitility .await ()
436
+ .atMost (Duration .ofSeconds (2 ))
437
+ .until (() -> {
438
+ return this .isReadyHandlerRun ;
439
+ });
440
+ }
441
+
442
+ /*
443
+ * Zero Value
444
+ */
445
+
446
+ // boolean value
447
+ @ When ("a zero-value boolean flag with key {string} is evaluated with default value {string}" )
448
+ public void a_zero_value_boolean_flag_with_key_is_evaluated_with_default_value (String flagKey ,
449
+ String defaultValue ) {
450
+ this .booleanFlagKey = flagKey ;
451
+ this .booleanFlagDefaultValue = Boolean .valueOf (defaultValue );
452
+ }
453
+
454
+ @ Then ("the resolved boolean zero-value should be {string}" )
455
+ public void the_resolved_boolean_zero_value_should_be (String expected ) {
456
+ boolean value = client .getBooleanValue (this .booleanFlagKey , this .booleanFlagDefaultValue );
457
+ assertEquals (Boolean .valueOf (expected ), value );
458
+ }
459
+
460
+ // float/double value
461
+ @ When ("a zero-value float flag with key {string} is evaluated with default value {double}" )
462
+ public void a_zero_value_float_flag_with_key_is_evaluated_with_default_value (String flagKey , Double defaultValue ) {
463
+ // TODO: There is a bug here with 0 value floats
464
+ // this.doubleFlagKey = flagKey;
465
+ // this.doubleFlagDefaultValue = defaultValue;
466
+ }
467
+
468
+ @ Then ("the resolved float zero-value should be {double}" )
469
+ public void the_resolved_float_zero_value_should_be (Double expected ) {
470
+ // FlagEvaluationDetails<Double> details =
471
+ // client.getDoubleDetails("float-zero-flag", this.doubleFlagDefaultValue);
472
+ // assertEquals(expected, details.getValue());
473
+ }
474
+
475
+ // integer value
476
+ @ When ("a zero-value integer flag with key {string} is evaluated with default value {int}" )
477
+ public void a_zero_value_integer_flag_with_key_is_evaluated_with_default_value (String flagKey ,
478
+ Integer defaultValue ) {
479
+ this .intFlagKey = flagKey ;
480
+ this .intFlagDefaultValue = defaultValue ;
481
+ }
482
+
483
+ @ Then ("the resolved integer zero-value should be {int}" )
484
+ public void the_resolved_integer_zero_value_should_be (Integer expected ) {
485
+ int value = client .getIntegerValue (this .intFlagKey , this .intFlagDefaultValue );
486
+ assertEquals (expected , value );
487
+ }
488
+
489
+ // string value
490
+ @ When ("a zero-value string flag with key {string} is evaluated with default value {string}" )
491
+ public void a_zero_value_string_flag_with_key_is_evaluated_with_default_value (String flagKey , String defaultValue ) {
492
+ this .stringFlagKey = flagKey ;
493
+ this .stringFlagDefaultValue = defaultValue ;
494
+ }
495
+
496
+ @ Then ("the resolved string zero-value should be {string}" )
497
+ public void the_resolved_string_zero_value_should_be (String expected ) {
498
+ String value = client .getStringValue (this .stringFlagKey , this .stringFlagDefaultValue );
375
499
assertEquals (expected , value );
376
500
}
377
501
}
0 commit comments