@Test public void validateTemplateUnexercisedVariable() throws JSONException, IOException { JSONObject template = new JSONObject(readResource("org/batfish/client/goodTemplate.json", UTF_8)); _thrown.expect(BatfishException.class); _thrown.expectMessage("Template validation should exercise all variables"); QuestionHelper.validateTemplate(template, ImmutableSortedMap.of("parameter1", new IntNode(1))); }
static Question validateTemplate(JSONObject questionJson, Map<String, JsonNode> parsedParameters) throws JSONException, IOException { JSONObject instanceJson = questionJson.getJSONObject(BfConsts.PROP_INSTANCE); InstanceData instanceData = BatfishObjectMapper.mapper() .readValue(instanceJson.toString(), new TypeReference<InstanceData>() {}); Map<String, Variable> variables = instanceData.getVariables(); Sets.SetView<String> extraVariables = Sets.difference(variables.keySet(), parsedParameters.keySet()); if (!extraVariables.isEmpty()) { throw new BatfishException( "Template validation should exercise all variables. Un-exercised variables: " + extraVariables); } String questionStr = questionJson.toString(); for (String variable : variables.keySet()) { if (!questionStr.contains("${" + variable + "}")) { throw new BatfishException("Unused variable: " + variable); } } return Question.parseQuestion(fillTemplate(questionJson, parsedParameters, "qname").toString()); }
QuestionHelper { static Question validateTemplate(JSONObject questionJson, Map<String, JsonNode> parsedParameters) throws JSONException, IOException { JSONObject instanceJson = questionJson.getJSONObject(BfConsts.PROP_INSTANCE); InstanceData instanceData = BatfishObjectMapper.mapper() .readValue(instanceJson.toString(), new TypeReference<InstanceData>() {}); Map<String, Variable> variables = instanceData.getVariables(); Sets.SetView<String> extraVariables = Sets.difference(variables.keySet(), parsedParameters.keySet()); if (!extraVariables.isEmpty()) { throw new BatfishException( "Template validation should exercise all variables. Un-exercised variables: " + extraVariables); } String questionStr = questionJson.toString(); for (String variable : variables.keySet()) { if (!questionStr.contains("${" + variable + "}")) { throw new BatfishException("Unused variable: " + variable); } } return Question.parseQuestion(fillTemplate(questionJson, parsedParameters, "qname").toString()); } }
QuestionHelper { static Question validateTemplate(JSONObject questionJson, Map<String, JsonNode> parsedParameters) throws JSONException, IOException { JSONObject instanceJson = questionJson.getJSONObject(BfConsts.PROP_INSTANCE); InstanceData instanceData = BatfishObjectMapper.mapper() .readValue(instanceJson.toString(), new TypeReference<InstanceData>() {}); Map<String, Variable> variables = instanceData.getVariables(); Sets.SetView<String> extraVariables = Sets.difference(variables.keySet(), parsedParameters.keySet()); if (!extraVariables.isEmpty()) { throw new BatfishException( "Template validation should exercise all variables. Un-exercised variables: " + extraVariables); } String questionStr = questionJson.toString(); for (String variable : variables.keySet()) { if (!questionStr.contains("${" + variable + "}")) { throw new BatfishException("Unused variable: " + variable); } } return Question.parseQuestion(fillTemplate(questionJson, parsedParameters, "qname").toString()); }  }
QuestionHelper { static Question validateTemplate(JSONObject questionJson, Map<String, JsonNode> parsedParameters) throws JSONException, IOException { JSONObject instanceJson = questionJson.getJSONObject(BfConsts.PROP_INSTANCE); InstanceData instanceData = BatfishObjectMapper.mapper() .readValue(instanceJson.toString(), new TypeReference<InstanceData>() {}); Map<String, Variable> variables = instanceData.getVariables(); Sets.SetView<String> extraVariables = Sets.difference(variables.keySet(), parsedParameters.keySet()); if (!extraVariables.isEmpty()) { throw new BatfishException( "Template validation should exercise all variables. Un-exercised variables: " + extraVariables); } String questionStr = questionJson.toString(); for (String variable : variables.keySet()) { if (!questionStr.contains("${" + variable + "}")) { throw new BatfishException("Unused variable: " + variable); } } return Question.parseQuestion(fillTemplate(questionJson, parsedParameters, "qname").toString()); }  static JSONObject fillTemplate(
      JSONObject questionJson, Map<String, JsonNode> parameters, String questionName); static Question getQuestion(
      String questionTypeStr, Map<String, Supplier<Question>> questions); static String getQuestionString(
      String questionTypeStr, Map<String, Supplier<Question>> questions, boolean full); }
QuestionHelper { static Question validateTemplate(JSONObject questionJson, Map<String, JsonNode> parsedParameters) throws JSONException, IOException { JSONObject instanceJson = questionJson.getJSONObject(BfConsts.PROP_INSTANCE); InstanceData instanceData = BatfishObjectMapper.mapper() .readValue(instanceJson.toString(), new TypeReference<InstanceData>() {}); Map<String, Variable> variables = instanceData.getVariables(); Sets.SetView<String> extraVariables = Sets.difference(variables.keySet(), parsedParameters.keySet()); if (!extraVariables.isEmpty()) { throw new BatfishException( "Template validation should exercise all variables. Un-exercised variables: " + extraVariables); } String questionStr = questionJson.toString(); for (String variable : variables.keySet()) { if (!questionStr.contains("${" + variable + "}")) { throw new BatfishException("Unused variable: " + variable); } } return Question.parseQuestion(fillTemplate(questionJson, parsedParameters, "qname").toString()); }  static JSONObject fillTemplate(
      JSONObject questionJson, Map<String, JsonNode> parameters, String questionName); static Question getQuestion(
      String questionTypeStr, Map<String, Supplier<Question>> questions); static String getQuestionString(
      String questionTypeStr, Map<String, Supplier<Question>> questions, boolean full);  }