+ @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());
+ }
+
+ @Test
+ public void testSchemaPathSerialization() throws IOException {
+ final SchemaPath expected = SchemaPath.create(true, TestModel.ANY_XML_QNAME);
+
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ try (NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos))) {
+ nnout.writeSchemaPath(expected);
+ }
+
+ NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(
+ bos.toByteArray()));
+ assertEquals(expected, nnin.readSchemaPath());
+ }
+
+ @Test
+ public void testWritePathArgument() throws IOException {
+ final NodeIdentifier expected = new NodeIdentifier(TestModel.BOOLEAN_LEAF_QNAME);
+
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+
+ try (NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos))) {
+ nnout.writePathArgument(expected);
+ }
+
+ NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(
+ bos.toByteArray()));
+ assertEquals(expected, nnin.readPathArgument());
+ }
+
+ private static String largeString(final int pow) {
+ StringBuilder sb = new StringBuilder("X");
+ for (int i = 0; i < pow; i++) {
+ sb.append(sb);
+ }
+ return sb.toString();