Use NormalizedNode streaming serialization in sal-remoterpc-connector
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / test / java / org / opendaylight / controller / cluster / datastore / node / utils / serialization / NormalizedNodeSerializerTest.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.serialization;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14
15 import com.google.common.base.Optional;
16 import org.junit.Rule;
17 import org.junit.Test;
18 import org.junit.rules.ExpectedException;
19 import org.opendaylight.controller.cluster.datastore.util.TestModel;
20 import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public class NormalizedNodeSerializerTest {
30     private static final Logger LOG = LoggerFactory.getLogger(NormalizedNodeSerializerTest.class);
31
32     @Rule
33     public ExpectedException expectedException = ExpectedException.none();
34
35     @Test
36     public void testSerializeDeSerialize() {
37
38         // This test basically serializes and deSerializes a largish document
39         // which contains most of the types of nodes that go into a normalized
40         // node and uses several value types as well. It is in general a good
41         // sanity test which could be augmented with specific unit tests.
42
43         long start = System.nanoTime();
44
45         NormalizedNode<?, ?> expectedNode =
46             TestModel.createDocumentOne(TestModel.createTestContext());
47
48         NormalizedNodeMessages.Node expected = NormalizedNodeSerializer
49             .serialize(expectedNode);
50
51         LOG.info("Serialize Time = {}", (System.nanoTime() - start) / 1000000);
52
53         LOG.info("Serialized Size = {}", expected.getSerializedSize());
54
55         LOG.info(expected.toString());
56
57         start = System.nanoTime();
58
59         NormalizedNode<?, ?> actualNode =
60             NormalizedNodeSerializer.deSerialize(expected);
61
62         LOG.info("DeSerialize Time = {}", (System.nanoTime() - start) / 1000000);
63
64         // Compare the original normalized node to the normalized node that was
65         // created by serializing the original node and deSerializing it back.
66         assertEquals(expectedNode, actualNode);
67
68         byte[] binaryData = new byte[5];
69         for (byte i = 0; i < 5; i++) {
70             binaryData[i] = i;
71         }
72
73         ContainerNode node1 = TestModel.createBaseTestContainerBuilder()
74                 .withChild(ImmutableNodes.leafNode(TestModel.SOME_BINARY_DATA_QNAME, binaryData))
75                 .build();
76
77         NormalizedNodeMessages.Node serializedNode1 = NormalizedNodeSerializer
78                 .serialize(node1);
79
80         ContainerNode node2 =
81                 (ContainerNode) NormalizedNodeSerializer.deSerialize(serializedNode1);
82
83
84         // FIXME: This will not work due to BUG 2326. Once that is fixed we can uncomment this assertion
85         // assertEquals(node1, node2);
86
87         Optional<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> child =
88                 node2.getChild(new YangInstanceIdentifier.NodeIdentifier(TestModel.SOME_BINARY_DATA_QNAME));
89
90         Object value = child.get().getValue();
91
92         assertTrue("value should be of type byte[]", value instanceof byte[]);
93
94         byte[] bytesValue = (byte[]) value;
95
96         for (byte i = 0; i < 5; i++) {
97             assertEquals(i, bytesValue[i]);
98         }
99
100     }
101
102     @Test(expected = NullPointerException.class)
103     public void testSerializeNullNormalizedNode() {
104         assertNotNull(NormalizedNodeSerializer.serialize(null));
105     }
106
107     @Test
108     public void testDeSerializeNullProtocolBufferNode() {
109         expectedException.expect(NullPointerException.class);
110         expectedException.expectMessage("node should not be null");
111
112         NormalizedNodeSerializer.deSerialize(null);
113     }
114
115     @Test
116     public void testDeSerializePathArgumentNullNode() {
117         expectedException.expect(NullPointerException.class);
118         expectedException.expectMessage("node should not be null");
119
120         NormalizedNodeSerializer
121             .deSerialize(null, NormalizedNodeMessages.PathArgument.getDefaultInstance());
122     }
123
124     @Test
125     public void testDeSerializePathArgumentNullPathArgument() {
126         expectedException.expect(NullPointerException.class);
127         expectedException.expectMessage("pathArgument should not be null");
128
129         NormalizedNodeSerializer.deSerialize(NormalizedNodeMessages.Node.getDefaultInstance() , null);
130     }
131
132     @Test
133     public void testDeSerializePathArgument() {
134
135         NormalizedNodeMessages.Node.Builder nodeBuilder = NormalizedNodeMessages.Node.newBuilder();
136
137         nodeBuilder.addCode("urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test1");
138         nodeBuilder.addCode("urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test");
139
140
141         nodeBuilder.addCode("2014-04-13");
142         nodeBuilder.addCode("2014-05-13");
143         nodeBuilder.addCode("2014-03-13");
144
145         nodeBuilder.addCode("dummy1");
146         nodeBuilder.addCode("dummy2");
147         nodeBuilder.addCode("dummy3");
148         nodeBuilder.addCode("capability");
149
150
151
152         NormalizedNodeMessages.PathArgument.Builder pathBuilder = NormalizedNodeMessages.PathArgument.newBuilder();
153
154         pathBuilder.setIntType(PathArgumentType.NODE_IDENTIFIER.ordinal());
155
156         NormalizedNodeMessages.QName.Builder qnameBuilder = NormalizedNodeMessages.QName.newBuilder();
157         qnameBuilder.setNamespace(1);
158         qnameBuilder.setRevision(4);
159         qnameBuilder.setLocalName(8);
160
161         pathBuilder.setNodeType(qnameBuilder);
162
163         YangInstanceIdentifier.PathArgument pathArgument =
164             NormalizedNodeSerializer
165                 .deSerialize(nodeBuilder.build(), pathBuilder.build());
166
167         assertNotNull(pathArgument);
168
169         assertEquals("urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test",
170                 pathArgument.getNodeType().getNamespace().toString());
171         assertEquals("2014-03-13", pathArgument.getNodeType().getFormattedRevision());
172         assertEquals("capability", pathArgument.getNodeType().getLocalName());
173     }
174 }