Adjust to yangtools-2.0.0 changes
[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.QName;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
34 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
37 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
39 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
40 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
41 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
42 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeBuilder;
43 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
44 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
45 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
46 import org.w3c.dom.Node;
47 import org.xml.sax.InputSource;
48
49 public class NormalizedNodeStreamReaderWriterTest {
50
51     @Test
52     public void testNormalizedNodeStreaming() throws IOException {
53
54         ByteArrayOutputStream bos = new ByteArrayOutputStream();
55         NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos));
56
57         NormalizedNode<?, ?> testContainer = createTestContainer();
58         nnout.writeNormalizedNode(testContainer);
59
60         QName toaster = QName.create("http://netconfcentral.org/ns/toaster","2009-11-20","toaster");
61         QName darknessFactor = QName.create("http://netconfcentral.org/ns/toaster","2009-11-20","darknessFactor");
62         QName description = QName.create("http://netconfcentral.org/ns/toaster","2009-11-20","description");
63         ContainerNode toasterNode = Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(toaster))
64                 .withChild(ImmutableNodes.leafNode(darknessFactor, "1000"))
65                 .withChild(ImmutableNodes.leafNode(description, largeString(20))).build();
66
67         ContainerNode toasterContainer = Builders.containerBuilder()
68                 .withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME)).withChild(toasterNode).build();
69         nnout.writeNormalizedNode(toasterContainer);
70
71         NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(
72             bos.toByteArray()));
73
74         NormalizedNode<?,?> node = nnin.readNormalizedNode();
75         Assert.assertEquals(testContainer, node);
76
77         node = nnin.readNormalizedNode();
78         Assert.assertEquals(toasterContainer, node);
79     }
80
81     private static NormalizedNode<?, ?> createTestContainer() {
82         byte[] bytes1 = {1, 2, 3};
83         LeafSetEntryNode<Object> entry1 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
84                 new NodeWithValue<>(TestModel.BINARY_LEAF_LIST_QNAME, bytes1)).withValue(bytes1).build();
85
86         byte[] bytes2 = {};
87         LeafSetEntryNode<Object> entry2 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
88                 new NodeWithValue<>(TestModel.BINARY_LEAF_LIST_QNAME, bytes2)).withValue(bytes2).build();
89
90         return TestModel.createBaseTestContainerBuilder()
91                 .withChild(ImmutableLeafSetNodeBuilder.create().withNodeIdentifier(
92                         new NodeIdentifier(TestModel.BINARY_LEAF_LIST_QNAME))
93                         .withChild(entry1).withChild(entry2).build())
94                 .withChild(ImmutableNodes.leafNode(TestModel.SOME_BINARY_DATA_QNAME, new byte[]{1, 2, 3, 4}))
95                 .withChild(Builders.orderedMapBuilder()
96                       .withNodeIdentifier(new NodeIdentifier(TestModel.ORDERED_LIST_QNAME))
97                       .withChild(ImmutableNodes.mapEntry(TestModel.ORDERED_LIST_ENTRY_QNAME,
98                               TestModel.ID_QNAME, 11)).build()).build();
99     }
100
101     @Test
102     public void testYangInstanceIdentifierStreaming() throws IOException  {
103         YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.TEST_PATH)
104                 .node(TestModel.OUTER_LIST_QNAME).nodeWithKey(
105                         TestModel.INNER_LIST_QNAME, TestModel.ID_QNAME, 10).build();
106
107         ByteArrayOutputStream bos = new ByteArrayOutputStream();
108         NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos));
109
110         nnout.writeYangInstanceIdentifier(path);
111
112         NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(
113             bos.toByteArray()));
114
115         YangInstanceIdentifier newPath = nnin.readYangInstanceIdentifier();
116         Assert.assertEquals(path, newPath);
117     }
118
119     @Test
120     public void testNormalizedNodeAndYangInstanceIdentifierStreaming() throws IOException {
121
122         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
123         NormalizedNodeOutputStreamWriter writer = new NormalizedNodeOutputStreamWriter(
124             ByteStreams.newDataOutput(byteArrayOutputStream));
125
126         NormalizedNode<?, ?> testContainer = TestModel.createBaseTestContainerBuilder().build();
127         writer.writeNormalizedNode(testContainer);
128
129         YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.TEST_PATH)
130                 .node(TestModel.OUTER_LIST_QNAME).nodeWithKey(
131                         TestModel.INNER_LIST_QNAME, TestModel.ID_QNAME, 10).build();
132
133         writer.writeYangInstanceIdentifier(path);
134
135         NormalizedNodeDataInput reader = NormalizedNodeInputOutput.newDataInput(
136             ByteStreams.newDataInput(byteArrayOutputStream.toByteArray()));
137
138         NormalizedNode<?,?> node = reader.readNormalizedNode();
139         Assert.assertEquals(testContainer, node);
140
141         YangInstanceIdentifier newPath = reader.readYangInstanceIdentifier();
142         Assert.assertEquals(path, newPath);
143
144         writer.close();
145     }
146
147     @Test(expected = InvalidNormalizedNodeStreamException.class, timeout = 10000)
148     public void testInvalidNormalizedNodeStream() throws IOException {
149         byte[] invalidBytes = {1,2,3};
150         NormalizedNodeDataInput reader = NormalizedNodeInputOutput.newDataInput(
151                 ByteStreams.newDataInput(invalidBytes));
152
153         reader.readNormalizedNode();
154     }
155
156     @Test(expected = InvalidNormalizedNodeStreamException.class, timeout = 10000)
157     public void testInvalidYangInstanceIdentifierStream() throws IOException {
158         byte[] invalidBytes = {1,2,3};
159         NormalizedNodeDataInput reader = NormalizedNodeInputOutput.newDataInput(
160             ByteStreams.newDataInput(invalidBytes));
161
162         reader.readYangInstanceIdentifier();
163     }
164
165     @Test
166     public void testWithSerializable() {
167         NormalizedNode<?, ?> input = TestModel.createTestContainer();
168         SampleNormalizedNodeSerializable serializable = new SampleNormalizedNodeSerializable(input);
169         SampleNormalizedNodeSerializable clone =
170                 (SampleNormalizedNodeSerializable)SerializationUtils.clone(serializable);
171
172         Assert.assertEquals(input, clone.getInput());
173     }
174
175     @Test
176     public void testAnyXmlStreaming() throws Exception {
177         String xml = "<foo xmlns=\"http://www.w3.org/TR/html4/\" x=\"123\"><bar>one</bar><bar>two</bar></foo>";
178         final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
179         factory.setNamespaceAware(true);
180
181         Node xmlNode = factory.newDocumentBuilder().parse(
182                 new InputSource(new StringReader(xml))).getDocumentElement();
183
184         assertEquals("http://www.w3.org/TR/html4/", xmlNode.getNamespaceURI());
185
186         NormalizedNode<?, ?> anyXmlContainer = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
187                 new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).withChild(
188                         Builders.anyXmlBuilder().withNodeIdentifier(new NodeIdentifier(TestModel.ANY_XML_QNAME))
189                             .withValue(new DOMSource(xmlNode)).build()).build();
190
191         ByteArrayOutputStream bos = new ByteArrayOutputStream();
192         NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos));
193
194         nnout.writeNormalizedNode(anyXmlContainer);
195
196         NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(
197             bos.toByteArray()));
198
199         ContainerNode deserialized = (ContainerNode)nnin.readNormalizedNode();
200
201         Optional<DataContainerChild<? extends PathArgument, ?>> child =
202                 deserialized.getChild(new NodeIdentifier(TestModel.ANY_XML_QNAME));
203         assertEquals("AnyXml child present", true, child.isPresent());
204
205         StreamResult xmlOutput = new StreamResult(new StringWriter());
206         Transformer transformer = TransformerFactory.newInstance().newTransformer();
207         transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
208         transformer.transform(((AnyXmlNode)child.get()).getValue(), xmlOutput);
209
210         assertEquals("XML", xml, xmlOutput.getWriter().toString());
211         assertEquals("http://www.w3.org/TR/html4/", ((AnyXmlNode)child.get()).getValue().getNode().getNamespaceURI());
212     }
213
214     @Test
215     public void testSchemaPathSerialization() throws Exception {
216         final SchemaPath expected = SchemaPath.create(true, TestModel.ANY_XML_QNAME);
217
218         ByteArrayOutputStream bos = new ByteArrayOutputStream();
219         NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos));
220         nnout.writeSchemaPath(expected);
221
222         NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(
223             bos.toByteArray()));
224         SchemaPath actual = nnin.readSchemaPath();
225         assertEquals(expected, actual);
226     }
227
228     private static String largeString(final int pow) {
229         StringBuilder sb = new StringBuilder("X");
230         for (int i = 0; i < pow; i++) {
231             sb.append(sb);
232         }
233         return sb.toString();
234     }
235 }

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