Define RaftVersions.ARGON_VERSION
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / test / java / org / opendaylight / controller / cluster / raft / messages / InstallSnapshotTest.java
1 /*
2  * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.raft.messages;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12
13 import java.util.List;
14 import java.util.Optional;
15 import java.util.OptionalInt;
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;
21
22 /**
23  * Unit tests for InstallSnapshot.
24  *
25  * @author Thomas Pantelis
26  */
27 public class InstallSnapshotTest {
28     @Test
29     public void testSerialization() {
30         byte[] data = new byte[1000];
31         for (int i = 0, j = 0; i < data.length; i++) {
32             data[i] = (byte)j;
33             if (++j >= 255) {
34                 j = 0;
35             }
36         }
37
38         ServerConfigurationPayload serverConfig = new ServerConfigurationPayload(List.of(
39                 new ServerInfo("leader", true), new ServerInfo("follower", false)));
40         assertInstallSnapshot(1302, new InstallSnapshot(3L, "leaderId", 11L, 2L, data, 5, 6, OptionalInt.of(54321),
41             Optional.of(serverConfig), RaftVersions.CURRENT_VERSION));
42
43         assertInstallSnapshot(1165, new InstallSnapshot(3L, "leaderId", 11L, 2L, data, 5, 6, OptionalInt.empty(),
44             Optional.empty(), RaftVersions.CURRENT_VERSION));
45     }
46
47     private static void assertInstallSnapshot(final int expectedSize, final InstallSnapshot expected) {
48         final var bytes = SerializationUtils.serialize(expected);
49         assertEquals(expectedSize, bytes.length);
50         verifyInstallSnapshot(expected, (InstallSnapshot) SerializationUtils.deserialize(bytes));
51     }
52
53     private static void verifyInstallSnapshot(final InstallSnapshot expected, final InstallSnapshot actual) {
54         assertEquals("getTerm", expected.getTerm(), actual.getTerm());
55         assertEquals("getChunkIndex", expected.getChunkIndex(), actual.getChunkIndex());
56         assertEquals("getTotalChunks", expected.getTotalChunks(), actual.getTotalChunks());
57         assertEquals("getLastIncludedTerm", expected.getLastIncludedTerm(), actual.getLastIncludedTerm());
58         assertEquals("getLastIncludedIndex", expected.getLastIncludedIndex(), actual.getLastIncludedIndex());
59         assertEquals("getLeaderId", expected.getLeaderId(), actual.getLeaderId());
60         assertEquals("getChunkIndex", expected.getChunkIndex(), actual.getChunkIndex());
61         assertArrayEquals("getData", expected.getData(), actual.getData());
62
63         assertEquals("getLastChunkHashCode present", expected.getLastChunkHashCode().isPresent(),
64                 actual.getLastChunkHashCode().isPresent());
65         if (expected.getLastChunkHashCode().isPresent()) {
66             assertEquals("getLastChunkHashCode", expected.getLastChunkHashCode(),
67                     actual.getLastChunkHashCode());
68         }
69
70         assertEquals("getServerConfig present", expected.getServerConfig().isPresent(),
71                 actual.getServerConfig().isPresent());
72         if (expected.getServerConfig().isPresent()) {
73             assertEquals("getServerConfig", expected.getServerConfig().get().getServerConfig(),
74                     actual.getServerConfig().get().getServerConfig());
75         }
76     }
77 }