Skip to content

Commit

Permalink
Add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
nmayorsplit committed Aug 31, 2023
1 parent cfde3f7 commit e9df6a6
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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 Down Expand Up @@ -31,12 +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(), "UTF-8")));
JsonReader jsonReader = new JsonReader(new BufferedReader(new InputStreamReader(_inputStreamProvider.get(), Charsets.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("Problem fetching splitChanges: " + i.getMessage(), 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
10 changes: 7 additions & 3 deletions client/src/main/java/io/split/client/SplitFactoryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,9 @@ protected SplitFactoryImpl(SplitClientConfig config) {

// SplitFetcher
LocalhostPair pair = getInputStreamProviderAndFileType(config.splitFile(), config.inputStream(), config.fileType());
SplitChangeFetcher splitChangeFetcher = new LegacyLocalhostSplitChangeFetcher(config.splitFile());
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()) {
Expand All @@ -382,6 +383,9 @@ protected SplitFactoryImpl(SplitClientConfig config) {
break;
case YAML:
splitChangeFetcher = new YamlLocalhostSplitChangeFetcher(pair.getInputStreamProvider());
break;
default:
splitChangeFetcher = new LegacyLocalhostSplitChangeFetcher(config.splitFile());
}
}
SplitParser splitParser = new SplitParser();
Expand Down Expand Up @@ -652,7 +656,7 @@ private UniqueKeysTracker createUniqueKeysTracker(SplitClientConfig config){
return null;
}

private LocalhostPair getInputStreamProviderAndFileType(String splitFile, InputStream inputStream,
LocalhostPair getInputStreamProviderAndFileType(String splitFile, InputStream inputStream,
FileTypeEnum fileType) {
if (splitFile != null && inputStream != null) {
_log.warn("splitFile or inputStreamProvider should have a value, not both");
Expand Down Expand Up @@ -681,7 +685,7 @@ private LocalhostPair getInputStreamProviderAndFileType(String splitFile, InputS
"considered comments",
splitFile, splitFile), e);
}
} else if (inputStream != null) {
} else {
return new LocalhostPair(new InputStreamProviderImp(inputStream), fileType);
}
return null;
Expand Down
36 changes: 34 additions & 2 deletions client/src/test/java/io/split/client/SplitFactoryImplTest.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
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;
import io.split.storages.enums.OperationMode;
import io.split.storages.pluggable.domain.UserStorageWrapper;
import io.split.telemetry.storage.TelemetryStorage;
import io.split.telemetry.synchronizer.TelemetrySynchronizer;
import junit.framework.TestCase;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
import pluggable.CustomStorageWrapper;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
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 @@ -162,8 +170,6 @@ public void testFactoryConsumerInstantiation() throws Exception {
Mockito.verify(telemetrySynchronizer, Mockito.times(1)).synchronizeConfig(Mockito.anyObject(), Mockito.anyLong(), Mockito.anyObject(), Mockito.anyObject());
}



@Test
public void testFactoryConsumerInstantiationRetryReadiness() throws Exception {
CustomStorageWrapper customStorageWrapper = Mockito.mock(CustomStorageWrapper.class);
Expand Down Expand Up @@ -221,4 +227,30 @@ public void testFactoryConsumerDestroy() throws NoSuchFieldException, URISyntaxE
assertTrue(splitFactory.isDestroyed());
Mockito.verify(userStorageWrapper, Mockito.times(1)).disconnect();
}

@Test
public void testGetInputStreamProviderAndFileType() throws URISyntaxException, FileNotFoundException {
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
InputStream inputStream = new FileInputStream("src/test/resources/split_init.json");
Assert.assertNull(splitFactory.getInputStreamProviderAndFileType("test", inputStream, FileTypeEnum.JSON));

//inputStream is not null and fileType is null
Assert.assertNull(splitFactory.getInputStreamProviderAndFileType(null, inputStream, null));

//split file not null
LocalhostPair localhostPair = splitFactory.getInputStreamProviderAndFileType("src/test/resources/split_init.json",
null, null);
Assert.assertNotNull(localhostPair);

//inputStream is not null and filetype is not null
localhostPair = splitFactory.getInputStreamProviderAndFileType(null, inputStream, FileTypeEnum.JSON);
Assert.assertNotNull(localhostPair);
}
}

0 comments on commit e9df6a6

Please sign in to comment.