Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions core-api/src/main/java/com/optimizely/ab/Optimizely.java
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,13 @@ private void track(@Nonnull String eventName,
* @return a {@link ProjectConfig} instance given a json string
*/
private static ProjectConfig getProjectConfig(String datafile) throws ConfigParseException {
if (datafile == null) {
throw new ConfigParseException("Unable to parse null datafile.");
}
if (datafile.length() == 0) {
throw new ConfigParseException("Unable to parse empty datafile.");
}

return DefaultConfigParser.getInstance().parseProjectConfig(datafile);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ final class GsonConfigParser implements ConfigParser {

@Override
public ProjectConfig parseProjectConfig(@Nonnull String json) throws ConfigParseException {
if (json == null) {
throw new ConfigParseException("Unable to parse null json.");
}
if (json.length() == 0) {
throw new ConfigParseException("Unable to parse empty json.");
}
Gson gson = new GsonBuilder()
.registerTypeAdapter(ProjectConfig.class, new ProjectConfigGsonDeserializer())
.registerTypeAdapter(Audience.class, new AudienceGsonDeserializer())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,19 @@ public void withCustomClientVersion() throws Exception {
assertThat(((EventBuilderV2)optimizelyClient.eventBuilder).clientVersion, is("0.0.0"));
}

@SuppressFBWarnings(value="NP_NONNULL_PARAM_VIOLATION", justification="Testing nullness contract violation")
@Test
public void builderThrowsConfigParseExceptionForNullDatafile() throws Exception {
thrown.expect(ConfigParseException.class);
Optimizely.builder(null, mockEventHandler).build();
}

@Test
public void builderThrowsConfigParseExceptionForEmptyDatafile() throws Exception {
thrown.expect(ConfigParseException.class);
Optimizely.builder("", mockEventHandler).build();
}

@Test
public void builderThrowsConfigParseExceptionForInvalidDatafile() throws Exception {
thrown.expect(ConfigParseException.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ public class DefaultConfigParserTest {
public void createThrowException() throws Exception {
// FIXME - mdodsworth: hmmm, this isn't going to be the easiest thing to test
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.optimizely.ab.config.ProjectConfig;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
Expand Down Expand Up @@ -86,4 +87,27 @@ public void validJsonRequiredFieldMissingExceptionWrapping() throws Exception {
GsonConfigParser parser = new GsonConfigParser();
parser.parseProjectConfig("{\"valid\": \"json\"}");
}
}

/**
* Verify that empty string JSON results in a {@link ConfigParseException} being thrown.
*/
@Test
public void emptyJsonExceptionWrapping() throws Exception {
thrown.expect(ConfigParseException.class);

GsonConfigParser parser = new GsonConfigParser();
parser.parseProjectConfig("");
}

/**
* Verify that null JSON results in a {@link ConfigParseException} being thrown.
*/
@Test
@SuppressFBWarnings(value="NP_NONNULL_PARAM_VIOLATION", justification="Testing nullness contract violation")
public void nullJsonExceptionWrapping() throws Exception {
thrown.expect(ConfigParseException.class);

GsonConfigParser parser = new GsonConfigParser();
parser.parseProjectConfig(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.optimizely.ab.config.ProjectConfig;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
Expand Down Expand Up @@ -86,4 +87,27 @@ public void validJsonRequiredFieldMissingExceptionWrapping() throws Exception {
JacksonConfigParser parser = new JacksonConfigParser();
parser.parseProjectConfig("{\"valid\": \"json\"}");
}
}

/**
* Verify that empty string JSON results in a {@link ConfigParseException} being thrown.
*/
@Test
public void emptyJsonExceptionWrapping() throws Exception {
thrown.expect(ConfigParseException.class);

JacksonConfigParser parser = new JacksonConfigParser();
parser.parseProjectConfig("");
}

/**
* Verify that null JSON results in a {@link ConfigParseException} being thrown.
*/
@Test
@SuppressFBWarnings(value="NP_NONNULL_PARAM_VIOLATION", justification="Testing nullness contract violation")
public void nullJsonExceptionWrapping() throws Exception {
thrown.expect(ConfigParseException.class);

JacksonConfigParser parser = new JacksonConfigParser();
parser.parseProjectConfig(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.optimizely.ab.config.ProjectConfig;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
Expand Down Expand Up @@ -86,4 +87,27 @@ public void validJsonRequiredFieldMissingExceptionWrapping() throws Exception {
JsonConfigParser parser = new JsonConfigParser();
parser.parseProjectConfig("{\"valid\": \"json\"}");
}
}

/**
* Verify that empty string JSON results in a {@link ConfigParseException} being thrown.
*/
@Test
public void emptyJsonExceptionWrapping() throws Exception {
thrown.expect(ConfigParseException.class);

JsonConfigParser parser = new JsonConfigParser();
parser.parseProjectConfig("");
}

/**
* Verify that null JSON results in a {@link ConfigParseException} being thrown.
*/
@Test
@SuppressFBWarnings(value="NP_NONNULL_PARAM_VIOLATION", justification="Testing nullness contract violation")
public void nullJsonExceptionWrapping() throws Exception {
thrown.expect(ConfigParseException.class);

JsonConfigParser parser = new JsonConfigParser();
parser.parseProjectConfig(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.optimizely.ab.config.ProjectConfig;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
Expand Down Expand Up @@ -86,4 +87,25 @@ public void validJsonRequiredFieldMissingExceptionWrapping() throws Exception {
JsonSimpleConfigParser parser = new JsonSimpleConfigParser();
parser.parseProjectConfig("{\"valid\": \"json\"}");
}
}
/**
* Verify that empty string JSON results in a {@link ConfigParseException} being thrown.
*/
@Test
public void emptyJsonExceptionWrapping() throws Exception {
thrown.expect(ConfigParseException.class);

JsonSimpleConfigParser parser = new JsonSimpleConfigParser();
parser.parseProjectConfig("");
}
/**
* Verify that null JSON results in a {@link ConfigParseException} being thrown.
*/
@Test
@SuppressFBWarnings(value="NP_NONNULL_PARAM_VIOLATION", justification="Testing nullness contract violation")
public void nullJsonExceptionWrapping() throws Exception {
thrown.expect(ConfigParseException.class);

JsonSimpleConfigParser parser = new JsonSimpleConfigParser();
parser.parseProjectConfig(null);
}
}