2 * Copyright (c) 2017 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.raft.persisted;
10 import static org.junit.Assert.assertEquals;
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;
19 * Unit tests for Snapshot.
21 * @author Thomas Pantelis
23 public class SnapshotTest {
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);
31 private static void testSerialization(final byte[] state, final List<ReplicatedLogEntry> unapplied,
32 final int expectedSize) {
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)));
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);
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());