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.util.List;
14 import java.util.Optional;
15 import java.util.OptionalInt;
16 import org.apache.commons.lang3.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 {
29 public void testCurrentSerialization() {
30 testSerialization(RaftVersions.CURRENT_VERSION, 1262, 1125);
34 public void testFluorineSerialization() {
35 testSerialization(RaftVersions.FLUORINE_VERSION, 1302, 1165);
38 private static void testSerialization(final short raftVersion, final int fullSize, final int emptySize) {
39 byte[] data = new byte[1000];
40 for (int i = 0, j = 0; i < data.length; i++) {
47 var serverConfig = new ServerConfigurationPayload(List.of(
48 new ServerInfo("leader", true), new ServerInfo("follower", false)));
49 assertInstallSnapshot(fullSize, new InstallSnapshot(3L, "leaderId", 11L, 2L, data, 5, 6, OptionalInt.of(54321),
50 Optional.of(serverConfig), raftVersion));
52 assertInstallSnapshot(emptySize, new InstallSnapshot(3L, "leaderId", 11L, 2L, data, 5, 6, OptionalInt.empty(),
53 Optional.empty(), raftVersion));
56 private static void assertInstallSnapshot(final int expectedSize, final InstallSnapshot expected) {
57 final var bytes = SerializationUtils.serialize(expected);
58 assertEquals(expectedSize, bytes.length);
59 verifyInstallSnapshot(expected, (InstallSnapshot) SerializationUtils.deserialize(bytes));
62 private static void verifyInstallSnapshot(final InstallSnapshot expected, final InstallSnapshot actual) {
63 assertEquals("getTerm", expected.getTerm(), actual.getTerm());
64 assertEquals("getChunkIndex", expected.getChunkIndex(), actual.getChunkIndex());
65 assertEquals("getTotalChunks", expected.getTotalChunks(), actual.getTotalChunks());
66 assertEquals("getLastIncludedTerm", expected.getLastIncludedTerm(), actual.getLastIncludedTerm());
67 assertEquals("getLastIncludedIndex", expected.getLastIncludedIndex(), actual.getLastIncludedIndex());
68 assertEquals("getLeaderId", expected.getLeaderId(), actual.getLeaderId());
69 assertEquals("getChunkIndex", expected.getChunkIndex(), actual.getChunkIndex());
70 assertArrayEquals("getData", expected.getData(), actual.getData());
72 assertEquals("getLastChunkHashCode present", expected.getLastChunkHashCode().isPresent(),
73 actual.getLastChunkHashCode().isPresent());
74 if (expected.getLastChunkHashCode().isPresent()) {
75 assertEquals("getLastChunkHashCode", expected.getLastChunkHashCode(),
76 actual.getLastChunkHashCode());
79 assertEquals("getServerConfig present", expected.getServerConfig().isPresent(),
80 actual.getServerConfig().isPresent());
81 if (expected.getServerConfig().isPresent()) {
82 assertEquals("getServerConfig", expected.getServerConfig().orElseThrow().getServerConfig(),
83 actual.getServerConfig().orElseThrow().getServerConfig());