286cd3c9c4e0abec4108f4113fd27f5ea5b62e1e
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / test / java / org / opendaylight / controller / cluster / raft / messages / AppendEntriesTest.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.raft.messages;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertSame;
12 import java.util.Arrays;
13 import java.util.Collections;
14 import java.util.Iterator;
15 import org.apache.commons.lang.SerializationUtils;
16 import org.junit.Assert;
17 import org.junit.Test;
18 import org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload;
19 import org.opendaylight.controller.cluster.raft.RaftVersions;
20 import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
21 import org.opendaylight.controller.cluster.raft.ReplicatedLogImplEntry;
22 import org.opendaylight.controller.protobuff.messages.cluster.raft.AppendEntriesMessages;
23
24 /**
25  * Unit tests for AppendEntries.
26  *
27  * @author Thomas Pantelis
28  */
29 public class AppendEntriesTest {
30
31     @Test
32     public void testSerialization() {
33         ReplicatedLogEntry entry1 = new ReplicatedLogImplEntry(1, 2, new MockPayload("payload1"));
34
35         ReplicatedLogEntry entry2 = new ReplicatedLogImplEntry(3, 4, new MockPayload("payload2"));
36
37         short payloadVersion = 5;
38         AppendEntries expected = new AppendEntries(5L, "node1", 7L, 8L, Arrays.asList(entry1, entry2), 10L,
39                 -1, payloadVersion);
40
41         AppendEntries cloned = (AppendEntries) SerializationUtils.clone(expected);
42
43         verifyAppendEntries(expected, cloned);
44     }
45
46     @Test
47     public void testToAndFromSerializable() {
48         AppendEntries entries = new AppendEntries(5L, "node1", 7L, 8L,
49                 Collections.<ReplicatedLogEntry>emptyList(), 10L, -1, (short)0);
50
51         assertSame("toSerializable", entries, entries.toSerializable());
52         assertSame("fromSerializable", entries,
53                 org.opendaylight.controller.cluster.raft.SerializationUtils.fromSerializable(entries));
54     }
55
56     @Test
57     public void testToAndFromLegacySerializable() {
58         ReplicatedLogEntry entry = new ReplicatedLogImplEntry(3, 4, new MockPayload("payload"));
59         AppendEntries entries = new AppendEntries(5L, "node1", 7L, 8L, Arrays.asList(entry), 10L, -1, (short)0);
60
61         Object serializable = entries.toSerializable(RaftVersions.HELIUM_VERSION);
62         Assert.assertTrue(serializable instanceof AppendEntriesMessages.AppendEntries);
63
64         AppendEntries entries2 = (AppendEntries)
65                 org.opendaylight.controller.cluster.raft.SerializationUtils.fromSerializable(serializable);
66
67         verifyAppendEntries(entries, entries2);
68     }
69
70     private void verifyAppendEntries(AppendEntries expected, AppendEntries actual) {
71         assertEquals("getLeaderId", expected.getLeaderId(), actual.getLeaderId());
72         assertEquals("getTerm", expected.getTerm(), actual.getTerm());
73         assertEquals("getLeaderCommit", expected.getLeaderCommit(), actual.getLeaderCommit());
74         assertEquals("getPrevLogIndex", expected.getPrevLogIndex(), actual.getPrevLogIndex());
75         assertEquals("getPrevLogTerm", expected.getPrevLogTerm(), actual.getPrevLogTerm());
76         assertEquals("getReplicatedToAllIndex", expected.getReplicatedToAllIndex(), actual.getReplicatedToAllIndex());
77         assertEquals("getPayloadVersion", expected.getPayloadVersion(), actual.getPayloadVersion());
78
79         assertEquals("getEntries size", expected.getEntries().size(), actual.getEntries().size());
80         Iterator<ReplicatedLogEntry> iter = expected.getEntries().iterator();
81         for(ReplicatedLogEntry e: actual.getEntries()) {
82             verifyReplicatedLogEntry(iter.next(), e);
83         }
84     }
85
86     private void verifyReplicatedLogEntry(ReplicatedLogEntry expected, ReplicatedLogEntry actual) {
87         assertEquals("getIndex", expected.getIndex(), actual.getIndex());
88         assertEquals("getTerm", expected.getTerm(), actual.getTerm());
89         assertEquals("getData", expected.getData().toString(), actual.getData().toString());
90     }
91 }

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