b7bb741e7335f58450f03387b7f1b0b496fdd3c5
[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
12 import java.util.Arrays;
13 import java.util.Iterator;
14 import org.apache.commons.lang.SerializationUtils;
15 import org.junit.Test;
16 import org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload;
17 import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
18 import org.opendaylight.controller.cluster.raft.ReplicatedLogImplEntry;
19
20 /**
21  * Unit tests for AppendEntries.
22  *
23  * @author Thomas Pantelis
24  */
25 public class AppendEntriesTest {
26
27     @Test
28     public void testSerialization() {
29         ReplicatedLogEntry entry1 = new ReplicatedLogImplEntry(1, 2, new MockPayload("payload1"));
30
31         ReplicatedLogEntry entry2 = new ReplicatedLogImplEntry(3, 4, new MockPayload("payload2"));
32
33         short payloadVersion = 5;
34         AppendEntries expected = new AppendEntries(5L, "node1", 7L, 8L, Arrays.asList(entry1, entry2), 10L,
35                 -1, payloadVersion);
36
37         AppendEntries cloned = (AppendEntries) SerializationUtils.clone(expected);
38
39         verifyAppendEntries(expected, cloned);
40     }
41
42     private static void verifyAppendEntries(AppendEntries expected, AppendEntries actual) {
43         assertEquals("getLeaderId", expected.getLeaderId(), actual.getLeaderId());
44         assertEquals("getTerm", expected.getTerm(), actual.getTerm());
45         assertEquals("getLeaderCommit", expected.getLeaderCommit(), actual.getLeaderCommit());
46         assertEquals("getPrevLogIndex", expected.getPrevLogIndex(), actual.getPrevLogIndex());
47         assertEquals("getPrevLogTerm", expected.getPrevLogTerm(), actual.getPrevLogTerm());
48         assertEquals("getReplicatedToAllIndex", expected.getReplicatedToAllIndex(), actual.getReplicatedToAllIndex());
49         assertEquals("getPayloadVersion", expected.getPayloadVersion(), actual.getPayloadVersion());
50
51         assertEquals("getEntries size", expected.getEntries().size(), actual.getEntries().size());
52         Iterator<ReplicatedLogEntry> iter = expected.getEntries().iterator();
53         for (ReplicatedLogEntry e: actual.getEntries()) {
54             verifyReplicatedLogEntry(iter.next(), e);
55         }
56     }
57
58     private static void verifyReplicatedLogEntry(ReplicatedLogEntry expected, ReplicatedLogEntry actual) {
59         assertEquals("getIndex", expected.getIndex(), actual.getIndex());
60         assertEquals("getTerm", expected.getTerm(), actual.getTerm());
61         assertEquals("getData", expected.getData().toString(), actual.getData().toString());
62     }
63 }

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