add verbose checking and feedback for ScriptParams usage

This commit is contained in:
Jonathan Shook 2020-04-27 10:56:00 -05:00
parent 4362e389ce
commit d5f7a138d3
3 changed files with 78 additions and 5 deletions

View File

@ -47,6 +47,7 @@ import java.util.stream.Collectors;
public class Scenario implements Callable<ScenarioResult> {
private static final Logger logger = (Logger) LoggerFactory.getLogger(Scenario.class);
private static final ScriptEngineManager engineManager = new ScriptEngineManager();
private final List<String> scripts = new ArrayList<>();
private ScriptEngine scriptEngine;

View File

@ -17,23 +17,72 @@
package io.nosqlbench.engine.core.script;
import ch.qos.logback.classic.Logger;
import io.nosqlbench.nb.api.errors.BasicError;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
public class ScriptParams extends HashMap<String,String> {
public class ScriptParams extends HashMap<String, String> {
public Map<String,String> withOverrides(Map<String,String> overrides) {
HashMap<String,String> result = new HashMap<>();
private static final Logger logger = (Logger) LoggerFactory.getLogger(ScriptParams.class);
public Map<String, String> withOverrides(Map<String, String> overrides) {
if (overrides == null) {
logger.warn("A null map was provided to withOverrides. This could be a bug in your script.");
overrides=Map.of();
}
checkForNulls("params", "calling withOverrides", this);
checkForNulls("overrides", "calling withOverrides", overrides);
HashMap<String, String> result = new HashMap<>();
result.putAll(this);
result.putAll(overrides);
return result;
}
public Map<String,String> withDefaults(Map<String,String> defaults) {
HashMap<String,String> result = new HashMap<>();
public Map<String, String> withDefaults(Map<String, String> defaults) {
if (defaults == null) {
logger.warn("A null map was provided to withDefaults. This could be a bug in your script.");
defaults=Map.of();
}
HashMap<String, String> result = new HashMap<>();
checkForNulls("params", "calling withDefaults", this);
checkForNulls("defaults", "calling withDefaults", defaults);
result.putAll(defaults);
result.putAll(this);
return result;
}
private static void checkForNulls(String name, String action, Map<String, String> map) {
for (String s : map.keySet()) {
if (s == null) {
printMapToLog(name,map);
throw new BasicError("Found a null key in " + name + " while " + action + ". Please ensure that you " +
"only provide non-null keys and values in parameter maps.");
} else if (map.get(s) == null) {
printMapToLog(name,map);
throw new BasicError("Found a null value for key '" + s + "' in " + name + " while " + action + ". " +
"Please ensure you provide non-null keys and values in parameter maps.");
}
}
}
private static void printMapToLog(String name, Map<String, String> map) {
logger.info("contents of map '" + name + "':");
String mapdetail = map.entrySet()
.stream()
.map(e -> valueOf(e.getKey()) + ":" + valueOf(e.getValue()))
.collect(Collectors.joining(","));
logger.info(mapdetail);
}
private static String valueOf(Object o) {
if (o == null) {
return "NULL";
}
return String.valueOf(o);
}
}

View File

@ -0,0 +1,23 @@
package io.nosqlbench.engine.core.script;
import io.nosqlbench.nb.api.errors.BasicError;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
public class ScriptParamsTest {
@Test(expected = BasicError.class)
public void testThatNullOverridesKeyThrowsBasicError() {
ScriptParams p = new ScriptParams();
p.putAll(Map.of("a","b"));
p.withDefaults(Map.of("c","d"));
HashMap<String, String> overrides = new HashMap<>();
overrides.put(null,"test");
p.withOverrides(overrides);
}
}