1 package org.opendaylight.controller.cluster.datastore.node.utils.serialization;
3 import com.google.common.base.Optional;
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;
15 import static org.junit.Assert.assertEquals;
16 import static org.junit.Assert.assertNotNull;
17 import static org.junit.Assert.assertTrue;
19 public class NormalizedNodeSerializerTest {
22 public ExpectedException expectedException = ExpectedException.none();
25 public void testSerializeDeSerialize(){
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.
32 long start = System.nanoTime();
34 NormalizedNode<?, ?> expectedNode =
35 TestModel.createDocumentOne(TestModel.createTestContext());
37 NormalizedNodeMessages.Node expected = NormalizedNodeSerializer
38 .serialize(expectedNode);
40 System.out.println("Serialize Time = " + (System.nanoTime() - start)/1000000);
42 System.out.println("Serialized Size = " + expected.getSerializedSize());
44 System.out.println(expected.toString());
46 start = System.nanoTime();
48 NormalizedNode<?, ?> actualNode =
49 NormalizedNodeSerializer.deSerialize(expected);
51 System.out.println("DeSerialize Time = " + (System.nanoTime() - start)/1000000);
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);
57 byte[] binaryData = new byte[5];
58 for(byte i=0;i<5;i++){
62 ContainerNode node1 = TestModel.createBaseTestContainerBuilder()
63 .withChild(ImmutableNodes.leafNode(TestModel.SOME_BINARY_DATA_QNAME, binaryData))
66 NormalizedNodeMessages.Node serializedNode1 = NormalizedNodeSerializer
70 (ContainerNode) NormalizedNodeSerializer.deSerialize(serializedNode1);
73 // FIXME: This will not work due to BUG 2326. Once that is fixed we can uncomment this assertion
74 // assertEquals(node1, node2);
76 Optional<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> child = node2.getChild(new YangInstanceIdentifier.NodeIdentifier(TestModel.SOME_BINARY_DATA_QNAME));
78 Object value = child.get().getValue();
80 assertTrue("value should be of type byte[]", value instanceof byte[]);
82 byte[] bytesValue = (byte[]) value;
84 for(byte i=0;i<5;i++){
85 assertEquals(i, bytesValue[i]);
90 @Test(expected = NullPointerException.class)
91 public void testSerializeNullNormalizedNode(){
92 assertNotNull(NormalizedNodeSerializer.serialize(null));
96 public void testDeSerializeNullProtocolBufferNode(){
97 expectedException.expect(NullPointerException.class);
98 expectedException.expectMessage("node should not be null");
100 NormalizedNodeSerializer.deSerialize(null);
104 public void testDeSerializePathArgumentNullNode(){
105 expectedException.expect(NullPointerException.class);
106 expectedException.expectMessage("node should not be null");
108 NormalizedNodeSerializer
109 .deSerialize(null, NormalizedNodeMessages.PathArgument.getDefaultInstance());
113 public void testDeSerializePathArgumentNullPathArgument(){
114 expectedException.expect(NullPointerException.class);
115 expectedException.expectMessage("pathArgument should not be null");
117 NormalizedNodeSerializer.deSerialize(NormalizedNodeMessages.Node.getDefaultInstance() , null);
121 public void testDeSerializePathArgument(){
123 NormalizedNodeMessages.Node.Builder nodeBuilder = NormalizedNodeMessages.Node.newBuilder();
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");
129 nodeBuilder.addCode("2014-04-13");
130 nodeBuilder.addCode("2014-05-13");
131 nodeBuilder.addCode("2014-03-13");
133 nodeBuilder.addCode("dummy1");
134 nodeBuilder.addCode("dummy2");
135 nodeBuilder.addCode("dummy3");
136 nodeBuilder.addCode("capability");
140 NormalizedNodeMessages.PathArgument.Builder pathBuilder = NormalizedNodeMessages.PathArgument.newBuilder();
142 pathBuilder.setIntType(PathArgumentType.NODE_IDENTIFIER.ordinal());
144 NormalizedNodeMessages.QName.Builder qNameBuilder = NormalizedNodeMessages.QName.newBuilder();
145 qNameBuilder.setNamespace(1);
146 qNameBuilder.setRevision(4);
147 qNameBuilder.setLocalName(8);
149 pathBuilder.setNodeType(qNameBuilder);
151 YangInstanceIdentifier.PathArgument pathArgument =
152 NormalizedNodeSerializer
153 .deSerialize(nodeBuilder.build(), pathBuilder.build());
155 assertNotNull(pathArgument);
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());