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;
13 import com.google.common.base.Optional;
14 import java.io.Serializable;
15 import java.util.Arrays;
16 import org.apache.commons.lang.SerializationUtils;
17 import org.junit.Test;
18 import org.opendaylight.controller.cluster.raft.RaftVersions;
19 import org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPayload;
20 import org.opendaylight.controller.cluster.raft.persisted.ServerInfo;
23 * Unit tests for InstallSnapshot.
25 * @author Thomas Pantelis
27 public class InstallSnapshotTest {
30 public void testSerialization() {
31 byte[] data = new byte[1000];
32 for (int i = 0, j = 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(
52 RaftVersions.CURRENT_VERSION));
53 verifyInstallSnapshot(expected, actual);
56 private static void verifyInstallSnapshot(InstallSnapshot expected, InstallSnapshot actual) {
57 assertEquals("getTerm", expected.getTerm(), actual.getTerm());
58 assertEquals("getChunkIndex", expected.getChunkIndex(), actual.getChunkIndex());
59 assertEquals("getTotalChunks", expected.getTotalChunks(), actual.getTotalChunks());
60 assertEquals("getLastIncludedTerm", expected.getLastIncludedTerm(), actual.getLastIncludedTerm());
61 assertEquals("getLastIncludedIndex", expected.getLastIncludedIndex(), actual.getLastIncludedIndex());
62 assertEquals("getLeaderId", expected.getLeaderId(), actual.getLeaderId());
63 assertEquals("getChunkIndex", expected.getChunkIndex(), actual.getChunkIndex());
64 assertArrayEquals("getData", expected.getData(), actual.getData());
66 assertEquals("getLastChunkHashCode present", expected.getLastChunkHashCode().isPresent(),
67 actual.getLastChunkHashCode().isPresent());
68 if (expected.getLastChunkHashCode().isPresent()) {
69 assertEquals("getLastChunkHashCode", expected.getLastChunkHashCode().get(),
70 actual.getLastChunkHashCode().get());
73 assertEquals("getServerConfig present", expected.getServerConfig().isPresent(),
74 actual.getServerConfig().isPresent());
75 if (expected.getServerConfig().isPresent()) {
76 assertEquals("getServerConfig", expected.getServerConfig().get().getServerConfig(),
77 actual.getServerConfig().get().getServerConfig());