2 * Copyright (c) 2015 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore.messages;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
13 import akka.actor.ExtendedActorSystem;
14 import akka.testkit.javadsl.TestKit;
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;
34 * Unit tests for ReadyLocalTransactionSerializer.
36 * @author Thomas Pantelis
38 public class ReadyLocalTransactionSerializerTest extends AbstractTest {
41 public void testToAndFromBinary() throws NotSerializableException {
42 DataTree dataTree = new InMemoryDataTreeFactory().create(
43 DataTreeConfiguration.DEFAULT_OPERATIONAL, TestModel.createTestContext());
44 DataTreeModification modification = dataTree.takeSnapshot().newModification();
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);
51 TransactionIdentifier txId = nextTransactionId();
52 ReadyLocalTransaction readyMessage = new ReadyLocalTransaction(txId, modification, true);
54 final ExtendedActorSystem system = (ExtendedActorSystem) ExtendedActorSystem.create("test");
55 final Object deserialized;
57 final ReadyLocalTransactionSerializer serializer = new ReadyLocalTransactionSerializer(system);
58 final byte[] bytes = serializer.toBinary(readyMessage);
59 deserialized = serializer.fromBinary(bytes, ReadyLocalTransaction.class);
61 TestKit.shutdownActorSystem(system);
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());
70 List<Modification> batchedMods = batched.getModifications();
71 assertEquals("getModifications size", 2, batchedMods.size());
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());
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());