Skip to content

Commit

Permalink
Add test cases for InputStream and localhost
Browse files Browse the repository at this point in the history
  • Loading branch information
nmayorsplit committed Aug 31, 2023
1 parent e9df6a6 commit eaa1609
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.split.client;

import com.google.common.base.Charsets;
import com.google.gson.stream.JsonReader;
import io.split.client.dtos.SplitChange;
import io.split.client.exceptions.InputStreamProviderException;
Expand All @@ -14,6 +13,7 @@

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
Expand All @@ -32,15 +32,13 @@ public JsonLocalhostSplitChangeFetcher(InputStreamProvider inputStreamProvider)
@Override
public SplitChange fetch(long since, FetchOptions options) {
try {
JsonReader jsonReader = new JsonReader(new BufferedReader(new InputStreamReader(_inputStreamProvider.get(), Charsets.UTF_8)));
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) {
_log.warn(String.format("Problem to fetch split change using file named %s", i.getFileName()));
throw new IllegalStateException(String.format("Problem fetching splitChanges using file named %s: %s",
i.getFileName(), i.getMessage()), i);
} catch (Exception e) {
_log.warn(String.format("Problem to fetch split change using a file"), e);
throw new IllegalStateException("Problem fetching splitChanges: " + e.getMessage(), e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,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.InputStreamProvider;
import io.split.client.utils.LocalhostConstants;
import io.split.engine.common.FetchOptions;
Expand All @@ -22,7 +23,6 @@

import static io.split.client.utils.LocalhostSanitizer.createCondition;


public class YamlLocalhostSplitChangeFetcher implements SplitChangeFetcher {

private static final Logger _log = LoggerFactory.getLogger(YamlLocalhostSplitChangeFetcher.class);
Expand Down Expand Up @@ -75,6 +75,9 @@ public SplitChange fetch(long since, FetchOptions options) {
splitChange.till = since;
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);
} catch (Exception e) {
_log.warn(String.format("Problem to fetch split change using a file"), e);
throw new IllegalStateException("Problem fetching splitChanges: " + e.getMessage(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,13 @@ public void testSplitChangeSplitsDifferentScenarios() throws IOException {
Assert.assertEquals(2323, splitChange.till);
Assert.assertEquals(2323, splitChange.since);
}

@Test(expected = IllegalStateException.class)
public void processTestForException() {
InputStreamProvider inputStreamProvider = new FileInputStreamProvider("src/test/resources/notExist.json");
JsonLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonLocalhostSplitChangeFetcher(inputStreamProvider);
FetchOptions fetchOptions = Mockito.mock(FetchOptions.class);

SplitChange splitChange = localhostSplitChangeFetcher.fetch(-1L, fetchOptions);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.split.client;

import io.split.client.impressions.ImpressionsManager;
import io.split.client.utils.FileInputStreamProvider;
import io.split.client.utils.FileTypeEnum;
import io.split.client.utils.LocalhostPair;
import io.split.integrations.IntegrationsConfig;
Expand All @@ -21,7 +20,6 @@
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.net.URISyntaxException;
import java.util.zip.InflaterInputStream;

public class SplitFactoryImplTest extends TestCase {
public static final String API_KEY ="29013ionasdasd09u";
Expand Down Expand Up @@ -253,4 +251,4 @@ public void testGetInputStreamProviderAndFileType() throws URISyntaxException, F
localhostPair = splitFactory.getInputStreamProviderAndFileType(null, inputStream, FileTypeEnum.JSON);
Assert.assertNotNull(localhostPair);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,13 @@ public void testParseSplitChange() throws IOException {
Assert.assertEquals("control", split.defaultTreatment);
}
}

@Test(expected = IllegalStateException.class)
public void processTestForException() {
InputStreamProvider inputStreamProvider = new FileInputStreamProvider("src/test/resources/notExist.yaml");
YamlLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new YamlLocalhostSplitChangeFetcher(inputStreamProvider);
FetchOptions fetchOptions = Mockito.mock(FetchOptions.class);

SplitChange splitChange = localhostSplitChangeFetcher.fetch(-1L, fetchOptions);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.split.client.utils;

import io.split.client.exceptions.InputStreamProviderException;
import org.junit.Test;

public class FileInputStreamProviderTest {

@Test(expected = InputStreamProviderException.class)
public void processTestForException() throws InputStreamProviderException {
FileInputStreamProvider fileInputStreamProvider = new FileInputStreamProvider("src/test/resources/notExist.json");
fileInputStreamProvider.get();
}
}

0 comments on commit eaa1609

Please sign in to comment.