-
Notifications
You must be signed in to change notification settings - Fork 51
feat: update test harness with metadata assertions #1467 #1319
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
aepfli
merged 8 commits into
open-feature:main
from
chrfwow:feat-Update-test-harness-with-metadata-assertions-#1467
Feb 19, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
154a1b9
feat: implement gherkin tests
chrfwow c5b1cbe
fixup! feat: implement gherkin tests
chrfwow 3eb233e
fixup! feat: implement gherkin tests
chrfwow 4aeac21
fixup! feat: implement gherkin tests
chrfwow e546ffa
Merge branch 'main' into feat-Update-test-harness-with-metadata-asser…
aepfli b1c23aa
Merge branch 'main' into feat-Update-test-harness-with-metadata-asser…
aepfli 82ae265
Merge branch 'main' into feat-Update-test-harness-with-metadata-asser…
aepfli 2a4d386
fixup: add version for dependency
aepfli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,18 @@ | ||
| package dev.openfeature.sdk.e2e; | ||
|
|
||
| import static io.cucumber.junit.platform.engine.Constants.GLUE_PROPERTY_NAME; | ||
| import static io.cucumber.junit.platform.engine.Constants.OBJECT_FACTORY_PROPERTY_NAME; | ||
| import static io.cucumber.junit.platform.engine.Constants.PLUGIN_PROPERTY_NAME; | ||
|
|
||
| import org.junit.platform.suite.api.ConfigurationParameter; | ||
| import org.junit.platform.suite.api.IncludeEngines; | ||
| import org.junit.platform.suite.api.SelectClasspathResource; | ||
| import org.junit.platform.suite.api.SelectDirectories; | ||
| import org.junit.platform.suite.api.Suite; | ||
|
|
||
| @Suite | ||
| @IncludeEngines("cucumber") | ||
| @SelectClasspathResource("features/evaluation.feature") | ||
| @SelectDirectories("spec/specification/assets/gherkin") | ||
aepfli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @ConfigurationParameter(key = PLUGIN_PROPERTY_NAME, value = "pretty") | ||
| @ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "dev.openfeature.sdk.e2e.evaluation") | ||
| @ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "dev.openfeature.sdk.e2e.steps") | ||
| @ConfigurationParameter(key = OBJECT_FACTORY_PROPERTY_NAME, value = "io.cucumber.picocontainer.PicoFactory") | ||
| public class EvaluationTest {} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package dev.openfeature.sdk.e2e; | ||
|
|
||
| public class Flag { | ||
| public String name; | ||
| public Object defaultValue; | ||
| public String type; | ||
|
|
||
| public Flag(String type, String name, Object defaultValue) { | ||
| this.name = name; | ||
| this.defaultValue = defaultValue; | ||
| this.type = type; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package dev.openfeature.sdk.e2e; | ||
|
|
||
| import dev.openfeature.sdk.EvaluationContext; | ||
| import dev.openfeature.sdk.FlagEvaluationDetails; | ||
| import dev.openfeature.sdk.Hook; | ||
| import dev.openfeature.sdk.HookContext; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import lombok.Getter; | ||
|
|
||
| public class MockHook implements Hook { | ||
| @Getter | ||
| private boolean beforeCalled; | ||
|
|
||
| @Getter | ||
| private boolean afterCalled; | ||
|
|
||
| @Getter | ||
| private boolean errorCalled; | ||
|
|
||
| @Getter | ||
| private boolean finallyAfterCalled; | ||
|
|
||
| @Getter | ||
| private final Map<String, FlagEvaluationDetails> evaluationDetails = new HashMap<>(); | ||
|
|
||
| @Override | ||
| public Optional<EvaluationContext> before(HookContext ctx, Map hints) { | ||
| beforeCalled = true; | ||
| return Optional.of(ctx.getCtx()); | ||
| } | ||
|
|
||
| @Override | ||
| public void after(HookContext ctx, FlagEvaluationDetails details, Map hints) { | ||
| afterCalled = true; | ||
| evaluationDetails.put("after", details); | ||
| } | ||
|
|
||
| @Override | ||
| public void error(HookContext ctx, Exception error, Map hints) { | ||
| errorCalled = true; | ||
| } | ||
|
|
||
| @Override | ||
| public void finallyAfter(HookContext ctx, FlagEvaluationDetails details, Map hints) { | ||
| finallyAfterCalled = true; | ||
| evaluationDetails.put("finally", details); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package dev.openfeature.sdk.e2e; | ||
|
|
||
| import dev.openfeature.sdk.Client; | ||
| import dev.openfeature.sdk.FlagEvaluationDetails; | ||
| import dev.openfeature.sdk.MutableContext; | ||
|
|
||
| public class State { | ||
| public Client client; | ||
| public Flag flag; | ||
| public MutableContext context = new MutableContext(); | ||
| public FlagEvaluationDetails evaluation; | ||
| public MockHook hook; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package dev.openfeature.sdk.e2e; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| public final class Utils { | ||
|
|
||
| private Utils() {} | ||
|
|
||
| public static Object convert(String value, String type) { | ||
| if (Objects.equals(value, "null")) { | ||
| return null; | ||
| } | ||
| switch (type.toLowerCase()) { | ||
| case "boolean": | ||
| return Boolean.parseBoolean(value); | ||
| case "string": | ||
| return value; | ||
| case "integer": | ||
| return Integer.parseInt(value); | ||
| case "float": | ||
| case "double": | ||
| return Double.parseDouble(value); | ||
| case "long": | ||
| return Long.parseLong(value); | ||
| } | ||
| throw new RuntimeException("Unknown config type: " + type); | ||
| } | ||
| } |
104 changes: 104 additions & 0 deletions
104
src/test/java/dev/openfeature/sdk/e2e/steps/FlagStepDefinitions.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| package dev.openfeature.sdk.e2e.steps; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import dev.openfeature.sdk.FlagEvaluationDetails; | ||
| import dev.openfeature.sdk.ImmutableMetadata; | ||
| import dev.openfeature.sdk.Value; | ||
| import dev.openfeature.sdk.e2e.Flag; | ||
| import dev.openfeature.sdk.e2e.State; | ||
| import dev.openfeature.sdk.e2e.Utils; | ||
| import io.cucumber.datatable.DataTable; | ||
| import io.cucumber.java.en.Given; | ||
| import io.cucumber.java.en.Then; | ||
| import io.cucumber.java.en.When; | ||
| import java.lang.reflect.Field; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
|
|
||
| public class FlagStepDefinitions { | ||
| private final State state; | ||
|
|
||
| public FlagStepDefinitions(State state) { | ||
| this.state = state; | ||
| } | ||
|
|
||
| @Given("a {}-flag with key {string} and a default value {string}") | ||
| public void givenAFlag(String type, String name, String defaultValue) { | ||
| state.flag = new Flag(type, name, Utils.convert(defaultValue, type)); | ||
| } | ||
|
|
||
| @When("the flag was evaluated with details") | ||
| public void the_flag_was_evaluated_with_details() { | ||
| FlagEvaluationDetails details; | ||
| switch (state.flag.type.toLowerCase()) { | ||
| case "string": | ||
| details = | ||
| state.client.getStringDetails(state.flag.name, (String) state.flag.defaultValue, state.context); | ||
| break; | ||
| case "boolean": | ||
| details = state.client.getBooleanDetails( | ||
| state.flag.name, (Boolean) state.flag.defaultValue, state.context); | ||
| break; | ||
| case "float": | ||
| details = | ||
| state.client.getDoubleDetails(state.flag.name, (Double) state.flag.defaultValue, state.context); | ||
| break; | ||
| case "integer": | ||
| details = state.client.getIntegerDetails( | ||
| state.flag.name, (Integer) state.flag.defaultValue, state.context); | ||
| break; | ||
| case "object": | ||
| details = | ||
| state.client.getObjectDetails(state.flag.name, (Value) state.flag.defaultValue, state.context); | ||
| break; | ||
| default: | ||
| throw new AssertionError(); | ||
| } | ||
| state.evaluation = details; | ||
| } | ||
|
|
||
| @Then("the resolved details value should be {string}") | ||
| public void the_resolved_details_value_should_be(String value) { | ||
| assertThat(state.evaluation.getValue()).isEqualTo(Utils.convert(value, state.flag.type)); | ||
| } | ||
|
|
||
| @Then("the reason should be {string}") | ||
| public void the_reason_should_be(String reason) { | ||
| assertThat(state.evaluation.getReason()).isEqualTo(reason); | ||
| } | ||
|
|
||
| @Then("the variant should be {string}") | ||
| public void the_variant_should_be(String variant) { | ||
| assertThat(state.evaluation.getVariant()).isEqualTo(variant); | ||
| } | ||
|
|
||
| @Then("the resolved metadata value \"{}\" with type \"{}\" should be \"{}\"") | ||
| public void theResolvedMetadataValueShouldBe(String key, String type, String value) | ||
| throws NoSuchFieldException, IllegalAccessException { | ||
| Field f = state.evaluation.getFlagMetadata().getClass().getDeclaredField("metadata"); | ||
| f.setAccessible(true); | ||
| HashMap<String, Object> metadata = (HashMap<String, Object>) f.get(state.evaluation.getFlagMetadata()); | ||
| assertThat(metadata).containsEntry(key, Utils.convert(value, type)); | ||
| } | ||
|
|
||
| @Then("the resolved metadata is empty") | ||
| public void theResolvedMetadataIsEmpty() { | ||
| assertThat(state.evaluation.getFlagMetadata().isEmpty()).isTrue(); | ||
| } | ||
|
|
||
| @Then("the resolved metadata should contain") | ||
| public void theResolvedMetadataShouldContain(DataTable dataTable) { | ||
| ImmutableMetadata evaluationMetadata = state.evaluation.getFlagMetadata(); | ||
| List<List<String>> asLists = dataTable.asLists(); | ||
| for (int i = 1; i < asLists.size(); i++) { // skip the header of the table | ||
| List<String> line = asLists.get(i); | ||
| String key = line.get(0); | ||
| String metadataType = line.get(1); | ||
| Object value = Utils.convert(line.get(2), metadataType); | ||
|
|
||
| assertThat(value).isNotNull(); | ||
| assertThat(evaluationMetadata.getValue(key, value.getClass())).isEqualTo(value); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.