private static void validateInstanceData(InstanceData instanceData) { String description = instanceData.getDescription(); String q = "Question: '" + instanceData.getInstanceName() + "'"; if (description == null || description.length() == 0) { throw new BatfishException(q + " is missing question description"); } for (Entry<String, Variable> e : instanceData.getVariables().entrySet()) { String variableName = e.getKey(); Variable variable = e.getValue(); String v = "Variable: '" + variableName + "' in " + q; String variableDescription = variable.getDescription(); if (variableDescription == null || variableDescription.length() == 0) { throw new BatfishException(v + " is missing description"); } } }
/** * Test that translating an InstanceData object to JSON and using that JSON to create a new * InstanceData object produces an object equal to the original instance */ @Test public void testJsonSerialization() throws IOException { InstanceData instanceData = new InstanceData(); instanceData.setInstanceName("instanceName"); instanceData.setDescription("The description"); instanceData.setLongDescription("The long description"); instanceData.setOrderedVariableNames(ImmutableList.of("b", "a")); instanceData.setTags(new TreeSet<>(Arrays.asList("tag1", "tag2"))); instanceData.setVariables(ImmutableSortedMap.of("v", new Variable())); assertThat(clone(instanceData), equalTo(instanceData)); } }
/** Test that getOrderedVariableNames returns orderedVariableNames in the correct order */ @Test public void testGetOrderedVariableNamesNonEmptyList() throws IOException { InstanceData instanceData = BatfishObjectMapper.mapper() .readValue("{\"orderedVariableNames\":[\"r\", \"a\", \"d\"]}", InstanceData.class); assertThat(instanceData.getOrderedVariableNames(), equalTo(ImmutableList.of("r", "a", "d"))); }
private Question createTestQuestion(String name, String description) { InstanceData instanceData = new InstanceData(); instanceData.setDescription(description); instanceData.setInstanceName(name); Question testQuestion = new Question() {
@Override public int hashCode() { return Objects.hash(getInstance().getInstanceName(), getInstance().getDescription()); } };
private static JsonPathQuestion makeQuestionWithHints(AbstractRoleConsistencyQuestion roleQ) { JsonPathQuestion ret = new JsonPathQuestion(); ret.setInnerQuestion(roleQ); JsonPathQuery path = new JsonPathQuery("$.answers[?(@.outliers)]", true); path.setDisplayHints(getDisplayHintsForQuestion(roleQ)); ret.setPaths(Collections.singletonList(path)); InstanceData instance = new InstanceData(); instance.setInstanceName(getInstanceNameForQuestion(roleQ)); ret.setInstance(instance); return ret; }
BatfishObjectMapper.mapper() .readValue(instanceJson.toString(), new TypeReference<InstanceData>() {}); Map<String, Variable> variables = instanceData.getVariables();
@VisibleForTesting static String readQuestionTemplate(Path file, Map<String, String> templates) throws JSONException, IOException { String questionText = CommonUtil.readFile(file); JSONObject questionObj = new JSONObject(questionText); if (questionObj.has(BfConsts.PROP_INSTANCE) && !questionObj.isNull(BfConsts.PROP_INSTANCE)) { JSONObject instanceDataObj = questionObj.getJSONObject(BfConsts.PROP_INSTANCE); String instanceDataStr = instanceDataObj.toString(); InstanceData instanceData = BatfishObjectMapper.mapper().readValue(instanceDataStr, InstanceData.class); String name = instanceData.getInstanceName(); String key = name.toLowerCase(); if (templates.containsKey(key) && _logger != null) { _logger.warnf( "Found duplicate template having instance name %s, only the last one in the list of templatedirs will be loaded\n", name); } templates.put(key, questionText); return name; } else { throw new BatfishException(String.format("Question in file:%s has no instance name", file)); } }
@Override public boolean equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final Question other = (Question) obj; return Objects.equals( getInstance().getInstanceName(), other.getInstance().getInstanceName()) && Objects.equals( getInstance().getDescription(), other.getInstance().getDescription()); }
BatfishObjectMapper.mapper() .readValue(instanceDataStr, new TypeReference<InstanceData>() {}); for (Entry<String, Variable> e : instanceData.getVariables().entrySet()) { String varName = e.getKey(); Variable variable = e.getValue(); for (Entry<String, Variable> e : instanceData.getVariables().entrySet()) { String varName = e.getKey(); Variable variable = e.getValue();
.setTag("testrig_name", getTestrigName().getId()); if (question.getInstance() != null) { activeSpan.setTag("question-name", question.getInstance().getInstanceName());
@Test public void testEquals() throws IOException { InstanceData instanceData = new InstanceData(); InstanceData initialInstance = clone(instanceData); EqualsTester equalsTester = new EqualsTester(); equalsTester.addEqualityGroup(initialInstance, initialInstance).addEqualityGroup(new Object()); instanceData.setInstanceName("instanceName"); equalsTester.addEqualityGroup(clone(instanceData)); instanceData.setDescription("The description"); equalsTester.addEqualityGroup(clone(instanceData)); instanceData.setLongDescription("The long description"); equalsTester.addEqualityGroup(clone(instanceData)); instanceData.setOrderedVariableNames(ImmutableList.of("b", "a")); equalsTester.addEqualityGroup(clone(instanceData)); instanceData.setTags(new TreeSet<>(Arrays.asList("tag1", "tag2"))); equalsTester.addEqualityGroup(clone(instanceData)); Variable variable = new Variable(); variable.setType(Type.INTEGER); instanceData.setVariables(ImmutableSortedMap.of("v", variable)); equalsTester.addEqualityGroup(clone(instanceData)); equalsTester.testEquals(); }
/** * Given the JSON representation of a question template and desired values of some parameters in * the template, this functions fills out the template with those values. It also fills the * template with the provided name. * * @param questionJson The question template to modify * @param parameters The parameters and values to fill * @param questionName The name to embed in the template * @return A JSONObject representation of the filled out template */ public static JSONObject fillTemplate( JSONObject questionJson, Map<String, JsonNode> parameters, String questionName) throws JSONException, IOException { JSONObject clonedQuestionJson = new JSONObject(questionJson.toString()); // deep copy JSONObject instanceJson = clonedQuestionJson.getJSONObject(BfConsts.PROP_INSTANCE); instanceJson.put(BfConsts.PROP_INSTANCE_NAME, questionName); String instanceDataStr = instanceJson.toString(); InstanceData instanceData = BatfishObjectMapper.mapper() .readValue(instanceDataStr, new TypeReference<InstanceData>() {}); Map<String, Variable> variables = instanceData.getVariables(); Client.validateAndSet(parameters, variables); Client.checkVariableState(variables); JSONObject modifiedInstanceData = new JSONObject(BatfishObjectMapper.writePrettyString(instanceData)); clonedQuestionJson.put(BfConsts.PROP_INSTANCE, modifiedInstanceData); return clonedQuestionJson; }
/** * Test that getOrderedVariableNames returns an empty list when orderedVariableNames are not * provided */ @Test public void testGetOrderedVariableNamesMissing() throws IOException { InstanceData instanceData = BatfishObjectMapper.mapper().readValue("{}", InstanceData.class); assertThat(instanceData.getOrderedVariableNames(), empty()); }
currentAnswer.getQuestion().getDataPlane(), BatfishObjectMapper.writeString( currentAnswer.getQuestion().getInstance().getVariables())); } catch (JsonProcessingException e) { throw new BatfishException(
/** * Test that getOrderedVariableNames returns an empty list when orderedVariableNames is an empty * list */ @Test public void testGetOrderedVariableNamesEmptyList() throws IOException { InstanceData instanceData = BatfishObjectMapper.mapper().readValue("{\"orderedVariableNames\":[]}", InstanceData.class); assertThat(instanceData.getOrderedVariableNames(), empty()); }