|
| 1 | +package dev.openfeature.sdk.e2e.steps; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 6 | + |
| 7 | +import dev.openfeature.sdk.EvaluationContext; |
| 8 | +import dev.openfeature.sdk.Hook; |
| 9 | +import dev.openfeature.sdk.HookContext; |
| 10 | +import dev.openfeature.sdk.ImmutableContext; |
| 11 | +import dev.openfeature.sdk.OpenFeatureAPI; |
| 12 | +import dev.openfeature.sdk.ThreadLocalTransactionContextPropagator; |
| 13 | +import dev.openfeature.sdk.Value; |
| 14 | +import dev.openfeature.sdk.e2e.ContextStoringProvider; |
| 15 | +import dev.openfeature.sdk.e2e.State; |
| 16 | +import io.cucumber.datatable.DataTable; |
| 17 | +import io.cucumber.java.en.And; |
| 18 | +import io.cucumber.java.en.Given; |
| 19 | +import io.cucumber.java.en.Then; |
| 20 | +import io.cucumber.java.en.When; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.Optional; |
| 24 | + |
| 25 | +public class ContextSteps { |
| 26 | + private final State state; |
| 27 | + |
| 28 | + public ContextSteps(State state) { |
| 29 | + this.state = state; |
| 30 | + } |
| 31 | + |
| 32 | + @Given("a stable provider with retrievable context is registered") |
| 33 | + public void setup() { |
| 34 | + ContextStoringProvider provider = new ContextStoringProvider(); |
| 35 | + state.provider = provider; |
| 36 | + OpenFeatureAPI.getInstance().setProviderAndWait(provider); |
| 37 | + state.client = OpenFeatureAPI.getInstance().getClient(); |
| 38 | + OpenFeatureAPI.getInstance().setTransactionContextPropagator(new ThreadLocalTransactionContextPropagator()); |
| 39 | + } |
| 40 | + |
| 41 | + @When("A context entry with key {string} and value {string} is added to the {string} level") |
| 42 | + public void aContextWithKeyAndValueIsAddedToTheLevel(String contextKey, String contextValue, String level) { |
| 43 | + addContextEntry(contextKey, contextValue, level); |
| 44 | + } |
| 45 | + |
| 46 | + private void addContextEntry(String contextKey, String contextValue, String level) { |
| 47 | + Map<String, Value> data = new HashMap<>(); |
| 48 | + data.put(contextKey, new Value(contextValue)); |
| 49 | + EvaluationContext context = new ImmutableContext(data); |
| 50 | + if ("API".equals(level)) { |
| 51 | + OpenFeatureAPI.getInstance().setEvaluationContext(context); |
| 52 | + } else if ("Transaction".equals(level)) { |
| 53 | + OpenFeatureAPI.getInstance().setTransactionContext(context); |
| 54 | + } else if ("Client".equals(level)) { |
| 55 | + state.client.setEvaluationContext(context); |
| 56 | + } else if ("Invocation".equals(level)) { |
| 57 | + state.invocationContext = context; |
| 58 | + } else if ("Before Hooks".equals(level)) { |
| 59 | + state.client.addHooks(new Hook() { |
| 60 | + @Override |
| 61 | + public Optional<EvaluationContext> before(HookContext ctx, Map hints) { |
| 62 | + return Optional.of(context); |
| 63 | + } |
| 64 | + }); |
| 65 | + } else { |
| 66 | + throw new IllegalArgumentException("Unknown level: " + level); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + @When("Some flag was evaluated") |
| 71 | + public void someFlagWasEvaluated() { |
| 72 | + state.evaluation = state.client.getStringDetails("unused", "unused", state.invocationContext); |
| 73 | + } |
| 74 | + |
| 75 | + @Then("The merged context contains an entry with key {string} and value {string}") |
| 76 | + public void theMergedContextContainsAnEntryWithKeyAndValue(String contextKey, String contextValue) { |
| 77 | + assertInstanceOf( |
| 78 | + ContextStoringProvider.class, |
| 79 | + state.provider, |
| 80 | + "In order to use this step, you need to set a ContextStoringProvider"); |
| 81 | + EvaluationContext ctx = ((ContextStoringProvider) state.provider).getEvaluationContext(); |
| 82 | + assertNotNull(ctx); |
| 83 | + assertNotNull(ctx.getValue(contextKey)); |
| 84 | + assertNotNull(ctx.getValue(contextKey).asString()); |
| 85 | + assertEquals(contextValue, ctx.getValue(contextKey).asString()); |
| 86 | + } |
| 87 | + |
| 88 | + @Given("A table with levels of increasing precedence") |
| 89 | + public void aTableWithLevelsOfIncreasingPrecedence(DataTable levelsTable) { |
| 90 | + state.levels = levelsTable.asList(); |
| 91 | + } |
| 92 | + |
| 93 | + @And( |
| 94 | + "Context entries for each level from API level down to the {string} level, with key {string} and value {string}") |
| 95 | + public void contextEntriesForEachLevelFromAPILevelDownToTheLevelWithKeyAndValue( |
| 96 | + String maxLevel, String key, String value) { |
| 97 | + for (String level : state.levels) { |
| 98 | + addContextEntry(key, value, level); |
| 99 | + if (level.equals(maxLevel)) { |
| 100 | + return; |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments