X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-akka-raft%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fraft%2Fpersisted%2FSnapshotTest.java;fp=opendaylight%2Fmd-sal%2Fsal-akka-raft%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fraft%2Fpersisted%2FSnapshotTest.java;h=19f0ec132b35fac68af6a9ebf3fb4c00626dd149;hb=2faf656bf68dd3843fd59520b27a7ec2abbdcc68;hp=0000000000000000000000000000000000000000;hpb=d04b71990a802071a786fe8f0df57bc4adbdec3f;p=controller.git diff --git a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/persisted/SnapshotTest.java b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/persisted/SnapshotTest.java new file mode 100644 index 0000000000..19f0ec132b --- /dev/null +++ b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/persisted/SnapshotTest.java @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2017 Brocade Communications Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.controller.cluster.raft.persisted; + +import static org.junit.Assert.assertEquals; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import org.apache.commons.lang.SerializationUtils; +import org.junit.Test; +import org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload; +import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry; + +/** + * Unit tests for Snapshot. + * + * @author Thomas Pantelis + */ +public class SnapshotTest { + + @Test + public void testSerialization() throws Exception { + testSerialization(new byte[]{1, 2, 3, 4, 5, 6, 7}, Arrays.asList( + new SimpleReplicatedLogEntry(6, 2, new MockPayload("payload")))); + testSerialization(new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9}, Collections.emptyList()); + } + + private void testSerialization(byte[] state, List unapplied) throws Exception { + long lastIndex = 6; + long lastTerm = 2; + long lastAppliedIndex = 5; + long lastAppliedTerm = 1; + long electionTerm = 3; + String electionVotedFor = "member-1"; + ServerConfigurationPayload serverConfig = new ServerConfigurationPayload(Arrays.asList( + new ServerInfo("1", true), new ServerInfo("2", false))); + + Snapshot expected = Snapshot.create(ByteState.of(state), unapplied, lastIndex, lastTerm, lastAppliedIndex, + lastAppliedTerm, electionTerm, electionVotedFor, serverConfig); + Snapshot cloned = (Snapshot) SerializationUtils.clone(expected); + + assertEquals("lastIndex", expected.getLastIndex(), cloned.getLastIndex()); + assertEquals("lastTerm", expected.getLastTerm(), cloned.getLastTerm()); + assertEquals("lastAppliedIndex", expected.getLastAppliedIndex(), cloned.getLastAppliedIndex()); + assertEquals("lastAppliedTerm", expected.getLastAppliedTerm(), cloned.getLastAppliedTerm()); + assertEquals("unAppliedEntries", expected.getUnAppliedEntries(), cloned.getUnAppliedEntries()); + assertEquals("electionTerm", expected.getElectionTerm(), cloned.getElectionTerm()); + assertEquals("electionVotedFor", expected.getElectionVotedFor(), cloned.getElectionVotedFor()); + assertEquals("state", expected.getState(), cloned.getState()); + assertEquals("serverConfig", expected.getServerConfiguration().getServerConfig(), + cloned.getServerConfiguration().getServerConfig()); + } +}