mirror of
https://github.com/nosqlbench/nosqlbench.git
synced 2025-02-25 18:55:28 -06:00
add verbose checking and feedback for ScriptParams usage
This commit is contained in:
parent
4362e389ce
commit
d5f7a138d3
@ -47,6 +47,7 @@ import java.util.stream.Collectors;
|
|||||||
public class Scenario implements Callable<ScenarioResult> {
|
public class Scenario implements Callable<ScenarioResult> {
|
||||||
|
|
||||||
private static final Logger logger = (Logger) LoggerFactory.getLogger(Scenario.class);
|
private static final Logger logger = (Logger) LoggerFactory.getLogger(Scenario.class);
|
||||||
|
|
||||||
private static final ScriptEngineManager engineManager = new ScriptEngineManager();
|
private static final ScriptEngineManager engineManager = new ScriptEngineManager();
|
||||||
private final List<String> scripts = new ArrayList<>();
|
private final List<String> scripts = new ArrayList<>();
|
||||||
private ScriptEngine scriptEngine;
|
private ScriptEngine scriptEngine;
|
||||||
|
@ -17,23 +17,72 @@
|
|||||||
|
|
||||||
package io.nosqlbench.engine.core.script;
|
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.HashMap;
|
||||||
import java.util.Map;
|
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) {
|
private static final Logger logger = (Logger) LoggerFactory.getLogger(ScriptParams.class);
|
||||||
HashMap<String,String> result = new HashMap<>();
|
|
||||||
|
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(this);
|
||||||
result.putAll(overrides);
|
result.putAll(overrides);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String,String> withDefaults(Map<String,String> defaults) {
|
public Map<String, String> withDefaults(Map<String, String> defaults) {
|
||||||
HashMap<String,String> result = new HashMap<>();
|
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(defaults);
|
||||||
result.putAll(this);
|
result.putAll(this);
|
||||||
return result;
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user