Switch default stream output to Sodium SR1
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / test / java / org / opendaylight / controller / cluster / datastore / node / utils / stream / NormalizedNodeStreamReaderWriterTest.java
1 /*
2  * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.cluster.datastore.node.utils.stream;
10
11 import static org.junit.Assert.assertEquals;
12
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.NodeIdentifierWithPredicates;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
36 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
39 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
42 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
43 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
44 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
45 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
46 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
47 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
48 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeBuilder;
49 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
50 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
51 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
52 import org.w3c.dom.Node;
53 import org.xml.sax.InputSource;
54
55 public class NormalizedNodeStreamReaderWriterTest {
56
57     @Test
58     public void testNormalizedNodeStreaming() throws IOException {
59
60         ByteArrayOutputStream bos = new ByteArrayOutputStream();
61         NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos));
62
63         NormalizedNode<?, ?> testContainer = createTestContainer();
64         nnout.writeNormalizedNode(testContainer);
65
66         QName toaster = QName.create("http://netconfcentral.org/ns/toaster","2009-11-20","toaster");
67         QName darknessFactor = QName.create("http://netconfcentral.org/ns/toaster","2009-11-20","darknessFactor");
68         QName description = QName.create("http://netconfcentral.org/ns/toaster","2009-11-20","description");
69         ContainerNode toasterNode = Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(toaster))
70                 .withChild(ImmutableNodes.leafNode(darknessFactor, "1000"))
71                 .withChild(ImmutableNodes.leafNode(description, largeString(20))).build();
72
73         ContainerNode toasterContainer = Builders.containerBuilder()
74                 .withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME)).withChild(toasterNode).build();
75         nnout.writeNormalizedNode(toasterContainer);
76
77         final byte[] bytes = bos.toByteArray();
78         assertEquals(1049619, bytes.length);
79
80         NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
81
82         NormalizedNode<?, ?> node = nnin.readNormalizedNode();
83         Assert.assertEquals(testContainer, node);
84
85         node = nnin.readNormalizedNode();
86         Assert.assertEquals(toasterContainer, node);
87     }
88
89     private static NormalizedNode<?, ?> createTestContainer() {
90         byte[] bytes1 = {1, 2, 3};
91         LeafSetEntryNode<Object> entry1 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
92                 new NodeWithValue<>(TestModel.BINARY_LEAF_LIST_QNAME, bytes1)).withValue(bytes1).build();
93
94         byte[] bytes2 = {};
95         LeafSetEntryNode<Object> entry2 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
96                 new NodeWithValue<>(TestModel.BINARY_LEAF_LIST_QNAME, bytes2)).withValue(bytes2).build();
97
98         return TestModel.createBaseTestContainerBuilder()
99                 .withChild(ImmutableLeafSetNodeBuilder.create().withNodeIdentifier(
100                         new NodeIdentifier(TestModel.BINARY_LEAF_LIST_QNAME))
101                         .withChild(entry1).withChild(entry2).build())
102                 .withChild(ImmutableNodes.leafNode(TestModel.SOME_BINARY_DATA_QNAME, new byte[]{1, 2, 3, 4}))
103                 .withChild(ImmutableNodes.leafNode(TestModel.EMPTY_QNAME, Empty.getInstance()))
104                 .withChild(Builders.orderedMapBuilder()
105                       .withNodeIdentifier(new NodeIdentifier(TestModel.ORDERED_LIST_QNAME))
106                       .withChild(ImmutableNodes.mapEntry(TestModel.ORDERED_LIST_ENTRY_QNAME,
107                               TestModel.ID_QNAME, 11)).build()).build();
108     }
109
110     @Test
111     public void testYangInstanceIdentifierStreaming() throws IOException  {
112         YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.TEST_PATH)
113                 .node(TestModel.OUTER_LIST_QNAME).nodeWithKey(
114                         TestModel.INNER_LIST_QNAME, TestModel.ID_QNAME, 10).build();
115
116         ByteArrayOutputStream bos = new ByteArrayOutputStream();
117         NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos));
118
119         nnout.writeYangInstanceIdentifier(path);
120
121         final byte[] bytes = bos.toByteArray();
122         assertEquals(139, bytes.length);
123
124         NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
125
126         YangInstanceIdentifier newPath = nnin.readYangInstanceIdentifier();
127         Assert.assertEquals(path, newPath);
128     }
129
130     @Test
131     public void testNormalizedNodeAndYangInstanceIdentifierStreaming() throws IOException {
132
133         ByteArrayOutputStream bos = new ByteArrayOutputStream();
134         NormalizedNodeDataOutput writer = NormalizedNodeInputOutput.newDataOutput(
135             ByteStreams.newDataOutput(bos));
136
137         NormalizedNode<?, ?> testContainer = TestModel.createBaseTestContainerBuilder().build();
138         writer.writeNormalizedNode(testContainer);
139
140         YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.TEST_PATH)
141                 .node(TestModel.OUTER_LIST_QNAME).nodeWithKey(
142                         TestModel.INNER_LIST_QNAME, TestModel.ID_QNAME, 10).build();
143
144         writer.writeYangInstanceIdentifier(path);
145
146         final byte[] bytes = bos.toByteArray();
147         assertEquals(826, bytes.length);
148
149         NormalizedNodeDataInput reader = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
150
151         NormalizedNode<?,?> node = reader.readNormalizedNode();
152         Assert.assertEquals(testContainer, node);
153
154         YangInstanceIdentifier newPath = reader.readYangInstanceIdentifier();
155         Assert.assertEquals(path, newPath);
156
157         writer.close();
158     }
159
160     @Test(expected = InvalidNormalizedNodeStreamException.class, timeout = 10000)
161     public void testInvalidNormalizedNodeStream() throws IOException {
162         byte[] invalidBytes = {1, 2, 3};
163         NormalizedNodeDataInput reader = NormalizedNodeInputOutput.newDataInput(
164                 ByteStreams.newDataInput(invalidBytes));
165
166         reader.readNormalizedNode();
167     }
168
169     @Test(expected = InvalidNormalizedNodeStreamException.class, timeout = 10000)
170     public void testInvalidYangInstanceIdentifierStream() throws IOException {
171         byte[] invalidBytes = {1,2,3};
172         NormalizedNodeDataInput reader = NormalizedNodeInputOutput.newDataInput(
173             ByteStreams.newDataInput(invalidBytes));
174
175         reader.readYangInstanceIdentifier();
176     }
177
178     @Test
179     public void testWithSerializable() {
180         NormalizedNode<?, ?> input = TestModel.createTestContainer();
181         SampleNormalizedNodeSerializable serializable = new SampleNormalizedNodeSerializable(input);
182         SampleNormalizedNodeSerializable clone =
183                 (SampleNormalizedNodeSerializable)SerializationUtils.clone(serializable);
184
185         Assert.assertEquals(input, clone.getInput());
186     }
187
188     @Test
189     public void testAnyXmlStreaming() throws Exception {
190         String xml = "<foo xmlns=\"http://www.w3.org/TR/html4/\" x=\"123\"><bar>one</bar><bar>two</bar></foo>";
191         final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
192         factory.setNamespaceAware(true);
193
194         Node xmlNode = factory.newDocumentBuilder().parse(
195                 new InputSource(new StringReader(xml))).getDocumentElement();
196
197         assertEquals("http://www.w3.org/TR/html4/", xmlNode.getNamespaceURI());
198
199         NormalizedNode<?, ?> anyXmlContainer = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
200                 new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).withChild(
201                         Builders.anyXmlBuilder().withNodeIdentifier(new NodeIdentifier(TestModel.ANY_XML_QNAME))
202                             .withValue(new DOMSource(xmlNode)).build()).build();
203
204         ByteArrayOutputStream bos = new ByteArrayOutputStream();
205         NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos));
206
207         nnout.writeNormalizedNode(anyXmlContainer);
208
209         final byte[] bytes = bos.toByteArray();
210         assertEquals(229, bytes.length);
211
212         NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
213
214         ContainerNode deserialized = (ContainerNode)nnin.readNormalizedNode();
215
216         Optional<DataContainerChild<? extends PathArgument, ?>> child =
217                 deserialized.getChild(new NodeIdentifier(TestModel.ANY_XML_QNAME));
218         assertEquals("AnyXml child present", true, child.isPresent());
219
220         StreamResult xmlOutput = new StreamResult(new StringWriter());
221         Transformer transformer = TransformerFactory.newInstance().newTransformer();
222         transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
223         transformer.transform(((AnyXmlNode)child.get()).getValue(), xmlOutput);
224
225         assertEquals("XML", xml, xmlOutput.getWriter().toString());
226         assertEquals("http://www.w3.org/TR/html4/", ((AnyXmlNode)child.get()).getValue().getNode().getNamespaceURI());
227     }
228
229     @Test
230     public void testSchemaPathSerialization() throws IOException {
231         final SchemaPath expected = SchemaPath.create(true, TestModel.ANY_XML_QNAME);
232
233         ByteArrayOutputStream bos = new ByteArrayOutputStream();
234         try (NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos))) {
235             nnout.writeSchemaPath(expected);
236         }
237
238         final byte[] bytes = bos.toByteArray();
239         assertEquals(99, bytes.length);
240
241         NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
242         assertEquals(expected, nnin.readSchemaPath());
243     }
244
245     @Test
246     public void testWritePathArgument() throws IOException {
247         final NodeIdentifier expected = new NodeIdentifier(TestModel.BOOLEAN_LEAF_QNAME);
248
249         ByteArrayOutputStream bos = new ByteArrayOutputStream();
250
251         try (NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos))) {
252             nnout.writePathArgument(expected);
253         }
254
255         final byte[] bytes = bos.toByteArray();
256         assertEquals(103, bytes.length);
257
258         NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
259         assertEquals(expected, nnin.readPathArgument());
260     }
261
262     /*
263      * This tests the encoding of a MapNode with a lot of entries, each of them having the key leaf (a string)
264      * and an integer.
265      */
266     @Test
267     public void testHugeEntries() throws IOException {
268         final CollectionNodeBuilder<MapEntryNode, MapNode> mapBuilder = Builders.mapBuilder()
269                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME));
270         final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> entryBuilder =
271                 Builders.mapEntryBuilder().withChild(ImmutableNodes.leafNode(TestModel.DESC_QNAME, (byte) 42));
272
273         for (int i = 0; i < 100_000; ++i) {
274             final String key = "xyzzy" + i;
275             mapBuilder.addChild(entryBuilder
276                 .withNodeIdentifier(NodeIdentifierWithPredicates.of(TestModel.TEST_QNAME,
277                     TestModel.CHILD_NAME_QNAME, key))
278                 .withChild(ImmutableNodes.leafNode(TestModel.CHILD_NAME_QNAME, key))
279                 .build());
280         }
281
282         final MapNode expected = mapBuilder.build();
283         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
284
285         try (NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos))) {
286             nnout.writeNormalizedNode(expected);
287         }
288
289         final byte[] bytes = bos.toByteArray();
290         assertEquals(2_289_103, bytes.length);
291
292         NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
293         assertEquals(expected, nnin.readNormalizedNode());
294     }
295
296     private static String largeString(final int pow) {
297         StringBuilder sb = new StringBuilder("X");
298         for (int i = 0; i < pow; i++) {
299             sb.append(sb);
300         }
301         return sb.toString();
302     }
303 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.