b2d518e1ede8be15470701a12144d32a06d8d349
[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 org.apache.commons.lang.SerializationUtils;
15 import org.junit.Test;
16 import org.opendaylight.controller.cluster.raft.RaftVersions;
17 import org.opendaylight.controller.protobuff.messages.cluster.raft.InstallSnapshotMessages;
18
19 /**
20  * Unit tests for InstallSnapshot.
21  *
22  * @author Thomas Pantelis
23  */
24 public class InstallSnapshotTest {
25
26     @Test
27     public void testSerialization() {
28         byte[] data = new byte[1000];
29         int j = 0;
30         for(int i = 0; i < data.length; i++) {
31             data[i] = (byte)j;
32             if(++j >= 255) {
33                 j = 0;
34             }
35         }
36
37         InstallSnapshot expected = new InstallSnapshot(3L, "leaderId", 11L, 2L, data, 5, 6, Optional.<Integer>of(54321));
38
39         Object serialized = expected.toSerializable(RaftVersions.CURRENT_VERSION);
40         assertEquals("Serialized type", InstallSnapshot.class, serialized.getClass());
41
42         InstallSnapshot actual = InstallSnapshot.fromSerializable(SerializationUtils.clone((Serializable) serialized));
43         verifyInstallSnapshot(expected, actual);
44
45         expected = new InstallSnapshot(3L, "leaderId", 11L, 2L, data, 5, 6);
46         actual = InstallSnapshot.fromSerializable(SerializationUtils.clone(
47                 (Serializable) expected.toSerializable(RaftVersions.CURRENT_VERSION)));
48         verifyInstallSnapshot(expected, actual);
49     }
50
51     @Test
52     public void testSerializationWithPreBoronVersion() {
53         byte[] data = {0,1,2,3,4,5,7,8,9};
54         InstallSnapshot expected = new InstallSnapshot(3L, "leaderId", 11L, 2L, data, 5, 6, Optional.<Integer>of(54321));
55
56         Object serialized = expected.toSerializable(RaftVersions.LITHIUM_VERSION);
57         assertEquals("Serialized type", InstallSnapshot.SERIALIZABLE_CLASS, serialized.getClass());
58
59         InstallSnapshot actual = InstallSnapshot.fromSerializable(SerializationUtils.clone((Serializable) serialized));
60         verifyInstallSnapshot(expected, actual);
61     }
62
63     @Test
64     public void testIsSerializedType() {
65         assertEquals("isSerializedType", true, InstallSnapshot.isSerializedType(
66                 InstallSnapshotMessages.InstallSnapshot.newBuilder().build()));
67         assertEquals("isSerializedType", true, InstallSnapshot.isSerializedType(new InstallSnapshot()));
68         assertEquals("isSerializedType", false, InstallSnapshot.isSerializedType(new Object()));
69     }
70
71     private void verifyInstallSnapshot(InstallSnapshot expected, InstallSnapshot actual) {
72         assertEquals("getTerm", expected.getTerm(), actual.getTerm());
73         assertEquals("getChunkIndex", expected.getChunkIndex(), actual.getChunkIndex());
74         assertEquals("getTotalChunks", expected.getTotalChunks(), actual.getTotalChunks());
75         assertEquals("getLastIncludedTerm", expected.getLastIncludedTerm(), actual.getLastIncludedTerm());
76         assertEquals("getLastIncludedIndex", expected.getLastIncludedIndex(), actual.getLastIncludedIndex());
77         assertEquals("getLeaderId", expected.getLeaderId(), actual.getLeaderId());
78         assertEquals("getChunkIndex", expected.getChunkIndex(), actual.getChunkIndex());
79         assertArrayEquals("getData", expected.getData(), actual.getData());
80         assertEquals("getLastChunkHashCode present", expected.getLastChunkHashCode().isPresent(),
81                 actual.getLastChunkHashCode().isPresent());
82         if(expected.getLastChunkHashCode().isPresent()) {
83             assertEquals("getLastChunkHashCode", expected.getLastChunkHashCode().get(),
84                     actual.getLastChunkHashCode().get());
85         }
86     }
87 }