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.Empty;
30 import org.opendaylight.yangtools.yang.common.QName;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
35 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
38 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
40 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
41 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
42 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
43 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeBuilder;
44 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
45 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
46 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
47 import org.w3c.dom.Node;
48 import org.xml.sax.InputSource;
50 public class NormalizedNodeStreamReaderWriterTest {
53 public void testNormalizedNodeStreaming() throws IOException {
55 ByteArrayOutputStream bos = new ByteArrayOutputStream();
56 NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos));
58 NormalizedNode<?, ?> testContainer = createTestContainer();
59 nnout.writeNormalizedNode(testContainer);
61 QName toaster = QName.create("http://netconfcentral.org/ns/toaster","2009-11-20","toaster");
62 QName darknessFactor = QName.create("http://netconfcentral.org/ns/toaster","2009-11-20","darknessFactor");
63 QName description = QName.create("http://netconfcentral.org/ns/toaster","2009-11-20","description");
64 ContainerNode toasterNode = Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(toaster))
65 .withChild(ImmutableNodes.leafNode(darknessFactor, "1000"))
66 .withChild(ImmutableNodes.leafNode(description, largeString(20))).build();
68 ContainerNode toasterContainer = Builders.containerBuilder()
69 .withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME)).withChild(toasterNode).build();
70 nnout.writeNormalizedNode(toasterContainer);
72 final byte[] bytes = bos.toByteArray();
73 assertEquals(1049950, bytes.length);
75 NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
77 NormalizedNode<?, ?> node = nnin.readNormalizedNode();
78 Assert.assertEquals(testContainer, node);
80 node = nnin.readNormalizedNode();
81 Assert.assertEquals(toasterContainer, node);
84 private static NormalizedNode<?, ?> createTestContainer() {
85 byte[] bytes1 = {1, 2, 3};
86 LeafSetEntryNode<Object> entry1 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
87 new NodeWithValue<>(TestModel.BINARY_LEAF_LIST_QNAME, bytes1)).withValue(bytes1).build();
90 LeafSetEntryNode<Object> entry2 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
91 new NodeWithValue<>(TestModel.BINARY_LEAF_LIST_QNAME, bytes2)).withValue(bytes2).build();
93 return TestModel.createBaseTestContainerBuilder()
94 .withChild(ImmutableLeafSetNodeBuilder.create().withNodeIdentifier(
95 new NodeIdentifier(TestModel.BINARY_LEAF_LIST_QNAME))
96 .withChild(entry1).withChild(entry2).build())
97 .withChild(ImmutableNodes.leafNode(TestModel.SOME_BINARY_DATA_QNAME, new byte[]{1, 2, 3, 4}))
98 .withChild(ImmutableNodes.leafNode(TestModel.EMPTY_QNAME, Empty.getInstance()))
99 .withChild(Builders.orderedMapBuilder()
100 .withNodeIdentifier(new NodeIdentifier(TestModel.ORDERED_LIST_QNAME))
101 .withChild(ImmutableNodes.mapEntry(TestModel.ORDERED_LIST_ENTRY_QNAME,
102 TestModel.ID_QNAME, 11)).build()).build();
106 public void testYangInstanceIdentifierStreaming() throws IOException {
107 YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.TEST_PATH)
108 .node(TestModel.OUTER_LIST_QNAME).nodeWithKey(
109 TestModel.INNER_LIST_QNAME, TestModel.ID_QNAME, 10).build();
111 ByteArrayOutputStream bos = new ByteArrayOutputStream();
112 NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos));
114 nnout.writeYangInstanceIdentifier(path);
116 final byte[] bytes = bos.toByteArray();
117 assertEquals(161, bytes.length);
119 NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
121 YangInstanceIdentifier newPath = nnin.readYangInstanceIdentifier();
122 Assert.assertEquals(path, newPath);
126 public void testNormalizedNodeAndYangInstanceIdentifierStreaming() throws IOException {
128 ByteArrayOutputStream bos = new ByteArrayOutputStream();
129 NormalizedNodeDataOutput writer = NormalizedNodeInputOutput.newDataOutput(
130 ByteStreams.newDataOutput(bos));
132 NormalizedNode<?, ?> testContainer = TestModel.createBaseTestContainerBuilder().build();
133 writer.writeNormalizedNode(testContainer);
135 YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.TEST_PATH)
136 .node(TestModel.OUTER_LIST_QNAME).nodeWithKey(
137 TestModel.INNER_LIST_QNAME, TestModel.ID_QNAME, 10).build();
139 writer.writeYangInstanceIdentifier(path);
141 final byte[] bytes = bos.toByteArray();
142 assertEquals(1163, bytes.length);
144 NormalizedNodeDataInput reader = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
146 NormalizedNode<?,?> node = reader.readNormalizedNode();
147 Assert.assertEquals(testContainer, node);
149 YangInstanceIdentifier newPath = reader.readYangInstanceIdentifier();
150 Assert.assertEquals(path, newPath);
155 @Test(expected = InvalidNormalizedNodeStreamException.class, timeout = 10000)
156 public void testInvalidNormalizedNodeStream() throws IOException {
157 byte[] invalidBytes = {1, 2, 3};
158 NormalizedNodeDataInput reader = NormalizedNodeInputOutput.newDataInput(
159 ByteStreams.newDataInput(invalidBytes));
161 reader.readNormalizedNode();
164 @Test(expected = InvalidNormalizedNodeStreamException.class, timeout = 10000)
165 public void testInvalidYangInstanceIdentifierStream() throws IOException {
166 byte[] invalidBytes = {1,2,3};
167 NormalizedNodeDataInput reader = NormalizedNodeInputOutput.newDataInput(
168 ByteStreams.newDataInput(invalidBytes));
170 reader.readYangInstanceIdentifier();
174 public void testWithSerializable() {
175 NormalizedNode<?, ?> input = TestModel.createTestContainer();
176 SampleNormalizedNodeSerializable serializable = new SampleNormalizedNodeSerializable(input);
177 SampleNormalizedNodeSerializable clone =
178 (SampleNormalizedNodeSerializable)SerializationUtils.clone(serializable);
180 Assert.assertEquals(input, clone.getInput());
184 public void testAnyXmlStreaming() throws Exception {
185 String xml = "<foo xmlns=\"http://www.w3.org/TR/html4/\" x=\"123\"><bar>one</bar><bar>two</bar></foo>";
186 final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
187 factory.setNamespaceAware(true);
189 Node xmlNode = factory.newDocumentBuilder().parse(
190 new InputSource(new StringReader(xml))).getDocumentElement();
192 assertEquals("http://www.w3.org/TR/html4/", xmlNode.getNamespaceURI());
194 NormalizedNode<?, ?> anyXmlContainer = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
195 new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).withChild(
196 Builders.anyXmlBuilder().withNodeIdentifier(new NodeIdentifier(TestModel.ANY_XML_QNAME))
197 .withValue(new DOMSource(xmlNode)).build()).build();
199 ByteArrayOutputStream bos = new ByteArrayOutputStream();
200 NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos));
202 nnout.writeNormalizedNode(anyXmlContainer);
204 final byte[] bytes = bos.toByteArray();
205 assertEquals(235, bytes.length);
207 NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
209 ContainerNode deserialized = (ContainerNode)nnin.readNormalizedNode();
211 Optional<DataContainerChild<? extends PathArgument, ?>> child =
212 deserialized.getChild(new NodeIdentifier(TestModel.ANY_XML_QNAME));
213 assertEquals("AnyXml child present", true, child.isPresent());
215 StreamResult xmlOutput = new StreamResult(new StringWriter());
216 Transformer transformer = TransformerFactory.newInstance().newTransformer();
217 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
218 transformer.transform(((AnyXmlNode)child.get()).getValue(), xmlOutput);
220 assertEquals("XML", xml, xmlOutput.getWriter().toString());
221 assertEquals("http://www.w3.org/TR/html4/", ((AnyXmlNode)child.get()).getValue().getNode().getNamespaceURI());
225 public void testSchemaPathSerialization() throws IOException {
226 final SchemaPath expected = SchemaPath.create(true, TestModel.ANY_XML_QNAME);
228 ByteArrayOutputStream bos = new ByteArrayOutputStream();
229 try (NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos))) {
230 nnout.writeSchemaPath(expected);
233 final byte[] bytes = bos.toByteArray();
234 assertEquals(100, bytes.length);
236 NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
237 assertEquals(expected, nnin.readSchemaPath());
241 public void testWritePathArgument() throws IOException {
242 final NodeIdentifier expected = new NodeIdentifier(TestModel.BOOLEAN_LEAF_QNAME);
244 ByteArrayOutputStream bos = new ByteArrayOutputStream();
246 try (NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos))) {
247 nnout.writePathArgument(expected);
250 final byte[] bytes = bos.toByteArray();
251 assertEquals(105, bytes.length);
253 NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
254 assertEquals(expected, nnin.readPathArgument());
257 private static String largeString(final int pow) {
258 StringBuilder sb = new StringBuilder("X");
259 for (int i = 0; i < pow; i++) {
262 return sb.toString();