2 * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.cluster.datastore.node.utils.stream;
11 import com.google.common.io.ByteStreams;
12 import java.io.ByteArrayOutputStream;
13 import java.io.IOException;
14 import org.apache.commons.lang.SerializationUtils;
15 import org.junit.Assert;
16 import org.junit.Test;
17 import org.opendaylight.controller.cluster.datastore.node.NormalizedNodeToNodeCodec;
18 import org.opendaylight.controller.cluster.datastore.util.TestModel;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
26 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
27 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeBuilder;
28 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 public class NormalizedNodeStreamReaderWriterTest {
34 public void testNormalizedNodeStreaming() throws IOException {
36 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
37 NormalizedNodeOutputStreamWriter writer = new NormalizedNodeOutputStreamWriter(
38 ByteStreams.newDataOutput(byteArrayOutputStream));
40 NormalizedNode<?, ?> testContainer = createTestContainer();
41 writer.writeNormalizedNode(testContainer);
43 QName toaster = QName.create("http://netconfcentral.org/ns/toaster","2009-11-20","toaster");
44 QName darknessFactor = QName.create("http://netconfcentral.org/ns/toaster","2009-11-20","darknessFactor");
45 QName description = QName.create("http://netconfcentral.org/ns/toaster","2009-11-20","description");
46 ContainerNode toasterNode = Builders.containerBuilder().
47 withNodeIdentifier(new NodeIdentifier(toaster)).
48 withChild(ImmutableNodes.leafNode(darknessFactor, "1000")).
49 withChild(ImmutableNodes.leafNode(description, largeString(20)))
52 ContainerNode toasterContainer = Builders.containerBuilder().
53 withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME)).
54 withChild(toasterNode).build();
55 writer.writeNormalizedNode(toasterContainer);
57 NormalizedNodeInputStreamReader reader = new NormalizedNodeInputStreamReader(
58 ByteStreams.newDataInput(byteArrayOutputStream.toByteArray()));
60 NormalizedNode<?,?> node = reader.readNormalizedNode();
61 Assert.assertEquals(testContainer, node);
63 node = reader.readNormalizedNode();
64 Assert.assertEquals(toasterContainer, node);
69 private static NormalizedNode<?, ?> createTestContainer() {
70 byte[] bytes1 = {1,2,3};
71 LeafSetEntryNode<Object> entry1 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
72 new YangInstanceIdentifier.NodeWithValue(TestModel.BINARY_LEAF_LIST_QNAME, bytes1)).
73 withValue(bytes1).build();
76 LeafSetEntryNode<Object> entry2 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
77 new YangInstanceIdentifier.NodeWithValue(TestModel.BINARY_LEAF_LIST_QNAME, bytes2)).
78 withValue(bytes2).build();
80 LeafSetEntryNode<Object> entry3 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
81 new YangInstanceIdentifier.NodeWithValue(TestModel.BINARY_LEAF_LIST_QNAME, null)).
82 withValue(null).build();
85 return TestModel.createBaseTestContainerBuilder().
86 withChild(ImmutableLeafSetNodeBuilder.create().withNodeIdentifier(
87 new YangInstanceIdentifier.NodeIdentifier(TestModel.BINARY_LEAF_LIST_QNAME)).
88 withChild(entry1).withChild(entry2).withChild(entry3).build()).
89 withChild(ImmutableNodes.leafNode(TestModel.SOME_BINARY_DATA_QNAME, new byte[]{1,2,3,4})).
90 withChild(Builders.orderedMapBuilder().
91 withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.ORDERED_LIST_QNAME)).
92 withChild(ImmutableNodes.mapEntry(TestModel.ORDERED_LIST_ENTRY_QNAME,
93 TestModel.ID_QNAME, 11)).build()).
98 public void testYangInstanceIdentifierStreaming() throws IOException {
99 YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.TEST_PATH).
100 node(TestModel.OUTER_LIST_QNAME).nodeWithKey(
101 TestModel.INNER_LIST_QNAME, TestModel.ID_QNAME, 10).build();
103 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
104 NormalizedNodeOutputStreamWriter writer =
105 new NormalizedNodeOutputStreamWriter(ByteStreams.newDataOutput(byteArrayOutputStream));
106 writer.writeYangInstanceIdentifier(path);
108 NormalizedNodeInputStreamReader reader = new NormalizedNodeInputStreamReader(
109 ByteStreams.newDataInput(byteArrayOutputStream.toByteArray()));
111 YangInstanceIdentifier newPath = reader.readYangInstanceIdentifier();
112 Assert.assertEquals(path, newPath);
118 public void testNormalizedNodeAndYangInstanceIdentifierStreaming() throws IOException {
120 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
121 NormalizedNodeOutputStreamWriter writer = new NormalizedNodeOutputStreamWriter(
122 ByteStreams.newDataOutput(byteArrayOutputStream));
124 NormalizedNode<?, ?> testContainer = TestModel.createBaseTestContainerBuilder().build();
125 writer.writeNormalizedNode(testContainer);
127 YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.TEST_PATH).
128 node(TestModel.OUTER_LIST_QNAME).nodeWithKey(
129 TestModel.INNER_LIST_QNAME, TestModel.ID_QNAME, 10).build();
131 writer.writeYangInstanceIdentifier(path);
133 NormalizedNodeInputStreamReader reader = new NormalizedNodeInputStreamReader(
134 ByteStreams.newDataInput(byteArrayOutputStream.toByteArray()));
136 NormalizedNode<?,?> node = reader.readNormalizedNode();
137 Assert.assertEquals(testContainer, node);
139 YangInstanceIdentifier newPath = reader.readYangInstanceIdentifier();
140 Assert.assertEquals(path, newPath);
145 @Test(expected=InvalidNormalizedNodeStreamException.class, timeout=10000)
146 public void testInvalidNormalizedNodeStream() throws IOException {
147 byte[] protobufBytes = new NormalizedNodeToNodeCodec(null).encode(
148 TestModel.createBaseTestContainerBuilder().build()).getNormalizedNode().toByteArray();
150 NormalizedNodeInputStreamReader reader = new NormalizedNodeInputStreamReader(
151 ByteStreams.newDataInput(protobufBytes));
153 reader.readNormalizedNode();
156 @Test(expected=InvalidNormalizedNodeStreamException.class, timeout=10000)
157 public void testInvalidYangInstanceIdentifierStream() throws IOException {
158 byte[] protobufBytes = {1,2,3};
159 NormalizedNodeInputStreamReader reader = new NormalizedNodeInputStreamReader(
160 ByteStreams.newDataInput(protobufBytes));
162 reader.readYangInstanceIdentifier();
166 public void testWithSerializable() {
167 NormalizedNode<?, ?> input = TestModel.createTestContainer();
168 SampleNormalizedNodeSerializable serializable = new SampleNormalizedNodeSerializable(input );
169 SampleNormalizedNodeSerializable clone = (SampleNormalizedNodeSerializable)SerializationUtils.clone(serializable);
171 Assert.assertEquals(input, clone.getInput());
175 private static String largeString(final int pow){
177 for(int i=0;i<pow;i++){
178 StringBuilder b = new StringBuilder();
179 b.append(s).append(s);