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 NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(
75 NormalizedNode<?,?> node = nnin.readNormalizedNode();
76 Assert.assertEquals(testContainer, node);
78 node = nnin.readNormalizedNode();
79 Assert.assertEquals(toasterContainer, node);
82 private static NormalizedNode<?, ?> createTestContainer() {
83 byte[] bytes1 = {1, 2, 3};
84 LeafSetEntryNode<Object> entry1 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
85 new NodeWithValue<>(TestModel.BINARY_LEAF_LIST_QNAME, bytes1)).withValue(bytes1).build();
88 LeafSetEntryNode<Object> entry2 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
89 new NodeWithValue<>(TestModel.BINARY_LEAF_LIST_QNAME, bytes2)).withValue(bytes2).build();
91 return TestModel.createBaseTestContainerBuilder()
92 .withChild(ImmutableLeafSetNodeBuilder.create().withNodeIdentifier(
93 new NodeIdentifier(TestModel.BINARY_LEAF_LIST_QNAME))
94 .withChild(entry1).withChild(entry2).build())
95 .withChild(ImmutableNodes.leafNode(TestModel.SOME_BINARY_DATA_QNAME, new byte[]{1, 2, 3, 4}))
96 .withChild(ImmutableNodes.leafNode(TestModel.EMPTY_QNAME, Empty.getInstance()))
97 .withChild(Builders.orderedMapBuilder()
98 .withNodeIdentifier(new NodeIdentifier(TestModel.ORDERED_LIST_QNAME))
99 .withChild(ImmutableNodes.mapEntry(TestModel.ORDERED_LIST_ENTRY_QNAME,
100 TestModel.ID_QNAME, 11)).build()).build();
104 public void testYangInstanceIdentifierStreaming() throws IOException {
105 YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.TEST_PATH)
106 .node(TestModel.OUTER_LIST_QNAME).nodeWithKey(
107 TestModel.INNER_LIST_QNAME, TestModel.ID_QNAME, 10).build();
109 ByteArrayOutputStream bos = new ByteArrayOutputStream();
110 NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos));
112 nnout.writeYangInstanceIdentifier(path);
114 NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(
117 YangInstanceIdentifier newPath = nnin.readYangInstanceIdentifier();
118 Assert.assertEquals(path, newPath);
122 public void testNormalizedNodeAndYangInstanceIdentifierStreaming() throws IOException {
124 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
125 NormalizedNodeDataOutput writer = NormalizedNodeInputOutput.newDataOutput(
126 ByteStreams.newDataOutput(byteArrayOutputStream));
128 NormalizedNode<?, ?> testContainer = TestModel.createBaseTestContainerBuilder().build();
129 writer.writeNormalizedNode(testContainer);
131 YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.TEST_PATH)
132 .node(TestModel.OUTER_LIST_QNAME).nodeWithKey(
133 TestModel.INNER_LIST_QNAME, TestModel.ID_QNAME, 10).build();
135 writer.writeYangInstanceIdentifier(path);
137 NormalizedNodeDataInput reader = NormalizedNodeInputOutput.newDataInput(
138 ByteStreams.newDataInput(byteArrayOutputStream.toByteArray()));
140 NormalizedNode<?,?> node = reader.readNormalizedNode();
141 Assert.assertEquals(testContainer, node);
143 YangInstanceIdentifier newPath = reader.readYangInstanceIdentifier();
144 Assert.assertEquals(path, newPath);
149 @Test(expected = InvalidNormalizedNodeStreamException.class, timeout = 10000)
150 public void testInvalidNormalizedNodeStream() throws IOException {
151 byte[] invalidBytes = {1,2,3};
152 NormalizedNodeDataInput reader = NormalizedNodeInputOutput.newDataInput(
153 ByteStreams.newDataInput(invalidBytes));
155 reader.readNormalizedNode();
158 @Test(expected = InvalidNormalizedNodeStreamException.class, timeout = 10000)
159 public void testInvalidYangInstanceIdentifierStream() throws IOException {
160 byte[] invalidBytes = {1,2,3};
161 NormalizedNodeDataInput reader = NormalizedNodeInputOutput.newDataInput(
162 ByteStreams.newDataInput(invalidBytes));
164 reader.readYangInstanceIdentifier();
168 public void testWithSerializable() {
169 NormalizedNode<?, ?> input = TestModel.createTestContainer();
170 SampleNormalizedNodeSerializable serializable = new SampleNormalizedNodeSerializable(input);
171 SampleNormalizedNodeSerializable clone =
172 (SampleNormalizedNodeSerializable)SerializationUtils.clone(serializable);
174 Assert.assertEquals(input, clone.getInput());
178 public void testAnyXmlStreaming() throws Exception {
179 String xml = "<foo xmlns=\"http://www.w3.org/TR/html4/\" x=\"123\"><bar>one</bar><bar>two</bar></foo>";
180 final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
181 factory.setNamespaceAware(true);
183 Node xmlNode = factory.newDocumentBuilder().parse(
184 new InputSource(new StringReader(xml))).getDocumentElement();
186 assertEquals("http://www.w3.org/TR/html4/", xmlNode.getNamespaceURI());
188 NormalizedNode<?, ?> anyXmlContainer = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
189 new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).withChild(
190 Builders.anyXmlBuilder().withNodeIdentifier(new NodeIdentifier(TestModel.ANY_XML_QNAME))
191 .withValue(new DOMSource(xmlNode)).build()).build();
193 ByteArrayOutputStream bos = new ByteArrayOutputStream();
194 NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos));
196 nnout.writeNormalizedNode(anyXmlContainer);
198 NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(
201 ContainerNode deserialized = (ContainerNode)nnin.readNormalizedNode();
203 Optional<DataContainerChild<? extends PathArgument, ?>> child =
204 deserialized.getChild(new NodeIdentifier(TestModel.ANY_XML_QNAME));
205 assertEquals("AnyXml child present", true, child.isPresent());
207 StreamResult xmlOutput = new StreamResult(new StringWriter());
208 Transformer transformer = TransformerFactory.newInstance().newTransformer();
209 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
210 transformer.transform(((AnyXmlNode)child.get()).getValue(), xmlOutput);
212 assertEquals("XML", xml, xmlOutput.getWriter().toString());
213 assertEquals("http://www.w3.org/TR/html4/", ((AnyXmlNode)child.get()).getValue().getNode().getNamespaceURI());
217 public void testSchemaPathSerialization() throws IOException {
218 final SchemaPath expected = SchemaPath.create(true, TestModel.ANY_XML_QNAME);
220 ByteArrayOutputStream bos = new ByteArrayOutputStream();
221 try (NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos))) {
222 nnout.writeSchemaPath(expected);
225 NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(
227 assertEquals(expected, nnin.readSchemaPath());
231 public void testWritePathArgument() throws IOException {
232 final NodeIdentifier expected = new NodeIdentifier(TestModel.BOOLEAN_LEAF_QNAME);
234 ByteArrayOutputStream bos = new ByteArrayOutputStream();
236 try (NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos))) {
237 nnout.writePathArgument(expected);
240 NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(
242 assertEquals(expected, nnin.readPathArgument());
245 private static String largeString(final int pow) {
246 StringBuilder sb = new StringBuilder("X");
247 for (int i = 0; i < pow; i++) {
250 return sb.toString();