Skip to content

Commit 3190ad2

Browse files
test: zero value and events tests (#468)
Signed-off-by: DBlanchard88 <[email protected]> Co-authored-by: Todd Baert <[email protected]>
1 parent 16643c8 commit 3190ad2

File tree

6 files changed

+160
-11
lines changed

6 files changed

+160
-11
lines changed

pom.xml

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,35 +148,42 @@
148148
<artifactId>mockito-inline</artifactId>
149149
<version>4.11.0</version>
150150
<scope>test</scope>
151-
</dependency>
151+
</dependency>
152152

153-
<dependency>
153+
<dependency>
154154
<groupId>uk.org.webcompere</groupId>
155155
<artifactId>system-stubs-core</artifactId>
156156
<version>2.0.3</version>
157157
<scope>test</scope>
158-
</dependency>
158+
</dependency>
159159

160-
<dependency>
160+
<dependency>
161161
<groupId>uk.org.webcompere</groupId>
162162
<artifactId>system-stubs-jupiter</artifactId>
163163
<version>2.1.3</version>
164164
<scope>test</scope>
165-
</dependency>
165+
</dependency>
166166

167-
<dependency>
167+
<dependency>
168168
<groupId>io.cucumber</groupId>
169169
<artifactId>cucumber-java</artifactId>
170170
<version>7.14.0</version>
171171
<scope>test</scope>
172-
</dependency>
172+
</dependency>
173173

174-
<dependency>
174+
<dependency>
175175
<groupId>io.cucumber</groupId>
176176
<artifactId>cucumber-junit-platform-engine</artifactId>
177177
<version>7.14.0</version>
178178
<scope>test</scope>
179-
</dependency>
179+
</dependency>
180+
181+
<dependency>
182+
<groupId>org.awaitility</groupId>
183+
<artifactId>awaitility</artifactId>
184+
<version>4.2.0</version>
185+
<scope>test</scope>
186+
</dependency>
180187
<!-- end test -->
181188
</dependencies>
182189

providers/flagd/pom.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,22 @@
314314
</arguments>
315315
</configuration>
316316
</execution>
317+
<execution>
318+
<id>copy-gherkin-flagd.feature</id>
319+
<phase>validate</phase>
320+
<goals>
321+
<goal>exec</goal>
322+
</goals>
323+
<configuration>
324+
<!-- copy the feature spec we want to test into resources so them can be easily loaded -->
325+
<!-- run: cp test-harness/features/flagd.feature src/test/resources/features/ -->
326+
<executable>cp</executable>
327+
<arguments>
328+
<argument>test-harness/gherkin/flagd.feature</argument>
329+
<argument>src/test/resources/features/</argument>
330+
</arguments>
331+
</configuration>
332+
</execution>
317333
</executions>
318334
</plugin>
319335
</plugins>

providers/flagd/src/test/java/dev/openfeature/contrib/providers/flagd/e2e/RunFlagdInProcessCucumberTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
@IncludeEngines("cucumber")
1515
@SelectClasspathResource("features/evaluation.feature")
1616
@SelectClasspathResource("features/flagd-json-evaluator.feature")
17+
@SelectClasspathResource("features/flagd.feature")
1718
@ConfigurationParameter(key = PLUGIN_PROPERTY_NAME, value = "pretty")
1819
@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "dev.openfeature.contrib.providers.flagd.e2e.process,dev.openfeature.contrib.providers.flagd.e2e.steps")
1920
public class RunFlagdInProcessCucumberTest {

providers/flagd/src/test/java/dev/openfeature/contrib/providers/flagd/e2e/RunFlagdRpcCucumberTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
@IncludeEngines("cucumber")
1515
@SelectClasspathResource("features/evaluation.feature")
1616
@SelectClasspathResource("features/flagd-json-evaluator.feature")
17+
@SelectClasspathResource("features/flagd.feature")
1718
@ConfigurationParameter(key = PLUGIN_PROPERTY_NAME, value = "pretty")
1819
@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "dev.openfeature.contrib.providers.flagd.e2e.rpc,dev.openfeature.contrib.providers.flagd.e2e.steps")
1920
public class RunFlagdRpcCucumberTest {

providers/flagd/src/test/java/dev/openfeature/contrib/providers/flagd/e2e/steps/StepDefinitions.java

Lines changed: 125 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44

55
import java.util.HashMap;
66
import java.util.Map;
7+
import java.util.function.Consumer;
78

9+
import org.awaitility.Awaitility;
810
import org.junit.jupiter.api.BeforeEach;
911
import org.junit.jupiter.api.Order;
1012
import org.junit.jupiter.api.parallel.Isolated;
1113

1214
import dev.openfeature.sdk.Client;
1315
import dev.openfeature.sdk.EvaluationContext;
16+
import dev.openfeature.sdk.EventDetails;
1417
import dev.openfeature.sdk.FeatureProvider;
1518
import dev.openfeature.sdk.FlagEvaluationDetails;
1619
import dev.openfeature.sdk.ImmutableContext;
@@ -25,6 +28,8 @@
2528
import io.cucumber.java.en.Then;
2629
import io.cucumber.java.en.When;
2730

31+
import java.time.Duration;
32+
2833
/**
2934
* Common test suite used by both RPC and in-process flagd providers.
3035
*/
@@ -63,6 +68,12 @@ public class StepDefinitions {
6368

6469
private EvaluationContext customEvaluatorContext;
6570

71+
private boolean isChangeHandlerRun = false;
72+
private boolean isReadyHandlerRun = false;
73+
74+
private Consumer<EventDetails> changeHandler;
75+
private Consumer<EventDetails> readyHandler;
76+
6677
/**
6778
* Injects the client to use for this test.
6879
* 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) {
110121
}
111122

112123
// string value
124+
113125
@When("a string flag with key {string} is evaluated with default value {string}")
114126
public void a_string_flag_with_key_is_evaluated_with_default_value(String flagKey, String defaultValue) {
115127
this.stringFlagKey = flagKey;
@@ -371,7 +383,119 @@ public void a_context_containing_a_key_with_value(String key, String value) {
371383

372384
@Then("the returned value should be {string}")
373385
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);
375499
assertEquals(expected, value);
376500
}
377501
}

providers/flagd/test-harness

0 commit comments

Comments
 (0)