2 * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.cluster.datastore.node.utils.serialization;
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;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
27 public class NormalizedNodeSerializerTest {
30 public ExpectedException expectedException = ExpectedException.none();
33 public void testSerializeDeSerialize(){
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.
40 long start = System.nanoTime();
42 NormalizedNode<?, ?> expectedNode =
43 TestModel.createDocumentOne(TestModel.createTestContext());
45 NormalizedNodeMessages.Node expected = NormalizedNodeSerializer
46 .serialize(expectedNode);
48 System.out.println("Serialize Time = " + (System.nanoTime() - start)/1000000);
50 System.out.println("Serialized Size = " + expected.getSerializedSize());
52 System.out.println(expected.toString());
54 start = System.nanoTime();
56 NormalizedNode<?, ?> actualNode =
57 NormalizedNodeSerializer.deSerialize(expected);
59 System.out.println("DeSerialize Time = " + (System.nanoTime() - start)/1000000);
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);
65 byte[] binaryData = new byte[5];
66 for(byte i=0;i<5;i++){
70 ContainerNode node1 = TestModel.createBaseTestContainerBuilder()
71 .withChild(ImmutableNodes.leafNode(TestModel.SOME_BINARY_DATA_QNAME, binaryData))
74 NormalizedNodeMessages.Node serializedNode1 = NormalizedNodeSerializer
78 (ContainerNode) NormalizedNodeSerializer.deSerialize(serializedNode1);
81 // FIXME: This will not work due to BUG 2326. Once that is fixed we can uncomment this assertion
82 // assertEquals(node1, node2);
84 Optional<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> child = node2.getChild(new YangInstanceIdentifier.NodeIdentifier(TestModel.SOME_BINARY_DATA_QNAME));
86 Object value = child.get().getValue();
88 assertTrue("value should be of type byte[]", value instanceof byte[]);
90 byte[] bytesValue = (byte[]) value;
92 for(byte i=0;i<5;i++){
93 assertEquals(i, bytesValue[i]);
98 @Test(expected = NullPointerException.class)
99 public void testSerializeNullNormalizedNode(){
100 assertNotNull(NormalizedNodeSerializer.serialize(null));
104 public void testDeSerializeNullProtocolBufferNode(){
105 expectedException.expect(NullPointerException.class);
106 expectedException.expectMessage("node should not be null");
108 NormalizedNodeSerializer.deSerialize(null);
112 public void testDeSerializePathArgumentNullNode(){
113 expectedException.expect(NullPointerException.class);
114 expectedException.expectMessage("node should not be null");
116 NormalizedNodeSerializer
117 .deSerialize(null, NormalizedNodeMessages.PathArgument.getDefaultInstance());
121 public void testDeSerializePathArgumentNullPathArgument(){
122 expectedException.expect(NullPointerException.class);
123 expectedException.expectMessage("pathArgument should not be null");
125 NormalizedNodeSerializer.deSerialize(NormalizedNodeMessages.Node.getDefaultInstance() , null);
129 public void testDeSerializePathArgument(){
131 NormalizedNodeMessages.Node.Builder nodeBuilder = NormalizedNodeMessages.Node.newBuilder();
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");
137 nodeBuilder.addCode("2014-04-13");
138 nodeBuilder.addCode("2014-05-13");
139 nodeBuilder.addCode("2014-03-13");
141 nodeBuilder.addCode("dummy1");
142 nodeBuilder.addCode("dummy2");
143 nodeBuilder.addCode("dummy3");
144 nodeBuilder.addCode("capability");
148 NormalizedNodeMessages.PathArgument.Builder pathBuilder = NormalizedNodeMessages.PathArgument.newBuilder();
150 pathBuilder.setIntType(PathArgumentType.NODE_IDENTIFIER.ordinal());
152 NormalizedNodeMessages.QName.Builder qNameBuilder = NormalizedNodeMessages.QName.newBuilder();
153 qNameBuilder.setNamespace(1);
154 qNameBuilder.setRevision(4);
155 qNameBuilder.setLocalName(8);
157 pathBuilder.setNodeType(qNameBuilder);
159 YangInstanceIdentifier.PathArgument pathArgument =
160 NormalizedNodeSerializer
161 .deSerialize(nodeBuilder.build(), pathBuilder.build());
163 assertNotNull(pathArgument);
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());