Skip to content

Commit

Permalink
fix: merge eval context (open-feature#149)
Browse files Browse the repository at this point in the history
fix merge eval context

Signed-off-by: Robert Grassian <robert.grassian@split.io>

Signed-off-by: Robert Grassian <robert.grassian@split.io>
  • Loading branch information
rgrassian-split authored Oct 13, 2022
1 parent 54fbf08 commit fad0f35
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/dev/openfeature/sdk/OpenFeatureClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private <T> FlagEvaluationDetails<T> evaluateFlag(FlagValueType type, String key
apiContext = openfeatureApi.getEvaluationContext() != null
? openfeatureApi.getEvaluationContext()
: new MutableContext();
clientContext = openfeatureApi.getEvaluationContext() != null
clientContext = this.getEvaluationContext() != null
? this.getEvaluationContext()
: new MutableContext();

Expand Down
26 changes: 26 additions & 0 deletions src/test/java/dev/openfeature/sdk/OpenFeatureClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,30 @@ void shouldNotThrowExceptionIfHookHasDifferentTypeArgumentThanHookContext() {
assertThat(actual.getValue()).isTrue();
assertThat(TEST_LOGGER.getLoggingEvents()).filteredOn(event -> event.getLevel().equals(Level.ERROR)).isEmpty();
}

@Test
void mergeContextTest() {
TEST_LOGGER.clear();

String flag = "feature key";
boolean defaultValue = false;
String targetingKey = "targeting key";
EvaluationContext ctx = new MutableContext(targetingKey);

OpenFeatureAPI api = mock(OpenFeatureAPI.class);
FeatureProvider mockProvider = mock(FeatureProvider.class);
// this makes it so that true is returned only if the targeting key set at the client level is honored
when(mockProvider.getBooleanEvaluation(
eq(flag), eq(defaultValue), argThat(
context -> context.getTargetingKey().equals(targetingKey)))).thenReturn(ProviderEvaluation.<Boolean>builder()
.value(true).build());
when(api.getProvider()).thenReturn(mockProvider);

OpenFeatureClient client = new OpenFeatureClient(api, "name", "version");
client.setEvaluationContext(ctx);

FlagEvaluationDetails<Boolean> result = client.getBooleanDetails(flag, defaultValue);

assertThat(result.getValue()).isTrue();
}
}

0 comments on commit fad0f35

Please sign in to comment.