+ }
+
+ @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);
+
+ final byte[] bytes = bos.toByteArray();
+ assertEquals(229, bytes.length);
+
+ NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
+
+ 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(((DOMSourceAnyxmlNode)child.get()).getValue(), xmlOutput);
+
+ assertEquals("XML", xml, xmlOutput.getWriter().toString());
+ assertEquals("http://www.w3.org/TR/html4/",
+ ((DOMSourceAnyxmlNode)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);
+ }
+
+ final byte[] bytes = bos.toByteArray();
+ assertEquals(99, bytes.length);
+
+ NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
+ 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);
+ }
+
+ final byte[] bytes = bos.toByteArray();
+ assertEquals(103, bytes.length);
+
+ NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
+ assertEquals(expected, nnin.readPathArgument());
+ }
+
+ /*
+ * This tests the encoding of a MapNode with a lot of entries, each of them having the key leaf (a string)
+ * and an integer.
+ */
+ @Test
+ public void testHugeEntries() throws IOException {
+ final CollectionNodeBuilder<MapEntryNode, MapNode> mapBuilder = Builders.mapBuilder()
+ .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME));
+ final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> entryBuilder =
+ Builders.mapEntryBuilder().withChild(ImmutableNodes.leafNode(TestModel.DESC_QNAME, (byte) 42));
+
+ for (int i = 0; i < 100_000; ++i) {
+ final String key = "xyzzy" + i;
+ mapBuilder.addChild(entryBuilder
+ .withNodeIdentifier(NodeIdentifierWithPredicates.of(TestModel.TEST_QNAME,
+ TestModel.CHILD_NAME_QNAME, key))
+ .withChild(ImmutableNodes.leafNode(TestModel.CHILD_NAME_QNAME, key))
+ .build());
+ }
+
+ final MapNode expected = mapBuilder.build();
+ final ByteArrayOutputStream bos = new ByteArrayOutputStream();
+
+ try (NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos))) {
+ nnout.writeNormalizedNode(expected);
+ }
+
+ final byte[] bytes = bos.toByteArray();
+ assertEquals(2_289_103, bytes.length);
+
+ NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
+ assertEquals(expected, nnin.readNormalizedNode());
+ }
+
+ @Test
+ public void testAugmentationIdentifier() throws IOException {
+ final List<QName> qnames = new ArrayList<>();
+ for (int i = 0; i < 257; ++i) {
+ qnames.add(QName.create(TestModel.TEST_QNAME, "a" + i));
+ }
+
+ for (int i = 0; i < qnames.size(); ++i) {
+ final AugmentationIdentifier expected = AugmentationIdentifier.create(
+ ImmutableSet.copyOf(qnames.subList(0, i)));
+
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+
+ try (NormalizedNodeDataOutput nnout =
+ NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos),
+ NormalizedNodeStreamVersion.SODIUM_SR1)) {
+ nnout.writePathArgument(expected);
+ }
+
+ final byte[] bytes = bos.toByteArray();
+
+ NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
+ PathArgument arg = nnin.readPathArgument();
+ assertEquals(expected, arg);
+ }
+ }