Add encoding size asserts
[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.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;
49
50 public class NormalizedNodeStreamReaderWriterTest {
51
52     @Test
53     public void testNormalizedNodeStreaming() throws IOException {
54
55         ByteArrayOutputStream bos = new ByteArrayOutputStream();
56         NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos));
57
58         NormalizedNode<?, ?> testContainer = createTestContainer();
59         nnout.writeNormalizedNode(testContainer);
60
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();
67
68         ContainerNode toasterContainer = Builders.containerBuilder()
69                 .withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME)).withChild(toasterNode).build();
70         nnout.writeNormalizedNode(toasterContainer);
71
72         final byte[] bytes = bos.toByteArray();
73         assertEquals(1049950, bytes.length);
74
75         NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
76
77         NormalizedNode<?, ?> node = nnin.readNormalizedNode();
78         Assert.assertEquals(testContainer, node);
79
80         node = nnin.readNormalizedNode();
81         Assert.assertEquals(toasterContainer, node);
82     }
83
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();
88
89         byte[] bytes2 = {};
90         LeafSetEntryNode<Object> entry2 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
91                 new NodeWithValue<>(TestModel.BINARY_LEAF_LIST_QNAME, bytes2)).withValue(bytes2).build();
92
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();
103     }
104
105     @Test
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();
110
111         ByteArrayOutputStream bos = new ByteArrayOutputStream();
112         NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos));
113
114         nnout.writeYangInstanceIdentifier(path);
115
116         final byte[] bytes = bos.toByteArray();
117         assertEquals(161, bytes.length);
118
119         NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
120
121         YangInstanceIdentifier newPath = nnin.readYangInstanceIdentifier();
122         Assert.assertEquals(path, newPath);
123     }
124
125     @Test
126     public void testNormalizedNodeAndYangInstanceIdentifierStreaming() throws IOException {
127
128         ByteArrayOutputStream bos = new ByteArrayOutputStream();
129         NormalizedNodeDataOutput writer = NormalizedNodeInputOutput.newDataOutput(
130             ByteStreams.newDataOutput(bos));
131
132         NormalizedNode<?, ?> testContainer = TestModel.createBaseTestContainerBuilder().build();
133         writer.writeNormalizedNode(testContainer);
134
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();
138
139         writer.writeYangInstanceIdentifier(path);
140
141         final byte[] bytes = bos.toByteArray();
142         assertEquals(1163, bytes.length);
143
144         NormalizedNodeDataInput reader = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
145
146         NormalizedNode<?,?> node = reader.readNormalizedNode();
147         Assert.assertEquals(testContainer, node);
148
149         YangInstanceIdentifier newPath = reader.readYangInstanceIdentifier();
150         Assert.assertEquals(path, newPath);
151
152         writer.close();
153     }
154
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));
160
161         reader.readNormalizedNode();
162     }
163
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));
169
170         reader.readYangInstanceIdentifier();
171     }
172
173     @Test
174     public void testWithSerializable() {
175         NormalizedNode<?, ?> input = TestModel.createTestContainer();
176         SampleNormalizedNodeSerializable serializable = new SampleNormalizedNodeSerializable(input);
177         SampleNormalizedNodeSerializable clone =
178                 (SampleNormalizedNodeSerializable)SerializationUtils.clone(serializable);
179
180         Assert.assertEquals(input, clone.getInput());
181     }
182
183     @Test
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);
188
189         Node xmlNode = factory.newDocumentBuilder().parse(
190                 new InputSource(new StringReader(xml))).getDocumentElement();
191
192         assertEquals("http://www.w3.org/TR/html4/", xmlNode.getNamespaceURI());
193
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();
198
199         ByteArrayOutputStream bos = new ByteArrayOutputStream();
200         NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos));
201
202         nnout.writeNormalizedNode(anyXmlContainer);
203
204         final byte[] bytes = bos.toByteArray();
205         assertEquals(235, bytes.length);
206
207         NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
208
209         ContainerNode deserialized = (ContainerNode)nnin.readNormalizedNode();
210
211         Optional<DataContainerChild<? extends PathArgument, ?>> child =
212                 deserialized.getChild(new NodeIdentifier(TestModel.ANY_XML_QNAME));
213         assertEquals("AnyXml child present", true, child.isPresent());
214
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);
219
220         assertEquals("XML", xml, xmlOutput.getWriter().toString());
221         assertEquals("http://www.w3.org/TR/html4/", ((AnyXmlNode)child.get()).getValue().getNode().getNamespaceURI());
222     }
223
224     @Test
225     public void testSchemaPathSerialization() throws IOException {
226         final SchemaPath expected = SchemaPath.create(true, TestModel.ANY_XML_QNAME);
227
228         ByteArrayOutputStream bos = new ByteArrayOutputStream();
229         try (NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos))) {
230             nnout.writeSchemaPath(expected);
231         }
232
233         final byte[] bytes = bos.toByteArray();
234         assertEquals(100, bytes.length);
235
236         NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
237         assertEquals(expected, nnin.readSchemaPath());
238     }
239
240     @Test
241     public void testWritePathArgument() throws IOException {
242         final NodeIdentifier expected = new NodeIdentifier(TestModel.BOOLEAN_LEAF_QNAME);
243
244         ByteArrayOutputStream bos = new ByteArrayOutputStream();
245
246         try (NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos))) {
247             nnout.writePathArgument(expected);
248         }
249
250         final byte[] bytes = bos.toByteArray();
251         assertEquals(105, bytes.length);
252
253         NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes));
254         assertEquals(expected, nnin.readPathArgument());
255     }
256
257     private static String largeString(final int pow) {
258         StringBuilder sb = new StringBuilder("X");
259         for (int i = 0; i < pow; i++) {
260             sb.append(sb);
261         }
262         return sb.toString();
263     }
264 }

©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.