diff --git a/src/main/java/dev/openfeature/sdk/OpenFeatureClient.java b/src/main/java/dev/openfeature/sdk/OpenFeatureClient.java index 3759ecea7..827c9215b 100644 --- a/src/main/java/dev/openfeature/sdk/OpenFeatureClient.java +++ b/src/main/java/dev/openfeature/sdk/OpenFeatureClient.java @@ -118,7 +118,7 @@ private FlagEvaluationDetails 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(); diff --git a/src/test/java/dev/openfeature/sdk/OpenFeatureClientTest.java b/src/test/java/dev/openfeature/sdk/OpenFeatureClientTest.java index eab962ac2..f4c6f100e 100644 --- a/src/test/java/dev/openfeature/sdk/OpenFeatureClientTest.java +++ b/src/test/java/dev/openfeature/sdk/OpenFeatureClientTest.java @@ -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.builder() + .value(true).build()); + when(api.getProvider()).thenReturn(mockProvider); + + OpenFeatureClient client = new OpenFeatureClient(api, "name", "version"); + client.setEvaluationContext(ctx); + + FlagEvaluationDetails result = client.getBooleanDetails(flag, defaultValue); + + assertThat(result.getValue()).isTrue(); + } }