Merge "Bug 1593 - Flow Statistics manager is updating store with incorrect key Statis...
[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 org.junit.Rule;
4 import org.junit.Test;
5 import org.junit.rules.ExpectedException;
6 import org.opendaylight.controller.cluster.datastore.util.TestModel;
7 import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages;
8 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
9 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13
14 public class NormalizedNodeSerializerTest {
15
16     @Rule
17     public ExpectedException expectedException = ExpectedException.none();
18
19     @Test
20     public void testSerializeDeSerialize(){
21
22         // This test basically serializes and deSerializes a largish document
23         // which contains most of the types of nodes that go into a normalized
24         // node and uses several value types as well. It is in general a good
25         // sanity test which could be augmented with specific unit tests.
26
27         long start = System.nanoTime();
28
29         NormalizedNode<?, ?> expectedNode =
30             TestModel.createDocumentOne(TestModel.createTestContext());
31
32         NormalizedNodeMessages.Node expected = NormalizedNodeSerializer
33             .serialize(expectedNode);
34
35         System.out.println("Serialize Time = " + (System.nanoTime() - start)/1000000);
36
37         System.out.println("Serialized Size = " + expected.getSerializedSize());
38
39         System.out.println(expected.toString());
40
41         start = System.nanoTime();
42
43         NormalizedNode actualNode =
44             NormalizedNodeSerializer.deSerialize(expected);
45
46         System.out.println("DeSerialize Time = " + (System.nanoTime() - start)/1000000);
47
48         // Compare the original normalized node to the normalized node that was
49         // created by serializing the original node and deSerializing it back.
50         assertEquals(expectedNode, actualNode);
51
52     }
53
54     @Test(expected = NullPointerException.class)
55     public void testSerializeNullNormalizedNode(){
56         assertNotNull(NormalizedNodeSerializer.serialize(null));
57     }
58
59     @Test
60     public void testDeSerializeNullProtocolBufferNode(){
61         expectedException.expect(NullPointerException.class);
62         expectedException.expectMessage("node should not be null");
63
64         NormalizedNodeSerializer.deSerialize(null);
65     }
66
67     @Test
68     public void testDeSerializePathArgumentNullNode(){
69         expectedException.expect(NullPointerException.class);
70         expectedException.expectMessage("node should not be null");
71
72         NormalizedNodeSerializer
73             .deSerialize(null, NormalizedNodeMessages.PathArgument.getDefaultInstance());
74     }
75
76     @Test
77     public void testDeSerializePathArgumentNullPathArgument(){
78         expectedException.expect(NullPointerException.class);
79         expectedException.expectMessage("pathArgument should not be null");
80
81         NormalizedNodeSerializer.deSerialize(NormalizedNodeMessages.Node.getDefaultInstance() , null);
82     }
83
84     @Test
85     public void testDeSerializePathArgument(){
86
87         NormalizedNodeMessages.Node.Builder nodeBuilder = NormalizedNodeMessages.Node.newBuilder();
88
89         nodeBuilder.addCode("urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test1");
90         nodeBuilder.addCode("urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test");
91
92
93         nodeBuilder.addCode("2014-04-13");
94         nodeBuilder.addCode("2014-05-13");
95         nodeBuilder.addCode("2014-03-13");
96
97         nodeBuilder.addCode("dummy1");
98         nodeBuilder.addCode("dummy2");
99         nodeBuilder.addCode("dummy3");
100         nodeBuilder.addCode("capability");
101
102
103
104         NormalizedNodeMessages.PathArgument.Builder pathBuilder = NormalizedNodeMessages.PathArgument.newBuilder();
105
106         pathBuilder.setIntType(PathArgumentType.NODE_IDENTIFIER.ordinal());
107
108         NormalizedNodeMessages.QName.Builder qNameBuilder = NormalizedNodeMessages.QName.newBuilder();
109         qNameBuilder.setNamespace(1);
110         qNameBuilder.setRevision(4);
111         qNameBuilder.setLocalName(8);
112
113         pathBuilder.setNodeType(qNameBuilder);
114
115         YangInstanceIdentifier.PathArgument pathArgument =
116             NormalizedNodeSerializer
117                 .deSerialize(nodeBuilder.build(), pathBuilder.build());
118
119         assertNotNull(pathArgument);
120
121         assertEquals("urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test", pathArgument.getNodeType().getNamespace().toString());
122         assertEquals("2014-03-13", pathArgument.getNodeType().getFormattedRevision());
123         assertEquals("capability", pathArgument.getNodeType().getLocalName());
124     }
125
126
127 }