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