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 org.apache.commons.lang.SerializationUtils;
15 import org.junit.Test;
16 import org.opendaylight.controller.cluster.raft.RaftVersions;
17 import org.opendaylight.controller.protobuff.messages.cluster.raft.InstallSnapshotMessages;
20 * Unit tests for InstallSnapshot.
22 * @author Thomas Pantelis
24 public class InstallSnapshotTest {
27 public void testSerialization() {
28 byte[] data = new byte[1000];
30 for(int i = 0; i < data.length; i++) {
37 InstallSnapshot expected = new InstallSnapshot(3L, "leaderId", 11L, 2L, data, 5, 6, Optional.<Integer>of(54321));
39 Object serialized = expected.toSerializable(RaftVersions.CURRENT_VERSION);
40 assertEquals("Serialized type", InstallSnapshot.class, serialized.getClass());
42 InstallSnapshot actual = InstallSnapshot.fromSerializable(SerializationUtils.clone((Serializable) serialized));
43 verifyInstallSnapshot(expected, actual);
45 expected = new InstallSnapshot(3L, "leaderId", 11L, 2L, data, 5, 6);
46 actual = InstallSnapshot.fromSerializable(SerializationUtils.clone(
47 (Serializable) expected.toSerializable(RaftVersions.CURRENT_VERSION)));
48 verifyInstallSnapshot(expected, actual);
52 public void testSerializationWithPreBoronVersion() {
53 byte[] data = {0,1,2,3,4,5,7,8,9};
54 InstallSnapshot expected = new InstallSnapshot(3L, "leaderId", 11L, 2L, data, 5, 6, Optional.<Integer>of(54321));
56 Object serialized = expected.toSerializable(RaftVersions.LITHIUM_VERSION);
57 assertEquals("Serialized type", InstallSnapshot.SERIALIZABLE_CLASS, serialized.getClass());
59 InstallSnapshot actual = InstallSnapshot.fromSerializable(SerializationUtils.clone((Serializable) serialized));
60 verifyInstallSnapshot(expected, actual);
64 public void testIsSerializedType() {
65 assertEquals("isSerializedType", true, InstallSnapshot.isSerializedType(
66 InstallSnapshotMessages.InstallSnapshot.newBuilder().build()));
67 assertEquals("isSerializedType", true, InstallSnapshot.isSerializedType(new InstallSnapshot()));
68 assertEquals("isSerializedType", false, InstallSnapshot.isSerializedType(new Object()));
71 private static void verifyInstallSnapshot(InstallSnapshot expected, InstallSnapshot actual) {
72 assertEquals("getTerm", expected.getTerm(), actual.getTerm());
73 assertEquals("getChunkIndex", expected.getChunkIndex(), actual.getChunkIndex());
74 assertEquals("getTotalChunks", expected.getTotalChunks(), actual.getTotalChunks());
75 assertEquals("getLastIncludedTerm", expected.getLastIncludedTerm(), actual.getLastIncludedTerm());
76 assertEquals("getLastIncludedIndex", expected.getLastIncludedIndex(), actual.getLastIncludedIndex());
77 assertEquals("getLeaderId", expected.getLeaderId(), actual.getLeaderId());
78 assertEquals("getChunkIndex", expected.getChunkIndex(), actual.getChunkIndex());
79 assertArrayEquals("getData", expected.getData(), actual.getData());
80 assertEquals("getLastChunkHashCode present", expected.getLastChunkHashCode().isPresent(),
81 actual.getLastChunkHashCode().isPresent());
82 if(expected.getLastChunkHashCode().isPresent()) {
83 assertEquals("getLastChunkHashCode", expected.getLastChunkHashCode().get(),
84 actual.getLastChunkHashCode().get());