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 java.io.Serializable;
14 import java.util.Arrays;
15 import java.util.Optional;
16 import java.util.OptionalInt;
17 import org.apache.commons.lang.SerializationUtils;
18 import org.junit.Test;
19 import org.opendaylight.controller.cluster.raft.RaftVersions;
20 import org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPayload;
21 import org.opendaylight.controller.cluster.raft.persisted.ServerInfo;
24 * Unit tests for InstallSnapshot.
26 * @author Thomas Pantelis
28 public class InstallSnapshotTest {
31 public void testSerialization() {
32 byte[] data = new byte[1000];
33 for (int i = 0, j = 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, OptionalInt.of(54321),
43 Optional.of(serverConfig));
45 Object serialized = expected.toSerializable(RaftVersions.CURRENT_VERSION);
46 assertEquals("Serialized type", InstallSnapshot.class, serialized.getClass());
48 var bytes = SerializationUtils.serialize((Serializable) serialized);
49 assertEquals(1302, bytes.length);
50 var actual = (InstallSnapshot) SerializationUtils.deserialize(bytes);
52 verifyInstallSnapshot(expected, actual);
54 expected = new InstallSnapshot(3L, "leaderId", 11L, 2L, data, 5, 6);
55 bytes = SerializationUtils.serialize((Serializable) expected.toSerializable(RaftVersions.CURRENT_VERSION));
56 assertEquals(1165, bytes.length);
57 actual = (InstallSnapshot) SerializationUtils.deserialize(bytes);
58 verifyInstallSnapshot(expected, actual);
61 private static void verifyInstallSnapshot(final InstallSnapshot expected, final InstallSnapshot actual) {
62 assertEquals("getTerm", expected.getTerm(), actual.getTerm());
63 assertEquals("getChunkIndex", expected.getChunkIndex(), actual.getChunkIndex());
64 assertEquals("getTotalChunks", expected.getTotalChunks(), actual.getTotalChunks());
65 assertEquals("getLastIncludedTerm", expected.getLastIncludedTerm(), actual.getLastIncludedTerm());
66 assertEquals("getLastIncludedIndex", expected.getLastIncludedIndex(), actual.getLastIncludedIndex());
67 assertEquals("getLeaderId", expected.getLeaderId(), actual.getLeaderId());
68 assertEquals("getChunkIndex", expected.getChunkIndex(), actual.getChunkIndex());
69 assertArrayEquals("getData", expected.getData(), actual.getData());
71 assertEquals("getLastChunkHashCode present", expected.getLastChunkHashCode().isPresent(),
72 actual.getLastChunkHashCode().isPresent());
73 if (expected.getLastChunkHashCode().isPresent()) {
74 assertEquals("getLastChunkHashCode", expected.getLastChunkHashCode(),
75 actual.getLastChunkHashCode());
78 assertEquals("getServerConfig present", expected.getServerConfig().isPresent(),
79 actual.getServerConfig().isPresent());
80 if (expected.getServerConfig().isPresent()) {
81 assertEquals("getServerConfig", expected.getServerConfig().get().getServerConfig(),
82 actual.getServerConfig().get().getServerConfig());