Skip to content

Commit b96a0d7

Browse files
committed
Gson parser behaves differently than other parsers
1 parent 1995271 commit b96a0d7

File tree

5 files changed

+89
-0
lines changed

5 files changed

+89
-0
lines changed

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

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

3434
@Override
3535
public ProjectConfig parseProjectConfig(@Nonnull String json) throws ConfigParseException {
36+
if (json == null || json.length() == 0) {
37+
throw new ConfigParseException("Unable to parse datafile: " + json);
38+
}
3639
Gson gson = new GsonBuilder()
3740
.registerTypeAdapter(ProjectConfig.class, new ProjectConfigGsonDeserializer())
3841
.registerTypeAdapter(Audience.class, new AudienceGsonDeserializer())

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,26 @@ public void validJsonRequiredFieldMissingExceptionWrapping() throws Exception {
8686
GsonConfigParser parser = new GsonConfigParser();
8787
parser.parseProjectConfig("{\"valid\": \"json\"}");
8888
}
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+
GsonConfigParser parser = new GsonConfigParser();
98+
parser.parseProjectConfig("");
99+
}
100+
101+
/**
102+
* Verify that null JSON results in a {@link ConfigParseException} being thrown.
103+
*/
104+
@Test
105+
public void nullJsonExceptionWrapping() throws Exception {
106+
thrown.expect(ConfigParseException.class);
107+
108+
GsonConfigParser parser = new GsonConfigParser();
109+
parser.parseProjectConfig(null);
110+
}
89111
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,26 @@ public void validJsonRequiredFieldMissingExceptionWrapping() throws Exception {
8686
JacksonConfigParser parser = new JacksonConfigParser();
8787
parser.parseProjectConfig("{\"valid\": \"json\"}");
8888
}
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+
JacksonConfigParser parser = new JacksonConfigParser();
98+
parser.parseProjectConfig("");
99+
}
100+
101+
/**
102+
* Verify that null JSON results in a {@link ConfigParseException} being thrown.
103+
*/
104+
@Test
105+
public void nullJsonExceptionWrapping() throws Exception {
106+
thrown.expect(ConfigParseException.class);
107+
108+
JacksonConfigParser parser = new JacksonConfigParser();
109+
parser.parseProjectConfig(null);
110+
}
89111
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,26 @@ public void validJsonRequiredFieldMissingExceptionWrapping() throws Exception {
8686
JsonConfigParser parser = new JsonConfigParser();
8787
parser.parseProjectConfig("{\"valid\": \"json\"}");
8888
}
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+
JsonConfigParser parser = new JsonConfigParser();
98+
parser.parseProjectConfig("");
99+
}
100+
101+
/**
102+
* Verify that null JSON results in a {@link ConfigParseException} being thrown.
103+
*/
104+
@Test
105+
public void nullJsonExceptionWrapping() throws Exception {
106+
thrown.expect(ConfigParseException.class);
107+
108+
JsonConfigParser parser = new JsonConfigParser();
109+
parser.parseProjectConfig(null);
110+
}
89111
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,24 @@ public void validJsonRequiredFieldMissingExceptionWrapping() throws Exception {
8686
JsonSimpleConfigParser parser = new JsonSimpleConfigParser();
8787
parser.parseProjectConfig("{\"valid\": \"json\"}");
8888
}
89+
/**
90+
* Verify that empty string JSON results in a {@link ConfigParseException} being thrown.
91+
*/
92+
@Test
93+
public void emptyJsonExceptionWrapping() throws Exception {
94+
thrown.expect(ConfigParseException.class);
95+
96+
JsonSimpleConfigParser parser = new JsonSimpleConfigParser();
97+
parser.parseProjectConfig("");
98+
}
99+
/**
100+
* Verify that null JSON results in a {@link ConfigParseException} being thrown.
101+
*/
102+
@Test
103+
public void nullJsonExceptionWrapping() throws Exception {
104+
thrown.expect(ConfigParseException.class);
105+
106+
JsonSimpleConfigParser parser = new JsonSimpleConfigParser();
107+
parser.parseProjectConfig(null);
108+
}
89109
}

0 commit comments

Comments
 (0)