@Override public String toString() { try { return BatfishObjectMapper.mapper().writeValueAsString(this); } catch (JsonProcessingException e) { return "Couldn't serialize AutocompleteSuggestion: " + e.getMessage(); } } }
private Users loadUsers() { try { return BatfishObjectMapper.mapper().readValue(_usersFile.toFile(), Users.class); } catch (IOException e) { throw new BatfishException( String.format("Error loading users from '%s'", _usersFile.toAbsolutePath()), e); } } }
@Override public CompletionMetadata loadCompletionMetadata(NetworkId networkId, SnapshotId snapshotId) throws IOException { Path completionMetadataPath = getSnapshotCompletionMetadataPath(networkId, snapshotId); if (!Files.exists(completionMetadataPath)) { return CompletionMetadata.EMPTY; } String completionMetadataStr = FileUtils.readFileToString(completionMetadataPath.toFile(), UTF_8); return BatfishObjectMapper.mapper() .readValue(completionMetadataStr, new TypeReference<CompletionMetadata>() {}); }
@Override public void storeMajorIssueConfig( NetworkId network, IssueSettingsId majorIssueType, MajorIssueConfig majorIssueConfig) throws IOException { Path path = _d.getMajorIssueConfigDir(network, majorIssueType); if (Files.notExists(path)) { Files.createDirectories(path.getParent()); } CommonUtil.writeFile(path, BatfishObjectMapper.mapper().writeValueAsString(majorIssueConfig)); }
public static AnalysisMetadata readMetadata(NetworkId networkId, AnalysisId analysisId) throws IOException { return BatfishObjectMapper.mapper() .readValue( storage().loadAnalysisMetadata(networkId, analysisId), new TypeReference<AnalysisMetadata>() {}); }
@Test public void serializationCheck() throws IOException { String serialized = BatfishObjectMapper.writePrettyString(FAILURE_SPECIFIER); assertThat( BatfishObjectMapper.mapper().readValue(serialized, DispositionSpecifier.class), equalTo(FAILURE_SPECIFIER)); }
@Test public void constructorFail() throws IOException { String aggStr = "{\"nonamefield\" : \"nodeName\"}"; _thrown.expect(com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.class); BatfishObjectMapper.mapper().readValue(aggStr, Aggregate.class); }
@Test public void constructorFail() throws IOException { String nodeStr = "{\"nonamefield\" : \"nodeName\"}"; _thrown.expect(com.fasterxml.jackson.databind.exc.InvalidDefinitionException.class); BatfishObjectMapper.mapper().readValue(nodeStr, Node.class); }
@Test public void testPutQuestionSettingsError() throws IOException { String settings = "{}"; JsonNode settingsNode = BatfishObjectMapper.mapper().readTree(settings); JsonNodeFactory factory = BatfishObjectMapper.mapper().getNodeFactory(); ObjectNode rootSettingsNode = new ObjectNode(factory); rootSettingsNode.set(PROP1, settingsNode); Response response = getQuestionSettingsJsonPathTarget(BAD_QUESTION, PROP1) .put(Entity.entity(settingsNode, MediaType.APPLICATION_JSON)); assertThat(response.getStatus(), equalTo(Status.INTERNAL_SERVER_ERROR.getStatusCode())); }
@Test public void firstCoversSecondObjectCovers() { JsonNode node1 = BatfishObjectMapper.mapper().createObjectNode().set("key1", new TextNode("value")); JsonNode node2 = BatfishObjectMapper.mapper().createObjectNode().set("key1", new TextNode("value")); // we couldn't just chain the two sets because set returns JsonNode, not ObjectNode ((ObjectNode) node2).set("key2", new TextNode("value")); boolean result = Exclusion.firstCoversSecond(node1, node2); assertThat(result, equalTo(true)); }
@Test public void serialization() { Link link = new Link("src", "dst"); Map<String, String> properties = new HashMap<>(); properties.put("key", "value"); link.setProperties(properties); JsonNode jsonNode = BatfishObjectMapper.mapper().valueToTree(link); assertThat(jsonNode.get("id").asText(), equalTo(Link.getId("src", "dst"))); assertThat(jsonNode.get("srcId").asText(), equalTo("src")); assertThat(jsonNode.get("dstId").asText(), equalTo("dst")); assertThat(jsonNode.get("properties").get("key").asText(), equalTo("value")); }
@Test public void testGetQuestionSettingsJsonPathQuestionPresentDepth2Present() throws IOException { JsonNode settings = BatfishObjectMapper.mapper() .readTree(String.format("{\"%s\":{\"%s\":%d}}", PROP1, PROP2, VAL)); Main.getWorkMgr().writeQuestionSettings(NETWORK, QUESTION, ImmutableList.of(), settings); Response response = getQuestionSettingsJsonPathTarget(QUESTION, String.format("%s/%s", PROP1, PROP2)).get(); assertThat(response.getStatus(), equalTo(OK.getStatusCode())); assertThat(response.readEntity(String.class), equalTo("5")); }
@Test public void getNonEmptyContainer() throws Exception { initNetwork(); initSnapshot(); Response response = _service.getNetwork("100", "0.0.0", _networkName); String entityStr = response.getEntity().toString(); Container network = BatfishObjectMapper.mapper().readValue(entityStr, Container.class); Container expected = Container.of(_networkName, Sets.newTreeSet(Collections.singleton(_snapshotName))); assertThat(network, equalTo(expected)); }
@Test public void testDeSerialization() throws IOException { PrefixRange pr = PrefixRange.fromString("1.2.3.4/8:17-19"); assertThat( BatfishObjectMapper.mapper().readValue("\"1.0.0.0/8:17-19\"", PrefixRange.class), equalTo(pr)); }
@Test public void serialization() { SnapshotMetadata metadata = new SnapshotMetadata(Instant.ofEpochMilli(758949005001L), new SnapshotId("blah")); JsonNode jsonNode = BatfishObjectMapper.mapper().valueToTree(metadata); assertThat(jsonNode.get(PROP_CREATION_TIMESTAMP).asText(), equalTo("1994-01-19T03:10:05.001Z")); assertThat(jsonNode.get(PROP_PARENT_SNAPSHOT_ID).asText(), equalTo("blah")); assertThat( jsonNode.get(PROP_INITIALIZATION_METADATA).get("currentStatus").asText(), equalTo(ProcessingStatus.UNINITIALIZED.toString())); } }
@Test public void constructorProperties() throws IOException { String nodeStr = "{\"name\" : \"nodeName\", \"properties\" : { \"key\": \"value\"}}"; Node node = BatfishObjectMapper.mapper().readValue(nodeStr, Node.class); assertThat(node.getId(), equalTo("node-nodeName")); assertThat(node.getName(), equalTo("nodeName")); assertThat(node.getProperties().size(), equalTo(1)); assertThat(node.getProperties().get("key"), equalTo("value")); }
/** 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); }
@Test public void constructorBasic() throws IOException { String aggStr = "{\"name\" : \"test\"}"; Aggregate aggregate = BatfishObjectMapper.mapper().readValue(aggStr, Aggregate.class); assertThat(aggregate.getId(), equalTo(Aggregate.getId("test"))); assertThat(aggregate.getName(), equalTo("test")); }
@Test public void constructorProperties() throws IOException { String ifaceStr = "{\"nodeId\" : \"node\", \"name\" : \"name\", \"properties\" : { \"key\": \"value\"}}"; Interface iface = BatfishObjectMapper.mapper().readValue(ifaceStr, Interface.class); assertThat(iface.getId(), equalTo(Interface.getId("node", "name"))); assertThat(iface.getNodeId(), equalTo("node")); assertThat(iface.getName(), equalTo("name")); assertThat(iface.getProperties().size(), equalTo(1)); assertThat(iface.getProperties().get("key"), equalTo("value")); }
@Test public void constructorBasic() throws IOException { String ifaceStr = "{\"nodeId\" : \"node\", \"name\" : \"iname\"}"; Interface iface = BatfishObjectMapper.mapper().readValue(ifaceStr, Interface.class); assertThat(iface.getId(), equalTo(Interface.getId("node", "iname"))); assertThat(iface.getNodeId(), equalTo("node")); assertThat(iface.getName(), equalTo("iname")); }