Skip to content

Commit

Permalink
More cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
gastonfournier committed Nov 16, 2023
1 parent 162d408 commit 128b5fe
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package io.getunleash.repository;

import io.getunleash.UnleashException;
import io.getunleash.engine.YggdrasilInvalidInputException;
import io.getunleash.util.UnleashConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -11,8 +13,6 @@
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HttpFeatureFetcher implements FeatureFetcher {
private static final Logger LOG = LoggerFactory.getLogger(HttpFeatureFetcher.class);
Expand Down
21 changes: 12 additions & 9 deletions src/main/java/io/getunleash/util/UnleashConfig.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.getunleash.util;

import static io.getunleash.DefaultUnleash.UNKNOWN_STRATEGY;

import io.getunleash.CustomHttpHeadersProvider;
import io.getunleash.DefaultCustomHttpHeadersProviderImpl;
import io.getunleash.UnleashContextProvider;
Expand All @@ -10,19 +12,21 @@
import io.getunleash.repository.HttpFeatureFetcher;
import io.getunleash.repository.ToggleBootstrapProvider;
import io.getunleash.strategy.Strategy;

import java.io.File;
import java.math.BigInteger;
import java.net.*;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.net.URI;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import static io.getunleash.DefaultUnleash.UNKNOWN_STRATEGY;
import java.util.*;

public class UnleashConfig {

Expand Down Expand Up @@ -97,8 +101,7 @@ private UnleashConfig(
@Nullable Strategy fallbackStrategy,
@Nullable ToggleBootstrapProvider unleashBootstrapProvider,
@Nullable Proxy proxy,
@Nullable Authenticator proxyAuthenticator
) {
@Nullable Authenticator proxyAuthenticator) {

if (appName == null) {
throw new IllegalStateException("You are required to specify the unleash appName");
Expand Down
33 changes: 17 additions & 16 deletions src/test/java/io/getunleash/DefaultUnleashTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,10 @@ public void should_evaluate_missing_segment_as_false() {
asList(semverConstraint),
asList(404),
Collections.emptyList());
when(featureRepository.getToggle(toggleName))
.thenReturn(new FeatureToggle(toggleName, true, asList(withMissingSegment)));
when(featureRepository.getSegment(404)).thenReturn(Segment.DENY_SEGMENT);
new UnleashEngineStateHandler(sut).setState(
Collections.singletonList(new FeatureToggle(toggleName, true, asList(withMissingSegment))),
Collections.singletonList(Segment.DENY_SEGMENT));

when(contextProvider.getContext())
.thenReturn(UnleashContext.builder().addProperty("version", semVer).build());
assertThat(sut.isEnabled(toggleName)).isFalse();
Expand All @@ -106,18 +107,18 @@ public void should_evaluate_segment_collection_with_one_missing_segment_as_false
asList(semverConstraint),
asList(404, 1),
Collections.emptyList());
when(featureRepository.getToggle(toggleName))
.thenReturn(new FeatureToggle(toggleName, true, asList(withMissingSegment)));
when(featureRepository.getSegment(1))
.thenReturn(
new Segment(
1,
"always true",
asList(
new Constraint(
"always_true",
Operator.NOT_IN,
Collections.EMPTY_LIST))));
new UnleashEngineStateHandler(sut).setState(
Collections.singletonList(new FeatureToggle(toggleName, true, asList(withMissingSegment))),
Collections.singletonList(new Segment(
1,
"always true",
asList(
new Constraint(
"always_true",
Operator.NOT_IN,
Collections.EMPTY_LIST)))));


when(contextProvider.getContext())
.thenReturn(UnleashContext.builder().addProperty("version", "1.2.2").build());
assertThat(sut.isEnabled(toggleName)).isFalse();
Expand Down Expand Up @@ -145,8 +146,8 @@ public void should_allow_fallback_strategy() {

ActivationStrategy as = new ActivationStrategy("forFallback", new HashMap<>());
FeatureToggle toggle = new FeatureToggle("toggle1", true, Collections.singletonList(as));
when(contextProvider.getContext()).thenReturn(UnleashContext.builder().build());
new UnleashEngineStateHandler(sut).setState(toggle);
when(contextProvider.getContext()).thenReturn(UnleashContext.builder().build());

sut.isEnabled("toggle1");

Expand Down
3 changes: 1 addition & 2 deletions src/test/java/io/getunleash/DependentFeatureToggleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,7 @@ public void should_trigger_impression_event_for_parent_variant_when_checking_chi
singletonList(
new FeatureDependency(
parentName, null, singletonList("disabled"))));
when(featureRepository.getToggle(childName)).thenReturn(child);
when(featureRepository.getToggle(parentName)).thenReturn(parent);
new UnleashEngineStateHandler(sut).setState(child, parent);
assertThat(sut.isEnabled(childName, UnleashContext.builder().build())).isFalse();
}
}
2 changes: 0 additions & 2 deletions src/test/java/io/getunleash/UnleashTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ public void should_support_multiple_strategies() {
FeatureToggle featureToggle =
new FeatureToggle("test", true, asList(strategy1, activeStrategy));

when(toggleRepository.getToggle("test")).thenReturn(featureToggle);
stateHandler.setState(featureToggle);

assertThat(unleash.isEnabled("test")).isTrue();
Expand Down Expand Up @@ -490,7 +489,6 @@ public void should_be_enabled_with_strategy_constraints() {

FeatureToggle featureToggle = new FeatureToggle("test", true, asList(activeStrategy));

when(toggleRepository.getToggle("test")).thenReturn(featureToggle);
stateHandler.setState(featureToggle);

assertThat(unleash.isEnabled("test")).isTrue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private Unleash setupUnleash(TestDefinition testDefinition) throws URISyntaxExce
UnleashConfig.builder()
.appName(testDefinition.getName())
.unleashAPI(new URI("http://localhost:" + serverMock.getPort() + "/api/"))
.synchronousFetchOnInitialisation(false)
.synchronousFetchOnInitialisation(true)
.backupFile(backupFile)
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import io.getunleash.DefaultUnleash;
import io.getunleash.FeatureToggle;
import io.getunleash.Segment;
import io.getunleash.engine.UnleashEngine;
import io.getunleash.engine.YggdrasilInvalidInputException;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class UnleashEngineStateHandler {
private final UnleashEngine unleashEngine;
Expand All @@ -33,6 +35,14 @@ public void setState(FeatureToggle ...featureToggles) {
setState(madeUp);
}

public void setState(List<FeatureToggle> featureToggles, List<Segment> segments) {
FeatureCollection madeUp = new FeatureCollection(
new ToggleCollection(featureToggles),
new SegmentCollection(segments)
);
setState(madeUp);
}

public void setState(FeatureCollection madeUp) {
setState(JsonFeatureParser.toJsonString(madeUp));
}
Expand Down

0 comments on commit 128b5fe

Please sign in to comment.