fix: merge eval context (#149)

fix merge eval context

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

Signed-off-by: Robert Grassian <robert.grassian@split.io>
This commit is contained in:
Robert Grassian 2022-10-13 16:24:25 -07:00 committed by GitHub
parent 54fbf081ed
commit fad0f35fc8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 1 deletions

View File

@ -118,7 +118,7 @@ public class OpenFeatureClient implements Client {
apiContext = openfeatureApi.getEvaluationContext() != null
? openfeatureApi.getEvaluationContext()
: new MutableContext();
clientContext = openfeatureApi.getEvaluationContext() != null
clientContext = this.getEvaluationContext() != null
? this.getEvaluationContext()
: new MutableContext();

View File

@ -29,4 +29,30 @@ class OpenFeatureClientTest implements HookFixtures {
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();
}
}