@Test public void testValidateWithValidInput() throws IOException { Map<String, JsonNode> parameters = new HashMap<>(); Map<String, Variable> variables = new HashMap<>(); parameters.put("integer", _mapper.readTree("10")); Variable integerVariable = new Variable(); integerVariable.setType(INTEGER); variables.put("integer", integerVariable); parameters.put("boolean", _mapper.readTree("true")); Variable booleanVariable = new Variable(); booleanVariable.setType(BOOLEAN); variables.put("boolean", booleanVariable); Client.validateAndSet(parameters, variables); }
static void validateAndSet(Map<String, JsonNode> parameters, Map<String, Variable> variables) throws BatfishException { for (Entry<String, JsonNode> e : parameters.entrySet()) { String parameterName = e.getKey(); JsonNode value = e.getValue(); Variable variable = variables.get(parameterName); if (variable == null) { throw new BatfishException( "No variable named: '" + parameterName + "' in supplied question template"); } if (variable.getMinElements() != null) { if (!value.isArray() || value.size() < variable.getMinElements()) { throw new BatfishException( String.format( "Invalid value for parameter %s: %s. " + "Expecting a JSON array of at least %d " + "elements", parameterName, value, variable.getMinElements())); } for (JsonNode node : value) { validateNode(node, variable, parameterName); } } else { validateNode(value, variable, parameterName); } variable.setValue(value); } }
Client extends AbstractClient implements IClient { static void validateAndSet(Map<String, JsonNode> parameters, Map<String, Variable> variables) throws BatfishException { for (Entry<String, JsonNode> e : parameters.entrySet()) { String parameterName = e.getKey(); JsonNode value = e.getValue(); Variable variable = variables.get(parameterName); if (variable == null) { throw new BatfishException( "No variable named: '" + parameterName + "' in supplied question template"); } if (variable.getMinElements() != null) { if (!value.isArray() || value.size() < variable.getMinElements()) { throw new BatfishException( String.format( "Invalid value for parameter %s: %s. " + "Expecting a JSON array of at least %d " + "elements", parameterName, value, variable.getMinElements())); } for (JsonNode node : value) { validateNode(node, variable, parameterName); } } else { validateNode(value, variable, parameterName); } variable.setValue(value); } } }
Client extends AbstractClient implements IClient { static void validateAndSet(Map<String, JsonNode> parameters, Map<String, Variable> variables) throws BatfishException { for (Entry<String, JsonNode> e : parameters.entrySet()) { String parameterName = e.getKey(); JsonNode value = e.getValue(); Variable variable = variables.get(parameterName); if (variable == null) { throw new BatfishException( "No variable named: '" + parameterName + "' in supplied question template"); } if (variable.getMinElements() != null) { if (!value.isArray() || value.size() < variable.getMinElements()) { throw new BatfishException( String.format( "Invalid value for parameter %s: %s. " + "Expecting a JSON array of at least %d " + "elements", parameterName, value, variable.getMinElements())); } for (JsonNode node : value) { validateNode(node, variable, parameterName); } } else { validateNode(value, variable, parameterName); } variable.setValue(value); } } Client(Settings settings);  Client(String[] args); }
Client extends AbstractClient implements IClient { static void validateAndSet(Map<String, JsonNode> parameters, Map<String, Variable> variables) throws BatfishException { for (Entry<String, JsonNode> e : parameters.entrySet()) { String parameterName = e.getKey(); JsonNode value = e.getValue(); Variable variable = variables.get(parameterName); if (variable == null) { throw new BatfishException( "No variable named: '" + parameterName + "' in supplied question template"); } if (variable.getMinElements() != null) { if (!value.isArray() || value.size() < variable.getMinElements()) { throw new BatfishException( String.format( "Invalid value for parameter %s: %s. " + "Expecting a JSON array of at least %d " + "elements", parameterName, value, variable.getMinElements())); } for (JsonNode node : value) { validateNode(node, variable, parameterName); } } else { validateNode(value, variable, parameterName); } variable.setValue(value); } } Client(Settings settings);  Client(String[] args); @Override BatfishLogger getLogger(); Settings getSettings(); void run(List<String> initialCommands); }
Client extends AbstractClient implements IClient { static void validateAndSet(Map<String, JsonNode> parameters, Map<String, Variable> variables) throws BatfishException { for (Entry<String, JsonNode> e : parameters.entrySet()) { String parameterName = e.getKey(); JsonNode value = e.getValue(); Variable variable = variables.get(parameterName); if (variable == null) { throw new BatfishException( "No variable named: '" + parameterName + "' in supplied question template"); } if (variable.getMinElements() != null) { if (!value.isArray() || value.size() < variable.getMinElements()) { throw new BatfishException( String.format( "Invalid value for parameter %s: %s. " + "Expecting a JSON array of at least %d " + "elements", parameterName, value, variable.getMinElements())); } for (JsonNode node : value) { validateNode(node, variable, parameterName); } } else { validateNode(value, variable, parameterName); } variable.setValue(value); } } Client(Settings settings);  Client(String[] args); @Override BatfishLogger getLogger(); Settings getSettings(); void run(List<String> initialCommands);  }