Migrate sal-akka-raft tests
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / test / java / org / opendaylight / controller / cluster / raft / behaviors / SnapshotTrackerTest.java
1 /*
2  * Copyright (c) 2014, 2015 Cisco 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.behaviors;
9
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;
15
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;
22 import java.util.Map;
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.junit.runner.RunWith;
28 import org.mockito.Mock;
29 import org.mockito.junit.MockitoJUnitRunner;
30 import org.opendaylight.controller.cluster.io.FileBackedOutputStream;
31 import org.opendaylight.controller.cluster.io.FileBackedOutputStreamFactory;
32 import org.opendaylight.controller.cluster.raft.RaftActorContext;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 @RunWith(MockitoJUnitRunner.StrictStubs.class)
37 public class SnapshotTrackerTest {
38     private static final Logger LOG = LoggerFactory.getLogger(SnapshotTrackerTest.class);
39
40     @Mock
41     private RaftActorContext mockContext;
42     private FileBackedOutputStream fbos;
43     private Map<String, String> data;
44     private ByteString byteString;
45     private byte[] chunk1;
46     private byte[] chunk2;
47     private byte[] chunk3;
48
49     @Before
50     public void setup() {
51         data = new HashMap<>();
52         data.put("key1", "value1");
53         data.put("key2", "value2");
54         data.put("key3", "value3");
55
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());
60
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();
65     }
66
67     @Test
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)));
73
74             ByteSource snapshotBytes = tracker.getSnapshotBytes();
75             assertEquals("Deserialized", data, SerializationUtils.deserialize(snapshotBytes.read()));
76         }
77
78         verify(fbos).cleanup();
79     }
80
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());
87         }
88     }
89
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());
94         }
95     }
96
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());
102         }
103     }
104
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));
110         }
111     }
112
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();
118         }
119     }
120
121     private static byte[] getNextChunk(final ByteString bs, final int offset, int size) {
122         int snapshotLength = bs.size();
123         int start = offset;
124         if (size > snapshotLength) {
125             size = snapshotLength;
126         } else {
127             if (start + size > snapshotLength) {
128                 size = snapshotLength - start;
129             }
130         }
131
132         byte[] nextChunk = new byte[size];
133         bs.copyTo(nextChunk, start, 0, size);
134         return nextChunk;
135     }
136 }

©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.