Make private methods static
[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 java.io.ByteArrayInputStream;
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.InstanceIdentifierUtils;
19 import org.opendaylight.controller.cluster.datastore.util.TestModel;
20 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
28 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
29 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeBuilder;
30 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32
33 public class NormalizedNodeStreamReaderWriterTest {
34
35     @Test
36     public void testNormalizedNodeStreaming() throws IOException {
37
38         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
39         NormalizedNodeOutputStreamWriter writer = new NormalizedNodeOutputStreamWriter(byteArrayOutputStream);
40
41         NormalizedNode<?, ?> testContainer = createTestContainer();
42         writer.writeNormalizedNode(testContainer);
43
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         ContainerNode toasterNode = Builders.containerBuilder().
47                 withNodeIdentifier(new NodeIdentifier(toaster)).
48                 withChild(ImmutableNodes.leafNode(darknessFactor, "1000")).build();
49
50         ContainerNode toasterContainer = Builders.containerBuilder().
51                 withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME)).
52                 withChild(toasterNode).build();
53         writer.writeNormalizedNode(toasterContainer);
54
55         NormalizedNodeInputStreamReader reader = new NormalizedNodeInputStreamReader(
56                 new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
57
58         NormalizedNode<?,?> node = reader.readNormalizedNode();
59         Assert.assertEquals(testContainer, node);
60
61         node = reader.readNormalizedNode();
62         Assert.assertEquals(toasterContainer, node);
63
64         writer.close();
65     }
66
67     private static NormalizedNode<?, ?> createTestContainer() {
68         byte[] bytes1 = {1,2,3};
69         LeafSetEntryNode<Object> entry1 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
70                 new YangInstanceIdentifier.NodeWithValue(TestModel.BINARY_LEAF_LIST_QNAME, bytes1)).
71                 withValue(bytes1).build();
72
73         byte[] bytes2 = {};
74         LeafSetEntryNode<Object> entry2 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
75                 new YangInstanceIdentifier.NodeWithValue(TestModel.BINARY_LEAF_LIST_QNAME, bytes2)).
76                 withValue(bytes2).build();
77
78         LeafSetEntryNode<Object> entry3 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
79                 new YangInstanceIdentifier.NodeWithValue(TestModel.BINARY_LEAF_LIST_QNAME, null)).
80                 withValue(null).build();
81
82
83         return TestModel.createBaseTestContainerBuilder().
84                 withChild(ImmutableLeafSetNodeBuilder.create().withNodeIdentifier(
85                         new YangInstanceIdentifier.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 YangInstanceIdentifier.NodeIdentifier(TestModel.ORDERED_LIST_QNAME)).
90                       withChild(ImmutableNodes.mapEntry(TestModel.ORDERED_LIST_ENTRY_QNAME,
91                               TestModel.ID_QNAME, 11)).build()).
92                 build();
93     }
94
95     @Test
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();
100
101         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
102         NormalizedNodeOutputStreamWriter writer =
103                 new NormalizedNodeOutputStreamWriter(byteArrayOutputStream);
104         writer.writeYangInstanceIdentifier(path);
105
106         NormalizedNodeInputStreamReader reader = new NormalizedNodeInputStreamReader(
107                 new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
108
109         YangInstanceIdentifier newPath = reader.readYangInstanceIdentifier();
110         Assert.assertEquals(path, newPath);
111
112         writer.close();
113     }
114
115     @Test
116     public void testNormalizedNodeAndYangInstanceIdentifierStreaming() throws IOException {
117
118         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
119         NormalizedNodeOutputStreamWriter writer = new NormalizedNodeOutputStreamWriter(byteArrayOutputStream);
120
121         NormalizedNode<?, ?> testContainer = TestModel.createBaseTestContainerBuilder().build();
122         writer.writeNormalizedNode(testContainer);
123
124         YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.TEST_PATH).
125                 node(TestModel.OUTER_LIST_QNAME).nodeWithKey(
126                         TestModel.INNER_LIST_QNAME, TestModel.ID_QNAME, 10).build();
127
128         writer.writeYangInstanceIdentifier(path);
129
130         NormalizedNodeInputStreamReader reader = new NormalizedNodeInputStreamReader(
131                 new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
132
133         NormalizedNode<?,?> node = reader.readNormalizedNode();
134         Assert.assertEquals(testContainer, node);
135
136         YangInstanceIdentifier newPath = reader.readYangInstanceIdentifier();
137         Assert.assertEquals(path, newPath);
138
139         writer.close();
140     }
141
142     @Test(expected=InvalidNormalizedNodeStreamException.class, timeout=10000)
143     public void testInvalidNormalizedNodeStream() throws IOException {
144         byte[] protobufBytes = new NormalizedNodeToNodeCodec(null).encode(
145                 TestModel.createBaseTestContainerBuilder().build()).getNormalizedNode().toByteArray();
146
147         NormalizedNodeInputStreamReader reader = new NormalizedNodeInputStreamReader(
148                 new ByteArrayInputStream(protobufBytes));
149
150         reader.readNormalizedNode();
151     }
152
153     @Test(expected=InvalidNormalizedNodeStreamException.class, timeout=10000)
154     public void testInvalidYangInstanceIdentifierStream() throws IOException {
155         YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.TEST_PATH).build();
156
157         byte[] protobufBytes = ShardTransactionMessages.DeleteData.newBuilder().setInstanceIdentifierPathArguments(
158                 InstanceIdentifierUtils.toSerializable(path)).build().toByteArray();
159
160         NormalizedNodeInputStreamReader reader = new NormalizedNodeInputStreamReader(
161                 new ByteArrayInputStream(protobufBytes));
162
163         reader.readYangInstanceIdentifier();
164     }
165
166     @Test
167     public void testWithSerializable() {
168         NormalizedNode<?, ?> input = TestModel.createTestContainer();
169         SampleNormalizedNodeSerializable serializable = new SampleNormalizedNodeSerializable(input );
170         SampleNormalizedNodeSerializable clone = (SampleNormalizedNodeSerializable)SerializationUtils.clone(serializable);
171
172         Assert.assertEquals(input, clone.getInput());
173
174     }
175 }

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