9803df937a721ce3d41d6b9bf12941852601e3ed
[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 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;
31
32 public class NormalizedNodeStreamReaderWriterTest {
33
34     @Test
35     public void testNormalizedNodeStreaming() throws IOException {
36
37         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
38         NormalizedNodeOutputStreamWriter writer = new NormalizedNodeOutputStreamWriter(
39             ByteStreams.newDataOutput(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         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)))
51                 .build();
52
53         ContainerNode toasterContainer = Builders.containerBuilder().
54                 withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME)).
55                 withChild(toasterNode).build();
56         writer.writeNormalizedNode(toasterContainer);
57
58         NormalizedNodeInputStreamReader reader = new NormalizedNodeInputStreamReader(
59             ByteStreams.newDataInput(byteArrayOutputStream.toByteArray()));
60
61         NormalizedNode<?,?> node = reader.readNormalizedNode();
62         Assert.assertEquals(testContainer, node);
63
64         node = reader.readNormalizedNode();
65         Assert.assertEquals(toasterContainer, node);
66
67         writer.close();
68     }
69
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();
74
75         byte[] bytes2 = {};
76         LeafSetEntryNode<Object> entry2 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
77                 new NodeWithValue<>(TestModel.BINARY_LEAF_LIST_QNAME, bytes2)).withValue(bytes2).build();
78
79         LeafSetEntryNode<Object> entry3 = ImmutableLeafSetEntryNodeBuilder.create().withNodeIdentifier(
80                 new NodeWithValue<>(TestModel.BINARY_LEAF_LIST_QNAME, null)).withValue(null).build();
81
82
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()).
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(ByteStreams.newDataOutput(byteArrayOutputStream));
104         writer.writeYangInstanceIdentifier(path);
105
106         NormalizedNodeInputStreamReader reader = new NormalizedNodeInputStreamReader(
107             ByteStreams.newDataInput(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(
120             ByteStreams.newDataOutput(byteArrayOutputStream));
121
122         NormalizedNode<?, ?> testContainer = TestModel.createBaseTestContainerBuilder().build();
123         writer.writeNormalizedNode(testContainer);
124
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();
128
129         writer.writeYangInstanceIdentifier(path);
130
131         NormalizedNodeInputStreamReader reader = new NormalizedNodeInputStreamReader(
132             ByteStreams.newDataInput(byteArrayOutputStream.toByteArray()));
133
134         NormalizedNode<?,?> node = reader.readNormalizedNode();
135         Assert.assertEquals(testContainer, node);
136
137         YangInstanceIdentifier newPath = reader.readYangInstanceIdentifier();
138         Assert.assertEquals(path, newPath);
139
140         writer.close();
141     }
142
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();
147
148         NormalizedNodeInputStreamReader reader = new NormalizedNodeInputStreamReader(
149             ByteStreams.newDataInput(protobufBytes));
150
151         reader.readNormalizedNode();
152     }
153
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));
159
160         reader.readYangInstanceIdentifier();
161     }
162
163     @Test
164     public void testWithSerializable() {
165         NormalizedNode<?, ?> input = TestModel.createTestContainer();
166         SampleNormalizedNodeSerializable serializable = new SampleNormalizedNodeSerializable(input );
167         SampleNormalizedNodeSerializable clone = (SampleNormalizedNodeSerializable)SerializationUtils.clone(serializable);
168
169         Assert.assertEquals(input, clone.getInput());
170
171     }
172
173     private static String largeString(final int pow){
174         String s = "X";
175         for(int i=0;i<pow;i++){
176             StringBuilder b = new StringBuilder();
177             b.append(s).append(s);
178             s = b.toString();
179         }
180         return s;
181     }
182 }