Skip to content

Commit 5558e3b

Browse files
committed
Moved logic from specific serializer to shared code
1 parent c5fa021 commit 5558e3b

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
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: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ 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-
}
3936
Gson gson = new GsonBuilder()
4037
.registerTypeAdapter(ProjectConfig.class, new ProjectConfigGsonDeserializer())
4138
.registerTypeAdapter(Audience.class, new AudienceGsonDeserializer())

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import org.junit.Rule;
2323
import org.junit.Test;
2424
import org.junit.rules.ExpectedException;
25+
import static junit.framework.Assert.assertNull;
26+
2527

2628
import static com.optimizely.ab.config.ProjectConfigTestUtils.validConfigJsonV1;
2729
import static com.optimizely.ab.config.ProjectConfigTestUtils.validProjectConfigV1;
@@ -90,24 +92,22 @@ public void validJsonRequiredFieldMissingExceptionWrapping() throws Exception {
9092

9193
/**
9294
* Verify that empty string JSON results in a {@link ConfigParseException} being thrown.
95+
* This serializer doesn't throw exceptions on null or empty string
9396
*/
9497
@Test
9598
public void emptyJsonExceptionWrapping() throws Exception {
96-
thrown.expect(ConfigParseException.class);
97-
9899
GsonConfigParser parser = new GsonConfigParser();
99-
parser.parseProjectConfig("");
100+
assertNull(parser.parseProjectConfig(""));
100101
}
101102

102103
/**
103104
* Verify that null JSON results in a {@link ConfigParseException} being thrown.
105+
* This serializer doesn't throw exceptions on null or empty string
104106
*/
105107
@Test
106108
@SuppressFBWarnings(value="NP_NONNULL_PARAM_VIOLATION", justification="Testing nullness contract violation")
107109
public void nullJsonExceptionWrapping() throws Exception {
108-
thrown.expect(ConfigParseException.class);
109-
110110
GsonConfigParser parser = new GsonConfigParser();
111-
parser.parseProjectConfig(null);
111+
assertNull(parser.parseProjectConfig(null));
112112
}
113113
}

0 commit comments

Comments
 (0)