Skip to content

Commit 545286d

Browse files
authored
Throw ConfigParseException consistently in all JSON deserializers on parsing empty or null datafile (#77)
1 parent c313636 commit 545286d

File tree

8 files changed

+125
-5
lines changed

8 files changed

+125
-5
lines changed

core-api/src/main/java/com/optimizely/ab/Optimizely.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,13 @@ private void track(@Nonnull String eventName,
498498
* @return a {@link ProjectConfig} instance given a json string
499499
*/
500500
private static ProjectConfig getProjectConfig(String datafile) throws ConfigParseException {
501+
if (datafile == null) {
502+
throw new ConfigParseException("Unable to parse null datafile.");
503+
}
504+
if (datafile.length() == 0) {
505+
throw new ConfigParseException("Unable to parse empty datafile.");
506+
}
507+
501508
return DefaultConfigParser.getInstance().parseProjectConfig(datafile);
502509
}
503510

core-api/src/main/java/com/optimizely/ab/config/parser/GsonConfigParser.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ final class GsonConfigParser implements ConfigParser {
3333

3434
@Override
3535
public ProjectConfig parseProjectConfig(@Nonnull String json) throws ConfigParseException {
36+
if (json == null) {
37+
throw new ConfigParseException("Unable to parse null json.");
38+
}
39+
if (json.length() == 0) {
40+
throw new ConfigParseException("Unable to parse empty json.");
41+
}
3642
Gson gson = new GsonBuilder()
3743
.registerTypeAdapter(ProjectConfig.class, new ProjectConfigGsonDeserializer())
3844
.registerTypeAdapter(Audience.class, new AudienceGsonDeserializer())

core-api/src/test/java/com/optimizely/ab/OptimizelyBuilderTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,19 @@ public void withCustomClientVersion() throws Exception {
164164
assertThat(((EventBuilderV2)optimizelyClient.eventBuilder).clientVersion, is("0.0.0"));
165165
}
166166

167+
@SuppressFBWarnings(value="NP_NONNULL_PARAM_VIOLATION", justification="Testing nullness contract violation")
168+
@Test
169+
public void builderThrowsConfigParseExceptionForNullDatafile() throws Exception {
170+
thrown.expect(ConfigParseException.class);
171+
Optimizely.builder(null, mockEventHandler).build();
172+
}
173+
174+
@Test
175+
public void builderThrowsConfigParseExceptionForEmptyDatafile() throws Exception {
176+
thrown.expect(ConfigParseException.class);
177+
Optimizely.builder("", mockEventHandler).build();
178+
}
179+
167180
@Test
168181
public void builderThrowsConfigParseExceptionForInvalidDatafile() throws Exception {
169182
thrown.expect(ConfigParseException.class);

core-api/src/test/java/com/optimizely/ab/config/parser/DefaultConfigParserTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ public class DefaultConfigParserTest {
2727
public void createThrowException() throws Exception {
2828
// FIXME - mdodsworth: hmmm, this isn't going to be the easiest thing to test
2929
}
30-
}
30+
}

core-api/src/test/java/com/optimizely/ab/config/parser/GsonConfigParserTest.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.optimizely.ab.config.ProjectConfig;
2020

21+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2122
import org.junit.Rule;
2223
import org.junit.Test;
2324
import org.junit.rules.ExpectedException;
@@ -86,4 +87,27 @@ public void validJsonRequiredFieldMissingExceptionWrapping() throws Exception {
8687
GsonConfigParser parser = new GsonConfigParser();
8788
parser.parseProjectConfig("{\"valid\": \"json\"}");
8889
}
89-
}
90+
91+
/**
92+
* Verify that empty string JSON results in a {@link ConfigParseException} being thrown.
93+
*/
94+
@Test
95+
public void emptyJsonExceptionWrapping() throws Exception {
96+
thrown.expect(ConfigParseException.class);
97+
98+
GsonConfigParser parser = new GsonConfigParser();
99+
parser.parseProjectConfig("");
100+
}
101+
102+
/**
103+
* Verify that null JSON results in a {@link ConfigParseException} being thrown.
104+
*/
105+
@Test
106+
@SuppressFBWarnings(value="NP_NONNULL_PARAM_VIOLATION", justification="Testing nullness contract violation")
107+
public void nullJsonExceptionWrapping() throws Exception {
108+
thrown.expect(ConfigParseException.class);
109+
110+
GsonConfigParser parser = new GsonConfigParser();
111+
parser.parseProjectConfig(null);
112+
}
113+
}

core-api/src/test/java/com/optimizely/ab/config/parser/JacksonConfigParserTest.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.optimizely.ab.config.ProjectConfig;
2020

21+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2122
import org.junit.Rule;
2223
import org.junit.Test;
2324
import org.junit.rules.ExpectedException;
@@ -86,4 +87,27 @@ public void validJsonRequiredFieldMissingExceptionWrapping() throws Exception {
8687
JacksonConfigParser parser = new JacksonConfigParser();
8788
parser.parseProjectConfig("{\"valid\": \"json\"}");
8889
}
89-
}
90+
91+
/**
92+
* Verify that empty string JSON results in a {@link ConfigParseException} being thrown.
93+
*/
94+
@Test
95+
public void emptyJsonExceptionWrapping() throws Exception {
96+
thrown.expect(ConfigParseException.class);
97+
98+
JacksonConfigParser parser = new JacksonConfigParser();
99+
parser.parseProjectConfig("");
100+
}
101+
102+
/**
103+
* Verify that null JSON results in a {@link ConfigParseException} being thrown.
104+
*/
105+
@Test
106+
@SuppressFBWarnings(value="NP_NONNULL_PARAM_VIOLATION", justification="Testing nullness contract violation")
107+
public void nullJsonExceptionWrapping() throws Exception {
108+
thrown.expect(ConfigParseException.class);
109+
110+
JacksonConfigParser parser = new JacksonConfigParser();
111+
parser.parseProjectConfig(null);
112+
}
113+
}

core-api/src/test/java/com/optimizely/ab/config/parser/JsonConfigParserTest.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.optimizely.ab.config.ProjectConfig;
2020

21+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2122
import org.junit.Rule;
2223
import org.junit.Test;
2324
import org.junit.rules.ExpectedException;
@@ -86,4 +87,27 @@ public void validJsonRequiredFieldMissingExceptionWrapping() throws Exception {
8687
JsonConfigParser parser = new JsonConfigParser();
8788
parser.parseProjectConfig("{\"valid\": \"json\"}");
8889
}
89-
}
90+
91+
/**
92+
* Verify that empty string JSON results in a {@link ConfigParseException} being thrown.
93+
*/
94+
@Test
95+
public void emptyJsonExceptionWrapping() throws Exception {
96+
thrown.expect(ConfigParseException.class);
97+
98+
JsonConfigParser parser = new JsonConfigParser();
99+
parser.parseProjectConfig("");
100+
}
101+
102+
/**
103+
* Verify that null JSON results in a {@link ConfigParseException} being thrown.
104+
*/
105+
@Test
106+
@SuppressFBWarnings(value="NP_NONNULL_PARAM_VIOLATION", justification="Testing nullness contract violation")
107+
public void nullJsonExceptionWrapping() throws Exception {
108+
thrown.expect(ConfigParseException.class);
109+
110+
JsonConfigParser parser = new JsonConfigParser();
111+
parser.parseProjectConfig(null);
112+
}
113+
}

core-api/src/test/java/com/optimizely/ab/config/parser/JsonSimpleConfigParserTest.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.optimizely.ab.config.ProjectConfig;
2020

21+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2122
import org.junit.Rule;
2223
import org.junit.Test;
2324
import org.junit.rules.ExpectedException;
@@ -86,4 +87,25 @@ public void validJsonRequiredFieldMissingExceptionWrapping() throws Exception {
8687
JsonSimpleConfigParser parser = new JsonSimpleConfigParser();
8788
parser.parseProjectConfig("{\"valid\": \"json\"}");
8889
}
89-
}
90+
/**
91+
* Verify that empty string JSON results in a {@link ConfigParseException} being thrown.
92+
*/
93+
@Test
94+
public void emptyJsonExceptionWrapping() throws Exception {
95+
thrown.expect(ConfigParseException.class);
96+
97+
JsonSimpleConfigParser parser = new JsonSimpleConfigParser();
98+
parser.parseProjectConfig("");
99+
}
100+
/**
101+
* Verify that null JSON results in a {@link ConfigParseException} being thrown.
102+
*/
103+
@Test
104+
@SuppressFBWarnings(value="NP_NONNULL_PARAM_VIOLATION", justification="Testing nullness contract violation")
105+
public void nullJsonExceptionWrapping() throws Exception {
106+
thrown.expect(ConfigParseException.class);
107+
108+
JsonSimpleConfigParser parser = new JsonSimpleConfigParser();
109+
parser.parseProjectConfig(null);
110+
}
111+
}

0 commit comments

Comments
 (0)