e950b78ab7d609a6b0aac6e34431cbb5a3f36584
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / messages / DeleteDataTest.java
1 /*
2  * Copyright (c) 2015 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.transaction.ShardTransactionMessages;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19
20 /**
21  * Unit tests for DeleteData.
22  *
23  * @author Thomas Pantelis
24  */
25 public class DeleteDataTest {
26
27     @Test
28     public void testSerialization() {
29         YangInstanceIdentifier path = TestModel.TEST_PATH;
30
31         DeleteData expected = new DeleteData(path);
32
33         Object serialized = expected.toSerializable(DataStoreVersions.CURRENT_VERSION);
34         assertEquals("Serialized type", DeleteData.class, serialized.getClass());
35         assertEquals("Version", DataStoreVersions.CURRENT_VERSION, ((DeleteData)serialized).getVersion());
36
37         Object clone = SerializationUtils.clone((Serializable) serialized);
38         assertEquals("Version", DataStoreVersions.CURRENT_VERSION, ((DeleteData)clone).getVersion());
39         DeleteData actual = DeleteData.fromSerializable(clone);
40         assertEquals("getPath", expected.getPath(), actual.getPath());
41     }
42
43     @Test
44     public void testIsSerializedType() {
45         assertEquals("isSerializedType", true, DeleteData.isSerializedType(
46                 ShardTransactionMessages.DeleteData.newBuilder()
47                     .setInstanceIdentifierPathArguments(InstanceIdentifier.getDefaultInstance()).build()));
48         assertEquals("isSerializedType", true,
49                 DeleteData.isSerializedType(new DeleteData()));
50         assertEquals("isSerializedType", false, DeleteData.isSerializedType(new Object()));
51     }
52
53     /**
54      * Tests backwards compatible serialization/deserialization of a DeleteData message with the
55      * base and R1 Helium versions, which used the protobuff DeleteData message.
56      */
57     @Test
58     public void testSerializationWithHeliumR1Version() throws Exception {
59         YangInstanceIdentifier path = TestModel.TEST_PATH;
60
61         DeleteData expected = new DeleteData(path);
62
63         Object serialized = expected.toSerializable(DataStoreVersions.HELIUM_1_VERSION);
64         assertEquals("Serialized type", ShardTransactionMessages.DeleteData.class, serialized.getClass());
65
66         DeleteData actual = DeleteData.fromSerializable(SerializationUtils.clone((Serializable) serialized));
67         assertEquals("getPath", expected.getPath(), actual.getPath());
68     }
69 }