diff --git a/.github/workflows/pull_requests.yml b/.github/workflows/pull_requests.yml
index 00dedd4e6..95b5929e0 100644
--- a/.github/workflows/pull_requests.yml
+++ b/.github/workflows/pull_requests.yml
@@ -3,10 +3,16 @@ on:
jobs:
build:
- runs-on: ubuntu-latest
+ runs-on: ${{ matrix.os }}-latest
strategy:
matrix:
- version: [ 8, 11, 17 ]
+ version: [8, 11, 17]
+ os: ["ubuntu", "windows", "macos"]
+ exclude:
+ - version: 8
+ os: macos
+ - version: 8
+ os: windows
steps:
- name: Checkout
uses: actions/checkout@v4
diff --git a/pom.xml b/pom.xml
index 8ae60f540..7e926ec38 100644
--- a/pom.xml
+++ b/pom.xml
@@ -10,7 +10,7 @@
5.10.3
4.12.0
UTF-8
- 5.1.5
+ 5.1.7
2.17.2
1.3.14
@@ -59,8 +59,8 @@
io.getunleash
yggdrasil-engine
- 0.1.0-alpha.9
- x86_64-linux
+ 0.1.0-alpha.12
+ ${os.detected.classifier}
com.google.code.gson
@@ -162,6 +162,13 @@
+
+
+ kr.motd.maven
+ os-maven-plugin
+ 1.7.0
+
+
src/main/resources
diff --git a/src/main/java/io/getunleash/DefaultUnleash.java b/src/main/java/io/getunleash/DefaultUnleash.java
index 220ddabbe..abf82cc3d 100644
--- a/src/main/java/io/getunleash/DefaultUnleash.java
+++ b/src/main/java/io/getunleash/DefaultUnleash.java
@@ -199,20 +199,16 @@ public Variant getVariant(String toggleName, UnleashContext context, Variant def
this.unleashEngine.getVariant(toggleName, adaptedContext),
defaultValue);
- Boolean enabled = this.unleashEngine.isEnabled(toggleName, adaptedContext);
-
- // TODO: Swap this for feature enabled
- if (enabled == null) {
- enabled = false;
- }
-
- this.unleashEngine.countToggle(toggleName, enabled);
+ this.unleashEngine.countToggle(toggleName, variant.isFeatureEnabled());
this.unleashEngine.countVariant(toggleName, variant.getName());
eventDispatcher.dispatch(new ToggleEvaluated(toggleName, variant.isEnabled()));
if (unleashEngine.shouldEmitImpressionEvent(toggleName)) {
eventDispatcher.dispatch(
new VariantImpressionEvent(
- toggleName, enabled, context, variant.getName()));
+ toggleName,
+ variant.isFeatureEnabled(),
+ context,
+ variant.getName()));
}
return variant;
} catch (YggdrasilInvalidInputException | YggdrasilError e) {
diff --git a/src/main/java/io/getunleash/Variant.java b/src/main/java/io/getunleash/Variant.java
index 2d8e2760b..d36e36bc3 100644
--- a/src/main/java/io/getunleash/Variant.java
+++ b/src/main/java/io/getunleash/Variant.java
@@ -12,27 +12,41 @@ public class Variant {
@Nullable private final Payload payload;
private final boolean enabled;
@Nullable private final String stickiness;
+ private final boolean feature_enabled;
- public Variant(String name, @Nullable Payload payload, boolean enabled) {
- this(name, payload, enabled, null);
+ public Variant(
+ String name, @Nullable Payload payload, boolean enabled, boolean feature_enabled) {
+ this(name, payload, enabled, null, feature_enabled);
}
- public Variant(String name, @Nullable Payload payload, boolean enabled, String stickiness) {
+ public Variant(
+ String name,
+ @Nullable Payload payload,
+ boolean enabled,
+ String stickiness,
+ boolean feature_enabled) {
this.name = name;
this.payload = payload;
this.enabled = enabled;
this.stickiness = stickiness;
+ this.feature_enabled = feature_enabled;
}
public Variant(String name, @Nullable String payload, boolean enabled) {
- this(name, payload, enabled, null);
+ this(name, payload, enabled, null, false);
}
- public Variant(String name, @Nullable String payload, boolean enabled, String stickiness) {
+ public Variant(
+ String name,
+ @Nullable String payload,
+ boolean enabled,
+ String stickiness,
+ boolean feature_enabled) {
this.name = name;
this.payload = new Payload("string", payload);
this.enabled = enabled;
this.stickiness = stickiness;
+ this.feature_enabled = feature_enabled;
}
public String getName() {
@@ -47,6 +61,10 @@ public boolean isEnabled() {
return enabled;
}
+ public boolean isFeatureEnabled() {
+ return feature_enabled;
+ }
+
@Nullable
public String getStickiness() {
return stickiness;
diff --git a/src/main/java/io/getunleash/YggdrasilAdapters.java b/src/main/java/io/getunleash/YggdrasilAdapters.java
index 18753674b..af9f6d503 100644
--- a/src/main/java/io/getunleash/YggdrasilAdapters.java
+++ b/src/main/java/io/getunleash/YggdrasilAdapters.java
@@ -40,7 +40,9 @@ public static UnleashContext adapt(Context context) {
try {
currentTime = ZonedDateTime.parse(context.getCurrentTime());
} catch (DateTimeParseException e) {
- LOGGER.warn("Could not parse current time from context, falling back to system time: ", context.getCurrentTime());
+ LOGGER.warn(
+ "Could not parse current time from context, falling back to system time: ",
+ context.getCurrentTime());
currentTime = ZonedDateTime.now();
}
}
@@ -73,7 +75,11 @@ public static Variant adapt(VariantDef variant, Variant defaultValue) {
if (variant == null) {
return defaultValue;
}
- return new Variant(variant.getName(), adapt(variant.getPayload()), variant.isEnabled());
+ return new Variant(
+ variant.getName(),
+ adapt(variant.getPayload()),
+ variant.isEnabled(),
+ variant.isFeatureEnabled());
}
public static @Nullable io.getunleash.variant.Payload adapt(@Nullable Payload payload) {
diff --git a/src/main/java/io/getunleash/variant/VariantDefinition.java b/src/main/java/io/getunleash/variant/VariantDefinition.java
index 74409933c..ea59a53c9 100644
--- a/src/main/java/io/getunleash/variant/VariantDefinition.java
+++ b/src/main/java/io/getunleash/variant/VariantDefinition.java
@@ -63,6 +63,6 @@ List getOverrides() {
}
Variant toVariant() {
- return new Variant(name, payload, true, stickiness);
+ return new Variant(name, payload, true, stickiness, false);
}
}
diff --git a/src/test/java/io/getunleash/UnleashTest.java b/src/test/java/io/getunleash/UnleashTest.java
index ea45f04b6..2db7c0914 100644
--- a/src/test/java/io/getunleash/UnleashTest.java
+++ b/src/test/java/io/getunleash/UnleashTest.java
@@ -588,6 +588,27 @@ public void should_be_disabled_with_strategy_variants() {
assertThat(unleash.isEnabled("test")).isFalse();
}
+ @Test
+ public void empty_variants_returns_disabled_variant() {
+ UnleashContext context = UnleashContext.builder().build();
+
+ Map params = new HashMap<>();
+ params.put("rollout", "100");
+ params.put("stickiness", "default");
+ params.put("groupId", "test");
+
+ ActivationStrategy strategy = new ActivationStrategy("flexibleRollout", params);
+ FeatureToggle featureToggle =
+ new FeatureToggle("test", true, asList(strategy), Collections.emptyList());
+
+ stateHandler.setState(featureToggle);
+ final Variant result = unleash.getVariant("test", context);
+
+ assertThat(result.getName()).isEqualTo("disabled");
+ assertThat(result.isEnabled()).isFalse();
+ assertThat(result.isFeatureEnabled()).isTrue();
+ }
+
private List getTestVariants() {
return asList(
new VariantDefinition(
diff --git a/src/test/java/io/getunleash/integration/ClientSpecificationTest.java b/src/test/java/io/getunleash/integration/ClientSpecificationTest.java
index d6cc50c21..76f1cbff8 100644
--- a/src/test/java/io/getunleash/integration/ClientSpecificationTest.java
+++ b/src/test/java/io/getunleash/integration/ClientSpecificationTest.java
@@ -113,6 +113,10 @@ private List createVariantTests(String fileName)
test.getExpectedResult().getPayload(),
result.getPayload(),
test.getDescription());
+ assertEquals(
+ test.getExpectedResult().isFeatureEnabled(),
+ result.isFeatureEnabled(),
+ test.getDescription());
}))
.collect(Collectors.toList());
}
diff --git a/src/test/java/io/getunleash/integration/TestCaseVariant.java b/src/test/java/io/getunleash/integration/TestCaseVariant.java
index bbb7adaa7..f70a09c39 100644
--- a/src/test/java/io/getunleash/integration/TestCaseVariant.java
+++ b/src/test/java/io/getunleash/integration/TestCaseVariant.java
@@ -22,7 +22,14 @@ public String getToggleName() {
public Variant getExpectedResult() {
if (expectedResult.getName().equals("disabled")) {
- return Variant.DISABLED_VARIANT;
+ Variant clone =
+ new Variant(
+ Variant.DISABLED_VARIANT.getName(),
+ Variant.DISABLED_VARIANT.getPayload().orElse(null),
+ Variant.DISABLED_VARIANT.isEnabled(),
+ Variant.DISABLED_VARIANT.getStickiness(),
+ expectedResult.isFeatureEnabled());
+ return clone;
}
return expectedResult;
diff --git a/src/test/java/io/getunleash/strategy/StrategyVariantTest.java b/src/test/java/io/getunleash/strategy/StrategyVariantTest.java
deleted file mode 100644
index 6508c6238..000000000
--- a/src/test/java/io/getunleash/strategy/StrategyVariantTest.java
+++ /dev/null
@@ -1,128 +0,0 @@
-package io.getunleash.strategy;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-import io.getunleash.FeatureEvaluationResult;
-import io.getunleash.UnleashContext;
-import io.getunleash.Variant;
-import io.getunleash.variant.Payload;
-import io.getunleash.variant.VariantDefinition;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.HashMap;
-import org.junit.jupiter.api.Test;
-
-public class StrategyVariantTest {
-
- @Test
- public void should_inherit_stickiness_from_the_strategy_first_variant() {
- HashMap params = new HashMap<>();
- params.put("rollout", "100");
- params.put("stickiness", "clientId");
- params.put("groupId", "a");
- FlexibleRolloutStrategy strategy = new FlexibleRolloutStrategy();
- VariantDefinition varA =
- new VariantDefinition(
- "variantNameA",
- 1,
- new Payload("string", "variantValueA"),
- Collections.emptyList());
- VariantDefinition varB =
- new VariantDefinition(
- "variantNameB",
- 1,
- new Payload("string", "variantValueB"),
- Collections.emptyList());
-
- UnleashContext context = UnleashContext.builder().addProperty("clientId", "1").build();
- FeatureEvaluationResult result =
- strategy.getResult(
- params, context, Collections.emptyList(), Arrays.asList(varA, varB));
- Variant selectedVariant = result.getVariant();
- assert selectedVariant != null;
- assertThat(selectedVariant.getName()).isEqualTo("variantNameA");
- }
-
- @Test
- public void should_inherit_stickiness_from_the_strategy_second_variant() {
- HashMap params = new HashMap<>();
- params.put("rollout", "100");
- params.put("stickiness", "clientId");
- params.put("groupId", "a");
- FlexibleRolloutStrategy strategy = new FlexibleRolloutStrategy();
- VariantDefinition varA =
- new VariantDefinition(
- "variantNameA",
- 1,
- new Payload("string", "variantValueA"),
- Collections.emptyList());
- VariantDefinition varB =
- new VariantDefinition(
- "variantNameB",
- 1,
- new Payload("string", "variantValueB"),
- Collections.emptyList());
-
- UnleashContext context = UnleashContext.builder().addProperty("clientId", "2").build();
- FeatureEvaluationResult result =
- strategy.getResult(
- params, context, Collections.emptyList(), Arrays.asList(varA, varB));
- Variant selectedVariant = result.getVariant();
- assert selectedVariant != null;
- assertThat(selectedVariant.getName()).isEqualTo("variantNameB");
- }
-
- @Test
- public void multiple_variants_should_choose_first_variant() {
- HashMap params = new HashMap<>();
- params.put("rollout", "100");
- params.put("groupId", "a");
- params.put("stickiness", "default");
- FlexibleRolloutStrategy strategy = new FlexibleRolloutStrategy();
- VariantDefinition varA =
- new VariantDefinition(
- "variantNameA",
- 1,
- new Payload("string", "variantValueA"),
- Collections.emptyList());
- VariantDefinition varB =
- new VariantDefinition(
- "variantNameB",
- 1,
- new Payload("string", "variantValueB"),
- Collections.emptyList());
- UnleashContext context = UnleashContext.builder().userId("5").build();
- FeatureEvaluationResult result =
- strategy.getResult(
- params, context, Collections.emptyList(), Arrays.asList(varA, varB));
- Variant selectedVariant = result.getVariant();
- assertThat(selectedVariant.getName()).isEqualTo("variantNameA");
- }
-
- @Test
- public void multiple_variants_should_choose_second_variant() {
- HashMap params = new HashMap<>();
- params.put("rollout", "100");
- params.put("groupId", "a");
- params.put("stickiness", "default");
- FlexibleRolloutStrategy strategy = new FlexibleRolloutStrategy();
- VariantDefinition varA =
- new VariantDefinition(
- "variantNameA",
- 1,
- new Payload("string", "variantValueA"),
- Collections.emptyList());
- VariantDefinition varB =
- new VariantDefinition(
- "variantNameB",
- 1,
- new Payload("string", "variantValueB"),
- Collections.emptyList());
- UnleashContext context = UnleashContext.builder().userId("0").build();
- FeatureEvaluationResult result =
- strategy.getResult(
- params, context, Collections.emptyList(), Arrays.asList(varA, varB));
- Variant selectedVariant = result.getVariant();
- assertThat(selectedVariant.getName()).isEqualTo("variantNameB");
- }
-}
diff --git a/src/test/java/io/getunleash/variant/VariantUtilTest.java b/src/test/java/io/getunleash/variant/VariantUtilTest.java
deleted file mode 100644
index 16325169f..000000000
--- a/src/test/java/io/getunleash/variant/VariantUtilTest.java
+++ /dev/null
@@ -1,441 +0,0 @@
-package io.getunleash.variant;
-
-import static io.getunleash.Variant.DISABLED_VARIANT;
-import static java.util.Arrays.asList;
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
-
-import io.getunleash.ActivationStrategy;
-import io.getunleash.FeatureToggle;
-import io.getunleash.UnleashContext;
-import io.getunleash.Variant;
-import io.getunleash.util.UnleashConfig;
-import io.getunleash.util.UnleashScheduledExecutor;
-import java.util.*;
-import java.util.function.Consumer;
-import java.util.stream.Collectors;
-import java.util.stream.IntStream;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-
-public class VariantUtilTest {
-
- private ActivationStrategy defaultStrategy;
-
- @BeforeEach
- void setUp() {
- UnleashConfig defaultConfig =
- new UnleashConfig.Builder()
- .appName("test")
- .unleashAPI("http://localhost:4242/api/")
- .scheduledExecutor(mock(UnleashScheduledExecutor.class))
- .fetchTogglesInterval(200L)
- .synchronousFetchOnInitialisation(true)
- .build();
-
- defaultStrategy = new ActivationStrategy("default", Collections.emptyMap());
- }
-
- @Test
- public void should_return_default_variant_when_toggle_has_no_variants() {
- FeatureToggle toggle = new FeatureToggle("test.variants", true, asList(defaultStrategy));
- UnleashContext context = UnleashContext.builder().build();
-
- Variant variant = VariantUtil.selectVariant(toggle, context, DISABLED_VARIANT);
-
- assertThat(variant).isEqualTo(DISABLED_VARIANT);
- }
-
- @Test
- public void should_return_variant1() {
- VariantDefinition v1 =
- new VariantDefinition(
- "a", 33, new Payload("string", "asd"), Collections.emptyList());
- VariantDefinition v2 = new VariantDefinition("b", 33);
- VariantDefinition v3 = new VariantDefinition("c", 34);
-
- FeatureToggle toggle =
- new FeatureToggle(
- "test.variants", true, asList(defaultStrategy), asList(v1, v2, v3));
-
- UnleashContext context = UnleashContext.builder().userId("11").build();
-
- Variant variant = VariantUtil.selectVariant(toggle, context, DISABLED_VARIANT);
-
- assertThat(variant.getName()).isEqualTo(v1.getName());
- assertThat(variant.getPayload()).hasValue(v1.getPayload());
- assertThat(variant.isEnabled()).isTrue();
- }
-
- @Test
- public void should_return_variant2() {
- VariantDefinition v1 =
- new VariantDefinition(
- "a", 33, new Payload("string", "asd"), Collections.emptyList());
- VariantDefinition v2 = new VariantDefinition("b", 33);
- VariantDefinition v3 = new VariantDefinition("c", 34);
-
- FeatureToggle toggle =
- new FeatureToggle(
- "test.variants", true, asList(defaultStrategy), asList(v1, v2, v3));
-
- UnleashContext context = UnleashContext.builder().userId("80").build();
-
- Variant variant = VariantUtil.selectVariant(toggle, context, DISABLED_VARIANT);
-
- assertThat(variant.getName()).isEqualTo(v2.getName());
- }
-
- @Test
- public void should_return_variant3() {
- VariantDefinition v1 = new VariantDefinition("a", 33);
- VariantDefinition v2 = new VariantDefinition("b", 33);
- VariantDefinition v3 = new VariantDefinition("c", 34);
-
- FeatureToggle toggle =
- new FeatureToggle(
- "test.variants", true, asList(defaultStrategy), asList(v1, v2, v3));
-
- UnleashContext context = UnleashContext.builder().userId("163").build();
-
- Variant variant = VariantUtil.selectVariant(toggle, context, DISABLED_VARIANT);
-
- assertThat(variant.getName()).isEqualTo(v3.getName());
- }
-
- @Test
- public void should_return_variant_override() {
- VariantDefinition v1 = new VariantDefinition("a", 33);
- VariantOverride override = new VariantOverride("userId", asList("11", "12", "123", "44"));
- VariantDefinition v2 = new VariantDefinition("b", 33, null, asList(override));
- VariantDefinition v3 = new VariantDefinition("c", 34);
-
- FeatureToggle toggle =
- new FeatureToggle(
- "test.variants", true, asList(defaultStrategy), asList(v1, v2, v3));
-
- UnleashContext context = UnleashContext.builder().userId("123").build();
-
- Variant variant = VariantUtil.selectVariant(toggle, context, DISABLED_VARIANT);
-
- assertThat(variant.getName()).isEqualTo(v2.getName());
- }
-
- @Test
- public void should_return_variant_override_on_remote_adr() {
- VariantDefinition v1 =
- new VariantDefinition(
- "a", 33, new Payload("string", "asd"), Collections.emptyList());
- VariantDefinition v2 = new VariantDefinition("b", 33, null, Collections.emptyList());
- VariantOverride override = new VariantOverride("remoteAddress", asList("11.11.11.11"));
- VariantDefinition v3 =
- new VariantDefinition("c", 34, new Payload("string", "blob"), asList(override));
-
- FeatureToggle toggle =
- new FeatureToggle(
- "test.variants", true, asList(defaultStrategy), asList(v1, v2, v3));
-
- UnleashContext context = UnleashContext.builder().remoteAddress("11.11.11.11").build();
-
- Variant variant = VariantUtil.selectVariant(toggle, context, DISABLED_VARIANT);
-
- assertThat(variant.getName()).isEqualTo(v3.getName());
- assertThat(variant.getPayload()).hasValue(v3.getPayload());
- assertThat(variant.isEnabled()).isTrue();
- }
-
- @Test
- public void should_return_variant_override_on_custom_prop() {
- VariantDefinition v1 = new VariantDefinition("a", 33);
- VariantOverride override = new VariantOverride("env", asList("ci", "local", "dev"));
- VariantDefinition v2 = new VariantDefinition("b", 33, null, asList(override));
- VariantDefinition v3 = new VariantDefinition("c", 34);
-
- FeatureToggle toggle =
- new FeatureToggle(
- "test.variants", true, asList(defaultStrategy), asList(v1, v2, v3));
-
- UnleashContext context =
- UnleashContext.builder().userId("11").addProperty("env", "dev").build();
-
- Variant variant = VariantUtil.selectVariant(toggle, context, DISABLED_VARIANT);
-
- assertThat(variant.getName()).isEqualTo(v2.getName());
- }
-
- @Test
- public void should_return_variant_override_on_sessionId() {
- String sessionId = "122221";
-
- VariantDefinition v1 = new VariantDefinition("a", 33);
- VariantOverride override_env = new VariantOverride("env", asList("dev"));
- VariantOverride override_session = new VariantOverride("sessionId", asList(sessionId));
- VariantDefinition v2 =
- new VariantDefinition("b", 33, null, asList(override_env, override_session));
- VariantDefinition v3 = new VariantDefinition("c", 34);
-
- FeatureToggle toggle =
- new FeatureToggle(
- "test.variants", true, asList(defaultStrategy), asList(v1, v2, v3));
-
- UnleashContext context =
- UnleashContext.builder()
- .userId("11")
- .addProperty("env", "prod")
- .sessionId(sessionId)
- .build();
-
- Variant variant = VariantUtil.selectVariant(toggle, context, DISABLED_VARIANT);
-
- assertThat(variant.getName()).isEqualTo(v2.getName());
- }
-
- @Test
- public void should_distribute_variants_according_to_stickiness() {
- VariantDefinition v1 = new VariantDefinition("blue", 1, null, null, "customField");
- VariantDefinition v2 = new VariantDefinition("red", 1, null, null, "customField");
- VariantDefinition v3 = new VariantDefinition("green", 1, null, null, "customField");
- VariantDefinition v4 = new VariantDefinition("yellow", 1, null, null, "customField");
- FeatureToggle variantToggle =
- new FeatureToggle(
- "toggle-with-variants",
- true,
- asList(defaultStrategy),
- asList(v1, v2, v3, v4));
- UnleashContext context =
- UnleashContext.builder()
- .userId("11")
- .addProperty("env", "prod")
- .sessionId("1222221")
- .build();
- Map> variantResults =
- IntStream.range(0, 10000)
- .mapToObj(
- i ->
- VariantUtil.selectVariant(
- variantToggle, context, DISABLED_VARIANT))
- .collect(Collectors.groupingBy(Variant::getName));
- assertThat(variantResults)
- .allSatisfy(
- (name, variantResult) ->
- assertThat(variantResult).hasSizeBetween(2300, 2700));
- }
-
- @Test
- public void should_return_same_variant_when_stickiness_is_set_to_default() {
- VariantDefinition v1 = new VariantDefinition("a", 33, null, null, "default");
- VariantDefinition v2 = new VariantDefinition("b", 33, null, null, "default");
- VariantDefinition v3 = new VariantDefinition("c", 34, null, null, "default");
-
- FeatureToggle toggle =
- new FeatureToggle(
- "test.variants", true, asList(defaultStrategy), asList(v1, v2, v3));
-
- UnleashContext context = UnleashContext.builder().userId("163;").build();
- List results =
- IntStream.range(0, 500)
- .mapToObj(i -> VariantUtil.selectVariant(toggle, context, DISABLED_VARIANT))
- .collect(Collectors.toList());
- assertThat(results)
- .allSatisfy(
- (Consumer)
- variant -> assertThat(variant.getName()).isEqualTo(v3.getName()));
- }
-
- @Test
- public void custom_stickiness_variants() {
- Map parameters = new HashMap<>();
- parameters.put("rollout", "100");
- parameters.put("stickiness", "customField");
- parameters.put("groupId", "Feature.flexible.rollout.custom.stickiness_100");
- ActivationStrategy flexibleRollout = new ActivationStrategy("flexibleRollout", parameters);
- List variants = new ArrayList<>();
- variants.add(
- new VariantDefinition(
- "blue",
- 25,
- new Payload("string", "val1"),
- Collections.emptyList(),
- "customField"));
- variants.add(
- new VariantDefinition(
- "red",
- 25,
- new Payload("string", "val1"),
- Collections.emptyList(),
- "customField"));
- variants.add(
- new VariantDefinition(
- "green",
- 25,
- new Payload("string", "val1"),
- Collections.emptyList(),
- "customField"));
- variants.add(
- new VariantDefinition(
- "yellow",
- 25,
- new Payload("string", "val1"),
- Collections.emptyList(),
- "customField"));
- FeatureToggle toggle =
- new FeatureToggle(
- "Feature.flexible.rollout.custom.stickiness_100",
- true,
- asList(flexibleRollout),
- variants);
- Variant variantCustom616 =
- VariantUtil.selectVariant(
- toggle,
- UnleashContext.builder().addProperty("customField", "616").build(),
- DISABLED_VARIANT);
- assertThat(variantCustom616.getName()).isEqualTo("blue");
- Variant variantCustom503 =
- VariantUtil.selectVariant(
- toggle,
- UnleashContext.builder().addProperty("customField", "503").build(),
- DISABLED_VARIANT);
- assertThat(variantCustom503.getName()).isEqualTo("red");
- Variant variantCustom438 =
- VariantUtil.selectVariant(
- toggle,
- UnleashContext.builder().addProperty("customField", "438").build(),
- DISABLED_VARIANT);
- assertThat(variantCustom438.getName()).isEqualTo("green");
- Variant variantCustom44 =
- VariantUtil.selectVariant(
- toggle,
- UnleashContext.builder().addProperty("customField", "44").build(),
- DISABLED_VARIANT);
- assertThat(variantCustom44.getName()).isEqualTo("yellow");
- }
-
- @Test
- public void feature_variants_variant_b_client_spec_tests() {
- List variants = new ArrayList<>();
- variants.add(
- new VariantDefinition(
- "variant1", 1, new Payload("string", "val1"), Collections.emptyList()));
- variants.add(
- new VariantDefinition(
- "variant2", 1, new Payload("string", "val2"), Collections.emptyList()));
- FeatureToggle toggle =
- new FeatureToggle("Feature.Variants.B", true, Collections.emptyList(), variants);
- Variant variantUser2 =
- VariantUtil.selectVariant(
- toggle, UnleashContext.builder().userId("2").build(), DISABLED_VARIANT);
- assertThat(variantUser2.getName()).isEqualTo("variant2");
- Variant variantUser0 =
- VariantUtil.selectVariant(
- toggle, UnleashContext.builder().userId("0").build(), DISABLED_VARIANT);
- assertThat(variantUser0.getName()).isEqualTo("variant1");
- }
-
- @Test
- public void feature_variants_variant_c_client_spec_tests() {
- List variants = new ArrayList<>();
- variants.add(
- new VariantDefinition(
- "variant1", 33, new Payload("string", "val1"), Collections.emptyList()));
- variants.add(
- new VariantDefinition(
- "variant2", 33, new Payload("string", "val1"), Collections.emptyList()));
- variants.add(
- new VariantDefinition(
- "variant3", 33, new Payload("string", "val1"), Collections.emptyList()));
- FeatureToggle toggle =
- new FeatureToggle("Feature.Variants.C", true, Collections.emptyList(), variants);
- Variant variantUser232 =
- VariantUtil.selectVariant(
- toggle, UnleashContext.builder().userId("232").build(), DISABLED_VARIANT);
- assertThat(variantUser232.getName()).isEqualTo("variant1");
- Variant variantUser607 =
- VariantUtil.selectVariant(
- toggle, UnleashContext.builder().userId("607").build(), DISABLED_VARIANT);
- assertThat(variantUser607.getName()).isEqualTo("variant2");
- Variant variantUser656 =
- VariantUtil.selectVariant(
- toggle, UnleashContext.builder().userId("656").build(), DISABLED_VARIANT);
- assertThat(variantUser656.getName()).isEqualTo("variant3");
- }
-
- @Test
- public void feature_variants_variant_d_client_spec_tests() {
- List variants = new ArrayList<>();
- variants.add(
- new VariantDefinition(
- "variant1", 1, new Payload("string", "val1"), Collections.emptyList()));
- variants.add(
- new VariantDefinition(
- "variant2", 49, new Payload("string", "val2"), Collections.emptyList()));
- variants.add(
- new VariantDefinition(
- "variant3", 50, new Payload("string", "val3"), Collections.emptyList()));
- FeatureToggle toggle =
- new FeatureToggle("Feature.Variants.D", true, Collections.emptyList(), variants);
- Variant variantUser712 =
- VariantUtil.selectVariant(
- toggle, UnleashContext.builder().userId("712").build(), DISABLED_VARIANT);
- assertThat(variantUser712.getName()).isEqualTo("variant1");
- Variant variantUser525 =
- VariantUtil.selectVariant(
- toggle, UnleashContext.builder().userId("525").build(), DISABLED_VARIANT);
- assertThat(variantUser525.getName()).isEqualTo("variant2");
- Variant variantUser537 =
- VariantUtil.selectVariant(
- toggle, UnleashContext.builder().userId("537").build(), DISABLED_VARIANT);
- assertThat(variantUser537.getName()).isEqualTo("variant3");
- }
-
- @Test
- public void feature_variants_variant_d_client_spec_tests_with_deprecated_seed() {
- List variants = new ArrayList<>();
- variants.add(
- new VariantDefinition(
- "variant1", 1, new Payload("string", "val1"), Collections.emptyList()));
- variants.add(
- new VariantDefinition(
- "variant2", 49, new Payload("string", "val2"), Collections.emptyList()));
- variants.add(
- new VariantDefinition(
- "variant3", 50, new Payload("string", "val3"), Collections.emptyList()));
- FeatureToggle toggle =
- new FeatureToggle("Feature.Variants.D", true, Collections.emptyList(), variants);
- Variant variantUser712 =
- VariantUtil.selectDeprecatedVariantHashingAlgo(
- toggle, UnleashContext.builder().userId("712").build(), DISABLED_VARIANT);
- assertThat(variantUser712.getName()).isEqualTo("variant3");
- Variant variantUser525 =
- VariantUtil.selectDeprecatedVariantHashingAlgo(
- toggle, UnleashContext.builder().userId("525").build(), DISABLED_VARIANT);
- assertThat(variantUser525.getName()).isEqualTo("variant3");
- Variant variantUser537 =
- VariantUtil.selectDeprecatedVariantHashingAlgo(
- toggle, UnleashContext.builder().userId("537").build(), DISABLED_VARIANT);
- assertThat(variantUser537.getName()).isEqualTo("variant2");
- }
-
- @Test
- public void feature_variants_variant_d_with_override_client_spec_tests() {
- List variants = new ArrayList<>();
- variants.add(
- new VariantDefinition(
- "variant1",
- 33,
- new Payload("string", "val1"),
- Arrays.asList(new VariantOverride("userId", asList("132", "61")))));
- variants.add(
- new VariantDefinition(
- "variant2", 33, new Payload("string", "val2"), Collections.emptyList()));
- variants.add(
- new VariantDefinition(
- "variant3", 34, new Payload("string", "val3"), Collections.emptyList()));
- FeatureToggle toggle =
- new FeatureToggle(
- "Feature.Variants.override.D", true, Collections.emptyList(), variants);
- Variant variantUser10 =
- VariantUtil.selectVariant(
- toggle, UnleashContext.builder().userId("10").build(), DISABLED_VARIANT);
- assertThat(variantUser10.getName()).isEqualTo("variant2");
- }
-}