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

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