Skip to content

Commit

Permalink
fix: fallbackAction and defaultSetting
Browse files Browse the repository at this point in the history
Should not be possible to define both a fallback action and
a defaultSetting at the same time.
  • Loading branch information
ivaosthu committed Nov 16, 2019
1 parent b731dba commit e96a173
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 44 deletions.
24 changes: 6 additions & 18 deletions src/main/java/no/finn/unleash/DefaultUnleash.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,33 +85,21 @@ public boolean isEnabled(final String toggleName, final boolean defaultSetting)

@Override
public boolean isEnabled(final String toggleName, final UnleashContext context, final boolean defaultSetting) {
return isEnabled(toggleName, context, defaultSetting, (name, unleashContext) -> defaultSetting);
return isEnabled(toggleName, context, (n, c) -> defaultSetting);
}

@Override
public boolean isEnabled(final String toggleName, final BiFunction<String, UnleashContext, Boolean> fallbackAction) {
return isEnabled(toggleName, false, fallbackAction);
}

public boolean isEnabled(final String toggleName, boolean defaultSetting, final BiFunction<String, UnleashContext, Boolean> fallbackAction) {
return isEnabled(toggleName, contextProvider.getContext(), defaultSetting, fallbackAction);
return isEnabled(toggleName, contextProvider.getContext(), fallbackAction);
}

public boolean isEnabled(final String toggleName, UnleashContext context, boolean defaultSetting, final BiFunction<String, UnleashContext, Boolean> fallbackAction) {
public boolean isEnabled(String toggleName, UnleashContext context, BiFunction<String, UnleashContext, Boolean> fallbackAction) {
FeatureToggle featureToggle = toggleRepository.getToggle(toggleName);

return isEnabled(toggleName, featureToggle, context, defaultSetting, fallbackAction);
}

private boolean isEnabled(String toggleName, FeatureToggle featureToggle, UnleashContext context, boolean defaultSetting, BiFunction<String, UnleashContext, Boolean> fallbackAction) {
boolean enabled;
UnleashContext enhancedContext = context.applyStaticFields(config);

if (featureToggle == null) {
enabled = defaultSetting;

if(fallbackAction != null) {
enabled = fallbackAction.apply(toggleName, enhancedContext);
}
enabled = fallbackAction.apply(toggleName, enhancedContext);
} else if(!featureToggle.isEnabled()) {
enabled = false;
} else if(featureToggle.getStrategies().size() == 0) {
Expand All @@ -133,7 +121,7 @@ public Variant getVariant(String toggleName, UnleashContext context) {
@Override
public Variant getVariant(String toggleName, UnleashContext context, Variant defaultValue) {
FeatureToggle featureToggle = toggleRepository.getToggle(toggleName);
boolean enabled = isEnabled(toggleName, context, false);
boolean enabled = isEnabled(toggleName, context, (n, c) -> false);
Variant variant = enabled ? selectVariant(featureToggle, context, defaultValue) : defaultValue;
metricService.countVariant(toggleName, variant.getName());
return variant;
Expand Down
19 changes: 2 additions & 17 deletions src/main/java/no/finn/unleash/FakeUnleash.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,8 @@ public boolean isEnabled(String toggleName, boolean defaultSetting) {
}

@Override
public boolean isEnabled(String toggleName, boolean defaultSetting, BiFunction<String, UnleashContext, Boolean> fallbackAction) {
if(enableAll) {
return true;
} else if(disableAll) {

return false;
} else {
if(!features.containsKey(toggleName)) {
return fallbackAction.apply(toggleName, UnleashContext.builder().build());
}
return features.getOrDefault(toggleName, defaultSetting);
}
}

@Override
public boolean isEnabled(String toggleName, UnleashContext context, boolean defaultSetting, BiFunction<String, UnleashContext, Boolean> fallbackAction) {
return isEnabled(toggleName, defaultSetting, fallbackAction);
public boolean isEnabled(String toggleName, UnleashContext context, BiFunction<String, UnleashContext, Boolean> fallbackAction) {
return isEnabled(toggleName, fallbackAction);
}

@Override
Expand Down
10 changes: 3 additions & 7 deletions src/main/java/no/finn/unleash/Unleash.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,11 @@ default boolean isEnabled(String toggleName, UnleashContext context, boolean def
}

default boolean isEnabled(final String toggleName, final BiFunction<String, UnleashContext, Boolean> fallbackAction) {
return isEnabled(toggleName, false, fallbackAction);
return isEnabled(toggleName, false);
}

default boolean isEnabled(String toggleName, UnleashContext context, boolean defaultSetting, BiFunction<String, UnleashContext, Boolean> fallbackAction) {
return isEnabled(toggleName, defaultSetting, fallbackAction);
}

default boolean isEnabled(final String toggleName, boolean defaultSetting, final BiFunction<String, UnleashContext, Boolean> fallbackAction) {
return isEnabled(toggleName, fallbackAction);
default boolean isEnabled(String toggleName, UnleashContext context, BiFunction<String, UnleashContext, Boolean> fallbackAction) {
return isEnabled(toggleName, context, false);
}

Variant getVariant(final String toggleName, final UnleashContext context);
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/no/finn/unleash/UnleashTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ public void fallback_function_should_be_invoked() {
void fallback_function_should_override_default_fallback_value_when_toggle_not_defined() {
when(toggleRepository.getToggle("test")).thenReturn(null);

assertThat(unleash.isEnabled("test", false, (name, unleashContext) -> true), is(true));
assertThat(unleash.isEnabled("test", (name, unleashContext) -> true), is(true));
}

@Test
void fallback_function_should_not_be_called_when_toggle_is_defined() {
when(toggleRepository.getToggle("test")).thenReturn(new FeatureToggle("test", true, asList(new ActivationStrategy("default", null))));

assertThat(unleash.isEnabled("test", false, (name, unleashContext) -> false), is(true));
assertThat(unleash.isEnabled("test", (name, unleashContext) -> false), is(true));
}

@Test
Expand Down

0 comments on commit e96a173

Please sign in to comment.