1 package org.opendaylight.controller.cluster.datastore.node.utils.serialization;
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;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
14 public class NormalizedNodeSerializerTest {
17 public ExpectedException expectedException = ExpectedException.none();
20 public void testSerializeDeSerialize(){
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.
27 long start = System.nanoTime();
29 NormalizedNode<?, ?> expectedNode =
30 TestModel.createDocumentOne(TestModel.createTestContext());
32 NormalizedNodeMessages.Node expected = NormalizedNodeSerializer
33 .serialize(expectedNode);
35 System.out.println("Serialize Time = " + (System.nanoTime() - start)/1000000);
37 System.out.println("Serialized Size = " + expected.getSerializedSize());
39 System.out.println(expected.toString());
41 start = System.nanoTime();
43 NormalizedNode actualNode =
44 NormalizedNodeSerializer.deSerialize(expected);
46 System.out.println("DeSerialize Time = " + (System.nanoTime() - start)/1000000);
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);
54 @Test(expected = NullPointerException.class)
55 public void testSerializeNullNormalizedNode(){
56 assertNotNull(NormalizedNodeSerializer.serialize(null));
60 public void testDeSerializeNullProtocolBufferNode(){
61 expectedException.expect(NullPointerException.class);
62 expectedException.expectMessage("node should not be null");
64 NormalizedNodeSerializer.deSerialize(null);
68 public void testDeSerializePathArgumentNullNode(){
69 expectedException.expect(NullPointerException.class);
70 expectedException.expectMessage("node should not be null");
72 NormalizedNodeSerializer
73 .deSerialize(null, NormalizedNodeMessages.PathArgument.getDefaultInstance());
77 public void testDeSerializePathArgumentNullPathArgument(){
78 expectedException.expect(NullPointerException.class);
79 expectedException.expectMessage("pathArgument should not be null");
81 NormalizedNodeSerializer.deSerialize(NormalizedNodeMessages.Node.getDefaultInstance() , null);
85 public void testDeSerializePathArgument(){
87 NormalizedNodeMessages.Node.Builder nodeBuilder = NormalizedNodeMessages.Node.newBuilder();
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");
93 nodeBuilder.addCode("2014-04-13");
94 nodeBuilder.addCode("2014-05-13");
95 nodeBuilder.addCode("2014-03-13");
97 nodeBuilder.addCode("dummy1");
98 nodeBuilder.addCode("dummy2");
99 nodeBuilder.addCode("dummy3");
100 nodeBuilder.addCode("capability");
104 NormalizedNodeMessages.PathArgument.Builder pathBuilder = NormalizedNodeMessages.PathArgument.newBuilder();
106 pathBuilder.setIntType(PathArgumentType.NODE_IDENTIFIER.ordinal());
108 NormalizedNodeMessages.QName.Builder qNameBuilder = NormalizedNodeMessages.QName.newBuilder();
109 qNameBuilder.setNamespace(1);
110 qNameBuilder.setRevision(4);
111 qNameBuilder.setLocalName(8);
113 pathBuilder.setNodeType(qNameBuilder);
115 YangInstanceIdentifier.PathArgument pathArgument =
116 NormalizedNodeSerializer
117 .deSerialize(nodeBuilder.build(), pathBuilder.build());
119 assertNotNull(pathArgument);
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());