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