diff --git a/client/src/main/java/io/split/client/JsonLocalhostSplitChangeFetcher.java b/client/src/main/java/io/split/client/JsonLocalhostSplitChangeFetcher.java index cf91712d3..e2cb5d5c9 100644 --- a/client/src/main/java/io/split/client/JsonLocalhostSplitChangeFetcher.java +++ b/client/src/main/java/io/split/client/JsonLocalhostSplitChangeFetcher.java @@ -2,7 +2,6 @@ import com.google.gson.stream.JsonReader; import io.split.client.dtos.SplitChange; -import io.split.client.exceptions.InputStreamProviderException; import io.split.client.utils.InputStreamProvider; import io.split.client.utils.Json; import io.split.client.utils.LocalhostSanitizer; @@ -30,13 +29,11 @@ public JsonLocalhostSplitChangeFetcher(InputStreamProvider inputStreamProvider) } @Override - public SplitChange fetch(long since, FetchOptions options) throws InputStreamProviderException { + public SplitChange fetch(long since, FetchOptions options) { try { JsonReader jsonReader = new JsonReader(new BufferedReader(new InputStreamReader(_inputStreamProvider.get(), StandardCharsets.UTF_8))); SplitChange splitChange = Json.fromJson(jsonReader, SplitChange.class); return processSplitChange(splitChange, since); - } catch (InputStreamProviderException i) { - throw i; } catch (Exception e) { throw new IllegalStateException("Problem fetching splitChanges: " + e.getMessage(), e); } diff --git a/client/src/main/java/io/split/client/SplitFactoryImpl.java b/client/src/main/java/io/split/client/SplitFactoryImpl.java index c2533d8d9..f93b970cf 100644 --- a/client/src/main/java/io/split/client/SplitFactoryImpl.java +++ b/client/src/main/java/io/split/client/SplitFactoryImpl.java @@ -1,5 +1,6 @@ package io.split.client; +import com.google.common.io.Files; import io.split.client.dtos.Metadata; import io.split.client.events.EventsSender; import io.split.client.events.EventsStorage; @@ -32,6 +33,7 @@ import io.split.client.interceptors.SdkMetadataInterceptorFilter; import io.split.client.utils.FileInputStreamProvider; import io.split.client.utils.FileTypeEnum; +import io.split.client.utils.InputStreamProvider; import io.split.client.utils.InputStreamProviderImp; import io.split.client.utils.SDKMetadata; import io.split.engine.SDKReadinessGates; @@ -101,6 +103,7 @@ import org.slf4j.LoggerFactory; import pluggable.CustomStorageWrapper; +import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.InetAddress; @@ -645,48 +648,48 @@ private SplitChangeFetcher createSplitChangeFetcher(SplitClientConfig splitClien String splitFile = splitClientConfig.splitFile(); InputStream inputStream = splitClientConfig.inputStream(); FileTypeEnum fileType = splitClientConfig.fileType(); - if (splitFile != null && inputStream != null) { - _log.warn("splitFile or inputStreamProvider should have a value, not both"); - _log.warn(LEGACY_LOG_MESSAGE); - return new LegacyLocalhostSplitChangeFetcher(splitFile); - } - if (inputStream != null && fileType == null) { - _log.warn("If inputStreamProvider is not null, then fileType must also have a non-null value"); - _log.warn(LEGACY_LOG_MESSAGE); - return new LegacyLocalhostSplitChangeFetcher(splitFile); - } - if (inputStream == null && splitFile == null){ - _log.warn("splitFile or inputStreamProvider should have a value"); - _log.warn(LEGACY_LOG_MESSAGE); - return new LegacyLocalhostSplitChangeFetcher(splitFile); - } - if (splitFile != null) { - try { - if (splitFile.toLowerCase().endsWith(".json")) { - return new JsonLocalhostSplitChangeFetcher(new FileInputStreamProvider(splitFile)); - } else if (splitFile.endsWith(".yaml") || splitFile.endsWith(".yml")) { - return new YamlLocalhostSplitChangeFetcher(new FileInputStreamProvider(splitFile)); - } - } catch (Exception e) { - _log.warn(String.format("There was no file named %s found. " + - "We created a split client that returns default treatments for all feature flags for all of your users. " + - "If you wish to return a specific treatment for a feature flag, enter the name of that feature flag name and " + - "treatment name separated by whitespace in %s; one pair per line. Empty lines or lines starting with '#' are " + - "considered comments", - splitFile, splitFile), e); + InputStreamProvider inputStreamProvider; + if (splitFile != null || !isInputStreamConfigValid(inputStream, fileType)) { + if (splitFile == null) { + _log.warn("The InputStream config is invalid"); } + fileType = getFileTypeFromFileName(splitFile); + inputStreamProvider = new FileInputStreamProvider(splitFile); } else { - switch (fileType) { + inputStreamProvider = new InputStreamProviderImp(inputStream); + } + try { + switch (fileType){ case JSON: - return new JsonLocalhostSplitChangeFetcher(new InputStreamProviderImp(inputStream)); + return new JsonLocalhostSplitChangeFetcher(inputStreamProvider); case YAML: - return new YamlLocalhostSplitChangeFetcher(new InputStreamProviderImp(inputStream)); + return new YamlLocalhostSplitChangeFetcher(inputStreamProvider); default: - _log.warn("The sdk initialize in localhost mode using Legacy file. The splitFile or inputStream doesn't add it to the config."); + _log.warn(LEGACY_LOG_MESSAGE); return new LegacyLocalhostSplitChangeFetcher(splitFile); } + } catch (Exception e) { + _log.warn(String.format("There was no file named %s found. " + + "We created a split client that returns default treatments for all feature flags for all of your users. " + + "If you wish to return a specific treatment for a feature flag, enter the name of that feature flag name and " + + "treatment name separated by whitespace in %s; one pair per line. Empty lines or lines starting with '#' are " + + "considered comments", + splitFile, splitFile), e); } _log.warn(LEGACY_LOG_MESSAGE); return new LegacyLocalhostSplitChangeFetcher(splitFile); } + + private Boolean isInputStreamConfigValid(InputStream inputStream, FileTypeEnum fileType) { + return inputStream != null && fileType != null; + } + + private FileTypeEnum getFileTypeFromFileName(String fileName) { + try { + return FileTypeEnum.valueOf(Files.getFileExtension(fileName).toUpperCase()); + } catch (Exception e) { + return FileTypeEnum.LEGACY; + } + + } } \ No newline at end of file diff --git a/client/src/main/java/io/split/client/YamlLocalhostSplitChangeFetcher.java b/client/src/main/java/io/split/client/YamlLocalhostSplitChangeFetcher.java index 5d5a87c2f..745d1c5f6 100644 --- a/client/src/main/java/io/split/client/YamlLocalhostSplitChangeFetcher.java +++ b/client/src/main/java/io/split/client/YamlLocalhostSplitChangeFetcher.java @@ -33,7 +33,7 @@ public YamlLocalhostSplitChangeFetcher(InputStreamProvider inputStreamProvider) } @Override - public SplitChange fetch(long since, FetchOptions options) throws InputStreamProviderException { + public SplitChange fetch(long since, FetchOptions options) { try { Yaml yaml = new Yaml(); List>> yamlSplits = yaml.load(_inputStreamProvider.get()); @@ -75,8 +75,6 @@ public SplitChange fetch(long since, FetchOptions options) throws InputStreamPro splitChange.till = since; splitChange.since = since; return splitChange; - } catch (InputStreamProviderException i) { - throw i; } catch (Exception e) { throw new IllegalStateException("Problem fetching splitChanges using a yaml file: " + e.getMessage(), e); } diff --git a/client/src/main/java/io/split/engine/experiments/SplitChangeFetcher.java b/client/src/main/java/io/split/engine/experiments/SplitChangeFetcher.java index c27fca1df..7c5fbe76e 100644 --- a/client/src/main/java/io/split/engine/experiments/SplitChangeFetcher.java +++ b/client/src/main/java/io/split/engine/experiments/SplitChangeFetcher.java @@ -1,7 +1,6 @@ package io.split.engine.experiments; import io.split.client.dtos.SplitChange; -import io.split.client.exceptions.InputStreamProviderException; import io.split.engine.common.FetchOptions; /** @@ -33,5 +32,5 @@ public interface SplitChangeFetcher { * @return SegmentChange * @throws java.lang.RuntimeException if there was a problem computing split changes */ - SplitChange fetch(long since, FetchOptions options) throws InputStreamProviderException; + SplitChange fetch(long since, FetchOptions options); } diff --git a/client/src/main/java/io/split/engine/experiments/SplitFetcherImp.java b/client/src/main/java/io/split/engine/experiments/SplitFetcherImp.java index a94c07735..1043ccfcf 100644 --- a/client/src/main/java/io/split/engine/experiments/SplitFetcherImp.java +++ b/client/src/main/java/io/split/engine/experiments/SplitFetcherImp.java @@ -1,7 +1,6 @@ package io.split.engine.experiments; import io.split.client.dtos.SplitChange; -import io.split.client.exceptions.InputStreamProviderException; import io.split.client.utils.FeatureFlagsToUpdate; import io.split.storages.SplitCacheProducer; import io.split.telemetry.domain.enums.LastSynchronizationRecordsEnum; @@ -90,7 +89,7 @@ public void run() { this.forceRefresh(new FetchOptions.Builder().cacheControlHeaders(false).build()); } - private Set runWithoutExceptionHandling(FetchOptions options) throws InterruptedException, InputStreamProviderException { + private Set runWithoutExceptionHandling(FetchOptions options) throws InterruptedException { SplitChange change = _splitChangeFetcher.fetch(_splitCacheProducer.getChangeNumber(), options); Set segments = new HashSet<>(); diff --git a/client/src/test/java/io/split/client/JsonLocalhostSplitChangeFetcherTest.java b/client/src/test/java/io/split/client/JsonLocalhostSplitChangeFetcherTest.java index 9a9e4519e..e9a3c5cdd 100644 --- a/client/src/test/java/io/split/client/JsonLocalhostSplitChangeFetcherTest.java +++ b/client/src/test/java/io/split/client/JsonLocalhostSplitChangeFetcherTest.java @@ -4,7 +4,6 @@ import io.split.client.dtos.Split; import io.split.client.dtos.SplitChange; import io.split.client.dtos.Status; -import io.split.client.exceptions.InputStreamProviderException; import io.split.client.utils.FileInputStreamProvider; import io.split.client.utils.InputStreamProvider; import io.split.client.utils.InputStreamProviderImp; @@ -34,7 +33,7 @@ public class JsonLocalhostSplitChangeFetcherTest { private String TEST_4 = "{\"splits\":[{\"trafficTypeName\":\"user\",\"name\":\"SPLIT_1\",\"trafficAllocation\":100,\"trafficAllocationSeed\":-1780071202,\"seed\":-1442762199,\"status\":\"ACTIVE\",\"killed\":false,\"defaultTreatment\":\"off\",\"changeNumber\":1675443537882,\"algo\":2,\"configurations\":{},\"conditions\":[{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"user\",\"attribute\":null},\"matcherType\":\"ALL_KEYS\",\"negate\":false,\"userDefinedSegmentMatcherData\":null,\"whitelistMatcherData\":null,\"unaryNumericMatcherData\":null,\"betweenMatcherData\":null,\"booleanMatcherData\":null,\"dependencyMatcherData\":null,\"stringMatcherData\":null}]},\"partitions\":[{\"treatment\":\"on\",\"size\":0},{\"treatment\":\"off\",\"size\":100}],\"label\":\"default rule\"}]}],\"since\":-1,\"till\":445345}"; private String TEST_5 = "{\"splits\":[{\"trafficTypeName\":\"user\",\"name\":\"SPLIT_1\",\"trafficAllocation\":100,\"trafficAllocationSeed\":-1780071202,\"seed\":-1442762199,\"status\":\"ACTIVE\",\"killed\":false,\"defaultTreatment\":\"off\",\"changeNumber\":1675443537882,\"algo\":2,\"configurations\":{},\"conditions\":[{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"user\",\"attribute\":null},\"matcherType\":\"ALL_KEYS\",\"negate\":false,\"userDefinedSegmentMatcherData\":null,\"whitelistMatcherData\":null,\"unaryNumericMatcherData\":null,\"betweenMatcherData\":null,\"booleanMatcherData\":null,\"dependencyMatcherData\":null,\"stringMatcherData\":null}]},\"partitions\":[{\"treatment\":\"on\",\"size\":0},{\"treatment\":\"off\",\"size\":100}],\"label\":\"default rule\"}]},{\"trafficTypeName\":\"user\",\"name\":\"SPLIT_2\",\"trafficAllocation\":100,\"trafficAllocationSeed\":-1780071202,\"seed\":-1442762199,\"status\":\"ACTIVE\",\"killed\":false,\"defaultTreatment\":\"off\",\"changeNumber\":1675443537882,\"algo\":2,\"configurations\":{},\"conditions\":[{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"user\",\"attribute\":null},\"matcherType\":\"ALL_KEYS\",\"negate\":false,\"userDefinedSegmentMatcherData\":null,\"whitelistMatcherData\":null,\"unaryNumericMatcherData\":null,\"betweenMatcherData\":null,\"booleanMatcherData\":null,\"dependencyMatcherData\":null,\"stringMatcherData\":null}]},\"partitions\":[{\"treatment\":\"on\",\"size\":0},{\"treatment\":\"off\",\"size\":100}],\"label\":\"default rule\"}]}],\"since\":-1,\"till\":-1}"; @Test - public void testParseSplitChange() throws FileNotFoundException, InputStreamProviderException { + public void testParseSplitChange() throws FileNotFoundException { InputStream inputStream = new FileInputStream("src/test/resources/split_init.json"); InputStreamProvider inputStreamProvider = new InputStreamProviderImp(inputStream); JsonLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonLocalhostSplitChangeFetcher(inputStreamProvider); @@ -49,7 +48,7 @@ public void testParseSplitChange() throws FileNotFoundException, InputStreamProv } @Test - public void testSinceAndTillSanitization() throws FileNotFoundException, InputStreamProviderException { + public void testSinceAndTillSanitization() throws FileNotFoundException { InputStream inputStream = new FileInputStream("src/test/resources/sanitizer/splitChangeTillSanitization.json"); InputStreamProvider inputStreamProvider = new InputStreamProviderImp(inputStream); JsonLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonLocalhostSplitChangeFetcher(inputStreamProvider); @@ -62,7 +61,7 @@ public void testSinceAndTillSanitization() throws FileNotFoundException, InputSt } @Test - public void testSplitChangeWithoutSplits() throws FileNotFoundException, InputStreamProviderException { + public void testSplitChangeWithoutSplits() throws FileNotFoundException { InputStream inputStream = new FileInputStream("src/test/resources/sanitizer/splitChangeWithoutSplits.json"); InputStreamProvider inputStreamProvider = new InputStreamProviderImp(inputStream); JsonLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonLocalhostSplitChangeFetcher(inputStreamProvider); @@ -74,7 +73,7 @@ public void testSplitChangeWithoutSplits() throws FileNotFoundException, InputSt } @Test - public void testSplitChangeSplitsToSanitize() throws FileNotFoundException, InputStreamProviderException { + public void testSplitChangeSplitsToSanitize() throws FileNotFoundException { InputStream inputStream = new FileInputStream("src/test/resources/sanitizer/splitChangeSplitsToSanitize.json"); InputStreamProvider inputStreamProvider = new InputStreamProviderImp(inputStream); JsonLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonLocalhostSplitChangeFetcher(inputStreamProvider); @@ -91,7 +90,7 @@ public void testSplitChangeSplitsToSanitize() throws FileNotFoundException, Inpu } @Test - public void testSplitChangeSplitsToSanitizeMatchersNull() throws FileNotFoundException, InputStreamProviderException { + public void testSplitChangeSplitsToSanitizeMatchersNull() throws FileNotFoundException { InputStream inputStream = new FileInputStream("src/test/resources/sanitizer/splitChangerMatchersNull.json"); InputStreamProvider inputStreamProvider = new InputStreamProviderImp(inputStream); JsonLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonLocalhostSplitChangeFetcher(inputStreamProvider); @@ -108,7 +107,7 @@ public void testSplitChangeSplitsToSanitizeMatchersNull() throws FileNotFoundExc } @Test - public void testSplitChangeSplitsDifferentScenarios() throws IOException, InputStreamProviderException { + public void testSplitChangeSplitsDifferentScenarios() throws IOException { File file = folder.newFile("test_0.json"); @@ -171,8 +170,8 @@ public void testSplitChangeSplitsDifferentScenarios() throws IOException, InputS Assert.assertEquals(2323, splitChange.since); } - @Test(expected = InputStreamProviderException.class) - public void processTestForException() throws InputStreamProviderException { + @Test(expected = IllegalStateException.class) + public void processTestForException() { InputStreamProvider inputStreamProvider = new FileInputStreamProvider("src/test/resources/notExist.json"); JsonLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonLocalhostSplitChangeFetcher(inputStreamProvider); FetchOptions fetchOptions = Mockito.mock(FetchOptions.class); diff --git a/client/src/test/java/io/split/client/YamlLocalhostSplitChangeFetcherTest.java b/client/src/test/java/io/split/client/YamlLocalhostSplitChangeFetcherTest.java index 81c85a449..dabd96781 100644 --- a/client/src/test/java/io/split/client/YamlLocalhostSplitChangeFetcherTest.java +++ b/client/src/test/java/io/split/client/YamlLocalhostSplitChangeFetcherTest.java @@ -2,7 +2,6 @@ import io.split.client.dtos.Split; import io.split.client.dtos.SplitChange; -import io.split.client.exceptions.InputStreamProviderException; import io.split.client.utils.FileInputStreamProvider; import io.split.client.utils.InputStreamProvider; import io.split.client.utils.LocalhostUtils; @@ -27,7 +26,7 @@ public class YamlLocalhostSplitChangeFetcherTest { public TemporaryFolder folder = new TemporaryFolder(); @Test - public void testParseSplitChange() throws IOException, InputStreamProviderException { + public void testParseSplitChange() throws IOException { File file = folder.newFile(SplitClientConfig.LOCALHOST_DEFAULT_FILE); List> allSplits = new ArrayList(); @@ -76,8 +75,8 @@ public void testParseSplitChange() throws IOException, InputStreamProviderExcept } } - @Test(expected = InputStreamProviderException.class) - public void processTestForException() throws InputStreamProviderException { + @Test(expected = IllegalStateException.class) + public void processTestForException() { InputStreamProvider inputStreamProvider = new FileInputStreamProvider("src/test/resources/notExist.yaml"); YamlLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new YamlLocalhostSplitChangeFetcher(inputStreamProvider); FetchOptions fetchOptions = Mockito.mock(FetchOptions.class); diff --git a/client/src/test/java/io/split/engine/common/LocalhostSynchronizerTest.java b/client/src/test/java/io/split/engine/common/LocalhostSynchronizerTest.java index 70f3bc0ea..4981cd6cc 100644 --- a/client/src/test/java/io/split/engine/common/LocalhostSynchronizerTest.java +++ b/client/src/test/java/io/split/engine/common/LocalhostSynchronizerTest.java @@ -2,7 +2,6 @@ import io.split.client.LocalhostSegmentChangeFetcher; import io.split.client.JsonLocalhostSplitChangeFetcher; -import io.split.client.exceptions.InputStreamProviderException; import io.split.client.utils.FileInputStreamProvider; import io.split.client.utils.InputStreamProvider; import io.split.engine.experiments.SplitChangeFetcher; @@ -51,7 +50,7 @@ public void testSyncAll(){ } @Test - public void testPeriodicFetching() throws InterruptedException, InputStreamProviderException { + public void testPeriodicFetching() throws InterruptedException { SplitCache splitCacheProducer = new InMemoryCacheImp(); SplitChangeFetcher splitChangeFetcher = Mockito.mock(JsonLocalhostSplitChangeFetcher.class); @@ -78,7 +77,7 @@ public void testPeriodicFetching() throws InterruptedException, InputStreamProvi } @Test - public void testRefreshSplits() throws InputStreamProviderException { + public void testRefreshSplits() { SplitCacheProducer splitCacheProducer = new InMemoryCacheImp(); SplitChangeFetcher splitChangeFetcher = Mockito.mock(SplitChangeFetcher.class); SplitParser splitParser = new SplitParser(); diff --git a/client/src/test/java/io/split/engine/experiments/SplitFetcherTest.java b/client/src/test/java/io/split/engine/experiments/SplitFetcherTest.java index c186429ad..a6be86240 100644 --- a/client/src/test/java/io/split/engine/experiments/SplitFetcherTest.java +++ b/client/src/test/java/io/split/engine/experiments/SplitFetcherTest.java @@ -1,7 +1,6 @@ package io.split.engine.experiments; import com.google.common.collect.Lists; -import io.split.client.exceptions.InputStreamProviderException; import io.split.storages.memory.InMemoryCacheImp; import io.split.storages.SegmentCache; import io.split.storages.memory.SegmentCacheInMemoryImpl; @@ -88,7 +87,7 @@ private void works(long startingChangeNumber) throws InterruptedException { } @Test - public void when_parser_fails_we_remove_the_experiment() throws InterruptedException, InputStreamProviderException { + public void when_parser_fails_we_remove_the_experiment() throws InterruptedException { Split validSplit = new Split(); validSplit.status = Status.ACTIVE; validSplit.seed = (int) -1; @@ -214,7 +213,7 @@ public void works_with_user_defined_segments() throws Exception { } @Test - public void testBypassCdnClearedAfterFirstHit() throws InputStreamProviderException { + public void testBypassCdnClearedAfterFirstHit() { SplitChangeFetcher mockFetcher = Mockito.mock(SplitChangeFetcher.class); SplitParser mockParser = new SplitParser(); SplitCache mockCache = new InMemoryCacheImp(); diff --git a/client/src/test/java/io/split/engine/experiments/SplitSynchronizationTaskTest.java b/client/src/test/java/io/split/engine/experiments/SplitSynchronizationTaskTest.java index a15e7c7d0..d53ac89e7 100644 --- a/client/src/test/java/io/split/engine/experiments/SplitSynchronizationTaskTest.java +++ b/client/src/test/java/io/split/engine/experiments/SplitSynchronizationTaskTest.java @@ -1,7 +1,6 @@ package io.split.engine.experiments; import io.split.client.JsonLocalhostSplitChangeFetcher; -import io.split.client.exceptions.InputStreamProviderException; import io.split.engine.common.FetchOptions; import io.split.storages.SplitCacheProducer; import io.split.storages.memory.InMemoryCacheImp; @@ -15,7 +14,7 @@ public class SplitSynchronizationTaskTest { private static final TelemetryStorage TELEMETRY_STORAGE_NOOP = Mockito.mock(NoopTelemetryStorage.class); @Test - public void testLocalhost() throws InterruptedException, InputStreamProviderException { + public void testLocalhost() throws InterruptedException { SplitCacheProducer splitCacheProducer = new InMemoryCacheImp(); SplitChangeFetcher splitChangeFetcher = Mockito.mock(JsonLocalhostSplitChangeFetcher.class);