Change InstallSnapshot and reply to use Externalizable Proxy
[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 import com.google.common.base.Optional;
13 import java.io.Serializable;
14 import java.util.Arrays;
15 import org.apache.commons.lang.SerializationUtils;
16 import org.junit.Test;
17 import org.opendaylight.controller.cluster.raft.RaftVersions;
18 import org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPayload;
19 import org.opendaylight.controller.cluster.raft.persisted.ServerInfo;
20
21 /**
22  * Unit tests for InstallSnapshot.
23  *
24  * @author Thomas Pantelis
25  */
26 public class InstallSnapshotTest {
27
28     @Test
29     public void testSerialization() {
30         byte[] data = new byte[1000];
31         int j = 0;
32         for(int i = 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(RaftVersions.CURRENT_VERSION));
52         verifyInstallSnapshot(expected, actual);
53     }
54
55     private static void verifyInstallSnapshot(InstallSnapshot expected, InstallSnapshot actual) {
56         assertEquals("getTerm", expected.getTerm(), actual.getTerm());
57         assertEquals("getChunkIndex", expected.getChunkIndex(), actual.getChunkIndex());
58         assertEquals("getTotalChunks", expected.getTotalChunks(), actual.getTotalChunks());
59         assertEquals("getLastIncludedTerm", expected.getLastIncludedTerm(), actual.getLastIncludedTerm());
60         assertEquals("getLastIncludedIndex", expected.getLastIncludedIndex(), actual.getLastIncludedIndex());
61         assertEquals("getLeaderId", expected.getLeaderId(), actual.getLeaderId());
62         assertEquals("getChunkIndex", expected.getChunkIndex(), actual.getChunkIndex());
63         assertArrayEquals("getData", expected.getData(), actual.getData());
64
65         assertEquals("getLastChunkHashCode present", expected.getLastChunkHashCode().isPresent(),
66                 actual.getLastChunkHashCode().isPresent());
67         if(expected.getLastChunkHashCode().isPresent()) {
68             assertEquals("getLastChunkHashCode", expected.getLastChunkHashCode().get(),
69                     actual.getLastChunkHashCode().get());
70         }
71
72         assertEquals("getServerConfig present", expected.getServerConfig().isPresent(),
73                 actual.getServerConfig().isPresent());
74         if(expected.getServerConfig().isPresent()) {
75             assertEquals("getServerConfig", expected.getServerConfig().get().getServerConfig(),
76                     actual.getServerConfig().get().getServerConfig());
77         }
78     }
79 }