From f95cd7edf52f7518ee024e902a64f422b09f6ea1 Mon Sep 17 00:00:00 2001 From: Nadia Mayor Date: Tue, 15 Aug 2023 16:09:49 -0300 Subject: [PATCH 1/2] Add new config fileResource and refactor localhost --- .../JsonFileLocalhostSplitChangeFetcher.java | 45 +++++++++++++++++++ .../JsonLocalhostSplitChangeFetcher.java | 30 +++---------- ...onResourceLocalhostSplitChangeFetcher.java | 38 ++++++++++++++++ .../LegacyLocalhostSplitChangeFetcher.java | 2 +- .../io/split/client/SplitClientConfig.java | 20 +++++++++ .../io/split/client/SplitFactoryImpl.java | 21 ++++++--- .../YamlFileLocalhostSplitChangeFetcher.java | 44 ++++++++++++++++++ .../YamlLocalhostSplitChangeFetcher.java | 28 +++--------- ...mlResourceLocalhostSplitChangeFetcher.java | 34 ++++++++++++++ .../JsonLocalhostSplitChangeFetcherTest.java | 12 ++--- ...sourceLocalhostSplitChangeFetcherTest.java | 6 +++ .../YamlLocalhostSplitChangeFetcherTest.java | 4 +- ...sourceLocalhostSplitChangeFetcherTest.java | 27 +++++++++++ .../common/LocalhostSynchronizerTest.java | 6 +-- .../experiments/SplitFetcherImpTest.java | 4 +- .../SplitSynchronizationTaskTest.java | 4 +- .../SegmentSynchronizationTaskImpTest.java | 4 +- 17 files changed, 261 insertions(+), 68 deletions(-) create mode 100644 client/src/main/java/io/split/client/JsonFileLocalhostSplitChangeFetcher.java create mode 100644 client/src/main/java/io/split/client/JsonResourceLocalhostSplitChangeFetcher.java create mode 100644 client/src/main/java/io/split/client/YamlFileLocalhostSplitChangeFetcher.java create mode 100644 client/src/main/java/io/split/client/YamlResourceLocalhostSplitChangeFetcher.java create mode 100644 client/src/test/java/io/split/client/JsonResourceLocalhostSplitChangeFetcherTest.java create mode 100644 client/src/test/java/io/split/client/YamlResourceLocalhostSplitChangeFetcherTest.java diff --git a/client/src/main/java/io/split/client/JsonFileLocalhostSplitChangeFetcher.java b/client/src/main/java/io/split/client/JsonFileLocalhostSplitChangeFetcher.java new file mode 100644 index 000000000..ad5e2d9a1 --- /dev/null +++ b/client/src/main/java/io/split/client/JsonFileLocalhostSplitChangeFetcher.java @@ -0,0 +1,45 @@ +package io.split.client; + +import com.google.gson.stream.JsonReader; +import io.split.client.dtos.SplitChange; +import io.split.client.utils.Json; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.util.List; +import java.util.Map; + +public class JsonFileLocalhostSplitChangeFetcher extends JsonLocalhostSplitChangeFetcher { + + private static final Logger _log = LoggerFactory.getLogger(JsonFileLocalhostSplitChangeFetcher.class); + private final File _file; + + + public JsonFileLocalhostSplitChangeFetcher(String filePath) { + _file = new File(filePath); + super.lastHash = new byte[0]; + } + + @Override + public JsonReader readFile() { + try { + return new JsonReader(new FileReader("_file")); + } catch (FileNotFoundException f) { + _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", + getFilePath(), getFilePath()), f); + throw new IllegalStateException("Problem fetching splitChanges: " + f.getMessage(), f); + } + } + + @Override + public String getFilePath() { + return _file.getPath(); + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/client/JsonLocalhostSplitChangeFetcher.java b/client/src/main/java/io/split/client/JsonLocalhostSplitChangeFetcher.java index 9b42fa4f2..6fa9b43d6 100644 --- a/client/src/main/java/io/split/client/JsonLocalhostSplitChangeFetcher.java +++ b/client/src/main/java/io/split/client/JsonLocalhostSplitChangeFetcher.java @@ -8,44 +8,26 @@ import io.split.engine.experiments.SplitChangeFetcher; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Arrays; -public class JsonLocalhostSplitChangeFetcher implements SplitChangeFetcher { - +public abstract class JsonLocalhostSplitChangeFetcher implements SplitChangeFetcher { private static final Logger _log = LoggerFactory.getLogger(JsonLocalhostSplitChangeFetcher.class); - private final File _file; - private byte [] lastHash; - - public JsonLocalhostSplitChangeFetcher(String filePath) { - _file = new File(filePath); - lastHash = new byte[0]; - } + byte [] lastHash; + public abstract JsonReader readFile(); + public abstract String getFilePath(); @Override public SplitChange fetch(long since, FetchOptions options) { try { - JsonReader jsonReader = new JsonReader(new FileReader(_file)); - SplitChange splitChange = Json.fromJson(jsonReader, SplitChange.class); + SplitChange splitChange = Json.fromJson(readFile(), SplitChange.class); return processSplitChange(splitChange, since); - } catch (FileNotFoundException f){ - _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", - _file.getPath(), _file.getPath()), f); - throw new IllegalStateException("Problem fetching splitChanges: " + f.getMessage(), f); } catch (Exception e) { _log.warn(String.format("Problem to fetch split change using the file %s", - _file.getPath()), e); + getFilePath()), e); throw new IllegalStateException("Problem fetching splitChanges: " + e.getMessage(), e); } } diff --git a/client/src/main/java/io/split/client/JsonResourceLocalhostSplitChangeFetcher.java b/client/src/main/java/io/split/client/JsonResourceLocalhostSplitChangeFetcher.java new file mode 100644 index 000000000..bd376b159 --- /dev/null +++ b/client/src/main/java/io/split/client/JsonResourceLocalhostSplitChangeFetcher.java @@ -0,0 +1,38 @@ +package io.split.client; + +import com.google.gson.stream.JsonReader; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.BufferedReader; +import java.io.InputStream; +import java.io.InputStreamReader; + +public class JsonResourceLocalhostSplitChangeFetcher extends JsonLocalhostSplitChangeFetcher { + private static final Logger _log = LoggerFactory.getLogger(JsonResourceLocalhostSplitChangeFetcher.class); + private final String _fileName; + + public JsonResourceLocalhostSplitChangeFetcher(String fileName) { + _fileName = fileName; + super.lastHash = new byte[0]; + } + + @Override + public JsonReader readFile() { + try { + InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(_fileName); + BufferedReader streamReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")); + JsonReader jsonReader = new JsonReader(streamReader); + return jsonReader; + } catch (Exception e){ + _log.warn(String.format("Problem to fetch split change using the file %s", + _fileName), e); + throw new IllegalStateException("Problem fetching splitChanges: " + e.getMessage(), e); + } + } + + @Override + public String getFilePath() { + return _fileName; + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/client/LegacyLocalhostSplitChangeFetcher.java b/client/src/main/java/io/split/client/LegacyLocalhostSplitChangeFetcher.java index a35c92cfe..c5b309662 100644 --- a/client/src/main/java/io/split/client/LegacyLocalhostSplitChangeFetcher.java +++ b/client/src/main/java/io/split/client/LegacyLocalhostSplitChangeFetcher.java @@ -22,7 +22,7 @@ public class LegacyLocalhostSplitChangeFetcher implements SplitChangeFetcher { - private static final Logger _log = LoggerFactory.getLogger(YamlLocalhostSplitChangeFetcher.class); + private static final Logger _log = LoggerFactory.getLogger(LegacyLocalhostSplitChangeFetcher.class); static final String FILENAME = ".split"; private final File _splitFile; diff --git a/client/src/main/java/io/split/client/SplitClientConfig.java b/client/src/main/java/io/split/client/SplitClientConfig.java index b362a2de4..807e3edf0 100644 --- a/client/src/main/java/io/split/client/SplitClientConfig.java +++ b/client/src/main/java/io/split/client/SplitClientConfig.java @@ -49,6 +49,7 @@ public class SplitClientConfig { private final int _maxStringLength; private final boolean _destroyOnShutDown; private final String _splitFile; + private final String _splitFileResource; private final String _segmentDirectory; private final IntegrationsConfig _integrationsConfig; private final boolean _streamingEnabled; @@ -111,6 +112,7 @@ private SplitClientConfig(String endpoint, int maxStringLength, boolean destroyOnShutDown, String splitFile, + String splitFileResource, String segmentDirectory, IntegrationsConfig integrationsConfig, boolean streamingEnabled, @@ -159,6 +161,7 @@ private SplitClientConfig(String endpoint, _maxStringLength = maxStringLength; _destroyOnShutDown = destroyOnShutDown; _splitFile = splitFile; + _splitFileResource = splitFileResource; _segmentDirectory = segmentDirectory; _integrationsConfig = integrationsConfig; _streamingEnabled = streamingEnabled; @@ -300,6 +303,9 @@ public boolean destroyOnShutDown() { public String splitFile() { return _splitFile; } + public String splitFileResource() { + return _splitFileResource; + } public String segmentDirectory() { return _segmentDirectory; @@ -394,6 +400,7 @@ public static final class Builder { private int _maxStringLength = 250; private boolean _destroyOnShutDown = true; private String _splitFile = null; + private String _splitFileResource = null; private String _segmentDirectory = null; private IntegrationsConfig _integrationsConfig = null; private boolean _streamingEnabled = true; @@ -748,6 +755,18 @@ public Builder splitFile(String splitFile) { return this; } + /** + * Set the location of the new yaml file for localhost when is located in resources, and it will be use inside a jar. + * This setting is optional. + * + * @param splitFileResource location + * @return this builder + */ + public Builder splitFileResource(String splitFileResource) { + _splitFileResource = splitFileResource; + return this; + } + /** * Set the location of the directory where are the segment json files for localhost mode. * This setting is optional. @@ -1005,6 +1024,7 @@ public SplitClientConfig build() { _maxStringLength, _destroyOnShutDown, _splitFile, + _splitFileResource, _segmentDirectory, _integrationsConfig, _streamingEnabled, diff --git a/client/src/main/java/io/split/client/SplitFactoryImpl.java b/client/src/main/java/io/split/client/SplitFactoryImpl.java index 3c46ff7fd..5f4eda15d 100644 --- a/client/src/main/java/io/split/client/SplitFactoryImpl.java +++ b/client/src/main/java/io/split/client/SplitFactoryImpl.java @@ -368,14 +368,25 @@ protected SplitFactoryImpl(SplitClientConfig config) { // SplitFetcher SplitChangeFetcher splitChangeFetcher; String splitFile = config.splitFile(); - if (splitFile != null && splitFile.toLowerCase().endsWith(".json")){ - splitChangeFetcher = new JsonLocalhostSplitChangeFetcher(config.splitFile()); - } else if (splitFile != null && !splitFile.isEmpty() && (splitFile.endsWith(".yaml") || splitFile.endsWith(".yml"))) { - splitChangeFetcher = new YamlLocalhostSplitChangeFetcher(splitFile); + if (splitFile != null){ + if (splitFile.toLowerCase().endsWith(".json")){ + splitChangeFetcher= null; + //splitChangeFetcher = new JsonFileLocalhostSplitChangeFetcher(config.splitFile()); + } else if (!splitFile.isEmpty() && (splitFile.endsWith(".yaml") || splitFile.endsWith(".yml"))) { + splitChangeFetcher = new YamlFileLocalhostSplitChangeFetcher(config.splitFile()); + } else { + splitChangeFetcher = new LegacyLocalhostSplitChangeFetcher(config.splitFile()); + } } else { - splitChangeFetcher = new LegacyLocalhostSplitChangeFetcher(config.splitFile()); + String splitFileResource = config.splitFileResource(); + if (splitFileResource != null && (splitFileResource.endsWith(".yaml") || splitFileResource.endsWith(".yml"))) { + splitChangeFetcher = new YamlResourceLocalhostSplitChangeFetcher(splitFileResource); + } else { + splitChangeFetcher = new YamlResourceLocalhostSplitChangeFetcher(splitFileResource); + } } + SplitParser splitParser = new SplitParser(); _splitFetcher = new SplitFetcherImp(splitChangeFetcher, splitParser, splitCache, _telemetryStorageProducer); diff --git a/client/src/main/java/io/split/client/YamlFileLocalhostSplitChangeFetcher.java b/client/src/main/java/io/split/client/YamlFileLocalhostSplitChangeFetcher.java new file mode 100644 index 000000000..825c9ebc9 --- /dev/null +++ b/client/src/main/java/io/split/client/YamlFileLocalhostSplitChangeFetcher.java @@ -0,0 +1,44 @@ +package io.split.client; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.yaml.snakeyaml.Yaml; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.util.List; +import java.util.Map; + +public class YamlFileLocalhostSplitChangeFetcher extends YamlLocalhostSplitChangeFetcher{ + + private static final Logger _log = LoggerFactory.getLogger(YamlFileLocalhostSplitChangeFetcher.class); + private final File _splitFile; + + public YamlFileLocalhostSplitChangeFetcher(String fileName) { + _splitFile = new File(fileName); + } + + @Override + public List>> readFile() { + try { + Yaml yaml = new Yaml(); + return yaml.load(new FileReader(_splitFile)); + } catch (FileNotFoundException f) { + _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.getPath(), _splitFile.getPath()), f); + throw new IllegalStateException("Problem fetching splitChanges: " + f.getMessage(), f); + } catch (Exception e) { + _log.warn(String.format("Problem to fetch split change using the file %s", + _splitFile.getPath()), e); + throw new IllegalStateException("Problem fetching splitChanges: " + e.getMessage(), e); + } + } + + @Override + public String getFilePath() { + return _splitFile.getPath(); + } +} \ 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 abde47d73..bbeecb0d6 100644 --- a/client/src/main/java/io/split/client/YamlLocalhostSplitChangeFetcher.java +++ b/client/src/main/java/io/split/client/YamlLocalhostSplitChangeFetcher.java @@ -10,11 +10,7 @@ import io.split.engine.experiments.SplitChangeFetcher; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.yaml.snakeyaml.Yaml; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -23,21 +19,18 @@ import static io.split.client.utils.LocalhostSanitizer.createCondition; +public abstract class YamlLocalhostSplitChangeFetcher implements SplitChangeFetcher { + private static final Logger _log = LoggerFactory.getLogger(YamlLocalhostSplitChangeFetcher.class); -public class YamlLocalhostSplitChangeFetcher implements SplitChangeFetcher { + public abstract List>> readFile(); - private static final Logger _log = LoggerFactory.getLogger(YamlLocalhostSplitChangeFetcher.class); - private final File _splitFile; + public abstract String getFilePath(); - public YamlLocalhostSplitChangeFetcher(String filePath) { - _splitFile = new File(filePath); - } @Override public SplitChange fetch(long since, FetchOptions options) { try { - Yaml yaml = new Yaml(); - List>> yamlSplits = yaml.load(new FileReader(_splitFile)); + List>> yamlSplits = readFile(); SplitChange splitChange = new SplitChange(); splitChange.splits = new ArrayList<>(); for(Map> aSplit : yamlSplits) { @@ -76,17 +69,10 @@ public SplitChange fetch(long since, FetchOptions options) { splitChange.till = since; splitChange.since = since; return splitChange; - } catch (FileNotFoundException f) { - _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.getPath(), _splitFile.getPath()), f); - throw new IllegalStateException("Problem fetching splitChanges: " + f.getMessage(), f); } catch (Exception e) { _log.warn(String.format("Problem to fetch split change using the file %s", - _splitFile.getPath()), e); + getFilePath()), e); throw new IllegalStateException("Problem fetching splitChanges: " + e.getMessage(), e); } } -} \ No newline at end of file +} diff --git a/client/src/main/java/io/split/client/YamlResourceLocalhostSplitChangeFetcher.java b/client/src/main/java/io/split/client/YamlResourceLocalhostSplitChangeFetcher.java new file mode 100644 index 000000000..9c4319a9d --- /dev/null +++ b/client/src/main/java/io/split/client/YamlResourceLocalhostSplitChangeFetcher.java @@ -0,0 +1,34 @@ +package io.split.client; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.yaml.snakeyaml.Yaml; +import java.io.InputStream; +import java.util.List; +import java.util.Map; + +public class YamlResourceLocalhostSplitChangeFetcher extends YamlLocalhostSplitChangeFetcher { + private static final Logger _log = LoggerFactory.getLogger(YamlFileLocalhostSplitChangeFetcher.class); + private final String _fileName; + + public YamlResourceLocalhostSplitChangeFetcher(String fileName) { + _fileName = fileName; + } + @Override + public List>> readFile() { + try { + Yaml yaml = new Yaml(); + InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(_fileName); + return yaml.load(inputStream); + } catch (Exception e) { + _log.warn(String.format("Problem to fetch split change using the file %s", + _fileName), e); + throw new IllegalStateException("Problem fetching splitChanges: " + e.getMessage(), e); + } + } + + @Override + public String getFilePath() { + return _fileName; + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/client/JsonLocalhostSplitChangeFetcherTest.java b/client/src/test/java/io/split/client/JsonLocalhostSplitChangeFetcherTest.java index 164da7740..49b40b0b8 100644 --- a/client/src/test/java/io/split/client/JsonLocalhostSplitChangeFetcherTest.java +++ b/client/src/test/java/io/split/client/JsonLocalhostSplitChangeFetcherTest.java @@ -28,7 +28,7 @@ public class JsonLocalhostSplitChangeFetcherTest { 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(){ - JsonLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonLocalhostSplitChangeFetcher("src/test/resources/split_init.json"); + JsonFileLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonFileLocalhostSplitChangeFetcher("src/test/resources/split_init.json"); FetchOptions fetchOptions = Mockito.mock(FetchOptions.class); SplitChange splitChange = localhostSplitChangeFetcher.fetch(-1L, fetchOptions); @@ -41,7 +41,7 @@ public void testParseSplitChange(){ @Test public void testSinceAndTillSanitization(){ - JsonLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonLocalhostSplitChangeFetcher("src/test/resources/sanitizer/splitChangeTillSanitization.json"); + JsonFileLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonFileLocalhostSplitChangeFetcher("src/test/resources/sanitizer/splitChangeTillSanitization.json"); FetchOptions fetchOptions = Mockito.mock(FetchOptions.class); SplitChange splitChange = localhostSplitChangeFetcher.fetch(-1L, fetchOptions); @@ -52,7 +52,7 @@ public void testSinceAndTillSanitization(){ @Test public void testSplitChangeWithoutSplits(){ - JsonLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonLocalhostSplitChangeFetcher("src/test/resources/sanitizer/splitChangeWithoutSplits.json"); + JsonFileLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonFileLocalhostSplitChangeFetcher("src/test/resources/sanitizer/splitChangeWithoutSplits.json"); FetchOptions fetchOptions = Mockito.mock(FetchOptions.class); SplitChange splitChange = localhostSplitChangeFetcher.fetch(-1L, fetchOptions); @@ -62,7 +62,7 @@ public void testSplitChangeWithoutSplits(){ @Test public void testSplitChangeSplitsToSanitize(){ - JsonLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonLocalhostSplitChangeFetcher("src/test/resources/sanitizer/splitChangeSplitsToSanitize.json"); + JsonFileLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonFileLocalhostSplitChangeFetcher("src/test/resources/sanitizer/splitChangeSplitsToSanitize.json"); FetchOptions fetchOptions = Mockito.mock(FetchOptions.class); SplitChange splitChange = localhostSplitChangeFetcher.fetch(-1L, fetchOptions); @@ -77,7 +77,7 @@ public void testSplitChangeSplitsToSanitize(){ @Test public void testSplitChangeSplitsToSanitizeMatchersNull(){ - JsonLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonLocalhostSplitChangeFetcher("src/test/resources/sanitizer/splitChangerMatchersNull.json"); + JsonFileLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonFileLocalhostSplitChangeFetcher("src/test/resources/sanitizer/splitChangerMatchersNull.json"); FetchOptions fetchOptions = Mockito.mock(FetchOptions.class); SplitChange splitChange = localhostSplitChangeFetcher.fetch(-1L, fetchOptions); @@ -98,7 +98,7 @@ public void testSplitChangeSplitsDifferentScenarios() throws IOException { byte[] test = TEST_0.getBytes(); com.google.common.io.Files.write(test, file); - JsonLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonLocalhostSplitChangeFetcher(file.getAbsolutePath()); + JsonFileLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonFileLocalhostSplitChangeFetcher(file.getAbsolutePath()); FetchOptions fetchOptions = Mockito.mock(FetchOptions.class); // 0) The CN from storage is -1, till and since are -1, and sha doesn't exist in the hash. It's going to return a split change with updates. diff --git a/client/src/test/java/io/split/client/JsonResourceLocalhostSplitChangeFetcherTest.java b/client/src/test/java/io/split/client/JsonResourceLocalhostSplitChangeFetcherTest.java new file mode 100644 index 000000000..e7ad06df2 --- /dev/null +++ b/client/src/test/java/io/split/client/JsonResourceLocalhostSplitChangeFetcherTest.java @@ -0,0 +1,6 @@ +package io.split.client; + + +public class JsonResourceLocalhostSplitChangeFetcherTest { + +} \ 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 4f89012ef..bdf8afdd4 100644 --- a/client/src/test/java/io/split/client/YamlLocalhostSplitChangeFetcherTest.java +++ b/client/src/test/java/io/split/client/YamlLocalhostSplitChangeFetcherTest.java @@ -52,13 +52,13 @@ public void testParseSplitChange() throws IOException { split2_user_a.put("split_2", split2_user_a_data); allSplits.add(split2_user_a); - Yaml yaml = new Yaml(); StringWriter writer = new StringWriter(); yaml.dump(allSplits, writer); LocalhostUtils.writeFile(file, writer); - YamlLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new YamlLocalhostSplitChangeFetcher(file.getAbsolutePath()); + YamlLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new YamlFileLocalhostSplitChangeFetcher( + file.getAbsolutePath()); FetchOptions fetchOptions = Mockito.mock(FetchOptions.class); SplitChange splitChange = localhostSplitChangeFetcher.fetch(-1L, fetchOptions); diff --git a/client/src/test/java/io/split/client/YamlResourceLocalhostSplitChangeFetcherTest.java b/client/src/test/java/io/split/client/YamlResourceLocalhostSplitChangeFetcherTest.java new file mode 100644 index 000000000..879752857 --- /dev/null +++ b/client/src/test/java/io/split/client/YamlResourceLocalhostSplitChangeFetcherTest.java @@ -0,0 +1,27 @@ +package io.split.client; + +import io.split.client.dtos.Split; +import io.split.client.dtos.SplitChange; +import io.split.engine.common.FetchOptions; +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mockito; + +public class YamlResourceLocalhostSplitChangeFetcherTest { + + @Test + public void testParseSplitChange() { + YamlLocalhostSplitChangeFetcher yamlLocalhostSplitChangeFetcher = new YamlResourceLocalhostSplitChangeFetcher("split.yaml"); + + FetchOptions fetchOptions = Mockito.mock(FetchOptions.class); + SplitChange splitChange = yamlLocalhostSplitChangeFetcher.fetch(-1L, fetchOptions); + + Assert.assertEquals(4, splitChange.splits.size()); + Assert.assertEquals(-1, splitChange.since); + Assert.assertEquals(-1, splitChange.till); + + for (Split split: splitChange.splits) { + Assert.assertEquals("control", split.defaultTreatment); + } + } +} \ No newline at end of file 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 3d303845e..922ba42cf 100644 --- a/client/src/test/java/io/split/engine/common/LocalhostSynchronizerTest.java +++ b/client/src/test/java/io/split/engine/common/LocalhostSynchronizerTest.java @@ -1,7 +1,7 @@ package io.split.engine.common; import io.split.client.LocalhostSegmentChangeFetcher; -import io.split.client.JsonLocalhostSplitChangeFetcher; +import io.split.client.JsonFileLocalhostSplitChangeFetcher; import io.split.engine.experiments.SplitChangeFetcher; import io.split.engine.experiments.SplitFetcher; import io.split.engine.experiments.SplitFetcherImp; @@ -28,7 +28,7 @@ public class LocalhostSynchronizerTest { public void testSyncAll() { SplitCache splitCacheProducer = new InMemoryCacheImp(); - SplitChangeFetcher splitChangeFetcher = new JsonLocalhostSplitChangeFetcher("src/test/resources/split_init.json"); + SplitChangeFetcher splitChangeFetcher = new JsonFileLocalhostSplitChangeFetcher("src/test/resources/split_init.json"); SplitParser splitParser = new SplitParser(); SplitFetcher splitFetcher = new SplitFetcherImp(splitChangeFetcher, splitParser, splitCacheProducer, TELEMETRY_STORAGE_NOOP); @@ -50,7 +50,7 @@ public void testSyncAll() { public void testPeriodicFetching() throws InterruptedException { SplitCache splitCacheProducer = new InMemoryCacheImp(); - SplitChangeFetcher splitChangeFetcher = Mockito.mock(JsonLocalhostSplitChangeFetcher.class); + SplitChangeFetcher splitChangeFetcher = Mockito.mock(JsonFileLocalhostSplitChangeFetcher.class); SplitParser splitParser = new SplitParser(); SplitFetcher splitFetcher = new SplitFetcherImp(splitChangeFetcher, splitParser, splitCacheProducer, TELEMETRY_STORAGE_NOOP); diff --git a/client/src/test/java/io/split/engine/experiments/SplitFetcherImpTest.java b/client/src/test/java/io/split/engine/experiments/SplitFetcherImpTest.java index d838cc0de..d6dac695c 100644 --- a/client/src/test/java/io/split/engine/experiments/SplitFetcherImpTest.java +++ b/client/src/test/java/io/split/engine/experiments/SplitFetcherImpTest.java @@ -1,6 +1,6 @@ package io.split.engine.experiments; -import io.split.client.JsonLocalhostSplitChangeFetcher; +import io.split.client.JsonFileLocalhostSplitChangeFetcher; import io.split.engine.common.FetchOptions; import io.split.storages.SplitCacheProducer; import io.split.storages.memory.InMemoryCacheImp; @@ -18,7 +18,7 @@ public class SplitFetcherImpTest { public void testLocalHost(){ SplitCacheProducer splitCacheProducer = new InMemoryCacheImp(); - SplitChangeFetcher splitChangeFetcher = new JsonLocalhostSplitChangeFetcher("src/test/resources/split_init.json"); + SplitChangeFetcher splitChangeFetcher = new JsonFileLocalhostSplitChangeFetcher("src/test/resources/split_init.json"); SplitParser splitParser = new SplitParser(); FetchOptions fetchOptions = new FetchOptions.Builder().build(); SplitFetcher splitFetcher = new SplitFetcherImp(splitChangeFetcher, splitParser, splitCacheProducer, TELEMETRY_STORAGE_NOOP); 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..3f64c952b 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,6 @@ package io.split.engine.experiments; -import io.split.client.JsonLocalhostSplitChangeFetcher; +import io.split.client.JsonFileLocalhostSplitChangeFetcher; import io.split.engine.common.FetchOptions; import io.split.storages.SplitCacheProducer; import io.split.storages.memory.InMemoryCacheImp; @@ -17,7 +17,7 @@ public class SplitSynchronizationTaskTest { public void testLocalhost() throws InterruptedException { SplitCacheProducer splitCacheProducer = new InMemoryCacheImp(); - SplitChangeFetcher splitChangeFetcher = Mockito.mock(JsonLocalhostSplitChangeFetcher.class); + SplitChangeFetcher splitChangeFetcher = Mockito.mock(JsonFileLocalhostSplitChangeFetcher.class); SplitParser splitParser = new SplitParser(); FetchOptions fetchOptions = new FetchOptions.Builder().build(); SplitFetcher splitFetcher = new SplitFetcherImp(splitChangeFetcher, splitParser, splitCacheProducer, TELEMETRY_STORAGE_NOOP); diff --git a/client/src/test/java/io/split/engine/segments/SegmentSynchronizationTaskImpTest.java b/client/src/test/java/io/split/engine/segments/SegmentSynchronizationTaskImpTest.java index f9e1e354d..df19e4553 100644 --- a/client/src/test/java/io/split/engine/segments/SegmentSynchronizationTaskImpTest.java +++ b/client/src/test/java/io/split/engine/segments/SegmentSynchronizationTaskImpTest.java @@ -2,7 +2,7 @@ import com.google.common.collect.Maps; import io.split.client.LocalhostSegmentChangeFetcher; -import io.split.client.JsonLocalhostSplitChangeFetcher; +import io.split.client.JsonFileLocalhostSplitChangeFetcher; import io.split.engine.common.FetchOptions; import io.split.engine.experiments.SplitChangeFetcher; import io.split.engine.experiments.SplitFetcher; @@ -149,7 +149,7 @@ public void testLocalhostSegmentChangeFetcher() throws InterruptedException { SplitCache splitCacheProducer = new InMemoryCacheImp(); - SplitChangeFetcher splitChangeFetcher = new JsonLocalhostSplitChangeFetcher("src/test/resources/split_init.json"); + SplitChangeFetcher splitChangeFetcher = new JsonFileLocalhostSplitChangeFetcher("src/test/resources/split_init.json"); SplitParser splitParser = new SplitParser(); FetchOptions fetchOptions = new FetchOptions.Builder().build(); SplitFetcher splitFetcher = new SplitFetcherImp(splitChangeFetcher, splitParser, splitCacheProducer, TELEMETRY_STORAGE_NOOP); From fbec0bcf524817d345cdff8242c8bf7b3329bb47 Mon Sep 17 00:00:00 2001 From: Nadia Mayor Date: Wed, 16 Aug 2023 08:44:16 -0300 Subject: [PATCH 2/2] [SDKS-7373] Remove abstract class --- .../JsonFileLocalhostSplitChangeFetcher.java | 45 ------------------- .../JsonLocalhostSplitChangeFetcher.java | 31 ++++++++++--- ...onResourceLocalhostSplitChangeFetcher.java | 38 ---------------- .../JsonLocalhostSplitChangeFetcherTest.java | 31 ++++++++----- ...sourceLocalhostSplitChangeFetcherTest.java | 6 --- .../common/LocalhostSynchronizerTest.java | 14 ++++-- .../experiments/SplitFetcherImpTest.java | 13 ++++-- .../SplitSynchronizationTaskTest.java | 4 +- .../SegmentSynchronizationTaskImpTest.java | 11 +++-- 9 files changed, 76 insertions(+), 117 deletions(-) delete mode 100644 client/src/main/java/io/split/client/JsonFileLocalhostSplitChangeFetcher.java delete mode 100644 client/src/main/java/io/split/client/JsonResourceLocalhostSplitChangeFetcher.java delete mode 100644 client/src/test/java/io/split/client/JsonResourceLocalhostSplitChangeFetcherTest.java diff --git a/client/src/main/java/io/split/client/JsonFileLocalhostSplitChangeFetcher.java b/client/src/main/java/io/split/client/JsonFileLocalhostSplitChangeFetcher.java deleted file mode 100644 index ad5e2d9a1..000000000 --- a/client/src/main/java/io/split/client/JsonFileLocalhostSplitChangeFetcher.java +++ /dev/null @@ -1,45 +0,0 @@ -package io.split.client; - -import com.google.gson.stream.JsonReader; -import io.split.client.dtos.SplitChange; -import io.split.client.utils.Json; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.util.List; -import java.util.Map; - -public class JsonFileLocalhostSplitChangeFetcher extends JsonLocalhostSplitChangeFetcher { - - private static final Logger _log = LoggerFactory.getLogger(JsonFileLocalhostSplitChangeFetcher.class); - private final File _file; - - - public JsonFileLocalhostSplitChangeFetcher(String filePath) { - _file = new File(filePath); - super.lastHash = new byte[0]; - } - - @Override - public JsonReader readFile() { - try { - return new JsonReader(new FileReader("_file")); - } catch (FileNotFoundException f) { - _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", - getFilePath(), getFilePath()), f); - throw new IllegalStateException("Problem fetching splitChanges: " + f.getMessage(), f); - } - } - - @Override - public String getFilePath() { - return _file.getPath(); - } -} \ No newline at end of file diff --git a/client/src/main/java/io/split/client/JsonLocalhostSplitChangeFetcher.java b/client/src/main/java/io/split/client/JsonLocalhostSplitChangeFetcher.java index 6fa9b43d6..cfb0d5113 100644 --- a/client/src/main/java/io/split/client/JsonLocalhostSplitChangeFetcher.java +++ b/client/src/main/java/io/split/client/JsonLocalhostSplitChangeFetcher.java @@ -8,16 +8,25 @@ import io.split.engine.experiments.SplitChangeFetcher; import org.slf4j.Logger; import org.slf4j.LoggerFactory; + +import java.io.BufferedReader; +import java.io.InputStream; +import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Arrays; -public abstract class JsonLocalhostSplitChangeFetcher implements SplitChangeFetcher { +public class JsonLocalhostSplitChangeFetcher implements SplitChangeFetcher { private static final Logger _log = LoggerFactory.getLogger(JsonLocalhostSplitChangeFetcher.class); - byte [] lastHash; - public abstract JsonReader readFile(); - public abstract String getFilePath(); + + private final InputStream _inputStream; + private byte [] lastHash; + + public JsonLocalhostSplitChangeFetcher(InputStream inputStream) { + _inputStream = inputStream; + lastHash = new byte[0]; + } @Override public SplitChange fetch(long since, FetchOptions options) { @@ -27,7 +36,7 @@ public SplitChange fetch(long since, FetchOptions options) { return processSplitChange(splitChange, since); } catch (Exception e) { _log.warn(String.format("Problem to fetch split change using the file %s", - getFilePath()), e); + _inputStream.toString()), e); throw new IllegalStateException("Problem fetching splitChanges: " + e.getMessage(), e); } } @@ -53,4 +62,16 @@ private SplitChange processSplitChange(SplitChange splitChange, long changeNumbe splitChangeToProcess.since = changeNumber; return splitChangeToProcess; } + + public JsonReader readFile() { + try { + BufferedReader streamReader = new BufferedReader(new InputStreamReader(_inputStream, "UTF-8")); + JsonReader jsonReader = new JsonReader(streamReader); + return jsonReader; + } catch (Exception e){ + _log.warn(String.format("Problem to fetch split change using the file %s", + _inputStream.toString()), e); + throw new IllegalStateException("Problem fetching splitChanges: " + e.getMessage(), e); + } + } } \ No newline at end of file diff --git a/client/src/main/java/io/split/client/JsonResourceLocalhostSplitChangeFetcher.java b/client/src/main/java/io/split/client/JsonResourceLocalhostSplitChangeFetcher.java deleted file mode 100644 index bd376b159..000000000 --- a/client/src/main/java/io/split/client/JsonResourceLocalhostSplitChangeFetcher.java +++ /dev/null @@ -1,38 +0,0 @@ -package io.split.client; - -import com.google.gson.stream.JsonReader; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.BufferedReader; -import java.io.InputStream; -import java.io.InputStreamReader; - -public class JsonResourceLocalhostSplitChangeFetcher extends JsonLocalhostSplitChangeFetcher { - private static final Logger _log = LoggerFactory.getLogger(JsonResourceLocalhostSplitChangeFetcher.class); - private final String _fileName; - - public JsonResourceLocalhostSplitChangeFetcher(String fileName) { - _fileName = fileName; - super.lastHash = new byte[0]; - } - - @Override - public JsonReader readFile() { - try { - InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(_fileName); - BufferedReader streamReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")); - JsonReader jsonReader = new JsonReader(streamReader); - return jsonReader; - } catch (Exception e){ - _log.warn(String.format("Problem to fetch split change using the file %s", - _fileName), e); - throw new IllegalStateException("Problem fetching splitChanges: " + e.getMessage(), e); - } - } - - @Override - public String getFilePath() { - return _fileName; - } -} \ No newline at end of file diff --git a/client/src/test/java/io/split/client/JsonLocalhostSplitChangeFetcherTest.java b/client/src/test/java/io/split/client/JsonLocalhostSplitChangeFetcherTest.java index 49b40b0b8..1e2cf0795 100644 --- a/client/src/test/java/io/split/client/JsonLocalhostSplitChangeFetcherTest.java +++ b/client/src/test/java/io/split/client/JsonLocalhostSplitChangeFetcherTest.java @@ -12,7 +12,10 @@ import org.mockito.Mockito; import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.IOException; +import java.io.InputStream; import java.util.List; import java.util.Optional; @@ -27,8 +30,9 @@ 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(){ - JsonFileLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonFileLocalhostSplitChangeFetcher("src/test/resources/split_init.json"); + public void testParseSplitChange() throws FileNotFoundException { + InputStream inputStream = new FileInputStream(new File("src/test/resources/split_init.json")); + JsonLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonLocalhostSplitChangeFetcher(inputStream); FetchOptions fetchOptions = Mockito.mock(FetchOptions.class); SplitChange splitChange = localhostSplitChangeFetcher.fetch(-1L, fetchOptions); @@ -40,8 +44,9 @@ public void testParseSplitChange(){ } @Test - public void testSinceAndTillSanitization(){ - JsonFileLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonFileLocalhostSplitChangeFetcher("src/test/resources/sanitizer/splitChangeTillSanitization.json"); + public void testSinceAndTillSanitization() throws FileNotFoundException { + InputStream inputStream = new FileInputStream(new File("src/test/resources/sanitizer/splitChangeTillSanitization.json")); + JsonLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonLocalhostSplitChangeFetcher(inputStream); FetchOptions fetchOptions = Mockito.mock(FetchOptions.class); SplitChange splitChange = localhostSplitChangeFetcher.fetch(-1L, fetchOptions); @@ -51,8 +56,9 @@ public void testSinceAndTillSanitization(){ } @Test - public void testSplitChangeWithoutSplits(){ - JsonFileLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonFileLocalhostSplitChangeFetcher("src/test/resources/sanitizer/splitChangeWithoutSplits.json"); + public void testSplitChangeWithoutSplits() throws FileNotFoundException { + InputStream inputStream = new FileInputStream(new File("src/test/resources/sanitizer/splitChangeWithoutSplits.json")); + JsonLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonLocalhostSplitChangeFetcher(inputStream); FetchOptions fetchOptions = Mockito.mock(FetchOptions.class); SplitChange splitChange = localhostSplitChangeFetcher.fetch(-1L, fetchOptions); @@ -61,8 +67,9 @@ public void testSplitChangeWithoutSplits(){ } @Test - public void testSplitChangeSplitsToSanitize(){ - JsonFileLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonFileLocalhostSplitChangeFetcher("src/test/resources/sanitizer/splitChangeSplitsToSanitize.json"); + public void testSplitChangeSplitsToSanitize() throws FileNotFoundException { + InputStream inputStream = new FileInputStream(new File("src/test/resources/sanitizer/splitChangeSplitsToSanitize.json")); + JsonLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonLocalhostSplitChangeFetcher(inputStream); FetchOptions fetchOptions = Mockito.mock(FetchOptions.class); SplitChange splitChange = localhostSplitChangeFetcher.fetch(-1L, fetchOptions); @@ -76,8 +83,9 @@ public void testSplitChangeSplitsToSanitize(){ } @Test - public void testSplitChangeSplitsToSanitizeMatchersNull(){ - JsonFileLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonFileLocalhostSplitChangeFetcher("src/test/resources/sanitizer/splitChangerMatchersNull.json"); + public void testSplitChangeSplitsToSanitizeMatchersNull() throws FileNotFoundException { + InputStream inputStream = new FileInputStream(new File("src/test/resources/sanitizer/splitChangerMatchersNull.json")); + JsonLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonLocalhostSplitChangeFetcher(inputStream); FetchOptions fetchOptions = Mockito.mock(FetchOptions.class); SplitChange splitChange = localhostSplitChangeFetcher.fetch(-1L, fetchOptions); @@ -98,7 +106,8 @@ public void testSplitChangeSplitsDifferentScenarios() throws IOException { byte[] test = TEST_0.getBytes(); com.google.common.io.Files.write(test, file); - JsonFileLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonFileLocalhostSplitChangeFetcher(file.getAbsolutePath()); + InputStream inputStream = new FileInputStream(file); + JsonLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonLocalhostSplitChangeFetcher(inputStream); FetchOptions fetchOptions = Mockito.mock(FetchOptions.class); // 0) The CN from storage is -1, till and since are -1, and sha doesn't exist in the hash. It's going to return a split change with updates. diff --git a/client/src/test/java/io/split/client/JsonResourceLocalhostSplitChangeFetcherTest.java b/client/src/test/java/io/split/client/JsonResourceLocalhostSplitChangeFetcherTest.java deleted file mode 100644 index e7ad06df2..000000000 --- a/client/src/test/java/io/split/client/JsonResourceLocalhostSplitChangeFetcherTest.java +++ /dev/null @@ -1,6 +0,0 @@ -package io.split.client; - - -public class JsonResourceLocalhostSplitChangeFetcherTest { - -} \ No newline at end of file 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 922ba42cf..60ec1da65 100644 --- a/client/src/test/java/io/split/engine/common/LocalhostSynchronizerTest.java +++ b/client/src/test/java/io/split/engine/common/LocalhostSynchronizerTest.java @@ -1,7 +1,7 @@ package io.split.engine.common; +import io.split.client.JsonLocalhostSplitChangeFetcher; import io.split.client.LocalhostSegmentChangeFetcher; -import io.split.client.JsonFileLocalhostSplitChangeFetcher; import io.split.engine.experiments.SplitChangeFetcher; import io.split.engine.experiments.SplitFetcher; import io.split.engine.experiments.SplitFetcherImp; @@ -20,15 +20,21 @@ import org.junit.Test; import org.mockito.Mockito; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.InputStream; + public class LocalhostSynchronizerTest { private static final TelemetryStorage TELEMETRY_STORAGE_NOOP = Mockito.mock(NoopTelemetryStorage.class); @Test - public void testSyncAll() { + public void testSyncAll() throws FileNotFoundException { SplitCache splitCacheProducer = new InMemoryCacheImp(); - SplitChangeFetcher splitChangeFetcher = new JsonFileLocalhostSplitChangeFetcher("src/test/resources/split_init.json"); + InputStream inputStream = new FileInputStream(new File("src/test/resources/split_init.json")); + SplitChangeFetcher splitChangeFetcher = new JsonLocalhostSplitChangeFetcher(inputStream); SplitParser splitParser = new SplitParser(); SplitFetcher splitFetcher = new SplitFetcherImp(splitChangeFetcher, splitParser, splitCacheProducer, TELEMETRY_STORAGE_NOOP); @@ -50,7 +56,7 @@ public void testSyncAll() { public void testPeriodicFetching() throws InterruptedException { SplitCache splitCacheProducer = new InMemoryCacheImp(); - SplitChangeFetcher splitChangeFetcher = Mockito.mock(JsonFileLocalhostSplitChangeFetcher.class); + SplitChangeFetcher splitChangeFetcher = Mockito.mock(JsonLocalhostSplitChangeFetcher.class); SplitParser splitParser = new SplitParser(); SplitFetcher splitFetcher = new SplitFetcherImp(splitChangeFetcher, splitParser, splitCacheProducer, TELEMETRY_STORAGE_NOOP); diff --git a/client/src/test/java/io/split/engine/experiments/SplitFetcherImpTest.java b/client/src/test/java/io/split/engine/experiments/SplitFetcherImpTest.java index d6dac695c..84a3d8179 100644 --- a/client/src/test/java/io/split/engine/experiments/SplitFetcherImpTest.java +++ b/client/src/test/java/io/split/engine/experiments/SplitFetcherImpTest.java @@ -1,6 +1,6 @@ package io.split.engine.experiments; -import io.split.client.JsonFileLocalhostSplitChangeFetcher; +import io.split.client.JsonLocalhostSplitChangeFetcher; import io.split.engine.common.FetchOptions; import io.split.storages.SplitCacheProducer; import io.split.storages.memory.InMemoryCacheImp; @@ -10,15 +10,22 @@ import org.junit.Test; import org.mockito.Mockito; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.InputStream; + public class SplitFetcherImpTest { private static final TelemetryStorage TELEMETRY_STORAGE_NOOP = Mockito.mock(NoopTelemetryStorage.class); @Test - public void testLocalHost(){ + public void testLocalHost() throws FileNotFoundException { SplitCacheProducer splitCacheProducer = new InMemoryCacheImp(); - SplitChangeFetcher splitChangeFetcher = new JsonFileLocalhostSplitChangeFetcher("src/test/resources/split_init.json"); + InputStream inputStream = new FileInputStream(new File("src/test/resources/split_init.json")); + SplitChangeFetcher splitChangeFetcher = new JsonLocalhostSplitChangeFetcher(inputStream); + SplitParser splitParser = new SplitParser(); FetchOptions fetchOptions = new FetchOptions.Builder().build(); SplitFetcher splitFetcher = new SplitFetcherImp(splitChangeFetcher, splitParser, splitCacheProducer, TELEMETRY_STORAGE_NOOP); 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 3f64c952b..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,6 +1,6 @@ package io.split.engine.experiments; -import io.split.client.JsonFileLocalhostSplitChangeFetcher; +import io.split.client.JsonLocalhostSplitChangeFetcher; import io.split.engine.common.FetchOptions; import io.split.storages.SplitCacheProducer; import io.split.storages.memory.InMemoryCacheImp; @@ -17,7 +17,7 @@ public class SplitSynchronizationTaskTest { public void testLocalhost() throws InterruptedException { SplitCacheProducer splitCacheProducer = new InMemoryCacheImp(); - SplitChangeFetcher splitChangeFetcher = Mockito.mock(JsonFileLocalhostSplitChangeFetcher.class); + SplitChangeFetcher splitChangeFetcher = Mockito.mock(JsonLocalhostSplitChangeFetcher.class); SplitParser splitParser = new SplitParser(); FetchOptions fetchOptions = new FetchOptions.Builder().build(); SplitFetcher splitFetcher = new SplitFetcherImp(splitChangeFetcher, splitParser, splitCacheProducer, TELEMETRY_STORAGE_NOOP); diff --git a/client/src/test/java/io/split/engine/segments/SegmentSynchronizationTaskImpTest.java b/client/src/test/java/io/split/engine/segments/SegmentSynchronizationTaskImpTest.java index df19e4553..cf8f8ee5f 100644 --- a/client/src/test/java/io/split/engine/segments/SegmentSynchronizationTaskImpTest.java +++ b/client/src/test/java/io/split/engine/segments/SegmentSynchronizationTaskImpTest.java @@ -1,8 +1,8 @@ package io.split.engine.segments; import com.google.common.collect.Maps; +import io.split.client.JsonLocalhostSplitChangeFetcher; import io.split.client.LocalhostSegmentChangeFetcher; -import io.split.client.JsonFileLocalhostSplitChangeFetcher; import io.split.engine.common.FetchOptions; import io.split.engine.experiments.SplitChangeFetcher; import io.split.engine.experiments.SplitFetcher; @@ -24,6 +24,10 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.InputStream; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.List; @@ -145,11 +149,12 @@ public void testFetchAllAsynchronousAndGetTrue() throws NoSuchFieldException, Il } @Test - public void testLocalhostSegmentChangeFetcher() throws InterruptedException { + public void testLocalhostSegmentChangeFetcher() throws InterruptedException, FileNotFoundException { SplitCache splitCacheProducer = new InMemoryCacheImp(); - SplitChangeFetcher splitChangeFetcher = new JsonFileLocalhostSplitChangeFetcher("src/test/resources/split_init.json"); + InputStream inputStream = new FileInputStream(new File("src/test/resources/split_init.json")); + SplitChangeFetcher splitChangeFetcher = new JsonLocalhostSplitChangeFetcher(inputStream); SplitParser splitParser = new SplitParser(); FetchOptions fetchOptions = new FetchOptions.Builder().build(); SplitFetcher splitFetcher = new SplitFetcherImp(splitChangeFetcher, splitParser, splitCacheProducer, TELEMETRY_STORAGE_NOOP);