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