1df8e9775b89219439f31cbafbec69e675a353d8
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / messages / BatchedModificationsTest.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.cluster.datastore.modification.DeleteModification;
16 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
17 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
18 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
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 BatchedModifications.
26  *
27  * @author Thomas Pantelis
28  */
29 public class BatchedModificationsTest {
30
31     @Test
32     public void testSerialization() {
33         YangInstanceIdentifier writePath = TestModel.TEST_PATH;
34         NormalizedNode<?, ?> writeData = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
35                 new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).
36                 withChild(ImmutableNodes.leafNode(TestModel.DESC_QNAME, "foo")).build();
37
38         YangInstanceIdentifier mergePath = TestModel.OUTER_LIST_PATH;
39         NormalizedNode<?, ?> mergeData = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
40                 new YangInstanceIdentifier.NodeIdentifier(TestModel.OUTER_LIST_QNAME)).build();
41
42         YangInstanceIdentifier deletePath = TestModel.TEST_PATH;
43
44         BatchedModifications batched = new BatchedModifications("tx1", DataStoreVersions.CURRENT_VERSION, "txChain");
45         batched.addModification(new WriteModification(writePath, writeData));
46         batched.addModification(new MergeModification(mergePath, mergeData));
47         batched.addModification(new DeleteModification(deletePath));
48         batched.setReady(true);
49         batched.setTotalMessagesSent(5);
50
51         BatchedModifications clone = (BatchedModifications) SerializationUtils.clone(
52                 (Serializable) batched.toSerializable());
53
54         assertEquals("getVersion", DataStoreVersions.CURRENT_VERSION, clone.getVersion());
55         assertEquals("getTransactionID", "tx1", clone.getTransactionID());
56         assertEquals("getTransactionChainID", "txChain", clone.getTransactionChainID());
57         assertEquals("isReady", true, clone.isReady());
58         assertEquals("getTotalMessagesSent", 5, clone.getTotalMessagesSent());
59
60         assertEquals("getModifications size", 3, clone.getModifications().size());
61
62         WriteModification write = (WriteModification)clone.getModifications().get(0);
63         assertEquals("getVersion", DataStoreVersions.CURRENT_VERSION, write.getVersion());
64         assertEquals("getPath", writePath, write.getPath());
65         assertEquals("getData", writeData, write.getData());
66
67         MergeModification merge = (MergeModification)clone.getModifications().get(1);
68         assertEquals("getVersion", DataStoreVersions.CURRENT_VERSION, merge.getVersion());
69         assertEquals("getPath", mergePath, merge.getPath());
70         assertEquals("getData", mergeData, merge.getData());
71
72         DeleteModification delete = (DeleteModification)clone.getModifications().get(2);
73         assertEquals("getVersion", DataStoreVersions.CURRENT_VERSION, delete.getVersion());
74         assertEquals("getPath", deletePath, delete.getPath());
75
76         // Test with different params.
77
78         batched = new BatchedModifications("tx2", (short)10, null);
79
80         clone = (BatchedModifications) SerializationUtils.clone((Serializable) batched.toSerializable());
81
82         assertEquals("getVersion", 10, clone.getVersion());
83         assertEquals("getTransactionID", "tx2", clone.getTransactionID());
84         assertEquals("getTransactionChainID", "", clone.getTransactionChainID());
85         assertEquals("isReady", false, clone.isReady());
86
87         assertEquals("getModifications size", 0, clone.getModifications().size());
88
89     }
90
91     @Test
92     public void testBatchedModificationsReplySerialization() {
93         BatchedModificationsReply clone = (BatchedModificationsReply) SerializationUtils.clone(
94                 (Serializable) new BatchedModificationsReply(100).toSerializable());
95         assertEquals("getNumBatched", 100, clone.getNumBatched());
96     }
97 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.