2 * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.raft.behaviors;
10 import static org.junit.Assert.assertEquals;
11 import static org.mockito.Mockito.doReturn;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.spy;
14 import static org.mockito.Mockito.verify;
16 import akka.protobuf.ByteString;
17 import com.google.common.io.ByteSource;
18 import java.io.IOException;
19 import java.io.Serializable;
20 import java.util.Arrays;
21 import java.util.HashMap;
23 import java.util.OptionalInt;
24 import org.apache.commons.lang3.SerializationUtils;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.Mock;
28 import org.mockito.MockitoAnnotations;
29 import org.opendaylight.controller.cluster.io.FileBackedOutputStream;
30 import org.opendaylight.controller.cluster.io.FileBackedOutputStreamFactory;
31 import org.opendaylight.controller.cluster.raft.RaftActorContext;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 public class SnapshotTrackerTest {
36 private static final Logger LOG = LoggerFactory.getLogger(SnapshotTrackerTest.class);
39 private RaftActorContext mockContext;
40 private FileBackedOutputStream fbos;
41 private Map<String, String> data;
42 private ByteString byteString;
43 private byte[] chunk1;
44 private byte[] chunk2;
45 private byte[] chunk3;
49 MockitoAnnotations.initMocks(this);
51 data = new HashMap<>();
52 data.put("key1", "value1");
53 data.put("key2", "value2");
54 data.put("key3", "value3");
56 byteString = ByteString.copyFrom(SerializationUtils.serialize((Serializable) data));
57 chunk1 = getNextChunk(byteString, 0, 10);
58 chunk2 = getNextChunk(byteString, 10, 10);
59 chunk3 = getNextChunk(byteString, 20, byteString.size());
61 fbos = spy(new FileBackedOutputStream(100000000, "target"));
62 FileBackedOutputStreamFactory mockFactory = mock(FileBackedOutputStreamFactory.class);
63 doReturn(fbos).when(mockFactory).newInstance();
64 doReturn(mockFactory).when(mockContext).getFileBackedOutputStreamFactory();
68 public void testAddChunks() throws IOException {
69 try (SnapshotTracker tracker = new SnapshotTracker(LOG, 3, "leader", mockContext)) {
70 tracker.addChunk(1, chunk1, OptionalInt.of(LeaderInstallSnapshotState.INITIAL_LAST_CHUNK_HASH_CODE));
71 tracker.addChunk(2, chunk2, OptionalInt.of(Arrays.hashCode(chunk1)));
72 tracker.addChunk(3, chunk3, OptionalInt.of(Arrays.hashCode(chunk2)));
74 ByteSource snapshotBytes = tracker.getSnapshotBytes();
75 assertEquals("Deserialized", data, SerializationUtils.deserialize(snapshotBytes.read()));
78 verify(fbos).cleanup();
81 @Test(expected = SnapshotTracker.InvalidChunkException.class)
82 public void testAddChunkWhenAlreadySealed() throws IOException {
83 try (SnapshotTracker tracker = new SnapshotTracker(LOG, 2, "leader", mockContext)) {
84 tracker.addChunk(1, chunk1, OptionalInt.empty());
85 tracker.addChunk(2, chunk2, OptionalInt.empty());
86 tracker.addChunk(3, chunk3, OptionalInt.empty());
90 @Test(expected = SnapshotTracker.InvalidChunkException.class)
91 public void testInvalidFirstChunkIndex() throws IOException {
92 try (SnapshotTracker tracker = new SnapshotTracker(LOG, 2, "leader", mockContext)) {
93 tracker.addChunk(LeaderInstallSnapshotState.FIRST_CHUNK_INDEX - 1, chunk1, OptionalInt.empty());
97 @Test(expected = SnapshotTracker.InvalidChunkException.class)
98 public void testOutOfSequenceChunk() throws IOException {
99 try (SnapshotTracker tracker = new SnapshotTracker(LOG, 2, "leader", mockContext)) {
100 tracker.addChunk(1, chunk1, OptionalInt.empty());
101 tracker.addChunk(3, chunk3, OptionalInt.empty());
105 @Test(expected = SnapshotTracker.InvalidChunkException.class)
106 public void testInvalidLastChunkHashCode() throws IOException {
107 try (SnapshotTracker tracker = new SnapshotTracker(LOG, 2, "leader", mockContext)) {
108 tracker.addChunk(1, chunk1, OptionalInt.of(LeaderInstallSnapshotState.INITIAL_LAST_CHUNK_HASH_CODE));
109 tracker.addChunk(2, chunk2, OptionalInt.of(1));
113 @Test(expected = IllegalStateException.class)
114 public void testGetSnapshotBytesWhenNotSealed() throws IOException {
115 try (SnapshotTracker tracker = new SnapshotTracker(LOG, 2, "leader", mockContext)) {
116 tracker.addChunk(1, chunk1, OptionalInt.empty());
117 tracker.getSnapshotBytes();
121 private static byte[] getNextChunk(final ByteString bs, final int offset, int size) {
122 int snapshotLength = bs.size();
124 if (size > snapshotLength) {
125 size = snapshotLength;
127 if (start + size > snapshotLength) {
128 size = snapshotLength - start;
132 byte[] nextChunk = new byte[size];
133 bs.copyTo(nextChunk, start, 0, size);