Test SchemaNodeIdentifier instead of SchemaPath
[yangtools.git] / yang / yang-data-codec-binfmt / src / test / java / org / opendaylight / yangtools / yang / data / codec / binfmt / 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 package org.opendaylight.yangtools.yang.data.codec.binfmt;
9
10 import static org.junit.Assert.assertEquals;
11
12 import com.google.common.collect.ImmutableSet;
13 import com.google.common.io.ByteStreams;
14 import java.io.ByteArrayInputStream;
15 import java.io.ByteArrayOutputStream;
16 import java.io.IOException;
17 import java.io.ObjectInputStream;
18 import java.io.ObjectOutputStream;
19 import java.io.Serializable;
20 import java.io.StringReader;
21 import java.io.StringWriter;
22 import java.math.BigInteger;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.List;
26 import java.util.Optional;
27 import java.util.function.Function;
28 import javax.xml.parsers.DocumentBuilderFactory;
29 import javax.xml.transform.OutputKeys;
30 import javax.xml.transform.Transformer;
31 import javax.xml.transform.TransformerFactory;
32 import javax.xml.transform.dom.DOMSource;
33 import javax.xml.transform.stream.StreamResult;
34 import org.junit.Assert;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.junit.runners.Parameterized;
38 import org.junit.runners.Parameterized.Parameter;
39 import org.junit.runners.Parameterized.Parameters;
40 import org.opendaylight.yangtools.yang.common.Empty;
41 import org.opendaylight.yangtools.yang.common.QName;
42 import org.opendaylight.yangtools.yang.common.Uint64;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
44 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
45 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
46 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
47 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
48 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
49 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
50 import org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode;
51 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
52 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
53 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
54 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
55 import org.opendaylight.yangtools.yang.data.api.schema.SystemMapNode;
56 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
57 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
58 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
59 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
60 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
61 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeBuilder;
62 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
63 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
64 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
65 import org.w3c.dom.Node;
66 import org.xml.sax.InputSource;
67
68 @RunWith(Parameterized.class)
69 public class NormalizedNodeStreamReaderWriterTest {
70     enum Unsigned implements Function<String, Number> {
71         BIG_INTEGER {
72             @Override
73             public BigInteger apply(final String str) {
74                 return new BigInteger(str);
75             }
76         },
77         UINT64 {
78             @Override
79             public Uint64 apply(final String str) {
80                 return Uint64.valueOf(str);
81             }
82         };
83     }
84
85     @Parameters(name = "{0} {1}")
86     public static Iterable<Object[]> data() {
87         return Arrays.asList(
88             new Object[] { NormalizedNodeStreamVersion.LITHIUM,    Unsigned.BIG_INTEGER,
89                 1_050_286, 9_577_973, 171, 1_553, 103, 237,  98 },
90             new Object[] { NormalizedNodeStreamVersion.NEON_SR2,   Unsigned.BIG_INTEGER,
91                 1_049_950, 5_577_993, 161, 1_163, 105, 235, 100 },
92             new Object[] { NormalizedNodeStreamVersion.SODIUM_SR1, Unsigned.BIG_INTEGER,
93                 1_049_619, 2_289_103, 139,   826, 103, 229,  99 },
94             new Object[] { NormalizedNodeStreamVersion.SODIUM_SR1, Unsigned.UINT64,
95                 1_049_618, 2_289_103, 139,   825, 103, 229,  99 },
96             new Object[] { NormalizedNodeStreamVersion.MAGNESIUM,  Unsigned.UINT64,
97                 1_049_618, 2_289_103, 139,   825, 103, 229,  99 });
98     }
99
100     @Parameter(0)
101     public NormalizedNodeStreamVersion version;
102     @Parameter(1)
103     public Unsigned uint64;
104     @Parameter(2)
105     public int normalizedNodeStreamingSize;
106     @Parameter(3)
107     public int hugeEntriesSize;
108     @Parameter(4)
109     public int yiidStreamingSize;
110     @Parameter(5)
111     public int nnYiidStreamingSize;
112     @Parameter(6)
113     public int writePathArgumentSize;
114     @Parameter(7)
115     public int anyxmlStreamingSize;
116     @Parameter(8)
117     public int schemaPathSize;
118
119     @Test
120     public void testNormalizedNodeStreaming() throws IOException {
121
122         ByteArrayOutputStream bos = new ByteArrayOutputStream();
123         NormalizedNodeDataOutput nnout = version.newDataOutput(ByteStreams.newDataOutput(bos));
124
125         NormalizedNode testContainer = createTestContainer();
126         nnout.writeNormalizedNode(testContainer);
127
128         QName toaster = QName.create("http://netconfcentral.org/ns/toaster","2009-11-20","toaster");
129         QName darknessFactor = QName.create("http://netconfcentral.org/ns/toaster","2009-11-20","darknessFactor");
130         QName description = QName.create("http://netconfcentral.org/ns/toaster","2009-11-20","description");
131         ContainerNode toasterNode = Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(toaster))
132                 .withChild(ImmutableNodes.leafNode(darknessFactor, "1000"))
133                 .withChild(ImmutableNodes.leafNode(description, largeString(20))).build();
134
135         ContainerNode toasterContainer = Builders.containerBuilder()
136                 .withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME)).withChild(toasterNode).build();
137         nnout.writeNormalizedNode(toasterContainer);
138
139         final byte[] bytes = bos.toByteArray();
140         assertEquals(normalizedNodeStreamingSize, bytes.length);
141
142         NormalizedNodeDataInput nnin = NormalizedNodeDataInput.newDataInput(ByteStreams.newDataInput(bytes));
143
144         NormalizedNode node = nnin.readNormalizedNode();
145         Assert.assertEquals(testContainer, node);
146
147         node = nnin.readNormalizedNode();
148         Assert.assertEquals(toasterContainer, node);
149     }
150
151     private NormalizedNode createTestContainer() {
152         byte[] bytes1 = {1, 2, 3};
153         LeafSetEntryNode<Object> entry1 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
154                 new NodeWithValue<>(TestModel.BINARY_LEAF_LIST_QNAME, bytes1)).withValue(bytes1).build();
155
156         byte[] bytes2 = {};
157         LeafSetEntryNode<Object> entry2 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
158                 new NodeWithValue<>(TestModel.BINARY_LEAF_LIST_QNAME, bytes2)).withValue(bytes2).build();
159
160         return TestModel.createBaseTestContainerBuilder(uint64)
161                 .withChild(ImmutableLeafSetNodeBuilder.create().withNodeIdentifier(
162                         new NodeIdentifier(TestModel.BINARY_LEAF_LIST_QNAME))
163                         .withChild(entry1).withChild(entry2).build())
164                 .withChild(ImmutableNodes.leafNode(TestModel.SOME_BINARY_DATA_QNAME, new byte[]{1, 2, 3, 4}))
165                 .withChild(ImmutableNodes.leafNode(TestModel.EMPTY_QNAME, Empty.getInstance()))
166                 .withChild(Builders.orderedMapBuilder()
167                       .withNodeIdentifier(new NodeIdentifier(TestModel.ORDERED_LIST_QNAME))
168                       .withChild(ImmutableNodes.mapEntry(TestModel.ORDERED_LIST_ENTRY_QNAME,
169                               TestModel.ID_QNAME, 11)).build()).build();
170     }
171
172     @Test
173     public void testYangInstanceIdentifierStreaming() throws IOException  {
174         YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.TEST_PATH)
175                 .node(TestModel.OUTER_LIST_QNAME).nodeWithKey(
176                         TestModel.INNER_LIST_QNAME, TestModel.ID_QNAME, 10).build();
177
178         ByteArrayOutputStream bos = new ByteArrayOutputStream();
179         NormalizedNodeDataOutput nnout = version.newDataOutput(ByteStreams.newDataOutput(bos));
180
181         nnout.writeYangInstanceIdentifier(path);
182
183         final byte[] bytes = bos.toByteArray();
184         assertEquals(yiidStreamingSize, bytes.length);
185
186         NormalizedNodeDataInput nnin = NormalizedNodeDataInput.newDataInput(ByteStreams.newDataInput(bytes));
187
188         YangInstanceIdentifier newPath = nnin.readYangInstanceIdentifier();
189         Assert.assertEquals(path, newPath);
190     }
191
192     @Test
193     public void testNormalizedNodeAndYangInstanceIdentifierStreaming() throws IOException {
194
195         ByteArrayOutputStream bos = new ByteArrayOutputStream();
196         NormalizedNodeDataOutput writer = version.newDataOutput(
197             ByteStreams.newDataOutput(bos));
198
199         NormalizedNode testContainer = TestModel.createBaseTestContainerBuilder(uint64).build();
200         writer.writeNormalizedNode(testContainer);
201
202         YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.TEST_PATH)
203                 .node(TestModel.OUTER_LIST_QNAME).nodeWithKey(
204                         TestModel.INNER_LIST_QNAME, TestModel.ID_QNAME, 10).build();
205
206         writer.writeYangInstanceIdentifier(path);
207
208         final byte[] bytes = bos.toByteArray();
209         assertEquals(nnYiidStreamingSize, bytes.length);
210
211         NormalizedNodeDataInput reader = NormalizedNodeDataInput.newDataInput(ByteStreams.newDataInput(bytes));
212
213         NormalizedNode node = reader.readNormalizedNode();
214         Assert.assertEquals(testContainer, node);
215
216         YangInstanceIdentifier newPath = reader.readYangInstanceIdentifier();
217         Assert.assertEquals(path, newPath);
218
219         writer.close();
220     }
221
222     @Test(expected = InvalidNormalizedNodeStreamException.class, timeout = 10000)
223     public void testInvalidNormalizedNodeStream() throws IOException {
224         byte[] invalidBytes = {1, 2, 3};
225         NormalizedNodeDataInput reader = NormalizedNodeDataInput.newDataInput(ByteStreams.newDataInput(invalidBytes));
226
227         reader.readNormalizedNode();
228     }
229
230     @Test(expected = InvalidNormalizedNodeStreamException.class, timeout = 10000)
231     public void testInvalidYangInstanceIdentifierStream() throws IOException {
232         byte[] invalidBytes = {1,2,3};
233         NormalizedNodeDataInput reader = NormalizedNodeDataInput.newDataInput(ByteStreams.newDataInput(invalidBytes));
234
235         reader.readYangInstanceIdentifier();
236     }
237
238     @Test
239     public void testWithSerializable() {
240         NormalizedNode input = TestModel.createTestContainer(uint64);
241         SampleNormalizedNodeSerializable serializable = new SampleNormalizedNodeSerializable(version, input);
242         SampleNormalizedNodeSerializable clone = clone(serializable);
243         Assert.assertEquals(input, clone.getInput());
244     }
245
246     @Test
247     public void testAnyXmlStreaming() throws Exception {
248         String xml = "<foo xmlns=\"http://www.w3.org/TR/html4/\" x=\"123\"><bar>one</bar><bar>two</bar></foo>";
249         final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
250         factory.setNamespaceAware(true);
251
252         Node xmlNode = factory.newDocumentBuilder().parse(
253                 new InputSource(new StringReader(xml))).getDocumentElement();
254
255         assertEquals("http://www.w3.org/TR/html4/", xmlNode.getNamespaceURI());
256
257         NormalizedNode anyXmlContainer = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
258                 new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).withChild(
259                         Builders.anyXmlBuilder().withNodeIdentifier(new NodeIdentifier(TestModel.ANY_XML_QNAME))
260                             .withValue(new DOMSource(xmlNode)).build()).build();
261
262         ByteArrayOutputStream bos = new ByteArrayOutputStream();
263         NormalizedNodeDataOutput nnout = version.newDataOutput(ByteStreams.newDataOutput(bos));
264
265         nnout.writeNormalizedNode(anyXmlContainer);
266
267         final byte[] bytes = bos.toByteArray();
268         assertEquals(anyxmlStreamingSize, bytes.length);
269
270         NormalizedNodeDataInput nnin = NormalizedNodeDataInput.newDataInput(ByteStreams.newDataInput(bytes));
271
272         ContainerNode deserialized = (ContainerNode)nnin.readNormalizedNode();
273
274         Optional<DataContainerChild> child = deserialized.findChildByArg(new NodeIdentifier(TestModel.ANY_XML_QNAME));
275         assertEquals("AnyXml child present", true, child.isPresent());
276
277         StreamResult xmlOutput = new StreamResult(new StringWriter());
278         Transformer transformer = TransformerFactory.newInstance().newTransformer();
279         transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
280         transformer.transform(((DOMSourceAnyxmlNode)child.get()).body(), xmlOutput);
281
282         assertEquals("XML", xml, xmlOutput.getWriter().toString());
283         assertEquals("http://www.w3.org/TR/html4/",
284             ((DOMSourceAnyxmlNode)child.get()).body().getNode().getNamespaceURI());
285     }
286
287     @Test
288     public void testSchemaPathSerialization() throws IOException {
289         final Absolute expected = Absolute.of(TestModel.ANY_XML_QNAME);
290
291         ByteArrayOutputStream bos = new ByteArrayOutputStream();
292         try (NormalizedNodeDataOutput nnout = version.newDataOutput(ByteStreams.newDataOutput(bos))) {
293             nnout.writeSchemaNodeIdentifier(expected);
294         }
295
296         final byte[] bytes = bos.toByteArray();
297         assertEquals(schemaPathSize, bytes.length);
298
299         NormalizedNodeDataInput nnin = NormalizedNodeDataInput.newDataInput(ByteStreams.newDataInput(bytes));
300         assertEquals(expected, nnin.readSchemaNodeIdentifier());
301     }
302
303     @Test
304     public void testWritePathArgument() throws IOException {
305         final NodeIdentifier expected = new NodeIdentifier(TestModel.BOOLEAN_LEAF_QNAME);
306
307         ByteArrayOutputStream bos = new ByteArrayOutputStream();
308
309         try (NormalizedNodeDataOutput nnout = version.newDataOutput(ByteStreams.newDataOutput(bos))) {
310             nnout.writePathArgument(expected);
311         }
312
313         final byte[] bytes = bos.toByteArray();
314         assertEquals(writePathArgumentSize, bytes.length);
315
316         NormalizedNodeDataInput nnin = NormalizedNodeDataInput.newDataInput(ByteStreams.newDataInput(bytes));
317         assertEquals(expected, nnin.readPathArgument());
318     }
319
320     /*
321      * This tests the encoding of a MapNode with a lot of entries, each of them having the key leaf (a string)
322      * and an integer.
323      */
324     @Test
325     public void testHugeEntries() throws IOException {
326         final CollectionNodeBuilder<MapEntryNode, SystemMapNode> mapBuilder = Builders.mapBuilder()
327                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME));
328         final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> entryBuilder =
329                 Builders.mapEntryBuilder().withChild(ImmutableNodes.leafNode(TestModel.DESC_QNAME, (byte) 42));
330
331         for (int i = 0; i < 100_000; ++i) {
332             final String key = "xyzzy" + i;
333             mapBuilder.addChild(entryBuilder
334                 .withNodeIdentifier(NodeIdentifierWithPredicates.of(TestModel.TEST_QNAME,
335                     TestModel.CHILD_NAME_QNAME, key))
336                 .withChild(ImmutableNodes.leafNode(TestModel.CHILD_NAME_QNAME, key))
337                 .build());
338         }
339
340         final SystemMapNode expected = mapBuilder.build();
341         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
342
343         try (NormalizedNodeDataOutput nnout = version.newDataOutput(ByteStreams.newDataOutput(bos))) {
344             nnout.writeNormalizedNode(expected);
345         }
346
347         final byte[] bytes = bos.toByteArray();
348         assertEquals(hugeEntriesSize, bytes.length);
349
350         NormalizedNodeDataInput nnin = NormalizedNodeDataInput.newDataInput(ByteStreams.newDataInput(bytes));
351         assertEquals(expected, nnin.readNormalizedNode());
352     }
353
354     @Test
355     public void testAugmentationIdentifier() throws IOException {
356         final List<QName> qnames = new ArrayList<>();
357         for (int i = 0; i < 257; ++i) {
358             qnames.add(QName.create(TestModel.TEST_QNAME, "a" + Integer.toHexString(i)));
359         }
360
361         for (int i = 0; i < qnames.size(); ++i) {
362             assertAugmentationIdentifier(AugmentationIdentifier.create(ImmutableSet.copyOf(qnames.subList(0, i))));
363         }
364
365         for (int i = qnames.size(); i < 65536; ++i) {
366             qnames.add(QName.create(TestModel.TEST_QNAME, "a" + Integer.toHexString(i)));
367         }
368         assertAugmentationIdentifier(AugmentationIdentifier.create(ImmutableSet.copyOf(qnames)));
369     }
370
371     private void assertAugmentationIdentifier(final AugmentationIdentifier expected) throws IOException {
372         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
373         try (NormalizedNodeDataOutput nnout = version.newDataOutput(ByteStreams.newDataOutput(bos))) {
374             nnout.writePathArgument(expected);
375         }
376
377         final byte[] bytes = bos.toByteArray();
378
379         NormalizedNodeDataInput nnin = NormalizedNodeDataInput.newDataInput(ByteStreams.newDataInput(bytes));
380         PathArgument arg = nnin.readPathArgument();
381         assertEquals(expected, arg);
382     }
383
384     private static <T extends Serializable> T clone(final T obj) {
385         final ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
386         try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
387             oos.writeObject(obj);
388         } catch (IOException e) {
389             throw new AssertionError("Failed to serialize object", e);
390         }
391
392         final byte[] bytes = baos.toByteArray();
393         try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes))) {
394             return (T) ois.readObject();
395         } catch (ClassNotFoundException | IOException e) {
396             throw new AssertionError("Failed to deserialize object", e);
397         }
398     }
399
400     private static String largeString(final int pow) {
401         StringBuilder sb = new StringBuilder("X");
402         for (int i = 0; i < pow; i++) {
403             sb.append(sb);
404         }
405         return sb.toString();
406     }
407 }