2 * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.cluster.datastore.node.utils.stream;
11 import static org.junit.Assert.assertEquals;
13 import com.google.common.io.ByteStreams;
14 import java.io.ByteArrayOutputStream;
15 import java.io.IOException;
16 import java.io.StringReader;
17 import java.io.StringWriter;
18 import java.util.Optional;
19 import javax.xml.parsers.DocumentBuilderFactory;
20 import javax.xml.transform.OutputKeys;
21 import javax.xml.transform.Transformer;
22 import javax.xml.transform.TransformerFactory;
23 import javax.xml.transform.dom.DOMSource;
24 import javax.xml.transform.stream.StreamResult;
25 import org.apache.commons.lang.SerializationUtils;
26 import org.junit.Assert;
27 import org.junit.Test;
28 import org.opendaylight.controller.cluster.datastore.util.TestModel;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
34 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
37 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
39 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
40 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
41 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
42 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeBuilder;
43 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
44 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
45 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
46 import org.w3c.dom.Node;
47 import org.xml.sax.InputSource;
49 public class NormalizedNodeStreamReaderWriterTest {
52 public void testNormalizedNodeStreaming() throws IOException {
54 ByteArrayOutputStream bos = new ByteArrayOutputStream();
55 NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos));
57 NormalizedNode<?, ?> testContainer = createTestContainer();
58 nnout.writeNormalizedNode(testContainer);
60 QName toaster = QName.create("http://netconfcentral.org/ns/toaster","2009-11-20","toaster");
61 QName darknessFactor = QName.create("http://netconfcentral.org/ns/toaster","2009-11-20","darknessFactor");
62 QName description = QName.create("http://netconfcentral.org/ns/toaster","2009-11-20","description");
63 ContainerNode toasterNode = Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(toaster))
64 .withChild(ImmutableNodes.leafNode(darknessFactor, "1000"))
65 .withChild(ImmutableNodes.leafNode(description, largeString(20))).build();
67 ContainerNode toasterContainer = Builders.containerBuilder()
68 .withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME)).withChild(toasterNode).build();
69 nnout.writeNormalizedNode(toasterContainer);
71 NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(
74 NormalizedNode<?,?> node = nnin.readNormalizedNode();
75 Assert.assertEquals(testContainer, node);
77 node = nnin.readNormalizedNode();
78 Assert.assertEquals(toasterContainer, node);
81 private static NormalizedNode<?, ?> createTestContainer() {
82 byte[] bytes1 = {1, 2, 3};
83 LeafSetEntryNode<Object> entry1 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
84 new NodeWithValue<>(TestModel.BINARY_LEAF_LIST_QNAME, bytes1)).withValue(bytes1).build();
87 LeafSetEntryNode<Object> entry2 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
88 new NodeWithValue<>(TestModel.BINARY_LEAF_LIST_QNAME, bytes2)).withValue(bytes2).build();
90 return TestModel.createBaseTestContainerBuilder()
91 .withChild(ImmutableLeafSetNodeBuilder.create().withNodeIdentifier(
92 new NodeIdentifier(TestModel.BINARY_LEAF_LIST_QNAME))
93 .withChild(entry1).withChild(entry2).build())
94 .withChild(ImmutableNodes.leafNode(TestModel.SOME_BINARY_DATA_QNAME, new byte[]{1, 2, 3, 4}))
95 .withChild(Builders.orderedMapBuilder()
96 .withNodeIdentifier(new NodeIdentifier(TestModel.ORDERED_LIST_QNAME))
97 .withChild(ImmutableNodes.mapEntry(TestModel.ORDERED_LIST_ENTRY_QNAME,
98 TestModel.ID_QNAME, 11)).build()).build();
102 public void testYangInstanceIdentifierStreaming() throws IOException {
103 YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.TEST_PATH)
104 .node(TestModel.OUTER_LIST_QNAME).nodeWithKey(
105 TestModel.INNER_LIST_QNAME, TestModel.ID_QNAME, 10).build();
107 ByteArrayOutputStream bos = new ByteArrayOutputStream();
108 NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos));
110 nnout.writeYangInstanceIdentifier(path);
112 NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(
115 YangInstanceIdentifier newPath = nnin.readYangInstanceIdentifier();
116 Assert.assertEquals(path, newPath);
120 public void testNormalizedNodeAndYangInstanceIdentifierStreaming() throws IOException {
122 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
123 NormalizedNodeOutputStreamWriter writer = new NormalizedNodeOutputStreamWriter(
124 ByteStreams.newDataOutput(byteArrayOutputStream));
126 NormalizedNode<?, ?> testContainer = TestModel.createBaseTestContainerBuilder().build();
127 writer.writeNormalizedNode(testContainer);
129 YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.TEST_PATH)
130 .node(TestModel.OUTER_LIST_QNAME).nodeWithKey(
131 TestModel.INNER_LIST_QNAME, TestModel.ID_QNAME, 10).build();
133 writer.writeYangInstanceIdentifier(path);
135 NormalizedNodeDataInput reader = NormalizedNodeInputOutput.newDataInput(
136 ByteStreams.newDataInput(byteArrayOutputStream.toByteArray()));
138 NormalizedNode<?,?> node = reader.readNormalizedNode();
139 Assert.assertEquals(testContainer, node);
141 YangInstanceIdentifier newPath = reader.readYangInstanceIdentifier();
142 Assert.assertEquals(path, newPath);
147 @Test(expected = InvalidNormalizedNodeStreamException.class, timeout = 10000)
148 public void testInvalidNormalizedNodeStream() throws IOException {
149 byte[] invalidBytes = {1,2,3};
150 NormalizedNodeDataInput reader = NormalizedNodeInputOutput.newDataInput(
151 ByteStreams.newDataInput(invalidBytes));
153 reader.readNormalizedNode();
156 @Test(expected = InvalidNormalizedNodeStreamException.class, timeout = 10000)
157 public void testInvalidYangInstanceIdentifierStream() throws IOException {
158 byte[] invalidBytes = {1,2,3};
159 NormalizedNodeDataInput reader = NormalizedNodeInputOutput.newDataInput(
160 ByteStreams.newDataInput(invalidBytes));
162 reader.readYangInstanceIdentifier();
166 public void testWithSerializable() {
167 NormalizedNode<?, ?> input = TestModel.createTestContainer();
168 SampleNormalizedNodeSerializable serializable = new SampleNormalizedNodeSerializable(input);
169 SampleNormalizedNodeSerializable clone =
170 (SampleNormalizedNodeSerializable)SerializationUtils.clone(serializable);
172 Assert.assertEquals(input, clone.getInput());
176 public void testAnyXmlStreaming() throws Exception {
177 String xml = "<foo xmlns=\"http://www.w3.org/TR/html4/\" x=\"123\"><bar>one</bar><bar>two</bar></foo>";
178 final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
179 factory.setNamespaceAware(true);
181 Node xmlNode = factory.newDocumentBuilder().parse(
182 new InputSource(new StringReader(xml))).getDocumentElement();
184 assertEquals("http://www.w3.org/TR/html4/", xmlNode.getNamespaceURI());
186 NormalizedNode<?, ?> anyXmlContainer = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
187 new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).withChild(
188 Builders.anyXmlBuilder().withNodeIdentifier(new NodeIdentifier(TestModel.ANY_XML_QNAME))
189 .withValue(new DOMSource(xmlNode)).build()).build();
191 ByteArrayOutputStream bos = new ByteArrayOutputStream();
192 NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos));
194 nnout.writeNormalizedNode(anyXmlContainer);
196 NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(
199 ContainerNode deserialized = (ContainerNode)nnin.readNormalizedNode();
201 Optional<DataContainerChild<? extends PathArgument, ?>> child =
202 deserialized.getChild(new NodeIdentifier(TestModel.ANY_XML_QNAME));
203 assertEquals("AnyXml child present", true, child.isPresent());
205 StreamResult xmlOutput = new StreamResult(new StringWriter());
206 Transformer transformer = TransformerFactory.newInstance().newTransformer();
207 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
208 transformer.transform(((AnyXmlNode)child.get()).getValue(), xmlOutput);
210 assertEquals("XML", xml, xmlOutput.getWriter().toString());
211 assertEquals("http://www.w3.org/TR/html4/", ((AnyXmlNode)child.get()).getValue().getNode().getNamespaceURI());
215 public void testSchemaPathSerialization() throws Exception {
216 final SchemaPath expected = SchemaPath.create(true, TestModel.ANY_XML_QNAME);
218 ByteArrayOutputStream bos = new ByteArrayOutputStream();
219 NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos));
220 nnout.writeSchemaPath(expected);
222 NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(
224 SchemaPath actual = nnin.readSchemaPath();
225 assertEquals(expected, actual);
228 private static String largeString(final int pow) {
229 StringBuilder sb = new StringBuilder("X");
230 for (int i = 0; i < pow; i++) {
233 return sb.toString();