@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")); }
@Test public void serialization() { Interface iface = new Interface("node", "name", InterfaceType.PHYSICAL); Map<String, String> properties = new HashMap<>(); properties.put("key", "value"); iface.setProperties(properties); JsonNode jsonNode = BatfishObjectMapper.mapper().valueToTree(iface); assertThat(jsonNode.get("id").asText(), equalTo(Interface.getId("node", "name"))); assertThat(jsonNode.get("nodeId").asText(), equalTo("node")); assertThat(jsonNode.get("name").asText(), equalTo("name")); assertThat(jsonNode.get("type").asText(), equalTo("PHYSICAL")); assertThat(jsonNode.get("properties").get("key").asText(), equalTo("value")); } }
@JsonCreator public Interface( @JsonProperty(PROP_ID) String id, @Nonnull @JsonProperty(PROP_NODE_ID) String nodeId, @Nonnull @JsonProperty(PROP_NAME) String name, @JsonProperty(PROP_TYPE) InterfaceType type) { super(firstNonNull(id, getId(nodeId, name))); _nodeId = nodeId; _name = name; _type = firstNonNull(type, InterfaceType.UNKNOWN); if (nodeId == null) { throw new IllegalArgumentException("Cannot build interface: nodeId is null"); } if (name == null) { throw new IllegalArgumentException("Cannot build interface: name is null"); } }
@Test public void serialization() { Topology topo = new Topology("testrig"); topo.getAggregates().add(new Aggregate("cloud")); topo.getInterfaces().add(new Interface("node", "name")); topo.getLinks().add(new Link("src", "dst")); topo.getNodes().add(new Node("node")); Map<String, String> properties = new HashMap<>(); properties.put("key", "value"); topo.setProperties(properties); JsonNode jsonNode = BatfishObjectMapper.mapper().valueToTree(topo); assertThat(jsonNode.get("id").asText(), equalTo(Topology.getId("testrig"))); assertThat(jsonNode.get("aggregates").get(0).get("name").asText(), equalTo("cloud")); assertThat(jsonNode.get("interfaces").get(0).get("nodeId").asText(), equalTo("node")); assertThat(jsonNode.get("interfaces").get(0).get("name").asText(), equalTo("name")); assertThat(jsonNode.get("links").get(0).get("srcId").asText(), equalTo("src")); assertThat(jsonNode.get("links").get(0).get("dstId").asText(), equalTo("dst")); assertThat(jsonNode.get("nodes").get(0).get("name").asText(), equalTo("node")); assertThat(jsonNode.get("properties").get("key").asText(), equalTo("value")); } }