io.getunleash.FakeUnleash Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of unleash-client-java Show documentation
Show all versions of unleash-client-java Show documentation
A client library for Unleash
package io.getunleash;
import static java.util.Collections.emptyList;
import io.getunleash.lang.Nullable;
import java.util.*;
import java.util.function.BiPredicate;
import java.util.stream.Collectors;
public class FakeUnleash implements Unleash {
private boolean enableAll = false;
private boolean disableAll = false;
private Map excludedFeatures = new HashMap<>();
private Map features = new HashMap<>();
private Map variants = new HashMap<>();
@Override
public boolean isEnabled(String toggleName, boolean defaultSetting) {
if (enableAll) {
return excludedFeatures.getOrDefault(toggleName, true);
} else if (disableAll) {
return excludedFeatures.getOrDefault(toggleName, false);
} else {
return more().getFeatureToggleDefinition(toggleName)
.map(FeatureToggle::isEnabled)
.orElse(defaultSetting);
}
}
@Override
public boolean isEnabled(
String toggleName,
UnleashContext context,
BiPredicate fallbackAction) {
return isEnabled(toggleName, fallbackAction);
}
@Override
public boolean isEnabled(
String toggleName, BiPredicate fallbackAction) {
if (!features.containsKey(toggleName)) {
return fallbackAction.test(toggleName, UnleashContext.builder().build());
}
return isEnabled(toggleName);
}
@Override
public Variant getVariant(String toggleName, UnleashContext context) {
return getVariant(toggleName, Variant.DISABLED_VARIANT);
}
@Override
public Variant getVariant(String toggleName, UnleashContext context, Variant defaultValue) {
return getVariant(toggleName, defaultValue);
}
@Override
public Variant getVariant(String toggleName) {
return getVariant(toggleName, Variant.DISABLED_VARIANT);
}
@Override
public Variant getVariant(String toggleName, Variant defaultValue) {
if (isEnabled(toggleName) && variants.containsKey(toggleName)) {
return variants.get(toggleName);
} else {
return defaultValue;
}
}
@Override
public List getFeatureToggleNames() {
return more().getFeatureToggleNames();
}
@Override
public MoreOperations more() {
return new FakeMore();
}
public void enableAll() {
disableAll = false;
enableAll = true;
excludedFeatures.clear();
features.clear();
}
public void enableAllExcept(String... excludedFeatures) {
enableAll();
for (String toggle : excludedFeatures) {
this.excludedFeatures.put(toggle, false);
}
}
public void disableAll() {
disableAll = true;
enableAll = false;
excludedFeatures.clear();
features.clear();
}
public void disableAllExcept(String... excludedFeatures) {
disableAll();
for (String toggle : excludedFeatures) {
this.excludedFeatures.put(toggle, true);
}
}
@Override
public Variant deprecatedGetVariant(String toggleName, UnleashContext context) {
return null;
}
@Override
public Variant deprecatedGetVariant(
String toggleName, UnleashContext context, Variant defaultValue) {
return null;
}
public void resetAll() {
disableAll = false;
enableAll = false;
excludedFeatures.clear();
features.clear();
variants.clear();
}
public void enable(String... features) {
for (String name : features) {
this.features.put(name, new FeatureToggle(name, true, emptyList()));
}
}
public void disable(String... features) {
for (String name : features) {
this.features.put(name, new FeatureToggle(name, false, emptyList()));
}
}
public void reset(String... features) {
for (String name : features) {
this.features.remove(name);
}
}
public void setVariant(String t1, Variant a) {
variants.put(t1, a);
}
public class FakeMore implements MoreOperations {
@Override
public List getFeatureToggleNames() {
return new ArrayList<>(features.keySet());
}
@Override
public Optional getFeatureToggleDefinition(String toggleName) {
return Optional.ofNullable(features.get(toggleName));
}
@Override
public List evaluateAllToggles() {
return evaluateAllToggles(null);
}
@Override
public List evaluateAllToggles(@Nullable UnleashContext context) {
return getFeatureToggleNames().stream()
.map(
toggleName ->
new EvaluatedToggle(
toggleName,
isEnabled(toggleName),
getVariant(toggleName)))
.collect(Collectors.toList());
}
@Override
public void count(String toggleName, boolean enabled) {
// Nothing to count
}
@Override
public void countVariant(String toggleName, String variantName) {
// Nothing to count
}
}
}