Skip to content

Commit

Permalink
[SDKS-7373] Remove abstract class
Browse files Browse the repository at this point in the history
  • Loading branch information
nmayorsplit committed Aug 16, 2023
1 parent f95cd7e commit fbec0bc
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 117 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
}
Expand All @@ -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);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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.
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit fbec0bc

Please sign in to comment.