use of org.opendaylight.yangtools.yang.data.api.schema.AnyxmlNode in project controller by opendaylight.
the class NormalizedNodePrunerTest method testAnyXMLNodeNotPrunedWhenHasParent.
@Test
public void testAnyXMLNodeNotPrunedWhenHasParent() throws IOException {
NormalizedNodePruner pruner = prunerFullSchema(TestModel.TEST_PATH);
AnyXmlNode child = Builders.anyXmlBuilder().withNodeIdentifier(new NodeIdentifier(TestModel.ANY_XML_QNAME)).withValue(mock(DOMSource.class)).build();
NormalizedNode<?, ?> input = Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME)).withChild(child).build();
NormalizedNodeWriter.forStreamWriter(pruner).write(input);
NormalizedNode<?, ?> actual = pruner.normalizedNode();
assertEquals("normalizedNode", input, actual);
}
use of org.opendaylight.yangtools.yang.data.api.schema.AnyxmlNode in project controller by opendaylight.
the class NormalizedNodePruner method anyxmlNode.
@SuppressWarnings("unchecked")
@Override
public void anyxmlNode(YangInstanceIdentifier.NodeIdentifier nodeIdentifier, Object value) throws IOException, IllegalArgumentException {
checkNotSealed();
NormalizedNodeBuilderWrapper parent = stack.peek();
AnyXmlNode anyXmlNode = Builders.anyXmlBuilder().withNodeIdentifier(nodeIdentifier).withValue((DOMSource) value).build();
if (parent != null) {
if (hasValidSchema(nodeIdentifier.getNodeType(), parent)) {
parent.builder().addChild(anyXmlNode);
}
} else {
// If there's no parent node then this is a stand alone AnyXmlNode.
if (nodePathSchemaNode != null) {
this.normalizedNode = anyXmlNode;
}
sealed = true;
}
}
use of org.opendaylight.yangtools.yang.data.api.schema.AnyxmlNode in project controller by opendaylight.
the class NormalizedNodeStreamReaderWriterTest method testAnyXmlStreaming.
@Test
public void testAnyXmlStreaming() throws Exception {
String xml = "<foo xmlns=\"http://www.w3.org/TR/html4/\" x=\"123\"><bar>one</bar><bar>two</bar></foo>";
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
Node xmlNode = factory.newDocumentBuilder().parse(new InputSource(new StringReader(xml))).getDocumentElement();
assertEquals("http://www.w3.org/TR/html4/", xmlNode.getNamespaceURI());
NormalizedNode<?, ?> anyXmlContainer = ImmutableContainerNodeBuilder.create().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).withChild(Builders.anyXmlBuilder().withNodeIdentifier(new NodeIdentifier(TestModel.ANY_XML_QNAME)).withValue(new DOMSource(xmlNode)).build()).build();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos));
nnout.writeNormalizedNode(anyXmlContainer);
NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bos.toByteArray()));
ContainerNode deserialized = (ContainerNode) nnin.readNormalizedNode();
Optional<DataContainerChild<? extends PathArgument, ?>> child = deserialized.getChild(new NodeIdentifier(TestModel.ANY_XML_QNAME));
assertEquals("AnyXml child present", true, child.isPresent());
StreamResult xmlOutput = new StreamResult(new StringWriter());
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.transform(((AnyXmlNode) child.get()).getValue(), xmlOutput);
assertEquals("XML", xml, xmlOutput.getWriter().toString());
assertEquals("http://www.w3.org/TR/html4/", ((AnyXmlNode) child.get()).getValue().getNode().getNamespaceURI());
}
use of org.opendaylight.yangtools.yang.data.api.schema.AnyxmlNode in project controller by opendaylight.
the class SerializationUtilsTest method testSerializeDeserializeAnyXmlNode.
@Test
public void testSerializeDeserializeAnyXmlNode() throws Exception {
final ByteArrayInputStream is = new ByteArrayInputStream("<xml><data/></xml>".getBytes(Charset.defaultCharset()));
final Document parse = UntrustedXML.newDocumentBuilder().parse(is);
final AnyXmlNode anyXmlNode = Builders.anyXmlBuilder().withNodeIdentifier(id("anyXmlNode")).withValue(new DOMSource(parse)).build();
final byte[] bytes = SerializationUtils.serializeNormalizedNode(anyXmlNode);
final NormalizedNode<?, ?> deserialized = SerializationUtils.deserializeNormalizedNode(bytes);
final DOMSource value = (DOMSource) deserialized.getValue();
final Diff diff = XMLUnit.compareXML((Document) anyXmlNode.getValue().getNode(), value.getNode().getOwnerDocument());
Assert.assertTrue(diff.toString(), diff.similar());
}
Aggregations