Repository: open-feature/java-sdk
Pull request #149 (2022-10-13 23:08:36)

fix: merge eval context
Signed-off-by: Robert Grassian <robert.grassian@split.io>

<!-- Please use this template for your pull request. -->
<!-- Please use the sections that you need and delete other sections -->

## This PR
- Fixes merging of eval contexts. Client eval context was ignored due to a bug.

### Related Issues
<!-- add here the GitHub issue that this PR resolves if applicable -->

Fixes #1234523

### Notes
<!-- any additional notes for this PR -->

### Follow-up Tasks
<!-- anything that is related to this PR but not done here should be noted under this section -->
<!-- if there is a need for a new issue, please link it here -->

### How to test
Added a new test for this in `OpenFeatureClientTest`. It passes with my fix and fails without it.




Patch:
diff --git a/src/main/java/dev/openfeature/sdk/OpenFeatureClient.java b/src/main/java/dev/openfeature/sdk/OpenFeatureClient.java
index 3759ece..827c921 100644
--- a/src/main/java/dev/openfeature/sdk/OpenFeatureClient.java
+++ b/src/main/java/dev/openfeature/sdk/OpenFeatureClient.java
@@ -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();
 


Test patch:
diff --git a/src/test/java/dev/openfeature/sdk/OpenFeatureClientTest.java b/src/test/java/dev/openfeature/sdk/OpenFeatureClientTest.java
index eab962a..f4c6f10 100644
--- a/src/test/java/dev/openfeature/sdk/OpenFeatureClientTest.java
+++ b/src/test/java/dev/openfeature/sdk/OpenFeatureClientTest.java
@@ -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();
+    }
 }