|
| 1 | +package com.spotify.confidence; |
| 2 | + |
| 3 | +import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 5 | + |
| 6 | +import dev.openfeature.sdk.Client; |
| 7 | +import dev.openfeature.sdk.EvaluationContext; |
| 8 | +import dev.openfeature.sdk.FlagEvaluationDetails; |
| 9 | +import dev.openfeature.sdk.MutableContext; |
| 10 | +import dev.openfeature.sdk.OpenFeatureAPI; |
| 11 | +import dev.openfeature.sdk.Value; |
| 12 | +import java.util.Map; |
| 13 | +import org.junit.jupiter.api.AfterAll; |
| 14 | +import org.junit.jupiter.api.BeforeAll; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | + |
| 17 | +/** |
| 18 | + * End-to-end tests for OpenFeatureLocalResolveProvider. |
| 19 | + * |
| 20 | + * <p>These tests verify the provider against real Confidence service flags. They require valid API |
| 21 | + * credentials to be set as environment variables. |
| 22 | + * |
| 23 | + * <p>Required environment variables: |
| 24 | + * |
| 25 | + * <ul> |
| 26 | + * <li>JAVA_E2E_CONFIDENCE_API_CLIENT_ID - API client ID for authentication |
| 27 | + * <li>JAVA_E2E_CONFIDENCE_API_CLIENT_SECRET - API client secret for authentication |
| 28 | + * </ul> |
| 29 | + */ |
| 30 | +class OpenFeatureLocalResolveProviderE2ETest { |
| 31 | + private static final String FLAG_CLIENT_SECRET = "RxDVTrXvc6op1XxiQ4OaR31dKbJ39aYV"; |
| 32 | + private static OpenFeatureLocalResolveProvider provider; |
| 33 | + private static Client client; |
| 34 | + |
| 35 | + @BeforeAll |
| 36 | + static void setup() { |
| 37 | + final String apiClientId = requireEnv("JS_E2E_CONFIDENCE_API_CLIENT_ID"); |
| 38 | + final String apiClientSecret = requireEnv("JS_E2E_CONFIDENCE_API_CLIENT_SECRET"); |
| 39 | + |
| 40 | + final ApiSecret apiSecret = new ApiSecret(apiClientId, apiClientSecret); |
| 41 | + |
| 42 | + provider = new OpenFeatureLocalResolveProvider(apiSecret, FLAG_CLIENT_SECRET); |
| 43 | + |
| 44 | + final OpenFeatureAPI api = OpenFeatureAPI.getInstance(); |
| 45 | + api.setProviderAndWait(provider); |
| 46 | + |
| 47 | + // Set evaluation context with targeting key |
| 48 | + final EvaluationContext context = new MutableContext("test-a"); |
| 49 | + api.setEvaluationContext(context); |
| 50 | + |
| 51 | + client = api.getClient(); |
| 52 | + } |
| 53 | + |
| 54 | + @AfterAll |
| 55 | + static void teardown() { |
| 56 | + if (provider != null) { |
| 57 | + provider.shutdown(); |
| 58 | + } |
| 59 | + OpenFeatureAPI.getInstance().shutdown(); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void shouldResolveBoolean() { |
| 64 | + final boolean value = client.getBooleanValue("web-sdk-e2e-flag.bool", true); |
| 65 | + assertThat(value).isFalse(); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void shouldResolveInt() { |
| 70 | + final int value = client.getIntegerValue("web-sdk-e2e-flag.int", 10); |
| 71 | + assertEquals(3, value); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void shouldResolveDouble() { |
| 76 | + final double value = client.getDoubleValue("web-sdk-e2e-flag.double", 10.0); |
| 77 | + assertEquals(3.5, value, 0.001); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + void shouldResolveString() { |
| 82 | + final String value = client.getStringValue("web-sdk-e2e-flag.str", "default"); |
| 83 | + assertEquals("control", value); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void shouldResolveStruct() { |
| 88 | + final Value value = client.getObjectValue("web-sdk-e2e-flag.obj", new Value()); |
| 89 | + |
| 90 | + assertThat(value.isStructure()).isTrue(); |
| 91 | + final Map<String, Value> struct = value.asStructure().asMap(); |
| 92 | + |
| 93 | + assertEquals(4, struct.get("int").asInteger()); |
| 94 | + assertEquals("obj control", struct.get("str").asString()); |
| 95 | + assertThat(struct.get("bool").asBoolean()).isFalse(); |
| 96 | + assertEquals(3.6, struct.get("double").asDouble(), 0.001); |
| 97 | + assertThat(struct.get("obj-obj").asStructure().asMap()).isEmpty(); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + void shouldResolveSubValueFromStruct() { |
| 102 | + final boolean value = client.getBooleanValue("web-sdk-e2e-flag.obj.bool", true); |
| 103 | + assertThat(value).isFalse(); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + void shouldResolveSubValueFromStructWithDetails() { |
| 108 | + final FlagEvaluationDetails<Double> details = |
| 109 | + client.getDoubleDetails("web-sdk-e2e-flag.obj.double", 1.0); |
| 110 | + |
| 111 | + assertEquals(3.6, details.getValue(), 0.001); |
| 112 | + assertEquals("flags/web-sdk-e2e-flag/variants/control", details.getVariant()); |
| 113 | + assertEquals("RESOLVE_REASON_MATCH", details.getReason()); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + void shouldResolveFlagWithStickyResolve() { |
| 118 | + final EvaluationContext stickyContext = |
| 119 | + new MutableContext("test-a") |
| 120 | + .add("sticky", true); |
| 121 | + |
| 122 | + final FlagEvaluationDetails<Double> details = |
| 123 | + client.getDoubleDetails("web-sdk-e2e-flag.double", -1.0, stickyContext); |
| 124 | + |
| 125 | + // The flag has a running experiment with a sticky assignment. The intake is paused but we |
| 126 | + // should still get the sticky assignment. |
| 127 | + // If this test breaks it could mean that the experiment was removed or that the bigtable |
| 128 | + // materialization was cleaned out. |
| 129 | + assertEquals(99.99, details.getValue(), 0.001); |
| 130 | + assertEquals("flags/web-sdk-e2e-flag/variants/sticky", details.getVariant()); |
| 131 | + assertEquals("RESOLVE_REASON_MATCH", details.getReason()); |
| 132 | + } |
| 133 | + |
| 134 | + private static String requireEnv(String name) { |
| 135 | + final String value = System.getenv(name); |
| 136 | + if (value == null || value.isEmpty()) { |
| 137 | + throw new IllegalStateException( |
| 138 | + String.format("Missing required environment variable: %s", name)); |
| 139 | + } |
| 140 | + return value; |
| 141 | + } |
| 142 | +} |
0 commit comments