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.YangInstanceIdentifier.NodeWithValue;
23 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
27 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
28 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeBuilder;
29 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32 public class NormalizedNodeStreamReaderWriterTest {
35 public void testNormalizedNodeStreaming() throws IOException {
37 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
38 NormalizedNodeOutputStreamWriter writer = new NormalizedNodeOutputStreamWriter(
39 ByteStreams.newDataOutput(byteArrayOutputStream));
41 NormalizedNode<?, ?> testContainer = createTestContainer();
42 writer.writeNormalizedNode(testContainer);
44 QName toaster = QName.create("http://netconfcentral.org/ns/toaster","2009-11-20","toaster");
45 QName darknessFactor = QName.create("http://netconfcentral.org/ns/toaster","2009-11-20","darknessFactor");
46 QName description = QName.create("http://netconfcentral.org/ns/toaster","2009-11-20","description");
47 ContainerNode toasterNode = Builders.containerBuilder().
48 withNodeIdentifier(new NodeIdentifier(toaster)).
49 withChild(ImmutableNodes.leafNode(darknessFactor, "1000")).
50 withChild(ImmutableNodes.leafNode(description, largeString(20)))
53 ContainerNode toasterContainer = Builders.containerBuilder().
54 withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME)).
55 withChild(toasterNode).build();
56 writer.writeNormalizedNode(toasterContainer);
58 NormalizedNodeInputStreamReader reader = new NormalizedNodeInputStreamReader(
59 ByteStreams.newDataInput(byteArrayOutputStream.toByteArray()));
61 NormalizedNode<?,?> node = reader.readNormalizedNode();
62 Assert.assertEquals(testContainer, node);
64 node = reader.readNormalizedNode();
65 Assert.assertEquals(toasterContainer, node);
70 private static NormalizedNode<?, ?> createTestContainer() {
71 byte[] bytes1 = {1,2,3};
72 LeafSetEntryNode<Object> entry1 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
73 new NodeWithValue<>(TestModel.BINARY_LEAF_LIST_QNAME, bytes1)).withValue(bytes1).build();
76 LeafSetEntryNode<Object> entry2 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
77 new NodeWithValue<>(TestModel.BINARY_LEAF_LIST_QNAME, bytes2)).withValue(bytes2).build();
79 LeafSetEntryNode<Object> entry3 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
80 new NodeWithValue<>(TestModel.BINARY_LEAF_LIST_QNAME, null)).withValue(null).build();
83 return TestModel.createBaseTestContainerBuilder().
84 withChild(ImmutableLeafSetNodeBuilder.create().withNodeIdentifier(
85 new NodeIdentifier(TestModel.BINARY_LEAF_LIST_QNAME)).
86 withChild(entry1).withChild(entry2).withChild(entry3).build()).
87 withChild(ImmutableNodes.leafNode(TestModel.SOME_BINARY_DATA_QNAME, new byte[]{1,2,3,4})).
88 withChild(Builders.orderedMapBuilder().
89 withNodeIdentifier(new NodeIdentifier(TestModel.ORDERED_LIST_QNAME)).
90 withChild(ImmutableNodes.mapEntry(TestModel.ORDERED_LIST_ENTRY_QNAME,
91 TestModel.ID_QNAME, 11)).build()).
96 public void testYangInstanceIdentifierStreaming() throws IOException {
97 YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.TEST_PATH).
98 node(TestModel.OUTER_LIST_QNAME).nodeWithKey(
99 TestModel.INNER_LIST_QNAME, TestModel.ID_QNAME, 10).build();
101 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
102 NormalizedNodeOutputStreamWriter writer =
103 new NormalizedNodeOutputStreamWriter(ByteStreams.newDataOutput(byteArrayOutputStream));
104 writer.writeYangInstanceIdentifier(path);
106 NormalizedNodeInputStreamReader reader = new NormalizedNodeInputStreamReader(
107 ByteStreams.newDataInput(byteArrayOutputStream.toByteArray()));
109 YangInstanceIdentifier newPath = reader.readYangInstanceIdentifier();
110 Assert.assertEquals(path, newPath);
116 public void testNormalizedNodeAndYangInstanceIdentifierStreaming() throws IOException {
118 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
119 NormalizedNodeOutputStreamWriter writer = new NormalizedNodeOutputStreamWriter(
120 ByteStreams.newDataOutput(byteArrayOutputStream));
122 NormalizedNode<?, ?> testContainer = TestModel.createBaseTestContainerBuilder().build();
123 writer.writeNormalizedNode(testContainer);
125 YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.TEST_PATH).
126 node(TestModel.OUTER_LIST_QNAME).nodeWithKey(
127 TestModel.INNER_LIST_QNAME, TestModel.ID_QNAME, 10).build();
129 writer.writeYangInstanceIdentifier(path);
131 NormalizedNodeInputStreamReader reader = new NormalizedNodeInputStreamReader(
132 ByteStreams.newDataInput(byteArrayOutputStream.toByteArray()));
134 NormalizedNode<?,?> node = reader.readNormalizedNode();
135 Assert.assertEquals(testContainer, node);
137 YangInstanceIdentifier newPath = reader.readYangInstanceIdentifier();
138 Assert.assertEquals(path, newPath);
143 @Test(expected=InvalidNormalizedNodeStreamException.class, timeout=10000)
144 public void testInvalidNormalizedNodeStream() throws IOException {
145 byte[] protobufBytes = new NormalizedNodeToNodeCodec(null).encode(
146 TestModel.createBaseTestContainerBuilder().build()).getNormalizedNode().toByteArray();
148 NormalizedNodeInputStreamReader reader = new NormalizedNodeInputStreamReader(
149 ByteStreams.newDataInput(protobufBytes));
151 reader.readNormalizedNode();
154 @Test(expected=InvalidNormalizedNodeStreamException.class, timeout=10000)
155 public void testInvalidYangInstanceIdentifierStream() throws IOException {
156 byte[] protobufBytes = {1,2,3};
157 NormalizedNodeInputStreamReader reader = new NormalizedNodeInputStreamReader(
158 ByteStreams.newDataInput(protobufBytes));
160 reader.readYangInstanceIdentifier();
164 public void testWithSerializable() {
165 NormalizedNode<?, ?> input = TestModel.createTestContainer();
166 SampleNormalizedNodeSerializable serializable = new SampleNormalizedNodeSerializable(input );
167 SampleNormalizedNodeSerializable clone = (SampleNormalizedNodeSerializable)SerializationUtils.clone(serializable);
169 Assert.assertEquals(input, clone.getInput());
173 private static String largeString(final int pow){
175 for(int i=0;i<pow;i++){
176 StringBuilder b = new StringBuilder();
177 b.append(s).append(s);