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.ServerConfigurationPayload;
19 import org.opendaylight.controller.cluster.raft.ServerConfigurationPayload.ServerInfo;
20 import org.opendaylight.controller.protobuff.messages.cluster.raft.InstallSnapshotMessages;
23 * Unit tests for InstallSnapshot.
25 * @author Thomas Pantelis
27 public class InstallSnapshotTest {
30 public void testSerialization() {
31 byte[] data = new byte[1000];
33 for(int i = 0; i < data.length; i++) {
40 ServerConfigurationPayload serverConfig = new ServerConfigurationPayload(Arrays.asList(
41 new ServerInfo("leader", true), new ServerInfo("follower", false)));
42 InstallSnapshot expected = new InstallSnapshot(3L, "leaderId", 11L, 2L, data, 5, 6,
43 Optional.<Integer>of(54321), Optional.of(serverConfig));
45 Object serialized = expected.toSerializable(RaftVersions.CURRENT_VERSION);
46 assertEquals("Serialized type", InstallSnapshot.class, serialized.getClass());
48 InstallSnapshot actual = InstallSnapshot.fromSerializable(SerializationUtils.clone((Serializable) serialized));
49 verifyInstallSnapshot(expected, actual);
51 expected = new InstallSnapshot(3L, "leaderId", 11L, 2L, data, 5, 6);
52 actual = InstallSnapshot.fromSerializable(SerializationUtils.clone(
53 (Serializable) expected.toSerializable(RaftVersions.CURRENT_VERSION)));
54 verifyInstallSnapshot(expected, actual);
58 public void testSerializationWithPreBoronVersion() {
59 byte[] data = {0,1,2,3,4,5,7,8,9};
60 InstallSnapshot expected = new InstallSnapshot(3L, "leaderId", 11L, 2L, data, 5, 6, Optional.<Integer>of(54321),
61 Optional.<ServerConfigurationPayload>absent());
63 Object serialized = expected.toSerializable(RaftVersions.LITHIUM_VERSION);
64 assertEquals("Serialized type", InstallSnapshot.SERIALIZABLE_CLASS, serialized.getClass());
66 InstallSnapshot actual = InstallSnapshot.fromSerializable(SerializationUtils.clone((Serializable) serialized));
67 verifyInstallSnapshot(expected, actual);
71 public void testIsSerializedType() {
72 assertEquals("isSerializedType", true, InstallSnapshot.isSerializedType(
73 InstallSnapshotMessages.InstallSnapshot.newBuilder().build()));
74 assertEquals("isSerializedType", true, InstallSnapshot.isSerializedType(new InstallSnapshot()));
75 assertEquals("isSerializedType", false, InstallSnapshot.isSerializedType(new Object()));
78 private static void verifyInstallSnapshot(InstallSnapshot expected, InstallSnapshot actual) {
79 assertEquals("getTerm", expected.getTerm(), actual.getTerm());
80 assertEquals("getChunkIndex", expected.getChunkIndex(), actual.getChunkIndex());
81 assertEquals("getTotalChunks", expected.getTotalChunks(), actual.getTotalChunks());
82 assertEquals("getLastIncludedTerm", expected.getLastIncludedTerm(), actual.getLastIncludedTerm());
83 assertEquals("getLastIncludedIndex", expected.getLastIncludedIndex(), actual.getLastIncludedIndex());
84 assertEquals("getLeaderId", expected.getLeaderId(), actual.getLeaderId());
85 assertEquals("getChunkIndex", expected.getChunkIndex(), actual.getChunkIndex());
86 assertArrayEquals("getData", expected.getData(), actual.getData());
88 assertEquals("getLastChunkHashCode present", expected.getLastChunkHashCode().isPresent(),
89 actual.getLastChunkHashCode().isPresent());
90 if(expected.getLastChunkHashCode().isPresent()) {
91 assertEquals("getLastChunkHashCode", expected.getLastChunkHashCode().get(),
92 actual.getLastChunkHashCode().get());
95 assertEquals("getServerConfig present", expected.getServerConfig().isPresent(),
96 actual.getServerConfig().isPresent());
97 if(expected.getServerConfig().isPresent()) {
98 assertEquals("getServerConfig", expected.getServerConfig().get().getServerConfig(),
99 actual.getServerConfig().get().getServerConfig());