BUG 2412 - restconf @GET getModule(identifier,uri) method migration
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / messages / WriteDataTest.java
1 /*
2  * Copyright (c) 2014 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.datastore.messages;
9
10 import static org.junit.Assert.assertEquals;
11 import java.io.Serializable;
12 import org.apache.commons.lang.SerializationUtils;
13 import org.junit.Test;
14 import org.opendaylight.controller.cluster.datastore.DataStoreVersions;
15 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
16 import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages.InstanceIdentifier;
17 import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages.Node;
18 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
22 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
23
24 /**
25  * Unit tests for WriteData.
26  *
27  * @author Thomas Pantelis
28  */
29 public class WriteDataTest {
30
31     @Test
32     public void testSerialization() {
33         YangInstanceIdentifier path = TestModel.TEST_PATH;
34         NormalizedNode<?, ?> data = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
35                 new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).
36                 withChild(ImmutableNodes.leafNode(TestModel.DESC_QNAME, "foo")).build();
37
38         WriteData expected = new WriteData(path, data);
39
40         Object serialized = expected.toSerializable(DataStoreVersions.CURRENT_VERSION);
41         assertEquals("Serialized type", WriteData.class, serialized.getClass());
42         assertEquals("Version", DataStoreVersions.CURRENT_VERSION, ((WriteData)serialized).getVersion());
43
44         Object clone = SerializationUtils.clone((Serializable) serialized);
45         assertEquals("Version", DataStoreVersions.CURRENT_VERSION, ((WriteData)clone).getVersion());
46         WriteData actual = WriteData.fromSerializable(clone);
47         assertEquals("getPath", expected.getPath(), actual.getPath());
48         assertEquals("getData", expected.getData(), actual.getData());
49     }
50
51     @Test
52     public void testIsSerializedType() {
53         assertEquals("isSerializedType", true, WriteData.isSerializedType(
54                 ShardTransactionMessages.WriteData.newBuilder()
55                     .setInstanceIdentifierPathArguments(InstanceIdentifier.getDefaultInstance())
56                     .setNormalizedNode(Node.getDefaultInstance()).build()));
57         assertEquals("isSerializedType", true, WriteData.isSerializedType(new WriteData()));
58         assertEquals("isSerializedType", false, WriteData.isSerializedType(new Object()));
59     }
60
61     /**
62      * Tests backwards compatible serialization/deserialization of a WriteData message with the
63      * base and R1 Helium versions, which used the protobuff WriteData message.
64      */
65     @Test
66     public void testSerializationWithHeliumR1Version() throws Exception {
67         YangInstanceIdentifier path = TestModel.TEST_PATH;
68         NormalizedNode<?, ?> data = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
69                 new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).
70                 withChild(ImmutableNodes.leafNode(TestModel.DESC_QNAME, "foo")).build();
71
72         WriteData expected = new WriteData(path, data);
73
74         Object serialized = expected.toSerializable(DataStoreVersions.HELIUM_1_VERSION);
75         assertEquals("Serialized type", ShardTransactionMessages.WriteData.class, serialized.getClass());
76
77         WriteData actual = WriteData.fromSerializable(SerializationUtils.clone((Serializable) serialized));
78         assertEquals("getPath", expected.getPath(), actual.getPath());
79         assertEquals("getData", expected.getData(), actual.getData());
80     }
81 }