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

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