7b123741b6e6b52700db1cb68d5f508c603afd73
[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.ServerConfigurationPayload;
19 import org.opendaylight.controller.cluster.raft.ServerConfigurationPayload.ServerInfo;
20 import org.opendaylight.controller.protobuff.messages.cluster.raft.InstallSnapshotMessages;
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         int j = 0;
33         for(int i = 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,
43                 Optional.<Integer>of(54321), 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.fromSerializable(SerializationUtils.clone((Serializable) serialized));
49         verifyInstallSnapshot(expected, actual);
50
51         expected = new InstallSnapshot(3L, "leaderId", 11L, 2L, data, 5, 6);
52         actual = InstallSnapshot.fromSerializable(SerializationUtils.clone(
53                 (Serializable) expected.toSerializable(RaftVersions.CURRENT_VERSION)));
54         verifyInstallSnapshot(expected, actual);
55     }
56
57     @Test
58     public void testSerializationWithPreBoronVersion() {
59         byte[] data = {0,1,2,3,4,5,7,8,9};
60         InstallSnapshot expected = new InstallSnapshot(3L, "leaderId", 11L, 2L, data, 5, 6, Optional.<Integer>of(54321),
61                 Optional.<ServerConfigurationPayload>absent());
62
63         Object serialized = expected.toSerializable(RaftVersions.LITHIUM_VERSION);
64         assertEquals("Serialized type", InstallSnapshot.SERIALIZABLE_CLASS, serialized.getClass());
65
66         InstallSnapshot actual = InstallSnapshot.fromSerializable(SerializationUtils.clone((Serializable) serialized));
67         verifyInstallSnapshot(expected, actual);
68     }
69
70     @Test
71     public void testIsSerializedType() {
72         assertEquals("isSerializedType", true, InstallSnapshot.isSerializedType(
73                 InstallSnapshotMessages.InstallSnapshot.newBuilder().build()));
74         assertEquals("isSerializedType", true, InstallSnapshot.isSerializedType(new InstallSnapshot()));
75         assertEquals("isSerializedType", false, InstallSnapshot.isSerializedType(new Object()));
76     }
77
78     private static void verifyInstallSnapshot(InstallSnapshot expected, InstallSnapshot actual) {
79         assertEquals("getTerm", expected.getTerm(), actual.getTerm());
80         assertEquals("getChunkIndex", expected.getChunkIndex(), actual.getChunkIndex());
81         assertEquals("getTotalChunks", expected.getTotalChunks(), actual.getTotalChunks());
82         assertEquals("getLastIncludedTerm", expected.getLastIncludedTerm(), actual.getLastIncludedTerm());
83         assertEquals("getLastIncludedIndex", expected.getLastIncludedIndex(), actual.getLastIncludedIndex());
84         assertEquals("getLeaderId", expected.getLeaderId(), actual.getLeaderId());
85         assertEquals("getChunkIndex", expected.getChunkIndex(), actual.getChunkIndex());
86         assertArrayEquals("getData", expected.getData(), actual.getData());
87
88         assertEquals("getLastChunkHashCode present", expected.getLastChunkHashCode().isPresent(),
89                 actual.getLastChunkHashCode().isPresent());
90         if(expected.getLastChunkHashCode().isPresent()) {
91             assertEquals("getLastChunkHashCode", expected.getLastChunkHashCode().get(),
92                     actual.getLastChunkHashCode().get());
93         }
94
95         assertEquals("getServerConfig present", expected.getServerConfig().isPresent(),
96                 actual.getServerConfig().isPresent());
97         if(expected.getServerConfig().isPresent()) {
98             assertEquals("getServerConfig", expected.getServerConfig().get().getServerConfig(),
99                     actual.getServerConfig().get().getServerConfig());
100         }
101     }
102 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.