public static synchronized String salt() { if (SALT == null) { SALT = readResource(BfConsts.ABSPATH_DEFAULT_SALT); } return SALT; }
@Before public void readDisplayHints() throws IOException { String text = CommonUtil.readResource("org/batfish/question/jsonpath/jsonPathDisplayHintsTestHints.json"); _displayHints = BatfishObjectMapper.mapper().readValue(text, DisplayHints.class); }
/** check that we deserialize successfully and into two books */ @Test public void libraryDeserialization() throws IOException { ReferenceLibrary library = BatfishObjectMapper.mapper() .readValue( CommonUtil.readResource("org/batfish/referencelibrary/libraryTwoBooks.json"), ReferenceLibrary.class); assertThat(library.getReferenceBooks(), hasSize(2)); }
/** check that we throw an error when the same name is used in a service object and group */ @Test public void bookDeserializationDupServiceName() throws IOException { _thrown.expect(InvalidDefinitionException.class); _thrown.expectMessage("Duplicate"); BatfishObjectMapper.mapper() .readValue( CommonUtil.readResource("org/batfish/referencelibrary/bookDupServiceName.json"), ReferenceBook.class); }
/** check that we throw an error for duplicate address groups */ @Test public void bookDeserializationDupAddressGroup() throws IOException { _thrown.expect(InvalidDefinitionException.class); _thrown.expectMessage("Duplicate"); BatfishObjectMapper.mapper() .readValue( CommonUtil.readResource("org/batfish/referencelibrary/bookDupAddressGroup.json"), ReferenceBook.class); }
/** check that we barf on duplicate book names */ @Test public void libraryDeserializationDuplicateBooks() throws IOException { _thrown.expect(InvalidDefinitionException.class); _thrown.expectMessage("Duplicate"); BatfishObjectMapper.mapper() .readValue( CommonUtil.readResource("org/batfish/referencelibrary/libraryDuplicateBooks.json"), ReferenceLibrary.class); }
/** check that we throw an error for undefined address groups */ @Test public void bookDeserializationUndefAddressGroup() throws IOException { _thrown.expect(InvalidDefinitionException.class); _thrown.expectMessage("Undefined"); BatfishObjectMapper.mapper() .readValue( CommonUtil.readResource("org/batfish/referencelibrary/bookUndefAddressGroup.json"), ReferenceBook.class); }
/** check that we throw an error for undefined service name */ @Test public void bookDeserializationUndefServiceName() throws IOException { _thrown.expect(InvalidDefinitionException.class); _thrown.expectMessage("Undefined"); BatfishObjectMapper.mapper() .readValue( CommonUtil.readResource("org/batfish/referencelibrary/bookUndefServiceName.json"), ReferenceBook.class); } }
@Test public void validateTemplateSuccess() throws JSONException, IOException { JSONObject template = new JSONObject(CommonUtil.readResource("org/batfish/client/goodTemplate.json")); QuestionHelperTestQuestion question = (QuestionHelperTestQuestion) QuestionHelper.validateTemplate( template, ImmutableSortedMap.of("parameter1", new IntNode(1), "parameter2", new IntNode(3))); assertThat(question.getParameterMandatory(), equalTo(1)); assertThat(question.getParameterOptional(), equalTo(3)); }
@Test public void testGetParseTreeSentencesLineNumbers() { String configText = CommonUtil.readResource("org/batfish/grammar/line_numbers"); GrammarSettings settings = new MockGrammarSettings(false, 0, 0, 1000, true, true, true, true); RecoveryCombinedParser cp = new RecoveryCombinedParser(configText, settings); ParserRuleContext tree = cp.parse(); ParseTreeSentences ptSentencesLineNums = ParseTreePrettyPrinter.getParseTreeSentences(tree, cp, true); /* Confirm printed parse tree includes line numbers when that option is set */ assertThat(ptSentencesLineNums.getSentences().get(3), containsString("SIMPLE:'simple' line:1")); assertThat(ptSentencesLineNums.getSentences().get(9), containsString("BLOCK:'block' line:2")); assertThat(ptSentencesLineNums.getSentences().get(12), containsString("INNER:'inner' line:3")); assertThat( ptSentencesLineNums.getSentences().get(14), containsString("SIMPLE:'simple' line:3)")); assertThat(ptSentencesLineNums.getSentences().get(16), containsString("EOF:<EOF> line:5)")); }
@Test public void fillTemplate() throws JSONException, IOException { JSONObject template = new JSONObject(CommonUtil.readResource("org/batfish/client/goodTemplate.json")); JSONObject filledTempate = QuestionHelper.fillTemplate( template, ImmutableSortedMap.of("parameter1", new IntNode(2)), "qname"); QuestionHelperTestQuestion question = (QuestionHelperTestQuestion) Question.parseQuestion(filledTempate.toString()); // the mandatory parameter should get the value we gave, and the optional one should get default assertThat(question.getParameterMandatory(), equalTo(2)); assertThat(question.getParameterOptional(), equalTo(QuestionHelperTestQuestion.DEFAULT_VALUE)); }
private static Set<JsonPathResultEntry> computeResults( String jsonFile, String path, boolean includeSuffix) { Configuration.setDefaults(BatfishJsonPathDefaults.INSTANCE); ConfigurationBuilder b = new ConfigurationBuilder(); Configuration baseConfiguration = b.build(); String jsonStr = CommonUtil.readResource(jsonFile); Object jsonObject = JsonPath.parse(jsonStr, baseConfiguration).json(); JsonPathQuery query = new JsonPathQuery(path, includeSuffix); JsonPathResult result = JsonPathAnswerer.computeResult(jsonObject, query); return new HashSet<>(result.getResult().values()); }
/** check that we deserialize a basic object correctly */ @Test public void bookDeserializationBasic() throws IOException { ReferenceBook book = BatfishObjectMapper.mapper() .readValue( CommonUtil.readResource("org/batfish/referencelibrary/bookBasic.json"), ReferenceBook.class); assertThat(book.getAddressGroups(), hasSize(2)); assertThat(book.getFilterGroups(), hasSize(2)); assertThat(book.getInterfaceGroups(), hasSize(2)); assertThat(book.getServiceEndpoints(), hasSize(2)); assertThat(book.getServiceObjects(), hasSize(2)); assertThat(book.getServiceObjectGroups(), hasSize(2)); }
@Test public void displayHintsParsingTest() throws IOException { String text = CommonUtil.readResource("org/batfish/datamodel/questions/displayHintsTest.json"); DisplayHints displayHints = BatfishObjectMapper.mapper().readValue(text, DisplayHints.class); // here, we only test for ExtractionHint level concepts // tests that sit with jsonpath question validate if prefix/suffix filters are parsed correctly assertThat( displayHints.getTextDesc().equals("${mynode} has nothing on ${myinterface}"), equalTo(true)); Map<String, Extraction> extractions = displayHints.getExtractions(); assertThat(extractions.size(), equalTo(3)); Extraction hint0 = extractions.get("node1"); assertThat(hint0.getSchemaAsObject(), equalTo(Schema.STRING)); assertThat( hint0.getSchemaAsObject().getBaseType().getCanonicalName(), equalTo("java.lang.String")); assertThat(hint0.getMethod().containsKey("use"), equalTo(true)); Extraction hint1 = extractions.get("interfaces1"); assertThat(hint1.getSchemaAsObject(), equalTo(Schema.list(Schema.STRING))); Extraction hint2 = extractions.get("nodes1"); assertThat(hint2.getSchemaAsObject(), equalTo(Schema.list(Schema.INTEGER))); } }
@Test public void testParsingRecoveryWithModes() { String recoveryText = CommonUtil.readResource("org/batfish/grammar/recovery/recovery_badmode"); GrammarSettings settings = new MockGrammarSettings(false, 0, 0, 0, false, false, true, true); RecoveryCombinedParser cp = new RecoveryCombinedParser(recoveryText, settings); RecoveryContext ctx = cp.parse(); RecoveryExtractor extractor = new RecoveryExtractor(); ParseTreeWalker walker = new ParseTreeWalker(); walker.walk(extractor, ctx); assertThat(extractor.getFirstErrorLine(), equalTo(4)); assertThat(extractor.getNumErrorNodes(), equalTo(1)); } }
private static JsonPathResult computeJsonPathResult( String jsonFile, String path, boolean includeSuffix) { Configuration.setDefaults(BatfishJsonPathDefaults.INSTANCE); ConfigurationBuilder b = new ConfigurationBuilder(); Configuration baseConfiguration = b.build(); String jsonStr = CommonUtil.readResource(jsonFile); Object jsonObject = JsonPath.parse(jsonStr, baseConfiguration).json(); JsonPathQuery query = new JsonPathQuery(path, includeSuffix); JsonPathResult result = JsonPathAnswerer.computeResult(jsonObject, query); return result; }
@Test public void validateTemplateUnexercisedVariable() throws JSONException, IOException { JSONObject template = new JSONObject(CommonUtil.readResource("org/batfish/client/goodTemplate.json")); _thrown.expect(BatfishException.class); _thrown.expectMessage("Template validation should exercise all variables"); QuestionHelper.validateTemplate(template, ImmutableSortedMap.of("parameter1", new IntNode(1))); } }
@Test public void validateTemplateExtraParameter() throws JSONException, IOException { JSONObject template = new JSONObject(CommonUtil.readResource("org/batfish/client/extraParameter.json")); _thrown.expect(BatfishException.class); _thrown.expectMessage("Unrecognized field"); QuestionHelper.validateTemplate( template, ImmutableSortedMap.of("parameter1", new IntNode(2), "parameter2", new IntNode(2))); }
@Test public void validateTemplateExtraVariable() throws JSONException, IOException { JSONObject template = new JSONObject(CommonUtil.readResource("org/batfish/client/extraVariable.json")); _thrown.expect(BatfishException.class); _thrown.expectMessage("Unused variable"); QuestionHelper.validateTemplate( template, ImmutableSortedMap.of("parameter1", new IntNode(1), "parameter2EXTRA", new IntNode(2))); }
public JsonPathTest() { Configuration.setDefaults(BatfishJsonPathDefaults.INSTANCE); ConfigurationBuilder b = new ConfigurationBuilder(); _baseConfiguration = b.build(); _oneNtpServerNodesAnswerStr = CommonUtil.readResource("org/batfish/question/jsonpath/oneNtpServer.json"); _oneNtpServerNodesAnswerJsonObject = JsonPath.parse(_oneNtpServerNodesAnswerStr, _baseConfiguration).json(); _twoNtpServersNodesAnswerStr = CommonUtil.readResource("org/batfish/question/jsonpath/twoNtpServers.json"); _twoNtpServersNodesAnswerJsonObject = JsonPath.parse(_twoNtpServersNodesAnswerStr, _baseConfiguration).json(); ConfigurationBuilder prefixCb = new ConfigurationBuilder(); prefixCb.mappingProvider(_baseConfiguration.mappingProvider()); prefixCb.jsonProvider(_baseConfiguration.jsonProvider()); prefixCb.evaluationListener(_baseConfiguration.getEvaluationListeners()); prefixCb.options(_baseConfiguration.getOptions()); prefixCb.options(Option.ALWAYS_RETURN_LIST); prefixCb.options(Option.AS_PATH_LIST); _prefixConfiguration = prefixCb.build(); }