if (!value.isTextual()) { throw new BatfishException( String.format("A Batfish %s must be a JSON string", expectedType.getName())); if (!value.isTextual()) { throw new BatfishException( String.format("A Batfish %s must be a JSON string", expectedType.getName())); if (!(value.isTextual())) { throw new BatfishException( String.format("A Batfish %s must be a JSON string", expectedType.getName())); if (!(value.isTextual())) { throw new BatfishException( String.format("A Batfish %s must be a JSON string", expectedType.getName())); if (!value.isTextual()) { throw new BatfishException( String.format("A Batfish %s must be a JSON string", expectedType.getName())); if (!value.isTextual()) { throw new BatfishException( String.format("A Batfish %s must be a JSON string", expectedType.getName())); if (!value.isBoolean()) { throw new BatfishException( String.format("It is not a valid JSON %s value", expectedType.getName())); String.format(
@Test public void testFromStringCompletionTypes() { assertThat( Variable.Type.fromString(CompletionType.BGP_PEER_PROPERTY.toString()), equalTo(BGP_PEER_PROPERTY_SPEC)); assertThat( Variable.Type.fromString(CompletionType.BGP_PROCESS_PROPERTY.toString()), equalTo(BGP_PROCESS_PROPERTY_SPEC)); assertThat( Variable.Type.fromString(CompletionType.INTERFACE_PROPERTY.toString()), equalTo(INTERFACE_PROPERTY_SPEC)); assertThat( Variable.Type.fromString(CompletionType.NAMED_STRUCTURE.toString()), equalTo(NAMED_STRUCTURE_SPEC)); assertThat(Variable.Type.fromString(CompletionType.NODE.toString()), equalTo(NODE_SPEC)); assertThat( Variable.Type.fromString(CompletionType.NODE_PROPERTY.toString()), equalTo(NODE_PROPERTY_SPEC)); assertThat( Variable.Type.fromString(CompletionType.OSPF_PROPERTY.toString()), equalTo(OSPF_PROPERTY_SPEC)); }
private static Map<String, Variable.Type> initMap() { ImmutableMap.Builder<String, Variable.Type> map = ImmutableMap.builder(); for (Variable.Type value : Type.values()) { String name = value._name.toLowerCase(); map.put(name, value); } return map.build(); }
JsonNode value = variable.getValue(); String valueJsonString = new ObjectMapper().writeValueAsString(value); boolean stringType = variable.getType().getStringType(); boolean setType = variable.getMinElements() != null; if (value != null) {
networkName, snapshotName, Variable.Type.fromString(completionType), query, Strings.isNullOrEmpty(maxSuggestions)
throw new BatfishException( String.format( "A Batfish %s must start with \"/\"", Variable.Type.JSON_PATH_REGEX.getName())); String.format( "A Batfish %s must end in either \"/\" or \"/i\"", Variable.Type.JSON_PATH_REGEX.getName())); String.format( "Invalid %s at interior of %s", Variable.Type.JAVA_REGEX.getName(), Variable.Type.JSON_PATH_REGEX.getName()), e);
/** * Validate that json-encoded {@code jsonPath} is a valid jsonPath dictionary (A valid jsonPath * contains key 'path' which mapping to a String, and an optional key 'suffix' which mapping to a * boolean value). * * @throws BatfishException if {@code jsonPath} is not a valid jsonPath dictionary. */ static void validateJsonPath(JsonNode jsonPath) throws BatfishException { if (!jsonPath.isContainerNode()) { throw new BatfishException( String.format( "Expecting a JSON dictionary for a Batfish %s", Variable.Type.JSON_PATH.getName())); } if (jsonPath.get("path") == null) { throw new BatfishException( String.format("Missing 'path' element of %s", Variable.Type.JSON_PATH.getName())); } if (!jsonPath.get("path").isTextual()) { throw new BatfishException( String.format( "'path' element of %s must be a JSON string", Variable.Type.JSON_PATH.getName())); } if (jsonPath.get("suffix") != null && !jsonPath.get("suffix").isBoolean()) { throw new BatfishException( String.format( "'suffix' element of %s must be a JSON boolean", Variable.Type.JSON_PATH.getName())); } }
@Override public int hashCode() { return Objects.hash( _description, _displayName, _fields, _longDescription, _minElements, _minLength, _optional, _type.ordinal(), _value, _values); }
@Test public void testPathRegexInvalidStart() { String invalidStart = "pathRegex"; _thrown.expect(BatfishException.class); _thrown.expectMessage( equalTo(String.format("A Batfish %s must start with \"/\"", JSON_PATH_REGEX.getName()))); Client.validateJsonPathRegex(invalidStart); }
@Test public void testPathRegexInvalidEnd() { String invalidEnd = "/pathRegex"; _thrown.expect(BatfishException.class); _thrown.expectMessage( equalTo( String.format( "A Batfish %s must end in either \"/\" or \"/i\"", JSON_PATH_REGEX.getName()))); Client.validateJsonPathRegex(invalidEnd); }
@Test public void testNonStringIpProtocolValue() throws IOException { String input = "10"; Type expectedType = IP_PROTOCOL; String expectedMessage = String.format("A Batfish %s must be a JSON string", expectedType.getName()); validateTypeWithInvalidInput(input, expectedMessage, expectedType); }
@Test public void testInvalidZoneValue() throws IOException { String input = "5"; Type expectedType = ZONE; String expectedMessage = String.format("A Batfish %s must be a JSON string", expectedType.getName()); validateTypeWithInvalidInput(input, expectedMessage, expectedType); }
@Test public void testInvalidInterfaceValue() throws IOException { String input = "5"; Type expectedType = INTERFACE; String expectedMessage = String.format("A Batfish %s must be a JSON string", expectedType.getName()); validateTypeWithInvalidInput(input, expectedMessage, expectedType); }
@Test public void testInvalidFlowStateValue() throws IOException { String input = "5"; Type expectedType = FLOW_STATE; String expectedMessage = String.format("A Batfish %s must be a JSON string", expectedType.getName()); validateTypeWithInvalidInput(input, expectedMessage, expectedType); }
@Test public void testInvalidDispositionSpecifierValue() throws IOException { String input = "5"; Type expectedType = DISPOSITION_SPEC; String expectedMessage = String.format("It is not a valid JSON %s value", DISPOSITION_SPEC.getName()); validateTypeWithInvalidInput(input, expectedMessage, expectedType); }
@Test public void testInvalidBgpSessionTypeValue() throws IOException { String input = "5"; Type expectedType = BGP_SESSION_TYPE; String expectedMessage = String.format("A Batfish %s must be a JSON string", expectedType.getName()); validateTypeWithInvalidInput(input, expectedMessage, expectedType); }
@Test public void testEmptyJsonPath() throws IOException { JsonNode emptyPath = _mapper.readTree("\"\""); _thrown.expect(BatfishException.class); _thrown.expectMessage( equalTo( String.format("Expecting a JSON dictionary for a Batfish %s", JSON_PATH.getName()))); Client.validateJsonPath(emptyPath); }
@Test public void testFromStringInvalid() { String name = "blah"; _thrown.expect(BatfishException.class); _thrown.expectMessage( "No " + Variable.Type.class.getSimpleName() + " with name: '" + name + "'"); Variable.Type.fromString(name); }
@Test public void testNonStringIpWildcardValue() throws IOException { String input = "10"; Type expectedType = IP_WILDCARD; String expectedMessage = String.format("A Batfish %s must be a JSON string", expectedType.getName()); validateTypeWithInvalidInput(input, expectedMessage, expectedType); }
@Test public void testNestedContainerPathValue() throws IOException { String invalidJsonPath = "{\"path\" : {\"innerVariable\" : \"content\"}}"; _thrown.expect(BatfishException.class); _thrown.expectMessage( equalTo(String.format("'path' element of %s must be a JSON string", JSON_PATH.getName()))); Client.validateJsonPath(_mapper.readTree(invalidJsonPath)); }