From cd7a61a78275c5c2337f2d53c5e51205cbe54467 Mon Sep 17 00:00:00 2001 From: Mark Phelps <209477+markphelps@users.noreply.github.com> Date: Tue, 17 Sep 2024 18:27:04 -0400 Subject: [PATCH] fix(flipt): set variant attachment as value for object evaluation Signed-off-by: Mark Phelps <209477+markphelps@users.noreply.github.com> --- providers/flipt/pom.xml | 15 +++++++++++++++ .../providers/flipt/FliptProvider.java | 16 +++++++++------- .../providers/flipt/FliptProviderTest.java | 19 ++++++------------- .../src/test/resources/variant-object.json | 6 ++++++ 4 files changed, 36 insertions(+), 20 deletions(-) create mode 100644 providers/flipt/src/test/resources/variant-object.json diff --git a/providers/flipt/pom.xml b/providers/flipt/pom.xml index da65c25e2..1ad904117 100644 --- a/providers/flipt/pom.xml +++ b/providers/flipt/pom.xml @@ -20,6 +20,15 @@ false + + + markphelps + Mark Phelps + Flipt Software + https://flipt.io/ + + + io.flipt @@ -33,6 +42,12 @@ 2.0.16 + + com.fasterxml.jackson.core + jackson-databind + 2.17.2 + + com.github.tomakehurst wiremock-jre8 diff --git a/providers/flipt/src/main/java/dev/openfeature/contrib/providers/flipt/FliptProvider.java b/providers/flipt/src/main/java/dev/openfeature/contrib/providers/flipt/FliptProvider.java index 5d69576a6..c0cdb9f0d 100644 --- a/providers/flipt/src/main/java/dev/openfeature/contrib/providers/flipt/FliptProvider.java +++ b/providers/flipt/src/main/java/dev/openfeature/contrib/providers/flipt/FliptProvider.java @@ -47,7 +47,7 @@ public class FliptProvider extends EventProvider { @Getter private ProviderState state = ProviderState.NOT_READY; - private AtomicBoolean isInitialized = new AtomicBoolean(false); + private final AtomicBoolean isInitialized = new AtomicBoolean(false); /** * Constructor. @@ -205,17 +205,19 @@ public ProviderEvaluation getObjectEvaluation(String key, Value defaultVa .build(); } + Value value = new Value(response.getVariantKey()); ImmutableMetadata.ImmutableMetadataBuilder flagMetadataBuilder = ImmutableMetadata.builder(); - if (response.getVariantAttachment() != null) { + if (response.getVariantAttachment() != null && !response.getVariantAttachment().isEmpty()) { flagMetadataBuilder.addString("variant-attachment", response.getVariantAttachment()); + value = new Value(response.getVariantAttachment()); } return ProviderEvaluation.builder() - .value(new Value(response.getVariantKey())) - .variant(response.getVariantKey()) - .reason(TARGETING_MATCH.name()) - .flagMetadata(flagMetadataBuilder.build()) - .build(); + .value(value) + .variant(response.getVariantKey()) + .reason(TARGETING_MATCH.name()) + .flagMetadata(flagMetadataBuilder.build()) + .build(); } @Override diff --git a/providers/flipt/src/test/java/dev/openfeature/contrib/providers/flipt/FliptProviderTest.java b/providers/flipt/src/test/java/dev/openfeature/contrib/providers/flipt/FliptProviderTest.java index 3378f840f..3265b6c3d 100644 --- a/providers/flipt/src/test/java/dev/openfeature/contrib/providers/flipt/FliptProviderTest.java +++ b/providers/flipt/src/test/java/dev/openfeature/contrib/providers/flipt/FliptProviderTest.java @@ -5,15 +5,6 @@ import com.github.tomakehurst.wiremock.client.WireMock; import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo; import com.github.tomakehurst.wiremock.junit5.WireMockTest; -import dev.openfeature.sdk.Client; -import dev.openfeature.sdk.FlagEvaluationDetails; -import dev.openfeature.sdk.ImmutableContext; -import dev.openfeature.sdk.ImmutableMetadata; -import dev.openfeature.sdk.MutableContext; -import dev.openfeature.sdk.OpenFeatureAPI; -import dev.openfeature.sdk.ProviderEvaluation; -import dev.openfeature.sdk.ProviderEventDetails; -import dev.openfeature.sdk.ProviderState; import dev.openfeature.sdk.exceptions.GeneralError; import dev.openfeature.sdk.exceptions.ProviderNotReadyError; import lombok.SneakyThrows; @@ -25,14 +16,15 @@ import java.net.URL; import java.nio.file.Files; import java.nio.file.Paths; +import java.util.HashMap; +import java.util.Map; import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; import static com.github.tomakehurst.wiremock.client.WireMock.post; import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.*; /** * FliptProvider test, based on APIs mocking. @@ -50,6 +42,8 @@ class FliptProviderTest { public static final Double DOUBLE_FLAG_VALUE = 1.23; public static final String USERS_FLAG_NAME = "users-flag"; public static final String TARGETING_KEY = "targeting_key"; + public static final String OBJECT_FLAG_NAME = "object-flag"; + private static FliptProvider fliptProvider; private static Client client; @@ -101,7 +95,6 @@ void getBooleanEvaluation() { mockFliptAPI("/evaluate/v1/boolean", "boolean.json", FLAG_NAME); MutableContext evaluationContext = new MutableContext(); evaluationContext.setTargetingKey(TARGETING_KEY); - assertEquals(true, fliptProvider.getBooleanEvaluation(FLAG_NAME, false, evaluationContext).getValue()); assertEquals(true, client.getBooleanValue(FLAG_NAME, false, evaluationContext)); assertEquals(false, client.getBooleanValue("non-existing", false, evaluationContext)); } @@ -171,7 +164,7 @@ void getEvaluationMetadataTest() { assertEquals("attachment-1", flagMetadata.getString("variant-attachment")); FlagEvaluationDetails nonExistingFlagEvaluation = client.getStringDetails("non-existing", "", evaluationContext); - assertEquals(null, nonExistingFlagEvaluation.getFlagMetadata().getBoolean("variant-attachment")); + assertNull(nonExistingFlagEvaluation.getFlagMetadata().getBoolean("variant-attachment")); } @SneakyThrows diff --git a/providers/flipt/src/test/resources/variant-object.json b/providers/flipt/src/test/resources/variant-object.json new file mode 100644 index 000000000..abf764532 --- /dev/null +++ b/providers/flipt/src/test/resources/variant-object.json @@ -0,0 +1,6 @@ +{ + "flagKey": "object-flag", + "match": true, + "variantKey": "object-variant", + "variantAttachment": "{\"key1\":\"value1\",\"key2\":42,\"key3\":true}" +} \ No newline at end of file