Skip to content

Commit

Permalink
feat: expose impression data property (#201)
Browse files Browse the repository at this point in the history
Co-authored-by: Christopher Kolstad <[email protected]>
  • Loading branch information
sighphyre and Christopher Kolstad authored Jun 14, 2023
1 parent 015340f commit 20f889f
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 36 deletions.
13 changes: 6 additions & 7 deletions src/main/java/io/getunleash/FakeUnleash.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static java.util.Collections.emptyList;

import io.getunleash.lang.Nullable;

import java.util.*;
import java.util.function.BiPredicate;
import java.util.stream.Collectors;
Expand All @@ -23,22 +22,22 @@ public boolean isEnabled(String toggleName, boolean defaultSetting) {
return excludedFeatures.getOrDefault(toggleName, false);
} else {
return more().getFeatureToggleDefinition(toggleName)
.map(FeatureToggle::isEnabled)
.orElse(defaultSetting);
.map(FeatureToggle::isEnabled)
.orElse(defaultSetting);
}
}

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

@Override
public boolean isEnabled(
String toggleName, BiPredicate<String, UnleashContext> fallbackAction) {
String toggleName, BiPredicate<String, UnleashContext> fallbackAction) {
if (!features.containsKey(toggleName)) {
return fallbackAction.test(toggleName, UnleashContext.builder().build());
}
Expand Down
22 changes: 20 additions & 2 deletions src/main/java/io/getunleash/FeatureToggle.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,31 @@ public final class FeatureToggle {
private final boolean enabled;
private final List<ActivationStrategy> strategies;
@Nullable private final List<VariantDefinition> variants;
private final boolean impressionData;

public FeatureToggle(String name, boolean enabled, List<ActivationStrategy> strategies) {
this(name, enabled, strategies, emptyList());
this(name, enabled, strategies, emptyList(), false);
}

public FeatureToggle(
String name,
boolean enabled,
List<ActivationStrategy> strategies,
@Nullable List<VariantDefinition> variants) {
List<VariantDefinition> variants) {
this(name, enabled, strategies, variants, false);
}

public FeatureToggle(
String name,
boolean enabled,
List<ActivationStrategy> strategies,
@Nullable List<VariantDefinition> variants,
@Nullable Boolean impressionData) {
this.name = name;
this.enabled = enabled;
this.strategies = strategies;
this.variants = variants;
this.impressionData = impressionData != null ? impressionData : false;
}

public String getName() {
Expand All @@ -48,6 +59,11 @@ public List<VariantDefinition> getVariants() {
}
}

@Nullable
public boolean hasImpressionData() {
return impressionData;
}

@Override
public String toString() {
return "FeatureToggle{"
Expand All @@ -61,6 +77,8 @@ public String toString() {
+ '\''
+ ", variants='"
+ variants
+ ", impressionData="
+ impressionData
+ '\''
+ '}';
}
Expand Down
43 changes: 19 additions & 24 deletions src/test/java/io/getunleash/DefaultUnleashTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,17 @@
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.read.ListAppender;
import io.getunleash.event.EventDispatcher;
import io.getunleash.metric.MetricSender;
import io.getunleash.metric.UnleashMetricService;
import io.getunleash.repository.*;
import io.getunleash.strategy.DefaultStrategy;
import io.getunleash.strategy.Strategy;
import io.getunleash.util.MetricSenderFactory;
import io.getunleash.util.UnleashConfig;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.getunleash.util.UnleashFeatureFetcherFactory;
import io.getunleash.util.UnleashScheduledExecutor;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.slf4j.LoggerFactory;

class DefaultUnleashTest {
Expand Down Expand Up @@ -220,35 +214,36 @@ public void supports_failing_hard_on_multiple_instantiations() {
@Test
public void synchronous_fetch_on_initialisation_fails_on_initialization() {
UnleashConfig config =
UnleashConfig.builder()
.unleashAPI("http://wrong:4242")
.appName("wrong_upstream")
.apiKey("default:development:1234567890123456")
.instanceId("multiple_connection_exception")
.synchronousFetchOnInitialisation(true)
.build();
UnleashConfig.builder()
.unleashAPI("http://wrong:4242")
.appName("wrong_upstream")
.apiKey("default:development:1234567890123456")
.instanceId("multiple_connection_exception")
.synchronousFetchOnInitialisation(true)
.build();

assertThatThrownBy(() -> new DefaultUnleash(config)).isInstanceOf(UnleashException.class);
}

@Test
public void asynchronous_fetch_on_initialisation_fails_silently_and_retries() throws InterruptedException {
public void asynchronous_fetch_on_initialisation_fails_silently_and_retries()
throws InterruptedException {
FeatureFetcher fetcher = mock(FeatureFetcher.class);
FeatureCollection expectedResponse = new FeatureCollection();
FeatureToggleResponse.Status expectedStatus = FeatureToggleResponse.Status.CHANGED;
when(fetcher.fetchFeatures())
.thenThrow(UnleashException.class)
.thenReturn(new ClientFeaturesResponse(expectedStatus, expectedResponse));
.thenThrow(UnleashException.class)
.thenReturn(new ClientFeaturesResponse(expectedStatus, expectedResponse));

UnleashConfig config =
UnleashConfig.builder()
.unleashAPI("http://wrong:4242")
.appName("wrong_upstream")
.apiKey("default:development:1234567890123456")
.instanceId("multiple_connection_exception")
.fetchTogglesInterval(1)
.unleashFeatureFetcherFactory((UnleashConfig c) -> fetcher)
.build();
UnleashConfig.builder()
.unleashAPI("http://wrong:4242")
.appName("wrong_upstream")
.apiKey("default:development:1234567890123456")
.instanceId("multiple_connection_exception")
.fetchTogglesInterval(1)
.unleashFeatureFetcherFactory((UnleashConfig c) -> fetcher)
.build();

Unleash unleash = new DefaultUnleash(config);
verify(fetcher, times(1)).fetchFeatures();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,12 @@ public void should_perform_synchronous_fetch_on_initialisation_and_fail_if_inval
// setup fetcher
when(toggleFetcher.fetchToggles()).thenThrow(RuntimeException.class);

assertThatThrownBy(() -> {
new FeatureToggleRepository(config, executor, toggleFetcher, toggleBackupHandler);
}).isInstanceOf(RuntimeException.class);
assertThatThrownBy(
() -> {
new FeatureToggleRepository(
config, executor, toggleFetcher, toggleBackupHandler);
})
.isInstanceOf(RuntimeException.class);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,21 @@ public void should_deserialize_list_of_toggles_with_variants() throws IOExceptio
.isNull();
}

@Test
public void should_deserialize_impression_data() throws IOException {
Reader content = getFileReader("/unleash-repo-v2-with-impression-data.json");
FeatureCollection featureCollection = JsonFeatureParser.fromJson(content);
ToggleCollection toggleCollection = featureCollection.getToggleCollection();

assertThat(toggleCollection.getFeatures()).hasSize(3);
assertThat(toggleCollection.getToggle("Test.impressionDataPresent").hasImpressionData())
.isTrue();
assertThat(toggleCollection.getToggle("Test.impressionDataSetToOff").hasImpressionData())
.isFalse();
assertThat(toggleCollection.getToggle("Test.impressionDataMissing").hasImpressionData())
.isFalse();
}

private Reader getFileReader(String filename) throws IOException {
InputStream in = this.getClass().getResourceAsStream(filename);
InputStreamReader reader = new InputStreamReader(in);
Expand Down
43 changes: 43 additions & 0 deletions src/test/resources/unleash-repo-v2-with-impression-data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"version": 2,
"features": [
{
"name": "Test.impressionDataPresent",
"description": "It's true, we do have impression data",
"enabled": true,
"strategies": [
{
"name": "default"
}
],
"impressionData": true,
"variants": null,
"createdAt": "2019-01-24T10:38:10.370Z"
},
{
"name": "Test.impressionDataSetToOff",
"description": "Explicitly no impression data",
"enabled": true,
"strategies": [
{
"name": "default"
}
],
"impressionData": false,
"variants": null,
"createdAt": "2019-01-24T10:38:10.370Z"
},
{
"name": "Test.impressionDataMissing",
"description": "No field defined",
"enabled": true,
"strategies": [
{
"name": "default"
}
],
"variants": null,
"createdAt": "2019-01-24T10:38:10.370Z"
}
]
}

0 comments on commit 20f889f

Please sign in to comment.