Skip to content

Commit

Permalink
fix(flipt): set variant attachment as value for object evaluation
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Phelps <[email protected]>
  • Loading branch information
markphelps committed Sep 17, 2024
1 parent 03dfc91 commit cd7a61a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 20 deletions.
15 changes: 15 additions & 0 deletions providers/flipt/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@
<javadoc.failOnWarnings>false</javadoc.failOnWarnings>
</properties>

<developers>
<developer>
<id>markphelps</id>
<name>Mark Phelps</name>
<organization>Flipt Software</organization>
<url>https://flipt.io/</url>
</developer>
</developers>

<dependencies>
<dependency>
<groupId>io.flipt</groupId>
Expand All @@ -33,6 +42,12 @@
<version>2.0.16</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.17.2</version>
</dependency>

<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -205,17 +205,19 @@ public ProviderEvaluation<Value> 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.<Value>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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -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;

Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -171,7 +164,7 @@ void getEvaluationMetadataTest() {
assertEquals("attachment-1", flagMetadata.getString("variant-attachment"));
FlagEvaluationDetails<String> nonExistingFlagEvaluation = client.getStringDetails("non-existing", "",
evaluationContext);
assertEquals(null, nonExistingFlagEvaluation.getFlagMetadata().getBoolean("variant-attachment"));
assertNull(nonExistingFlagEvaluation.getFlagMetadata().getBoolean("variant-attachment"));
}

@SneakyThrows
Expand Down
6 changes: 6 additions & 0 deletions providers/flipt/src/test/resources/variant-object.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"flagKey": "object-flag",
"match": true,
"variantKey": "object-variant",
"variantAttachment": "{\"key1\":\"value1\",\"key2\":42,\"key3\":true}"
}

0 comments on commit cd7a61a

Please sign in to comment.