Skip to content

Commit

Permalink
fix: use gradle artifact from MAVEN_HOME
Browse files Browse the repository at this point in the history
chriswk committed Nov 30, 2023
1 parent 35d6f8a commit 71ed14f
Showing 12 changed files with 447 additions and 435 deletions.
6 changes: 2 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -56,11 +56,9 @@

<dependencies>
<dependency>
<artifactId>unleash-engine</artifactId>
<artifactId>yggdrasil-engine</artifactId>
<groupId>io.getunleash</groupId>
<scope>system</scope>
<version>0.0.1-SNAPSHOT</version>
<systemPath>${project.basedir}/unleash-engine-all.jar</systemPath>
<version>0.1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
2 changes: 0 additions & 2 deletions src/main/java/io/getunleash/strategy/Strategy.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.getunleash.strategy;

import io.getunleash.UnleashContext;

import java.util.Map;

public interface Strategy {
@@ -13,5 +12,4 @@ public interface Strategy {
default boolean isEnabled(Map<String, String> parameters, UnleashContext unleashContext) {
return isEnabled(parameters);
}

}
1 change: 0 additions & 1 deletion src/main/java/io/getunleash/strategy/StrategyUtils.java
Original file line number Diff line number Diff line change
@@ -18,5 +18,4 @@ public static int getNormalizedNumber(String identifier, String groupId, long se
long hash = Murmur3.hash_x86_32(value, value.length, seed);
return (int) (hash % ONE_HUNDRED) + 1;
}

}
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
package io.getunleash.strategy;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;

import com.google.common.collect.ImmutableList;
import io.getunleash.ActivationStrategy;
import io.getunleash.DefaultUnleash;
import io.getunleash.FeatureToggle;
import io.getunleash.repository.UnleashEngineStateHandler;
import io.getunleash.util.UnleashConfig;
import io.getunleash.util.UnleashScheduledExecutor;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class ApplicationHostnameStrategyTest {

@@ -28,13 +27,12 @@ public class ApplicationHostnameStrategyTest {
@BeforeEach
void init() {
UnleashConfig config =
new UnleashConfig.Builder()
.appName("test")
.unleashAPI("http://localhost:4242/api/")
.environment("test")
.scheduledExecutor(mock(UnleashScheduledExecutor.class))
.build();

new UnleashConfig.Builder()
.appName("test")
.unleashAPI("http://localhost:4242/api/")
.environment("test")
.scheduledExecutor(mock(UnleashScheduledExecutor.class))
.build();

engine = new DefaultUnleash(config);
stateHandler = new UnleashEngineStateHandler(engine);
@@ -50,11 +48,11 @@ public void should_be_disabled_if_no_HostNames_in_params() {
Map<String, String> params = new HashMap<>();
params.put("hostNames", null);

stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("applicationHostname", params))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("applicationHostname", params))));
assertFalse(engine.isEnabled("test"));
}

@@ -63,11 +61,11 @@ public void should_be_disabled_if_hostname_not_in_list() {
Map<String, String> params = new HashMap<>();
params.put("hostNames", "MegaHost,MiniHost, happyHost");

stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("applicationHostname", params))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("applicationHostname", params))));
assertFalse(engine.isEnabled("test"));
}

@@ -79,11 +77,11 @@ public void so_close_but_no_cigar() {
Map<String, String> params = new HashMap<>();

params.put("hostNames", "MegaHost, MiniHost, SuperhostOne");
stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("applicationHostname", params))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("applicationHostname", params))));
assertFalse(engine.isEnabled("test"));
}

@@ -94,21 +92,22 @@ public void should_be_enabled_for_InetAddress() throws UnknownHostException {

Map<String, String> params = new HashMap<>();
params.put("hostNames", "MegaHost," + hostName + ",MiniHost, happyHost");
stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("applicationHostname", params))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("applicationHostname", params))));
assertTrue(engine.isEnabled("test"));
}

@Test
public void null_test() {
stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("applicationHostname", new HashMap<>()))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(
new ActivationStrategy("applicationHostname", new HashMap<>()))));
assertFalse(engine.isEnabled("test"));
}
}
165 changes: 84 additions & 81 deletions src/test/java/io/getunleash/strategy/FlexibleRolloutStrategyTest.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package io.getunleash.strategy;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;

import com.google.common.collect.ImmutableList;
import io.getunleash.*;
import io.getunleash.repository.UnleashEngineStateHandler;
import io.getunleash.util.UnleashConfig;
import io.getunleash.util.UnleashScheduledExecutor;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;

class FlexibleRolloutStrategyTest {

private DefaultUnleash engine;
@@ -24,25 +23,25 @@ class FlexibleRolloutStrategyTest {
@BeforeEach
void init() {
UnleashConfig config =
new UnleashConfig.Builder()
.appName("test")
.unleashAPI("http://localhost:4242/api/")
.environment("test")
.scheduledExecutor(mock(UnleashScheduledExecutor.class))
.build();

new UnleashConfig.Builder()
.appName("test")
.unleashAPI("http://localhost:4242/api/")
.environment("test")
.scheduledExecutor(mock(UnleashScheduledExecutor.class))
.build();

engine = new DefaultUnleash(config);
stateHandler = new UnleashEngineStateHandler(engine);
}

@Test
public void should_always_be_false() {
stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("flexibleRollout", new HashMap<>()))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(
new ActivationStrategy("flexibleRollout", new HashMap<>()))));
assertFalse(engine.isEnabled("test"));
}

@@ -54,11 +53,11 @@ public void should_NOT_be_enabled_for_rollout_9_and_userId_61() {
params.put("groupId", "Demo");

UnleashContext context = UnleashContext.builder().userId("61").build();
stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("flexibleRollout", params))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("flexibleRollout", params))));
assertFalse(engine.isEnabled("test", context));
}

@@ -70,11 +69,11 @@ public void should_be_enabled_for_rollout_10_and_userId_61() {
params.put("groupId", "Demo");

UnleashContext context = UnleashContext.builder().userId("61").build();
stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("flexibleRollout", params))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("flexibleRollout", params))));
assertTrue(engine.isEnabled("test", context));
}

@@ -86,11 +85,11 @@ public void should_be_enabled_for_rollout_10_and_userId_61_and_stickiness_userId
params.put("groupId", "Demo");

UnleashContext context = UnleashContext.builder().userId("61").build();
stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("flexibleRollout", params))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("flexibleRollout", params))));
assertTrue(engine.isEnabled("test", context));
}

@@ -102,11 +101,11 @@ public void should_be_disabled_when_userId_missing() {
params.put("groupId", "Demo");

UnleashContext context = UnleashContext.builder().build();
stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("flexibleRollout", params))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("flexibleRollout", params))));
assertFalse(engine.isEnabled("test", context));
}

@@ -118,11 +117,11 @@ public void should_be_enabled_for_rollout_10_and_sessionId_61() {
params.put("groupId", "Demo");

UnleashContext context = UnleashContext.builder().sessionId("61").build();
stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("flexibleRollout", params))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("flexibleRollout", params))));
assertTrue(engine.isEnabled("test", context));
}

@@ -134,45 +133,49 @@ public void should_be_enabled_for_rollout_10_and_randomId_61_and_stickiness_sess
params.put("groupId", "Demo");

UnleashContext context = UnleashContext.builder().sessionId("61").build();
stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("flexibleRollout", params))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("flexibleRollout", params))));
assertTrue(engine.isEnabled("test", context));
}

@Test
@Disabled // TODO we can't inject a random generator into the strategy it can be done providing a custom strategy though in which case we should keep the strategy classes around but it can't override the existing flexibleRollout
@Disabled // TODO we can't inject a random generator into the strategy it can be done providing
// a custom strategy though in which case we should keep the strategy classes around
// but it can't override the existing flexibleRollout
public void should_be_enabled_for_rollout_10_and_randomId_61_and_stickiness_default() {
Map<String, String> params = new HashMap<>();
params.put("rollout", "10");
params.put("stickiness", "default");
params.put("groupId", "Demo");

UnleashContext context = UnleashContext.builder().build();
stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("flexibleRollout", params))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("flexibleRollout", params))));
assertTrue(engine.isEnabled("test", context));
}

@Test
@Disabled // TODO we can't inject a random generator into the strategy it can be done providing a custom strategy though in which case we should keep the strategy classes around but it can't override the existing flexibleRollout
@Disabled // TODO we can't inject a random generator into the strategy it can be done providing
// a custom strategy though in which case we should keep the strategy classes around
// but it can't override the existing flexibleRollout
public void should_be_enabled_for_rollout_10_and_randomId_61_and_stickiness_random() {
Map<String, String> params = new HashMap<>();
params.put("rollout", "10");
params.put("stickiness", "random");
params.put("groupId", "Demo");

UnleashContext context = UnleashContext.builder().build();
stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("flexibleRollout", params))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("flexibleRollout", params))));
assertTrue(engine.isEnabled("test", context));
}

@@ -184,11 +187,11 @@ public void should_NOT_be_enabled_for_rollout_10_and_randomId_1() {
params.put("groupId", "Demo");

UnleashContext context = UnleashContext.builder().build();
stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("flexibleRollout", params))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("flexibleRollout", params))));
assertFalse(engine.isEnabled("test", context));
}

@@ -200,11 +203,11 @@ public void should_not_be_enabled_for_custom_field_402() {
params.put("groupId", "Feature.flexible.rollout.custom.stickiness_50");
UnleashContext context = UnleashContext.builder().addProperty("customField", "402").build();

stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("flexibleRollout", params))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("flexibleRollout", params))));
assertFalse(engine.isEnabled("test", context));
}

@@ -215,11 +218,11 @@ public void should_not_be_enabled_for_custom_field_388_and_39() {
params.put("stickiness", "customField");
params.put("groupId", "Feature.flexible.rollout.custom.stickiness_50");

stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("flexibleRollout", params))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("flexibleRollout", params))));

UnleashContext context = UnleashContext.builder().addProperty("customField", "388").build();
assertTrue(engine.isEnabled("test", context));
@@ -234,11 +237,11 @@ public void should_not_be_enabled_with_custom_stickiness_if_custom_field_is_miss
params.put("stickiness", "customField");
params.put("groupId", "Feature.flexible.rollout.custom.stickiness_50");
UnleashContext context = UnleashContext.builder().build();
stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("flexibleRollout", params))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("flexibleRollout", params))));
assertFalse(engine.isEnabled("test", context));
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
package io.getunleash.strategy;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;

import com.google.common.collect.ImmutableList;
import io.getunleash.*;
import io.getunleash.repository.UnleashEngineStateHandler;
import io.getunleash.util.UnleashConfig;
import io.getunleash.util.UnleashScheduledExecutor;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public final class GradualRolloutRandomStrategyTest {
private DefaultUnleash engine;
@@ -24,13 +22,12 @@ public final class GradualRolloutRandomStrategyTest {
@BeforeEach
void setUp() {
UnleashConfig config =
new UnleashConfig.Builder()
.appName("test")
.unleashAPI("http://localhost:4242/api/")
.environment("test")
.scheduledExecutor(mock(UnleashScheduledExecutor.class))
.build();

new UnleashConfig.Builder()
.appName("test")
.unleashAPI("http://localhost:4242/api/")
.environment("test")
.scheduledExecutor(mock(UnleashScheduledExecutor.class))
.build();

engine = new DefaultUnleash(config);
stateHandler = new UnleashEngineStateHandler(engine);
@@ -40,11 +37,12 @@ void setUp() {
public void should_not_be_enabled_when_percentage_not_set() {
final Map<String, String> parameters = new HashMap<>();

stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("gradualRolloutRandom", parameters))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(
new ActivationStrategy("gradualRolloutRandom", parameters))));
final boolean enabled = engine.isEnabled("test");

assertFalse(enabled);
@@ -59,12 +57,13 @@ public void should_not_be_enabled_when_percentage_is_not_a_not_a_number() {
}
};

stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("gradualRolloutRandom", parameters)),
Collections.emptyList()
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(
new ActivationStrategy("gradualRolloutRandom", parameters)),
Collections.emptyList()));
final boolean enabled = engine.isEnabled("test");

assertFalse(enabled);
@@ -79,12 +78,13 @@ public void should_not_be_enabled_when_percentage_is_not_a_not_a_valid_percentag
}
};

stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("gradualRolloutRandom", parameters)),
Collections.emptyList()
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(
new ActivationStrategy("gradualRolloutRandom", parameters)),
Collections.emptyList()));
final boolean enabled = engine.isEnabled("test");

assertFalse(enabled);
@@ -99,12 +99,13 @@ public void should_never_be_enabled_when_0_percent() {
}
};

stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("gradualRolloutRandom", parameters)),
Collections.emptyList()
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(
new ActivationStrategy("gradualRolloutRandom", parameters)),
Collections.emptyList()));

for (int i = 0; i < 1000; i++) {
final boolean enabled = engine.isEnabled("test");
@@ -121,12 +122,13 @@ public void should_always_be_enabled_when_100_percent() {
}
};

stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("gradualRolloutRandom", parameters)),
Collections.emptyList()
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(
new ActivationStrategy("gradualRolloutRandom", parameters)),
Collections.emptyList()));
for (int i = 0; i <= 100; i++) {
final boolean enabled = engine.isEnabled("test");
assertTrue(enabled, "Should be enabled for p=" + i);
@@ -149,12 +151,13 @@ public void should_diverage_at_most_with_one_percent_point() {
int rounds = 20000;
int countEnabled = 0;

stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("gradualRolloutRandom", parameters)),
Collections.emptyList()
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(
new ActivationStrategy("gradualRolloutRandom", parameters)),
Collections.emptyList()));
for (int i = 0; i < rounds; i++) {
final boolean enabled = engine.isEnabled("test");
if (enabled) {
Original file line number Diff line number Diff line change
@@ -4,19 +4,16 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import com.google.common.collect.ImmutableList;
import io.getunleash.*;

import io.getunleash.repository.UnleashEngineStateHandler;
import io.getunleash.util.UnleashConfig;
import io.getunleash.util.UnleashScheduledExecutor;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import io.getunleash.repository.UnleashEngineStateHandler;
import io.getunleash.util.UnleashConfig;
import io.getunleash.util.UnleashScheduledExecutor;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
@@ -35,49 +32,52 @@ public class GradualRolloutSessionIdStrategyTest {
@BeforeEach
void setUp() {
percentages =
ImmutableList.<Integer>builder()
.add(1)
.add(2)
.add(5)
.add(10)
.add(25)
.add(50)
.add(90)
.add(99)
.add(100)
.build();
ImmutableList.<Integer>builder()
.add(1)
.add(2)
.add(5)
.add(10)
.add(25)
.add(50)
.add(90)
.add(99)
.add(100)
.build();

UnleashConfig config =
new UnleashConfig.Builder()
.appName("test")
.unleashAPI("http://localhost:4242/api/")
.environment("test")
.scheduledExecutor(mock(UnleashScheduledExecutor.class))
.build();

new UnleashConfig.Builder()
.appName("test")
.unleashAPI("http://localhost:4242/api/")
.environment("test")
.scheduledExecutor(mock(UnleashScheduledExecutor.class))
.build();

engine = new DefaultUnleash(config);
stateHandler = new UnleashEngineStateHandler(engine);
}

@Test
public void should_require_context() {
stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("gradualRolloutSessionId", new HashMap<>()))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(
new ActivationStrategy(
"gradualRolloutSessionId", new HashMap<>()))));
assertThat(engine.isEnabled("test")).isFalse();
}

@Test
public void should_be_disabled_when_missing_user_id() {
UnleashContext context = UnleashContext.builder().build();
stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("gradualRolloutSessionId", new HashMap<>()))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(
new ActivationStrategy(
"gradualRolloutSessionId", new HashMap<>()))));
assertThat(engine.isEnabled("test", context)).isFalse();
}

@@ -86,11 +86,12 @@ public void should_have_same_result_for_multiple_executions() {
UnleashContext context = UnleashContext.builder().sessionId("1574576830").build();

Map<String, String> params = buildParams(1, "innfinn");
stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("gradualRolloutSessionId", params))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(
new ActivationStrategy("gradualRolloutSessionId", params))));

boolean firstRunResult = engine.isEnabled("test", context);
for (int i = 0; i < 10; i++) {
@@ -106,11 +107,12 @@ public void should_be_enabled_when_using_100percent_rollout() {
UnleashContext context = UnleashContext.builder().sessionId("1574576830").build();

Map<String, String> params = buildParams(100, "innfinn");
stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("gradualRolloutSessionId", params))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(
new ActivationStrategy("gradualRolloutSessionId", params))));
boolean result = engine.isEnabled("test", context);

assertThat(result).isTrue();
@@ -121,11 +123,12 @@ public void should_not_be_enabled_when_0percent_rollout() {
UnleashContext context = UnleashContext.builder().sessionId("1574576830").build();

Map<String, String> params = buildParams(0, "innfinn");
stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("gradualRolloutSessionId", params))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(
new ActivationStrategy("gradualRolloutSessionId", params))));
boolean actual = engine.isEnabled("test", context);

assertFalse(actual, "should not be enabled when 0% rollout");
@@ -139,15 +142,15 @@ public void should_be_enabled_above_minimum_percentage() {

UnleashContext context = UnleashContext.builder().sessionId(sessionId).build();


for (int p = minimumPercentage; p <= 100; p++) {
Map<String, String> params = buildParams(p, groupId);
// Ok, we're going to stress the setting the state
stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("gradualRolloutSessionId", params))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(
new ActivationStrategy("gradualRolloutSessionId", params))));
boolean actual = engine.isEnabled("test", context);
assertTrue(actual, "should be enabled when " + p + "% rollout");
}
@@ -181,13 +184,13 @@ protected int checkRandomLoginIDs(int numberOfIDs, int percentage) {
UnleashContext context =
UnleashContext.builder().sessionId(sessionId.toString()).build();


Map<String, String> params = buildParams(percentage, "");
stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("gradualRolloutSessionId", params))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(
new ActivationStrategy("gradualRolloutSessionId", params))));
boolean enabled = engine.isEnabled("test", context);
if (enabled) {
numberOfEnabledUsers++;
Original file line number Diff line number Diff line change
@@ -4,19 +4,16 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import com.google.common.collect.ImmutableList;
import io.getunleash.*;

import io.getunleash.repository.UnleashEngineStateHandler;
import io.getunleash.util.UnleashConfig;
import io.getunleash.util.UnleashScheduledExecutor;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import io.getunleash.repository.UnleashEngineStateHandler;
import io.getunleash.util.UnleashConfig;
import io.getunleash.util.UnleashScheduledExecutor;
import org.assertj.core.data.Offset;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
@@ -36,50 +33,51 @@ public class GradualRolloutUserIdStrategyTest {
@BeforeEach
public void init() {
percentages =
ImmutableList.<Integer>builder()
.add(1)
.add(2)
.add(5)
.add(10)
.add(25)
.add(50)
.add(90)
.add(99)
.add(100)
.build();
ImmutableList.<Integer>builder()
.add(1)
.add(2)
.add(5)
.add(10)
.add(25)
.add(50)
.add(90)
.add(99)
.add(100)
.build();

UnleashConfig config =
new UnleashConfig.Builder()
.appName("test")
.unleashAPI("http://localhost:4242/api/")
.environment("test")
.scheduledExecutor(mock(UnleashScheduledExecutor.class))
.build();

new UnleashConfig.Builder()
.appName("test")
.unleashAPI("http://localhost:4242/api/")
.environment("test")
.scheduledExecutor(mock(UnleashScheduledExecutor.class))
.build();

engine = new DefaultUnleash(config);
stateHandler = new UnleashEngineStateHandler(engine);
}

@Test
public void should_require_context() {
stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("gradualRolloutUserId", new HashMap<>()))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(
new ActivationStrategy("gradualRolloutUserId", new HashMap<>()))));
assertThat(engine.isEnabled("test")).isFalse();
}

@Test
public void should_be_disabled_when_missing_user_id() {
UnleashContext context = UnleashContext.builder().build();

stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("gradualRolloutUserId", new HashMap<>()))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(
new ActivationStrategy("gradualRolloutUserId", new HashMap<>()))));
assertThat(engine.isEnabled("test", context)).isFalse();
}

@@ -88,11 +86,11 @@ public void should_have_same_result_for_multiple_executions() {
UnleashContext context = UnleashContext.builder().userId("1574576830").build();

Map<String, String> params = buildParams(1, "innfinn");
stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("gradualRolloutUserId", params))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("gradualRolloutUserId", params))));
boolean firstRunResult = engine.isEnabled("test", context);

for (int i = 0; i < 10; i++) {
@@ -106,11 +104,11 @@ public void should_be_enabled_when_using_100percent_rollout() {
UnleashContext context = UnleashContext.builder().userId("1574576830").build();

Map<String, String> params = buildParams(100, "innfinn");
stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("gradualRolloutUserId", params))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("gradualRolloutUserId", params))));
boolean result = engine.isEnabled("test", context);

assertThat(result).isTrue();
@@ -121,11 +119,11 @@ public void should_not_be_enabled_when_0percent_rollout() {
UnleashContext context = UnleashContext.builder().userId("1574576830").build();

Map<String, String> params = buildParams(0, "innfinn");
stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("gradualRolloutUserId", params))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("gradualRolloutUserId", params))));
boolean actual = engine.isEnabled("test", context);
assertFalse(actual, "should not be enabled when 0% rollout");
}
@@ -139,11 +137,12 @@ public void should_be_enabled_above_minimum_percentage() {
UnleashContext context = UnleashContext.builder().userId(userId).build();
for (int p = minimumPercentage; p <= 100; p++) {
Map<String, String> params = buildParams(p, groupId);
stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("gradualRolloutUserId", params))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(
new ActivationStrategy("gradualRolloutUserId", params))));
boolean actual = engine.isEnabled("test", context);
assertTrue(actual, "should be enabled when " + p + "% rollout");
}
@@ -158,11 +157,11 @@ public void should_at_most_miss_with_one_percent_when_rolling_out_to_specified_p

Map<String, String> params = buildParams(percentage, groupId);

stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("gradualRolloutUserId", params))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("gradualRolloutUserId", params))));

for (int userId = 0; userId < rounds; userId++) {
UnleashContext context = UnleashContext.builder().userId("user" + userId).build();
@@ -204,13 +203,13 @@ protected int checkRandomLoginIDs(int numberOfIDs, int percentage) {
Long userId = getRandomLoginId();
UnleashContext context = UnleashContext.builder().userId(userId.toString()).build();


Map<String, String> params = buildParams(percentage, "");
stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("gradualRolloutUserId", params))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(
new ActivationStrategy("gradualRolloutUserId", params))));
boolean enabled = engine.isEnabled("test", context);
if (enabled) {
numberOfEnabledUsers++;
45 changes: 23 additions & 22 deletions src/test/java/io/getunleash/strategy/RemoteAddressStrategyTest.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package io.getunleash.strategy;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import com.google.common.collect.ImmutableList;
import io.getunleash.*;
import io.getunleash.repository.UnleashEngineStateHandler;
import io.getunleash.util.UnleashConfig;
import io.getunleash.util.UnleashScheduledExecutor;
import java.util.*;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.*;
import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class RemoteAddressStrategyTest {
private static final String FIRST_IPV4 = "127.0.0.1";
private static final String SECOND_IPV4 = "10.0.0.1";
@@ -74,27 +73,29 @@ static Stream<Arguments> data() {
@MethodSource("data")
void test_all_combinations(String actualIp, String parameterString, boolean expected) {
UnleashContextProvider contextProvider = mock(UnleashContextProvider.class);
when(contextProvider.getContext()).thenReturn(UnleashContext.builder().remoteAddress(actualIp).build());
when(contextProvider.getContext())
.thenReturn(UnleashContext.builder().remoteAddress(actualIp).build());

UnleashConfig config =
new UnleashConfig.Builder()
.appName("test")
.unleashAPI("http://localhost:4242/api/")
.environment("test")
.scheduledExecutor(mock(UnleashScheduledExecutor.class))
.unleashContextProvider(contextProvider)
.build();

new UnleashConfig.Builder()
.appName("test")
.unleashAPI("http://localhost:4242/api/")
.environment("test")
.scheduledExecutor(mock(UnleashScheduledExecutor.class))
.unleashContextProvider(contextProvider)
.build();

Map<String, String> parameters = setupParameterMap(parameterString);

DefaultUnleash engine = new DefaultUnleash(config);
new UnleashEngineStateHandler(engine).setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("remoteAddress", parameters)),
Collections.emptyList()
));
new UnleashEngineStateHandler(engine)
.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(
new ActivationStrategy("remoteAddress", parameters)),
Collections.emptyList()));

assertThat(engine.isEnabled("test")).isEqualTo(expected);
}
107 changes: 59 additions & 48 deletions src/test/java/io/getunleash/strategy/StrategyTest.java
Original file line number Diff line number Diff line change
@@ -6,16 +6,14 @@

import com.google.common.collect.ImmutableList;
import io.getunleash.*;

import io.getunleash.repository.UnleashEngineStateHandler;
import io.getunleash.util.UnleashConfig;
import io.getunleash.util.UnleashScheduledExecutor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.getunleash.repository.UnleashEngineStateHandler;
import io.getunleash.util.UnleashConfig;
import io.getunleash.util.UnleashScheduledExecutor;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

@@ -42,14 +40,13 @@ void init() {
when(contextProvider.getContext()).thenReturn(UnleashContext.builder().build());

UnleashConfig config =
new UnleashConfig.Builder()
.appName("test")
.unleashAPI("http://localhost:4242/api/")
.environment("test")
.scheduledExecutor(mock(UnleashScheduledExecutor.class))
.unleashContextProvider(contextProvider)
.build();

new UnleashConfig.Builder()
.appName("test")
.unleashAPI("http://localhost:4242/api/")
.environment("test")
.scheduledExecutor(mock(UnleashScheduledExecutor.class))
.unleashContextProvider(contextProvider)
.build();

engine = new DefaultUnleash(config, new AlwaysEnabled());
stateHandler = new UnleashEngineStateHandler(engine);
@@ -60,11 +57,13 @@ public void should_be_enabled_for_empty_constraints() {
Map parameters = new HashMap<String, String>();
UnleashContext context = UnleashContext.builder().build();
List<Constraint> constraints = new ArrayList<>();
stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("enabled", parameters, constraints, null, null))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(
new ActivationStrategy(
"enabled", parameters, constraints, null, null))));

boolean result = engine.isEnabled("test", context);
assertTrue(result);
@@ -76,11 +75,13 @@ public void should_be_enabled_for_null_constraints() {
UnleashContext context = UnleashContext.builder().build();
List<Constraint> constraints = null;

stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("enabled", parameters, constraints, null, null))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(
new ActivationStrategy(
"enabled", parameters, constraints, null, null))));

boolean result = engine.isEnabled("test", context);
assertTrue(result);
@@ -93,11 +94,13 @@ public void should_be_disabled_when_constraint_not_satisfied() {
List<Constraint> constraints = new ArrayList<>();
constraints.add(new Constraint("environment", Operator.IN, Arrays.asList("prod")));

stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("enabled", parameters, constraints, null, null))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(
new ActivationStrategy(
"enabled", parameters, constraints, null, null))));

boolean result = engine.isEnabled("test", context);
assertFalse(result);
@@ -110,11 +113,13 @@ public void should_be_enabled_when_constraint_is_satisfied() {
List<Constraint> constraints = new ArrayList<>();
constraints.add(new Constraint("environment", Operator.IN, Arrays.asList("test", "prod")));

stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("enabled", parameters, constraints, null, null))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(
new ActivationStrategy(
"enabled", parameters, constraints, null, null))));

boolean result = engine.isEnabled("test", context);
assertTrue(result);
@@ -127,11 +132,13 @@ public void should_be_enabled_when_constraint_NOT_IN_satisfied() {
List<Constraint> constraints = new ArrayList<>();
constraints.add(new Constraint("environment", Operator.NOT_IN, Arrays.asList("prod")));

stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("enabled", parameters, constraints, null, null))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(
new ActivationStrategy(
"enabled", parameters, constraints, null, null))));

boolean result = engine.isEnabled("test", context);
assertTrue(result);
@@ -151,11 +158,13 @@ public void should_be_enabled_when_all_constraints_are_satisfied() {
constraints.add(new Constraint("userId", Operator.IN, Arrays.asList("123")));
constraints.add(new Constraint("customerId", Operator.IN, Arrays.asList("red", "blue")));

stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("enabled", parameters, constraints, null, null))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(
new ActivationStrategy(
"enabled", parameters, constraints, null, null))));

boolean result = engine.isEnabled("test", context);
assertTrue(result);
@@ -175,11 +184,13 @@ public void should_be_disabled_when_not_all_constraints_are_satisfied() {
constraints.add(new Constraint("userId", Operator.IN, Arrays.asList("123")));
constraints.add(new Constraint("customerId", Operator.IN, Arrays.asList("red", "blue")));

stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("enabled", parameters, constraints, null, null))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(
new ActivationStrategy(
"enabled", parameters, constraints, null, null))));

boolean result = engine.isEnabled("test", context);
assertFalse(result);
4 changes: 2 additions & 2 deletions src/test/java/io/getunleash/strategy/StrategyUtilsTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package io.getunleash.strategy;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

public class StrategyUtilsTest {

@Test
116 changes: 57 additions & 59 deletions src/test/java/io/getunleash/strategy/UserWithIdStrategyTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package io.getunleash.strategy;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;

import com.google.common.collect.ImmutableList;
import io.getunleash.ActivationStrategy;
import io.getunleash.DefaultUnleash;
@@ -8,15 +12,10 @@
import io.getunleash.repository.UnleashEngineStateHandler;
import io.getunleash.util.UnleashConfig;
import io.getunleash.util.UnleashScheduledExecutor;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class UserWithIdStrategyTest {
private DefaultUnleash engine;
@@ -25,13 +24,12 @@ public class UserWithIdStrategyTest {
@BeforeEach
void setup() {
UnleashConfig config =
new UnleashConfig.Builder()
.appName("test")
.unleashAPI("http://localhost:4242/api/")
.environment("test")
.scheduledExecutor(mock(UnleashScheduledExecutor.class))
.build();

new UnleashConfig.Builder()
.appName("test")
.unleashAPI("http://localhost:4242/api/")
.environment("test")
.scheduledExecutor(mock(UnleashScheduledExecutor.class))
.build();

engine = new DefaultUnleash(config);
stateHandler = new UnleashEngineStateHandler(engine);
@@ -44,11 +42,11 @@ public void should_match_one_userId() {
UnleashContext context = UnleashContext.builder().userId("123").build();
parameters.put("userIds", "123");

stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("userWithId", parameters))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("userWithId", parameters))));
assertTrue(engine.isEnabled("test", context));
}

@@ -58,11 +56,11 @@ public void should_match_first_userId_in_list() {

UnleashContext context = UnleashContext.builder().userId("123").build();
parameters.put("userIds", "123, 122, 121");
stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("userWithId", parameters))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("userWithId", parameters))));
assertTrue(engine.isEnabled("test", context));
}

@@ -72,11 +70,11 @@ public void should_match_middle_userId_in_list() {

UnleashContext context = UnleashContext.builder().userId("123").build();
parameters.put("userIds", "123, 122, 121");
stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("userWithId", parameters))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("userWithId", parameters))));
assertTrue(engine.isEnabled("test", context));
}

@@ -86,11 +84,11 @@ public void should_match_last_userId_in_list() {

UnleashContext context = UnleashContext.builder().userId("123").build();
parameters.put("userIds", "123, 122, 121");
stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("userWithId", parameters))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("userWithId", parameters))));
assertTrue(engine.isEnabled("test", context));
}

@@ -100,11 +98,11 @@ public void should_not_match_subparts_of_ids() {

UnleashContext context = UnleashContext.builder().userId("12").build();
parameters.put("userIds", "123, 122, 121, 212");
stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("userWithId", parameters))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("userWithId", parameters))));
assertFalse(engine.isEnabled("test", context));
}

@@ -114,11 +112,11 @@ public void should_not_match_csv_without_space() {

UnleashContext context = UnleashContext.builder().userId("123").build();
parameters.put("userIds", "123,122,121");
stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("userWithId", parameters))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("userWithId", parameters))));
assertTrue(engine.isEnabled("test", context));
}

@@ -130,11 +128,11 @@ public void should_match_real_ids() {
parameters.put(
"userIds",
"160118738, 1823311338, 1422637466, 2125981185, 298261117, 1829486714, 463568019, 271166598");
stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("userWithId", parameters))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("userWithId", parameters))));
assertTrue(engine.isEnabled("test", context));
}

@@ -146,11 +144,11 @@ public void should_not_match_real_ids() {
parameters.put(
"userIds",
"160118738, 1823311338, 1422637466, 2125981185, 298261117, 1829486714, 463568019, 271166598");
stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("userWithId", parameters))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("userWithId", parameters))));
assertFalse(engine.isEnabled("test", context));
}

@@ -159,11 +157,11 @@ public void should_not_be_enabled_without_id() {
Map<String, String> parameters = new HashMap<>();

parameters.put("userIds", "160118738, 1823311338");
stateHandler.setState(new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("userWithId", parameters))
));
stateHandler.setState(
new FeatureToggle(
"test",
true,
ImmutableList.of(new ActivationStrategy("userWithId", parameters))));
assertFalse(engine.isEnabled("test"));
}
}

0 comments on commit 71ed14f

Please sign in to comment.