19f0ec132b35fac68af6a9ebf3fb4c00626dd149
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / test / java / org / opendaylight / controller / cluster / raft / persisted / SnapshotTest.java
1 /*
2  * Copyright (c) 2017 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.persisted;
9
10 import static org.junit.Assert.assertEquals;
11
12 import java.util.Arrays;
13 import java.util.Collections;
14 import java.util.List;
15 import org.apache.commons.lang.SerializationUtils;
16 import org.junit.Test;
17 import org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload;
18 import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
19
20 /**
21  * Unit tests for Snapshot.
22  *
23  * @author Thomas Pantelis
24  */
25 public class SnapshotTest {
26
27     @Test
28     public void testSerialization() throws Exception {
29         testSerialization(new byte[]{1, 2, 3, 4, 5, 6, 7}, Arrays.asList(
30                 new SimpleReplicatedLogEntry(6, 2, new MockPayload("payload"))));
31         testSerialization(new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9}, Collections.emptyList());
32     }
33
34     private void testSerialization(byte[] state, List<ReplicatedLogEntry> unapplied) throws Exception {
35         long lastIndex = 6;
36         long lastTerm = 2;
37         long lastAppliedIndex = 5;
38         long lastAppliedTerm = 1;
39         long electionTerm = 3;
40         String electionVotedFor = "member-1";
41         ServerConfigurationPayload serverConfig = new ServerConfigurationPayload(Arrays.asList(
42                 new ServerInfo("1", true), new ServerInfo("2", false)));
43
44         Snapshot expected = Snapshot.create(ByteState.of(state), unapplied, lastIndex, lastTerm, lastAppliedIndex,
45                 lastAppliedTerm, electionTerm, electionVotedFor, serverConfig);
46         Snapshot cloned = (Snapshot) SerializationUtils.clone(expected);
47
48         assertEquals("lastIndex", expected.getLastIndex(), cloned.getLastIndex());
49         assertEquals("lastTerm", expected.getLastTerm(), cloned.getLastTerm());
50         assertEquals("lastAppliedIndex", expected.getLastAppliedIndex(), cloned.getLastAppliedIndex());
51         assertEquals("lastAppliedTerm", expected.getLastAppliedTerm(), cloned.getLastAppliedTerm());
52         assertEquals("unAppliedEntries", expected.getUnAppliedEntries(), cloned.getUnAppliedEntries());
53         assertEquals("electionTerm", expected.getElectionTerm(), cloned.getElectionTerm());
54         assertEquals("electionVotedFor", expected.getElectionVotedFor(), cloned.getElectionVotedFor());
55         assertEquals("state", expected.getState(), cloned.getState());
56         assertEquals("serverConfig", expected.getServerConfiguration().getServerConfig(),
57                 cloned.getServerConfiguration().getServerConfig());
58     }
59 }