Use a simple list for string code table
[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 com.google.common.base.Optional;
14 import java.io.Serializable;
15 import java.util.Arrays;
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
29     @Test
30     public void testSerialization() {
31         byte[] data = new byte[1000];
32         for (int i = 0, j = 0; i < data.length; i++) {
33             data[i] = (byte)j;
34             if (++j >= 255) {
35                 j = 0;
36             }
37         }
38
39         ServerConfigurationPayload serverConfig = new ServerConfigurationPayload(Arrays.asList(
40                 new ServerInfo("leader", true), new ServerInfo("follower", false)));
41         InstallSnapshot expected = new InstallSnapshot(3L, "leaderId", 11L, 2L, data, 5, 6,
42                 Optional.<Integer>of(54321), Optional.of(serverConfig));
43
44         Object serialized = expected.toSerializable(RaftVersions.CURRENT_VERSION);
45         assertEquals("Serialized type", InstallSnapshot.class, serialized.getClass());
46
47         InstallSnapshot actual = (InstallSnapshot) SerializationUtils.clone((Serializable) serialized);
48         verifyInstallSnapshot(expected, actual);
49
50         expected = new InstallSnapshot(3L, "leaderId", 11L, 2L, data, 5, 6);
51         actual = (InstallSnapshot) SerializationUtils.clone((Serializable) expected.toSerializable(
52                 RaftVersions.CURRENT_VERSION));
53         verifyInstallSnapshot(expected, actual);
54     }
55
56     private static void verifyInstallSnapshot(InstallSnapshot expected, InstallSnapshot actual) {
57         assertEquals("getTerm", expected.getTerm(), actual.getTerm());
58         assertEquals("getChunkIndex", expected.getChunkIndex(), actual.getChunkIndex());
59         assertEquals("getTotalChunks", expected.getTotalChunks(), actual.getTotalChunks());
60         assertEquals("getLastIncludedTerm", expected.getLastIncludedTerm(), actual.getLastIncludedTerm());
61         assertEquals("getLastIncludedIndex", expected.getLastIncludedIndex(), actual.getLastIncludedIndex());
62         assertEquals("getLeaderId", expected.getLeaderId(), actual.getLeaderId());
63         assertEquals("getChunkIndex", expected.getChunkIndex(), actual.getChunkIndex());
64         assertArrayEquals("getData", expected.getData(), actual.getData());
65
66         assertEquals("getLastChunkHashCode present", expected.getLastChunkHashCode().isPresent(),
67                 actual.getLastChunkHashCode().isPresent());
68         if (expected.getLastChunkHashCode().isPresent()) {
69             assertEquals("getLastChunkHashCode", expected.getLastChunkHashCode().get(),
70                     actual.getLastChunkHashCode().get());
71         }
72
73         assertEquals("getServerConfig present", expected.getServerConfig().isPresent(),
74                 actual.getServerConfig().isPresent());
75         if (expected.getServerConfig().isPresent()) {
76             assertEquals("getServerConfig", expected.getServerConfig().get().getServerConfig(),
77                     actual.getServerConfig().get().getServerConfig());
78         }
79     }
80 }