Improve segmented journal actor metrics
[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.List;
13 import org.apache.commons.lang3.SerializationUtils;
14 import org.junit.Test;
15 import org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload;
16 import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
17
18 /**
19  * Unit tests for Snapshot.
20  *
21  * @author Thomas Pantelis
22  */
23 public class SnapshotTest {
24     @Test
25     public void testSerialization() {
26         testSerialization(new byte[]{1, 2, 3, 4, 5, 6, 7}, List.of(
27                 new SimpleReplicatedLogEntry(6, 2, new MockPayload("payload"))), 491);
28         testSerialization(new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9}, List.of(), 345);
29     }
30
31     private static void testSerialization(final byte[] state, final List<ReplicatedLogEntry> unapplied,
32             final int expectedSize) {
33         long lastIndex = 6;
34         long lastTerm = 2;
35         long lastAppliedIndex = 5;
36         long lastAppliedTerm = 1;
37         long electionTerm = 3;
38         String electionVotedFor = "member-1";
39         ServerConfigurationPayload serverConfig = new ServerConfigurationPayload(List.of(
40                 new ServerInfo("1", true), new ServerInfo("2", false)));
41
42         final var expected = Snapshot.create(ByteState.of(state), unapplied, lastIndex, lastTerm, lastAppliedIndex,
43                 lastAppliedTerm, electionTerm, electionVotedFor, serverConfig);
44         final var bytes = SerializationUtils.serialize(expected);
45         assertEquals(expectedSize, bytes.length);
46         final var cloned = (Snapshot) SerializationUtils.deserialize(bytes);
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 }