From bd15faa66d4c82a13a8735833aceae62ba1a133d Mon Sep 17 00:00:00 2001 From: Nadia Mayor Date: Fri, 1 Sep 2023 11:40:45 -0300 Subject: [PATCH] [7445] PR suggestions --- .../JsonLocalhostSplitChangeFetcher.java | 5 +- .../io/split/client/SplitFactoryImpl.java | 51 ++++---- .../YamlLocalhostSplitChangeFetcher.java | 5 +- .../InputStreamProviderException.java | 8 +- .../client/utils/FileInputStreamProvider.java | 3 +- .../client/utils/InputStreamProviderImp.java | 2 +- .../io/split/client/utils/LocalhostPair.java | 20 --- .../experiments/SplitChangeFetcher.java | 3 +- .../engine/experiments/SplitFetcherImp.java | 3 +- .../JsonLocalhostSplitChangeFetcherTest.java | 17 +-- .../io/split/client/SplitFactoryImplTest.java | 123 +++++++++++++++--- .../YamlLocalhostSplitChangeFetcherTest.java | 7 +- .../common/LocalhostSynchronizerTest.java | 9 +- .../engine/experiments/SplitFetcherTest.java | 5 +- .../SplitSynchronizationTaskTest.java | 3 +- 15 files changed, 166 insertions(+), 98 deletions(-) delete mode 100644 client/src/main/java/io/split/client/utils/LocalhostPair.java diff --git a/client/src/main/java/io/split/client/JsonLocalhostSplitChangeFetcher.java b/client/src/main/java/io/split/client/JsonLocalhostSplitChangeFetcher.java index 0917d3591..cf91712d3 100644 --- a/client/src/main/java/io/split/client/JsonLocalhostSplitChangeFetcher.java +++ b/client/src/main/java/io/split/client/JsonLocalhostSplitChangeFetcher.java @@ -30,14 +30,13 @@ public JsonLocalhostSplitChangeFetcher(InputStreamProvider inputStreamProvider) } @Override - public SplitChange fetch(long since, FetchOptions options) { + public SplitChange fetch(long since, FetchOptions options) throws InputStreamProviderException { 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 new IllegalStateException(String.format("Problem fetching splitChanges using file named %s: %s", - i.getFileName(), i.getMessage()), 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 d28964d6a..80f4df244 100644 --- a/client/src/main/java/io/split/client/SplitFactoryImpl.java +++ b/client/src/main/java/io/split/client/SplitFactoryImpl.java @@ -33,7 +33,6 @@ import io.split.client.utils.FileInputStreamProvider; import io.split.client.utils.FileTypeEnum; import io.split.client.utils.InputStreamProviderImp; -import io.split.client.utils.LocalhostPair; import io.split.client.utils.SDKMetadata; import io.split.engine.SDKReadinessGates; import io.split.engine.common.ConsumerSyncManager; @@ -371,23 +370,7 @@ protected SplitFactoryImpl(SplitClientConfig config) { config.getThreadFactory()); // SplitFetcher - LocalhostPair pair = getInputStreamProviderAndFileType(config.splitFile(), config.inputStream(), config.fileType()); - SplitChangeFetcher splitChangeFetcher; - if (pair == null) { - splitChangeFetcher = new LegacyLocalhostSplitChangeFetcher(config.splitFile()); - _log.warn("The sdk initialize in localhost mode using Legacy file. The splitFile or inputStream doesn't add it to the config."); - } else { - switch (pair.getFileTypeEnum()) { - case JSON: - splitChangeFetcher = new JsonLocalhostSplitChangeFetcher(pair.getInputStreamProvider()); - break; - case YAML: - splitChangeFetcher = new YamlLocalhostSplitChangeFetcher(pair.getInputStreamProvider()); - break; - default: - splitChangeFetcher = new LegacyLocalhostSplitChangeFetcher(config.splitFile()); - } - } + SplitChangeFetcher splitChangeFetcher = createSplitChangeFetcher(config); SplitParser splitParser = new SplitParser(); _splitFetcher = new SplitFetcherImp(splitChangeFetcher, splitParser, splitCache, _telemetryStorageProducer); @@ -656,26 +639,31 @@ private UniqueKeysTracker createUniqueKeysTracker(SplitClientConfig config){ return null; } - LocalhostPair getInputStreamProviderAndFileType(String splitFile, InputStream inputStream, - FileTypeEnum fileType) { + private SplitChangeFetcher createSplitChangeFetcher(SplitClientConfig splitClientConfig) { + 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"); - return null; + _log.warn("The sdk initialize in localhost mode using Legacy file. The splitFile or inputStream doesn't add it to the config."); + 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"); - return null; + _log.warn("The sdk initialize in localhost mode using Legacy file. The splitFile or inputStream doesn't add it to the config."); + return new LegacyLocalhostSplitChangeFetcher(splitFile); } if (inputStream == null && splitFile == null){ _log.warn("splitFile or inputStreamProvider should have a value"); - return null; + _log.warn("The sdk initialize in localhost mode using Legacy file. The splitFile or inputStream doesn't add it to the config."); + return new LegacyLocalhostSplitChangeFetcher(splitFile); } if (splitFile != null) { try { if (splitFile.toLowerCase().endsWith(".json")) { - return new LocalhostPair(new FileInputStreamProvider(splitFile), FileTypeEnum.JSON); + return new JsonLocalhostSplitChangeFetcher(new FileInputStreamProvider(splitFile)); } else if (splitFile.endsWith(".yaml") || splitFile.endsWith(".yml")) { - return new LocalhostPair(new FileInputStreamProvider(splitFile), FileTypeEnum.YAML); + return new YamlLocalhostSplitChangeFetcher(new FileInputStreamProvider(splitFile)); } } catch (Exception e) { _log.warn(String.format("There was no file named %s found. " + @@ -686,8 +674,17 @@ LocalhostPair getInputStreamProviderAndFileType(String splitFile, InputStream in splitFile, splitFile), e); } } else { - return new LocalhostPair(new InputStreamProviderImp(inputStream), fileType); + switch (fileType) { + case JSON: + return new JsonLocalhostSplitChangeFetcher(new InputStreamProviderImp(inputStream)); + case YAML: + return new YamlLocalhostSplitChangeFetcher(new InputStreamProviderImp(inputStream)); + default: + _log.warn("The sdk initialize in localhost mode using Legacy file. The splitFile or inputStream doesn't add it to the config."); + return new LegacyLocalhostSplitChangeFetcher(splitFile); + } } - return null; + _log.warn("The sdk initialize in localhost mode using Legacy file. The splitFile or inputStream doesn't add it to the config."); + return new LegacyLocalhostSplitChangeFetcher(splitFile); } } \ 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 68852de47..5d5a87c2f 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) { + public SplitChange fetch(long since, FetchOptions options) throws InputStreamProviderException { try { Yaml yaml = new Yaml(); List>> yamlSplits = yaml.load(_inputStreamProvider.get()); @@ -76,8 +76,7 @@ public SplitChange fetch(long since, FetchOptions options) { splitChange.since = since; return splitChange; } catch (InputStreamProviderException i) { - throw new IllegalStateException(String.format("Problem fetching splitChanges using file named %s: %s", - i.getFileName(), i.getMessage()), 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/client/exceptions/InputStreamProviderException.java b/client/src/main/java/io/split/client/exceptions/InputStreamProviderException.java index ca79b97e1..33fba556f 100644 --- a/client/src/main/java/io/split/client/exceptions/InputStreamProviderException.java +++ b/client/src/main/java/io/split/client/exceptions/InputStreamProviderException.java @@ -1,14 +1,8 @@ package io.split.client.exceptions; public class InputStreamProviderException extends Exception { - private final String _fileName; - public InputStreamProviderException(String fileName, String message) { + public InputStreamProviderException(String message) { super(message); - _fileName = fileName; - } - - public String getFileName() { - return _fileName; } } \ No newline at end of file diff --git a/client/src/main/java/io/split/client/utils/FileInputStreamProvider.java b/client/src/main/java/io/split/client/utils/FileInputStreamProvider.java index 81e15469c..c1d9f9812 100644 --- a/client/src/main/java/io/split/client/utils/FileInputStreamProvider.java +++ b/client/src/main/java/io/split/client/utils/FileInputStreamProvider.java @@ -19,7 +19,8 @@ public InputStream get() throws InputStreamProviderException { try { return new FileInputStream(_fileName); } catch (FileNotFoundException f) { - throw new InputStreamProviderException(_fileName, f.getMessage()); + throw new InputStreamProviderException(String.format("Problem fetching splitChanges using file named %s: %s", + _fileName, f.getMessage())); } } } \ No newline at end of file diff --git a/client/src/main/java/io/split/client/utils/InputStreamProviderImp.java b/client/src/main/java/io/split/client/utils/InputStreamProviderImp.java index c8e919cf4..4b945fcd3 100644 --- a/client/src/main/java/io/split/client/utils/InputStreamProviderImp.java +++ b/client/src/main/java/io/split/client/utils/InputStreamProviderImp.java @@ -5,7 +5,7 @@ import java.io.InputStream; public class InputStreamProviderImp implements InputStreamProvider { - private InputStream _inputStream; + private final InputStream _inputStream; public InputStreamProviderImp(InputStream inputStream){ _inputStream = inputStream; diff --git a/client/src/main/java/io/split/client/utils/LocalhostPair.java b/client/src/main/java/io/split/client/utils/LocalhostPair.java deleted file mode 100644 index 471aaced7..000000000 --- a/client/src/main/java/io/split/client/utils/LocalhostPair.java +++ /dev/null @@ -1,20 +0,0 @@ -package io.split.client.utils; - -public class LocalhostPair { - - private final InputStreamProvider _inputStreamProvider; - private final FileTypeEnum _fileTypeEnum; - - public LocalhostPair(InputStreamProvider inputStreamProvider, FileTypeEnum fileType) { - _inputStreamProvider = inputStreamProvider; - _fileTypeEnum = fileType; - } - - public InputStreamProvider getInputStreamProvider() { - return _inputStreamProvider; - } - - public FileTypeEnum getFileTypeEnum() { - return _fileTypeEnum; - } -} \ No newline at end of file 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 7c5fbe76e..c27fca1df 100644 --- a/client/src/main/java/io/split/engine/experiments/SplitChangeFetcher.java +++ b/client/src/main/java/io/split/engine/experiments/SplitChangeFetcher.java @@ -1,6 +1,7 @@ package io.split.engine.experiments; import io.split.client.dtos.SplitChange; +import io.split.client.exceptions.InputStreamProviderException; import io.split.engine.common.FetchOptions; /** @@ -32,5 +33,5 @@ public interface SplitChangeFetcher { * @return SegmentChange * @throws java.lang.RuntimeException if there was a problem computing split changes */ - SplitChange fetch(long since, FetchOptions options); + SplitChange fetch(long since, FetchOptions options) throws InputStreamProviderException; } 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 1043ccfcf..a94c07735 100644 --- a/client/src/main/java/io/split/engine/experiments/SplitFetcherImp.java +++ b/client/src/main/java/io/split/engine/experiments/SplitFetcherImp.java @@ -1,6 +1,7 @@ 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; @@ -89,7 +90,7 @@ public void run() { this.forceRefresh(new FetchOptions.Builder().cacheControlHeaders(false).build()); } - private Set runWithoutExceptionHandling(FetchOptions options) throws InterruptedException { + private Set runWithoutExceptionHandling(FetchOptions options) throws InterruptedException, InputStreamProviderException { 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 e9a3c5cdd..9a9e4519e 100644 --- a/client/src/test/java/io/split/client/JsonLocalhostSplitChangeFetcherTest.java +++ b/client/src/test/java/io/split/client/JsonLocalhostSplitChangeFetcherTest.java @@ -4,6 +4,7 @@ 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; @@ -33,7 +34,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 { + public void testParseSplitChange() throws FileNotFoundException, InputStreamProviderException { InputStream inputStream = new FileInputStream("src/test/resources/split_init.json"); InputStreamProvider inputStreamProvider = new InputStreamProviderImp(inputStream); JsonLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonLocalhostSplitChangeFetcher(inputStreamProvider); @@ -48,7 +49,7 @@ public void testParseSplitChange() throws FileNotFoundException { } @Test - public void testSinceAndTillSanitization() throws FileNotFoundException { + public void testSinceAndTillSanitization() throws FileNotFoundException, InputStreamProviderException { InputStream inputStream = new FileInputStream("src/test/resources/sanitizer/splitChangeTillSanitization.json"); InputStreamProvider inputStreamProvider = new InputStreamProviderImp(inputStream); JsonLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonLocalhostSplitChangeFetcher(inputStreamProvider); @@ -61,7 +62,7 @@ public void testSinceAndTillSanitization() throws FileNotFoundException { } @Test - public void testSplitChangeWithoutSplits() throws FileNotFoundException { + public void testSplitChangeWithoutSplits() throws FileNotFoundException, InputStreamProviderException { InputStream inputStream = new FileInputStream("src/test/resources/sanitizer/splitChangeWithoutSplits.json"); InputStreamProvider inputStreamProvider = new InputStreamProviderImp(inputStream); JsonLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonLocalhostSplitChangeFetcher(inputStreamProvider); @@ -73,7 +74,7 @@ public void testSplitChangeWithoutSplits() throws FileNotFoundException { } @Test - public void testSplitChangeSplitsToSanitize() throws FileNotFoundException { + public void testSplitChangeSplitsToSanitize() throws FileNotFoundException, InputStreamProviderException { InputStream inputStream = new FileInputStream("src/test/resources/sanitizer/splitChangeSplitsToSanitize.json"); InputStreamProvider inputStreamProvider = new InputStreamProviderImp(inputStream); JsonLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonLocalhostSplitChangeFetcher(inputStreamProvider); @@ -90,7 +91,7 @@ public void testSplitChangeSplitsToSanitize() throws FileNotFoundException { } @Test - public void testSplitChangeSplitsToSanitizeMatchersNull() throws FileNotFoundException { + public void testSplitChangeSplitsToSanitizeMatchersNull() throws FileNotFoundException, InputStreamProviderException { InputStream inputStream = new FileInputStream("src/test/resources/sanitizer/splitChangerMatchersNull.json"); InputStreamProvider inputStreamProvider = new InputStreamProviderImp(inputStream); JsonLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonLocalhostSplitChangeFetcher(inputStreamProvider); @@ -107,7 +108,7 @@ public void testSplitChangeSplitsToSanitizeMatchersNull() throws FileNotFoundExc } @Test - public void testSplitChangeSplitsDifferentScenarios() throws IOException { + public void testSplitChangeSplitsDifferentScenarios() throws IOException, InputStreamProviderException { File file = folder.newFile("test_0.json"); @@ -170,8 +171,8 @@ public void testSplitChangeSplitsDifferentScenarios() throws IOException { Assert.assertEquals(2323, splitChange.since); } - @Test(expected = IllegalStateException.class) - public void processTestForException() { + @Test(expected = InputStreamProviderException.class) + public void processTestForException() throws InputStreamProviderException { 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/SplitFactoryImplTest.java b/client/src/test/java/io/split/client/SplitFactoryImplTest.java index 06214b015..f51ccae36 100644 --- a/client/src/test/java/io/split/client/SplitFactoryImplTest.java +++ b/client/src/test/java/io/split/client/SplitFactoryImplTest.java @@ -2,7 +2,6 @@ import io.split.client.impressions.ImpressionsManager; import io.split.client.utils.FileTypeEnum; -import io.split.client.utils.LocalhostPair; import io.split.integrations.IntegrationsConfig; import io.split.storages.enums.OperationMode; import io.split.storages.pluggable.domain.UserStorageWrapper; @@ -18,6 +17,8 @@ import java.io.FileNotFoundException; import java.io.InputStream; import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.net.URISyntaxException; @@ -227,28 +228,120 @@ public void testFactoryConsumerDestroy() throws NoSuchFieldException, URISyntaxE } @Test - public void testGetInputStreamProviderAndFileType() throws URISyntaxException, FileNotFoundException { + public void testLocalhostLegacy() throws URISyntaxException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { SplitClientConfig splitClientConfig = SplitClientConfig.builder() .setBlockUntilReadyTimeout(10000) .build(); SplitFactoryImpl splitFactory = new SplitFactoryImpl("localhost", splitClientConfig); - //splitFile null and InputStream null - Assert.assertNull(splitFactory.getInputStreamProviderAndFileType(null, null, FileTypeEnum.JSON)); - //splitFile not null and InputStream not null + Method method = SplitFactoryImpl.class.getDeclaredMethod("createSplitChangeFetcher", SplitClientConfig.class); + method.setAccessible(true); + Object splitChangeFetcher = method.invoke(splitFactory, splitClientConfig); + Assert.assertTrue(splitChangeFetcher instanceof LegacyLocalhostSplitChangeFetcher); + } + + @Test + public void testLocalhostYaml() throws URISyntaxException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { + SplitClientConfig splitClientConfig = SplitClientConfig.builder() + .splitFile("src/test/resources/split.yaml") + .setBlockUntilReadyTimeout(10000) + .build(); + SplitFactoryImpl splitFactory = new SplitFactoryImpl("localhost", splitClientConfig); + + Method method = SplitFactoryImpl.class.getDeclaredMethod("createSplitChangeFetcher", SplitClientConfig.class); + method.setAccessible(true); + Object splitChangeFetcher = method.invoke(splitFactory, splitClientConfig); + Assert.assertTrue(splitChangeFetcher instanceof YamlLocalhostSplitChangeFetcher); + } + + @Test + public void testLocalhosJson() throws URISyntaxException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { + SplitClientConfig splitClientConfig = SplitClientConfig.builder() + .splitFile("src/test/resources/split_init.json") + .setBlockUntilReadyTimeout(10000) + .build(); + SplitFactoryImpl splitFactory = new SplitFactoryImpl("localhost", splitClientConfig); + + Method method = SplitFactoryImpl.class.getDeclaredMethod("createSplitChangeFetcher", SplitClientConfig.class); + method.setAccessible(true); + Object splitChangeFetcher = method.invoke(splitFactory, splitClientConfig); + Assert.assertTrue(splitChangeFetcher instanceof JsonLocalhostSplitChangeFetcher); + } + + @Test + public void testLocalhosYamlInputStream() throws URISyntaxException, NoSuchMethodException, InvocationTargetException, + IllegalAccessException, FileNotFoundException { + InputStream inputStream = new FileInputStream("src/test/resources/split.yaml"); + SplitClientConfig splitClientConfig = SplitClientConfig.builder() + .splitFile(inputStream, FileTypeEnum.YAML) + .setBlockUntilReadyTimeout(10000) + .build(); + SplitFactoryImpl splitFactory = new SplitFactoryImpl("localhost", splitClientConfig); + + Method method = SplitFactoryImpl.class.getDeclaredMethod("createSplitChangeFetcher", SplitClientConfig.class); + method.setAccessible(true); + Object splitChangeFetcher = method.invoke(splitFactory, splitClientConfig); + Assert.assertTrue(splitChangeFetcher instanceof YamlLocalhostSplitChangeFetcher); + } + + @Test + public void testLocalhosJsonInputStream() throws URISyntaxException, NoSuchMethodException, InvocationTargetException, + IllegalAccessException, FileNotFoundException { InputStream inputStream = new FileInputStream("src/test/resources/split_init.json"); - Assert.assertNull(splitFactory.getInputStreamProviderAndFileType("test", inputStream, FileTypeEnum.JSON)); + SplitClientConfig splitClientConfig = SplitClientConfig.builder() + .splitFile(inputStream, FileTypeEnum.JSON) + .setBlockUntilReadyTimeout(10000) + .build(); + SplitFactoryImpl splitFactory = new SplitFactoryImpl("localhost", splitClientConfig); + + Method method = SplitFactoryImpl.class.getDeclaredMethod("createSplitChangeFetcher", SplitClientConfig.class); + method.setAccessible(true); + Object splitChangeFetcher = method.invoke(splitFactory, splitClientConfig); + Assert.assertTrue(splitChangeFetcher instanceof JsonLocalhostSplitChangeFetcher); + } + + @Test + public void testLocalhosJsonInputStreamNull() throws URISyntaxException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { + SplitClientConfig splitClientConfig = SplitClientConfig.builder() + .splitFile(null, FileTypeEnum.JSON) + .setBlockUntilReadyTimeout(10000) + .build(); + SplitFactoryImpl splitFactory = new SplitFactoryImpl("localhost", splitClientConfig); - //inputStream is not null and fileType is null - Assert.assertNull(splitFactory.getInputStreamProviderAndFileType(null, inputStream, null)); + Method method = SplitFactoryImpl.class.getDeclaredMethod("createSplitChangeFetcher", SplitClientConfig.class); + method.setAccessible(true); + Object splitChangeFetcher = method.invoke(splitFactory, splitClientConfig); + Assert.assertTrue(splitChangeFetcher instanceof LegacyLocalhostSplitChangeFetcher); + } - //split file not null - LocalhostPair localhostPair = splitFactory.getInputStreamProviderAndFileType("src/test/resources/split_init.json", - null, null); - Assert.assertNotNull(localhostPair); + @Test + public void testLocalhosJsonInputStreamAndFileTypeNull() throws URISyntaxException, NoSuchMethodException, InvocationTargetException, + IllegalAccessException, FileNotFoundException { + InputStream inputStream = new FileInputStream("src/test/resources/split_init.json"); + SplitClientConfig splitClientConfig = SplitClientConfig.builder() + .splitFile(inputStream, null) + .setBlockUntilReadyTimeout(10000) + .build(); + SplitFactoryImpl splitFactory = new SplitFactoryImpl("localhost", splitClientConfig); + + Method method = SplitFactoryImpl.class.getDeclaredMethod("createSplitChangeFetcher", SplitClientConfig.class); + method.setAccessible(true); + Object splitChangeFetcher = method.invoke(splitFactory, splitClientConfig); + Assert.assertTrue(splitChangeFetcher instanceof LegacyLocalhostSplitChangeFetcher); + } + + @Test + public void testLocalhosJsonInputStreamNullAndFileTypeNull() throws URISyntaxException, NoSuchMethodException, InvocationTargetException, + IllegalAccessException { + SplitClientConfig splitClientConfig = SplitClientConfig.builder() + .splitFile(null, null) + .setBlockUntilReadyTimeout(10000) + .build(); + SplitFactoryImpl splitFactory = new SplitFactoryImpl("localhost", splitClientConfig); - //inputStream is not null and filetype is not null - localhostPair = splitFactory.getInputStreamProviderAndFileType(null, inputStream, FileTypeEnum.JSON); - Assert.assertNotNull(localhostPair); + Method method = SplitFactoryImpl.class.getDeclaredMethod("createSplitChangeFetcher", SplitClientConfig.class); + method.setAccessible(true); + Object splitChangeFetcher = method.invoke(splitFactory, splitClientConfig); + Assert.assertTrue(splitChangeFetcher instanceof LegacyLocalhostSplitChangeFetcher); } } \ No newline at end of file diff --git a/client/src/test/java/io/split/client/YamlLocalhostSplitChangeFetcherTest.java b/client/src/test/java/io/split/client/YamlLocalhostSplitChangeFetcherTest.java index dabd96781..81c85a449 100644 --- a/client/src/test/java/io/split/client/YamlLocalhostSplitChangeFetcherTest.java +++ b/client/src/test/java/io/split/client/YamlLocalhostSplitChangeFetcherTest.java @@ -2,6 +2,7 @@ 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; @@ -26,7 +27,7 @@ public class YamlLocalhostSplitChangeFetcherTest { public TemporaryFolder folder = new TemporaryFolder(); @Test - public void testParseSplitChange() throws IOException { + public void testParseSplitChange() throws IOException, InputStreamProviderException { File file = folder.newFile(SplitClientConfig.LOCALHOST_DEFAULT_FILE); List> allSplits = new ArrayList(); @@ -75,8 +76,8 @@ public void testParseSplitChange() throws IOException { } } - @Test(expected = IllegalStateException.class) - public void processTestForException() { + @Test(expected = InputStreamProviderException.class) + public void processTestForException() throws InputStreamProviderException { 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 ed77888f0..70f3bc0ea 100644 --- a/client/src/test/java/io/split/engine/common/LocalhostSynchronizerTest.java +++ b/client/src/test/java/io/split/engine/common/LocalhostSynchronizerTest.java @@ -2,6 +2,7 @@ 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; @@ -22,14 +23,12 @@ import org.junit.Test; import org.mockito.Mockito; -import java.io.FileNotFoundException; - public class LocalhostSynchronizerTest { private static final TelemetryStorage TELEMETRY_STORAGE_NOOP = Mockito.mock(NoopTelemetryStorage.class); @Test - public void testSyncAll() throws FileNotFoundException { + public void testSyncAll(){ SplitCache splitCacheProducer = new InMemoryCacheImp(); InputStreamProvider inputStreamProvider = new FileInputStreamProvider("src/test/resources/split_init.json"); @@ -52,7 +51,7 @@ public void testSyncAll() throws FileNotFoundException { } @Test - public void testPeriodicFetching() throws InterruptedException { + public void testPeriodicFetching() throws InterruptedException, InputStreamProviderException { SplitCache splitCacheProducer = new InMemoryCacheImp(); SplitChangeFetcher splitChangeFetcher = Mockito.mock(JsonLocalhostSplitChangeFetcher.class); @@ -79,7 +78,7 @@ public void testPeriodicFetching() throws InterruptedException { } @Test - public void testRefreshSplits() { + public void testRefreshSplits() throws InputStreamProviderException { 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 a6be86240..c186429ad 100644 --- a/client/src/test/java/io/split/engine/experiments/SplitFetcherTest.java +++ b/client/src/test/java/io/split/engine/experiments/SplitFetcherTest.java @@ -1,6 +1,7 @@ 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; @@ -87,7 +88,7 @@ private void works(long startingChangeNumber) throws InterruptedException { } @Test - public void when_parser_fails_we_remove_the_experiment() throws InterruptedException { + public void when_parser_fails_we_remove_the_experiment() throws InterruptedException, InputStreamProviderException { Split validSplit = new Split(); validSplit.status = Status.ACTIVE; validSplit.seed = (int) -1; @@ -213,7 +214,7 @@ public void works_with_user_defined_segments() throws Exception { } @Test - public void testBypassCdnClearedAfterFirstHit() { + public void testBypassCdnClearedAfterFirstHit() throws InputStreamProviderException { 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 d53ac89e7..a15e7c7d0 100644 --- a/client/src/test/java/io/split/engine/experiments/SplitSynchronizationTaskTest.java +++ b/client/src/test/java/io/split/engine/experiments/SplitSynchronizationTaskTest.java @@ -1,6 +1,7 @@ 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; @@ -14,7 +15,7 @@ public class SplitSynchronizationTaskTest { private static final TelemetryStorage TELEMETRY_STORAGE_NOOP = Mockito.mock(NoopTelemetryStorage.class); @Test - public void testLocalhost() throws InterruptedException { + public void testLocalhost() throws InterruptedException, InputStreamProviderException { SplitCacheProducer splitCacheProducer = new InMemoryCacheImp(); SplitChangeFetcher splitChangeFetcher = Mockito.mock(JsonLocalhostSplitChangeFetcher.class);