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 static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
15 import com.google.common.base.Optional;
16 import org.junit.Rule;
17 import org.junit.Test;
18 import org.junit.rules.ExpectedException;
19 import org.opendaylight.controller.cluster.datastore.util.TestModel;
20 import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 public class NormalizedNodeSerializerTest {
30 private static final Logger LOG = LoggerFactory.getLogger(NormalizedNodeSerializerTest.class);
33 public ExpectedException expectedException = ExpectedException.none();
36 public void testSerializeDeSerialize() {
38 // This test basically serializes and deSerializes a largish document
39 // which contains most of the types of nodes that go into a normalized
40 // node and uses several value types as well. It is in general a good
41 // sanity test which could be augmented with specific unit tests.
43 long start = System.nanoTime();
45 NormalizedNode<?, ?> expectedNode =
46 TestModel.createDocumentOne(TestModel.createTestContext());
48 NormalizedNodeMessages.Node expected = NormalizedNodeSerializer
49 .serialize(expectedNode);
51 LOG.info("Serialize Time = {}", (System.nanoTime() - start) / 1000000);
53 LOG.info("Serialized Size = {}", expected.getSerializedSize());
55 LOG.info(expected.toString());
57 start = System.nanoTime();
59 NormalizedNode<?, ?> actualNode =
60 NormalizedNodeSerializer.deSerialize(expected);
62 LOG.info("DeSerialize Time = {}", (System.nanoTime() - start) / 1000000);
64 // Compare the original normalized node to the normalized node that was
65 // created by serializing the original node and deSerializing it back.
66 assertEquals(expectedNode, actualNode);
68 byte[] binaryData = new byte[5];
69 for (byte i = 0; i < 5; i++) {
73 ContainerNode node1 = TestModel.createBaseTestContainerBuilder()
74 .withChild(ImmutableNodes.leafNode(TestModel.SOME_BINARY_DATA_QNAME, binaryData))
77 NormalizedNodeMessages.Node serializedNode1 = NormalizedNodeSerializer
81 (ContainerNode) NormalizedNodeSerializer.deSerialize(serializedNode1);
84 // FIXME: This will not work due to BUG 2326. Once that is fixed we can uncomment this assertion
85 // assertEquals(node1, node2);
87 Optional<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> child =
88 node2.getChild(new YangInstanceIdentifier.NodeIdentifier(TestModel.SOME_BINARY_DATA_QNAME));
90 Object value = child.get().getValue();
92 assertTrue("value should be of type byte[]", value instanceof byte[]);
94 byte[] bytesValue = (byte[]) value;
96 for (byte i = 0; i < 5; i++) {
97 assertEquals(i, bytesValue[i]);
102 @Test(expected = NullPointerException.class)
103 public void testSerializeNullNormalizedNode() {
104 assertNotNull(NormalizedNodeSerializer.serialize(null));
108 public void testDeSerializeNullProtocolBufferNode() {
109 expectedException.expect(NullPointerException.class);
110 expectedException.expectMessage("node should not be null");
112 NormalizedNodeSerializer.deSerialize(null);
116 public void testDeSerializePathArgumentNullNode() {
117 expectedException.expect(NullPointerException.class);
118 expectedException.expectMessage("node should not be null");
120 NormalizedNodeSerializer
121 .deSerialize(null, NormalizedNodeMessages.PathArgument.getDefaultInstance());
125 public void testDeSerializePathArgumentNullPathArgument() {
126 expectedException.expect(NullPointerException.class);
127 expectedException.expectMessage("pathArgument should not be null");
129 NormalizedNodeSerializer.deSerialize(NormalizedNodeMessages.Node.getDefaultInstance() , null);
133 public void testDeSerializePathArgument() {
135 NormalizedNodeMessages.Node.Builder nodeBuilder = NormalizedNodeMessages.Node.newBuilder();
137 nodeBuilder.addCode("urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test1");
138 nodeBuilder.addCode("urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test");
141 nodeBuilder.addCode("2014-04-13");
142 nodeBuilder.addCode("2014-05-13");
143 nodeBuilder.addCode("2014-03-13");
145 nodeBuilder.addCode("dummy1");
146 nodeBuilder.addCode("dummy2");
147 nodeBuilder.addCode("dummy3");
148 nodeBuilder.addCode("capability");
152 NormalizedNodeMessages.PathArgument.Builder pathBuilder = NormalizedNodeMessages.PathArgument.newBuilder();
154 pathBuilder.setIntType(PathArgumentType.NODE_IDENTIFIER.ordinal());
156 NormalizedNodeMessages.QName.Builder qnameBuilder = NormalizedNodeMessages.QName.newBuilder();
157 qnameBuilder.setNamespace(1);
158 qnameBuilder.setRevision(4);
159 qnameBuilder.setLocalName(8);
161 pathBuilder.setNodeType(qnameBuilder);
163 YangInstanceIdentifier.PathArgument pathArgument =
164 NormalizedNodeSerializer
165 .deSerialize(nodeBuilder.build(), pathBuilder.build());
167 assertNotNull(pathArgument);
169 assertEquals("urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test",
170 pathArgument.getNodeType().getNamespace().toString());
171 assertEquals("2014-03-13", pathArgument.getNodeType().getFormattedRevision());
172 assertEquals("capability", pathArgument.getNodeType().getLocalName());