aed87ad35543fece162ab782c7557340b6601849
[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         NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(
73             bos.toByteArray()));
74
75         NormalizedNode<?,?> node = nnin.readNormalizedNode();
76         Assert.assertEquals(testContainer, node);
77
78         node = nnin.readNormalizedNode();
79         Assert.assertEquals(toasterContainer, node);
80     }
81
82     private static NormalizedNode<?, ?> createTestContainer() {
83         byte[] bytes1 = {1, 2, 3};
84         LeafSetEntryNode<Object> entry1 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
85                 new NodeWithValue<>(TestModel.BINARY_LEAF_LIST_QNAME, bytes1)).withValue(bytes1).build();
86
87         byte[] bytes2 = {};
88         LeafSetEntryNode<Object> entry2 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
89                 new NodeWithValue<>(TestModel.BINARY_LEAF_LIST_QNAME, bytes2)).withValue(bytes2).build();
90
91         return TestModel.createBaseTestContainerBuilder()
92                 .withChild(ImmutableLeafSetNodeBuilder.create().withNodeIdentifier(
93                         new NodeIdentifier(TestModel.BINARY_LEAF_LIST_QNAME))
94                         .withChild(entry1).withChild(entry2).build())
95                 .withChild(ImmutableNodes.leafNode(TestModel.SOME_BINARY_DATA_QNAME, new byte[]{1, 2, 3, 4}))
96                 .withChild(ImmutableNodes.leafNode(TestModel.EMPTY_QNAME, Empty.getInstance()))
97                 .withChild(Builders.orderedMapBuilder()
98                       .withNodeIdentifier(new NodeIdentifier(TestModel.ORDERED_LIST_QNAME))
99                       .withChild(ImmutableNodes.mapEntry(TestModel.ORDERED_LIST_ENTRY_QNAME,
100                               TestModel.ID_QNAME, 11)).build()).build();
101     }
102
103     @Test
104     public void testYangInstanceIdentifierStreaming() throws IOException  {
105         YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.TEST_PATH)
106                 .node(TestModel.OUTER_LIST_QNAME).nodeWithKey(
107                         TestModel.INNER_LIST_QNAME, TestModel.ID_QNAME, 10).build();
108
109         ByteArrayOutputStream bos = new ByteArrayOutputStream();
110         NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos));
111
112         nnout.writeYangInstanceIdentifier(path);
113
114         NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(
115             bos.toByteArray()));
116
117         YangInstanceIdentifier newPath = nnin.readYangInstanceIdentifier();
118         Assert.assertEquals(path, newPath);
119     }
120
121     @Test
122     public void testNormalizedNodeAndYangInstanceIdentifierStreaming() throws IOException {
123
124         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
125         NormalizedNodeDataOutput writer = NormalizedNodeInputOutput.newDataOutput(
126             ByteStreams.newDataOutput(byteArrayOutputStream));
127
128         NormalizedNode<?, ?> testContainer = TestModel.createBaseTestContainerBuilder().build();
129         writer.writeNormalizedNode(testContainer);
130
131         YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.TEST_PATH)
132                 .node(TestModel.OUTER_LIST_QNAME).nodeWithKey(
133                         TestModel.INNER_LIST_QNAME, TestModel.ID_QNAME, 10).build();
134
135         writer.writeYangInstanceIdentifier(path);
136
137         NormalizedNodeDataInput reader = NormalizedNodeInputOutput.newDataInput(
138             ByteStreams.newDataInput(byteArrayOutputStream.toByteArray()));
139
140         NormalizedNode<?,?> node = reader.readNormalizedNode();
141         Assert.assertEquals(testContainer, node);
142
143         YangInstanceIdentifier newPath = reader.readYangInstanceIdentifier();
144         Assert.assertEquals(path, newPath);
145
146         writer.close();
147     }
148
149     @Test(expected = InvalidNormalizedNodeStreamException.class, timeout = 10000)
150     public void testInvalidNormalizedNodeStream() throws IOException {
151         byte[] invalidBytes = {1,2,3};
152         NormalizedNodeDataInput reader = NormalizedNodeInputOutput.newDataInput(
153                 ByteStreams.newDataInput(invalidBytes));
154
155         reader.readNormalizedNode();
156     }
157
158     @Test(expected = InvalidNormalizedNodeStreamException.class, timeout = 10000)
159     public void testInvalidYangInstanceIdentifierStream() throws IOException {
160         byte[] invalidBytes = {1,2,3};
161         NormalizedNodeDataInput reader = NormalizedNodeInputOutput.newDataInput(
162             ByteStreams.newDataInput(invalidBytes));
163
164         reader.readYangInstanceIdentifier();
165     }
166
167     @Test
168     public void testWithSerializable() {
169         NormalizedNode<?, ?> input = TestModel.createTestContainer();
170         SampleNormalizedNodeSerializable serializable = new SampleNormalizedNodeSerializable(input);
171         SampleNormalizedNodeSerializable clone =
172                 (SampleNormalizedNodeSerializable)SerializationUtils.clone(serializable);
173
174         Assert.assertEquals(input, clone.getInput());
175     }
176
177     @Test
178     public void testAnyXmlStreaming() throws Exception {
179         String xml = "<foo xmlns=\"http://www.w3.org/TR/html4/\" x=\"123\"><bar>one</bar><bar>two</bar></foo>";
180         final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
181         factory.setNamespaceAware(true);
182
183         Node xmlNode = factory.newDocumentBuilder().parse(
184                 new InputSource(new StringReader(xml))).getDocumentElement();
185
186         assertEquals("http://www.w3.org/TR/html4/", xmlNode.getNamespaceURI());
187
188         NormalizedNode<?, ?> anyXmlContainer = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
189                 new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).withChild(
190                         Builders.anyXmlBuilder().withNodeIdentifier(new NodeIdentifier(TestModel.ANY_XML_QNAME))
191                             .withValue(new DOMSource(xmlNode)).build()).build();
192
193         ByteArrayOutputStream bos = new ByteArrayOutputStream();
194         NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos));
195
196         nnout.writeNormalizedNode(anyXmlContainer);
197
198         NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(
199             bos.toByteArray()));
200
201         ContainerNode deserialized = (ContainerNode)nnin.readNormalizedNode();
202
203         Optional<DataContainerChild<? extends PathArgument, ?>> child =
204                 deserialized.getChild(new NodeIdentifier(TestModel.ANY_XML_QNAME));
205         assertEquals("AnyXml child present", true, child.isPresent());
206
207         StreamResult xmlOutput = new StreamResult(new StringWriter());
208         Transformer transformer = TransformerFactory.newInstance().newTransformer();
209         transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
210         transformer.transform(((AnyXmlNode)child.get()).getValue(), xmlOutput);
211
212         assertEquals("XML", xml, xmlOutput.getWriter().toString());
213         assertEquals("http://www.w3.org/TR/html4/", ((AnyXmlNode)child.get()).getValue().getNode().getNamespaceURI());
214     }
215
216     @Test
217     public void testSchemaPathSerialization() throws IOException {
218         final SchemaPath expected = SchemaPath.create(true, TestModel.ANY_XML_QNAME);
219
220         ByteArrayOutputStream bos = new ByteArrayOutputStream();
221         try (NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos))) {
222             nnout.writeSchemaPath(expected);
223         }
224
225         NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(
226             bos.toByteArray()));
227         assertEquals(expected, nnin.readSchemaPath());
228     }
229
230     @Test
231     public void testWritePathArgument() throws IOException {
232         final NodeIdentifier expected = new NodeIdentifier(TestModel.BOOLEAN_LEAF_QNAME);
233
234         ByteArrayOutputStream bos = new ByteArrayOutputStream();
235
236         try (NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(ByteStreams.newDataOutput(bos))) {
237             nnout.writePathArgument(expected);
238         }
239
240         NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(
241             bos.toByteArray()));
242         assertEquals(expected, nnin.readPathArgument());
243     }
244
245     private static String largeString(final int pow) {
246         StringBuilder sb = new StringBuilder("X");
247         for (int i = 0; i < pow; i++) {
248             sb.append(sb);
249         }
250         return sb.toString();
251     }
252 }

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