-
Notifications
You must be signed in to change notification settings - Fork 32
test(parser): Default parser #315
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…alize using that parser
…zely_default_parser because picking up this var from property utils Added unit test of default conifg parser will be usefull for travis ci env var optimizely_default_parser gets changed
Pull Request Test Coverage Report for Build 1120
💛 - Coveralls |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should be leveraging the enum
class better to remove the redundant mappings etc that we have now. I've provided some code samples as guidance.
if (isPresent("com.fasterxml.jackson.databind.ObjectMapper")) { | ||
String configParserName = PropertyUtils.get("default_parser"); | ||
|
||
if(configParserName != null && isPresent(configParserName)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the enum above and by assuming the configuration override is using the enum this can be cleaned up to:
String configParserName = PropertyUtils.get("default_parser");
if (configParserName != null) {
try {
ConfigParserSupplier supplier = ConfigParserSupplier.valueOf(configParserName);
if (supplier.isPresent()) {
ConfigParser configParser = supplier.get();
logger.debug("using json parser: {}, based on override config", configParser.getClass().getSimpleName());
return configParser;
}
logger.warn("configured parser {} is not available in the classpath", configParserName);
} catch (IllegalArgumentException e) {
logger.warn("configured parser {} is not a valid value", configParserName);
}
}
.travis.yml
Outdated
@@ -7,6 +7,11 @@ jdk: | |||
install: true | |||
addons: | |||
srcclr: true | |||
env: | |||
- optimizely_default_parser=com.google.gson.Gson |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should use the enums here to make it clear there are a set number of supported parsers.
|
||
private final String configParserValue; | ||
|
||
ConfigParsers(String configParserValue) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's use a supplier in the constructor and change the enum name:
public enum ConfigParserSupplier {
GSON_CONFIG_PARSER("com.google.gson.Gson", GsonConfigParser::new),
JACKSON_CONFIG_PARSER("com.fasterxml.jackson.databind.ObjectMapper", JacksonConfigParser::new),
JSON_CONFIG_PARSER("org.json.JSONObject", JsonConfigParser::new),
JSON_SIMPLE_CONFIG_PARSER("org.json.simple.JSONObject", JsonSimpleConfigParser::new);
private final String className;
private final Supplier<ConfigParser> supplier;
ConfigParserSupplier(String className, Supplier<ConfigParser> supplier) {
this.className = className;
this.supplier = supplier;
}
ConfigParser get() {
return supplier.get();
}
private boolean isPresent() {
try {
Class.forName(className);
return true;
} catch (ClassNotFoundException e) {
return false;
}
}
}
Note that I moved isPresent
into this enum, but not a strict requirement.
throw new MissingJsonParserException("unable to locate a JSON parser. " | ||
+ "Please see <link> for more information"); | ||
} | ||
} else if (isPresent("com.fasterxml.jackson.databind.ObjectMapper")) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that we have the enum as defined above this changes from if...else if...
, to:
for (ConfigParserSupplier supplier: ConfigParserSupplier.valuesI()) {
if (!supplier.isPresent()) {
continue;
}
ConfigParser configParser = supplier.get();
logger.info("using json parser: {}", configParser.getClass().getSimpleName());
return configParser;
}
throw new MissingJsonParserException("unable to locate a JSON parser. "
+ "Please see <link> for more information");
ProjectConfig expected = validProjectConfig; | ||
verifyProjectConfig(actual, expected); | ||
if(defaultParser != null) { | ||
if (DefaultConfigParser.ConfigParsers.GSON_CONFIG_PARSER.toString().equals(defaultParser)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with the enum as defined earlier we can use ConfigParserSupplier.valueOf(defaultParser)
.
} else if (DefaultConfigParser.ConfigParsers.JSON_SIMPLE_CONFIG_PARSER.toString().equals(defaultParser)) { | ||
Assert.assertThat(configParser, CoreMatchers.instanceOf(JsonSimpleConfigParser.class)); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we have an else
statement defaulting to the first enum that matches? not sure if this will be deterministic however, but worth a shot.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, we can do that first enum that matches will be GSON_CONFIG_PARSER
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One last change in the test case.
if(defaultParser != null) { | ||
DefaultConfigParser.ConfigParserSupplier defaultParserSupplier = DefaultConfigParser.ConfigParserSupplier.valueOf(defaultParser); | ||
|
||
if (DefaultConfigParser.ConfigParserSupplier.GSON_CONFIG_PARSER.equals(defaultParserSupplier)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be a switch
statement.
Class expectedParser = GsonConfigParser.class;
if(defaultParser != null) {
DefaultConfigParser.ConfigParserSupplier defaultParserSupplier = DefaultConfigParser.ConfigParserSupplier.valueOf(defaultParser);
switch (defaultParserSupplier) {
case GSON_CONFIG_PARSER:
expectedParser = GsonConfigParser.class;
break;
case JACKSON_CONFIG_PARSER:
expectedParser = JacksonConfigParser.class;
break;
case JSON_CONFIG_PARSER:
expectedParser = JsonConfigParser.class;
break;
case JSON_SIMPLE_CONFIG_PARSER:
expectedParser = JsonSimpleConfigParser.class;
break;
default:
fail("Not a valid config parser");
}
}
Assert.assertThat(configParser, CoreMatchers.instanceOf(expectedParser));
Summary
Modified travis file to test all unit tests with default config parsers and default config parser is now set using ENV.
All Parsers are defined in matrix to run all unit tests with each parser.
Test plan
Unit tests should be pased
Issues