Adjust to yangtools-2.0.0 changes
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / messages / ReadyLocalTransactionSerializerTest.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 static org.junit.Assert.assertNotNull;
12
13 import java.io.NotSerializableException;
14 import java.util.List;
15 import org.junit.Test;
16 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
17 import org.opendaylight.controller.cluster.datastore.AbstractTest;
18 import org.opendaylight.controller.cluster.datastore.DataStoreVersions;
19 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
20 import org.opendaylight.controller.cluster.datastore.modification.Modification;
21 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
22 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
23 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
28 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
29 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
30
31 /**
32  * Unit tests for ReadyLocalTransactionSerializer.
33  *
34  * @author Thomas Pantelis
35  */
36 public class ReadyLocalTransactionSerializerTest extends AbstractTest {
37
38     @Test
39     public void testToAndFromBinary() throws NotSerializableException {
40         DataTree dataTree = new InMemoryDataTreeFactory().create(
41             DataTreeConfiguration.DEFAULT_OPERATIONAL, TestModel.createTestContext());
42         DataTreeModification modification = dataTree.takeSnapshot().newModification();
43
44         ContainerNode writeData = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
45         new WriteModification(TestModel.TEST_PATH, writeData).apply(modification);
46         MapNode mergeData = ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build();
47         new MergeModification(TestModel.OUTER_LIST_PATH, mergeData).apply(modification);
48
49         TransactionIdentifier txId = nextTransactionId();
50         ReadyLocalTransaction readyMessage = new ReadyLocalTransaction(txId, modification, true);
51
52         ReadyLocalTransactionSerializer serializer = new ReadyLocalTransactionSerializer();
53
54         byte[] bytes = serializer.toBinary(readyMessage);
55
56         Object deserialized = serializer.fromBinary(bytes, ReadyLocalTransaction.class);
57
58         assertNotNull("fromBinary returned null", deserialized);
59         assertEquals("fromBinary return type", BatchedModifications.class, deserialized.getClass());
60         BatchedModifications batched = (BatchedModifications)deserialized;
61         assertEquals("getTransactionID", txId, batched.getTransactionId());
62         assertEquals("getVersion", DataStoreVersions.CURRENT_VERSION, batched.getVersion());
63
64         List<Modification> batchedMods = batched.getModifications();
65         assertEquals("getModifications size", 2, batchedMods.size());
66
67         Modification mod = batchedMods.get(0);
68         assertEquals("Modification type", WriteModification.class, mod.getClass());
69         assertEquals("Modification getPath", TestModel.TEST_PATH, ((WriteModification)mod).getPath());
70         assertEquals("Modification getData", writeData, ((WriteModification)mod).getData());
71
72         mod = batchedMods.get(1);
73         assertEquals("Modification type", MergeModification.class, mod.getClass());
74         assertEquals("Modification getPath", TestModel.OUTER_LIST_PATH, ((MergeModification)mod).getPath());
75         assertEquals("Modification getData", mergeData, ((MergeModification)mod).getData());
76     }
77 }

©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.