Migrate InstallSnapshot/SnapshotTracker use of Optional
[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.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;
22
23 /**
24  * Unit tests for InstallSnapshot.
25  *
26  * @author Thomas Pantelis
27  */
28 public class InstallSnapshotTest {
29
30     @Test
31     public void testSerialization() {
32         byte[] data = new byte[1000];
33         for (int i = 0, j = 0; i < data.length; i++) {
34             data[i] = (byte)j;
35             if (++j >= 255) {
36                 j = 0;
37             }
38         }
39
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));
44
45         Object serialized = expected.toSerializable(RaftVersions.CURRENT_VERSION);
46         assertEquals("Serialized type", InstallSnapshot.class, serialized.getClass());
47
48         InstallSnapshot actual = (InstallSnapshot) SerializationUtils.clone((Serializable) serialized);
49         verifyInstallSnapshot(expected, actual);
50
51         expected = new InstallSnapshot(3L, "leaderId", 11L, 2L, data, 5, 6);
52         actual = (InstallSnapshot) SerializationUtils.clone((Serializable) expected.toSerializable(
53                 RaftVersions.CURRENT_VERSION));
54         verifyInstallSnapshot(expected, actual);
55     }
56
57     private static void verifyInstallSnapshot(final InstallSnapshot expected, final InstallSnapshot actual) {
58         assertEquals("getTerm", expected.getTerm(), actual.getTerm());
59         assertEquals("getChunkIndex", expected.getChunkIndex(), actual.getChunkIndex());
60         assertEquals("getTotalChunks", expected.getTotalChunks(), actual.getTotalChunks());
61         assertEquals("getLastIncludedTerm", expected.getLastIncludedTerm(), actual.getLastIncludedTerm());
62         assertEquals("getLastIncludedIndex", expected.getLastIncludedIndex(), actual.getLastIncludedIndex());
63         assertEquals("getLeaderId", expected.getLeaderId(), actual.getLeaderId());
64         assertEquals("getChunkIndex", expected.getChunkIndex(), actual.getChunkIndex());
65         assertArrayEquals("getData", expected.getData(), actual.getData());
66
67         assertEquals("getLastChunkHashCode present", expected.getLastChunkHashCode().isPresent(),
68                 actual.getLastChunkHashCode().isPresent());
69         if (expected.getLastChunkHashCode().isPresent()) {
70             assertEquals("getLastChunkHashCode", expected.getLastChunkHashCode(),
71                     actual.getLastChunkHashCode());
72         }
73
74         assertEquals("getServerConfig present", expected.getServerConfig().isPresent(),
75                 actual.getServerConfig().isPresent());
76         if (expected.getServerConfig().isPresent()) {
77             assertEquals("getServerConfig", expected.getServerConfig().get().getServerConfig(),
78                     actual.getServerConfig().get().getServerConfig());
79         }
80     }
81 }