2 * Copyright (c) 2015 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.messages;
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import com.google.common.base.Optional;
13 import java.io.Serializable;
14 import java.util.Arrays;
15 import org.apache.commons.lang.SerializationUtils;
16 import org.junit.Test;
17 import org.opendaylight.controller.cluster.raft.RaftVersions;
18 import org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPayload;
19 import org.opendaylight.controller.cluster.raft.persisted.ServerInfo;
22 * Unit tests for InstallSnapshot.
24 * @author Thomas Pantelis
26 public class InstallSnapshotTest {
29 public void testSerialization() {
30 byte[] data = new byte[1000];
32 for(int i = 0; i < data.length; i++) {
39 ServerConfigurationPayload serverConfig = new ServerConfigurationPayload(Arrays.asList(
40 new ServerInfo("leader", true), new ServerInfo("follower", false)));
41 InstallSnapshot expected = new InstallSnapshot(3L, "leaderId", 11L, 2L, data, 5, 6,
42 Optional.<Integer>of(54321), Optional.of(serverConfig));
44 Object serialized = expected.toSerializable(RaftVersions.CURRENT_VERSION);
45 assertEquals("Serialized type", InstallSnapshot.class, serialized.getClass());
47 InstallSnapshot actual = (InstallSnapshot) SerializationUtils.clone((Serializable) serialized);
48 verifyInstallSnapshot(expected, actual);
50 expected = new InstallSnapshot(3L, "leaderId", 11L, 2L, data, 5, 6);
51 actual = (InstallSnapshot) SerializationUtils.clone((Serializable) expected.toSerializable(RaftVersions.CURRENT_VERSION));
52 verifyInstallSnapshot(expected, actual);
55 private static void verifyInstallSnapshot(InstallSnapshot expected, InstallSnapshot actual) {
56 assertEquals("getTerm", expected.getTerm(), actual.getTerm());
57 assertEquals("getChunkIndex", expected.getChunkIndex(), actual.getChunkIndex());
58 assertEquals("getTotalChunks", expected.getTotalChunks(), actual.getTotalChunks());
59 assertEquals("getLastIncludedTerm", expected.getLastIncludedTerm(), actual.getLastIncludedTerm());
60 assertEquals("getLastIncludedIndex", expected.getLastIncludedIndex(), actual.getLastIncludedIndex());
61 assertEquals("getLeaderId", expected.getLeaderId(), actual.getLeaderId());
62 assertEquals("getChunkIndex", expected.getChunkIndex(), actual.getChunkIndex());
63 assertArrayEquals("getData", expected.getData(), actual.getData());
65 assertEquals("getLastChunkHashCode present", expected.getLastChunkHashCode().isPresent(),
66 actual.getLastChunkHashCode().isPresent());
67 if(expected.getLastChunkHashCode().isPresent()) {
68 assertEquals("getLastChunkHashCode", expected.getLastChunkHashCode().get(),
69 actual.getLastChunkHashCode().get());
72 assertEquals("getServerConfig present", expected.getServerConfig().isPresent(),
73 actual.getServerConfig().isPresent());
74 if(expected.getServerConfig().isPresent()) {
75 assertEquals("getServerConfig", expected.getServerConfig().get().getServerConfig(),
76 actual.getServerConfig().get().getServerConfig());