Make Netty-3 dependency optional
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / test / java / org / opendaylight / controller / cluster / raft / behaviors / SnapshotTrackerTest.java
index 443b931d2599ffcc818eef0a0e72c8a9c4696b46..2c83f67582f4b97c452fa0f1e0b6c3554c85faef 100644 (file)
@@ -5,7 +5,6 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.controller.cluster.raft.behaviors;
 
 import static org.junit.Assert.assertEquals;
@@ -15,31 +14,32 @@ import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.verify;
 
 import akka.protobuf.ByteString;
-import com.google.common.base.Optional;
 import com.google.common.io.ByteSource;
 import java.io.IOException;
-import java.io.Serializable;
 import java.util.Arrays;
 import java.util.HashMap;
-import java.util.Map;
+import java.util.OptionalInt;
 import org.apache.commons.lang3.SerializationUtils;
 import org.junit.Before;
 import org.junit.Test;
+import org.junit.runner.RunWith;
 import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
+import org.mockito.junit.MockitoJUnitRunner;
 import org.opendaylight.controller.cluster.io.FileBackedOutputStream;
 import org.opendaylight.controller.cluster.io.FileBackedOutputStreamFactory;
 import org.opendaylight.controller.cluster.raft.RaftActorContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+@RunWith(MockitoJUnitRunner.StrictStubs.class)
 public class SnapshotTrackerTest {
     private static final Logger LOG = LoggerFactory.getLogger(SnapshotTrackerTest.class);
 
+    private final HashMap<String, String> data = new HashMap<>();
+
     @Mock
     private RaftActorContext mockContext;
     private FileBackedOutputStream fbos;
-    private Map<String, String> data;
     private ByteString byteString;
     private byte[] chunk1;
     private byte[] chunk2;
@@ -47,14 +47,11 @@ public class SnapshotTrackerTest {
 
     @Before
     public void setup() {
-        MockitoAnnotations.initMocks(this);
-
-        data = new HashMap<>();
         data.put("key1", "value1");
         data.put("key2", "value2");
         data.put("key3", "value3");
 
-        byteString = ByteString.copyFrom(SerializationUtils.serialize((Serializable) data));
+        byteString = ByteString.copyFrom(SerializationUtils.serialize(data));
         chunk1 = getNextChunk(byteString, 0, 10);
         chunk2 = getNextChunk(byteString, 10, 10);
         chunk3 = getNextChunk(byteString, 20, byteString.size());
@@ -68,9 +65,9 @@ public class SnapshotTrackerTest {
     @Test
     public void testAddChunks() throws IOException {
         try (SnapshotTracker tracker = new SnapshotTracker(LOG, 3, "leader", mockContext)) {
-            tracker.addChunk(1, chunk1, Optional.of(LeaderInstallSnapshotState.INITIAL_LAST_CHUNK_HASH_CODE));
-            tracker.addChunk(2, chunk2, Optional.of(Arrays.hashCode(chunk1)));
-            tracker.addChunk(3, chunk3, Optional.of(Arrays.hashCode(chunk2)));
+            tracker.addChunk(1, chunk1, OptionalInt.of(LeaderInstallSnapshotState.INITIAL_LAST_CHUNK_HASH_CODE));
+            tracker.addChunk(2, chunk2, OptionalInt.of(Arrays.hashCode(chunk1)));
+            tracker.addChunk(3, chunk3, OptionalInt.of(Arrays.hashCode(chunk2)));
 
             ByteSource snapshotBytes = tracker.getSnapshotBytes();
             assertEquals("Deserialized", data, SerializationUtils.deserialize(snapshotBytes.read()));
@@ -82,39 +79,39 @@ public class SnapshotTrackerTest {
     @Test(expected = SnapshotTracker.InvalidChunkException.class)
     public void testAddChunkWhenAlreadySealed() throws IOException {
         try (SnapshotTracker tracker = new SnapshotTracker(LOG, 2, "leader", mockContext)) {
-            tracker.addChunk(1, chunk1, Optional.<Integer>absent());
-            tracker.addChunk(2, chunk2, Optional.<Integer>absent());
-            tracker.addChunk(3, chunk3, Optional.<Integer>absent());
+            tracker.addChunk(1, chunk1, OptionalInt.empty());
+            tracker.addChunk(2, chunk2, OptionalInt.empty());
+            tracker.addChunk(3, chunk3, OptionalInt.empty());
         }
     }
 
     @Test(expected = SnapshotTracker.InvalidChunkException.class)
     public void testInvalidFirstChunkIndex() throws IOException {
         try (SnapshotTracker tracker = new SnapshotTracker(LOG, 2, "leader", mockContext)) {
-            tracker.addChunk(LeaderInstallSnapshotState.FIRST_CHUNK_INDEX - 1, chunk1, Optional.<Integer>absent());
+            tracker.addChunk(LeaderInstallSnapshotState.FIRST_CHUNK_INDEX - 1, chunk1, OptionalInt.empty());
         }
     }
 
     @Test(expected = SnapshotTracker.InvalidChunkException.class)
     public void testOutOfSequenceChunk() throws IOException {
         try (SnapshotTracker tracker = new SnapshotTracker(LOG, 2, "leader", mockContext)) {
-            tracker.addChunk(1, chunk1, Optional.<Integer>absent());
-            tracker.addChunk(3, chunk3, Optional.<Integer>absent());
+            tracker.addChunk(1, chunk1, OptionalInt.empty());
+            tracker.addChunk(3, chunk3, OptionalInt.empty());
         }
     }
 
     @Test(expected = SnapshotTracker.InvalidChunkException.class)
     public void testInvalidLastChunkHashCode() throws IOException {
         try (SnapshotTracker tracker = new SnapshotTracker(LOG, 2, "leader", mockContext)) {
-            tracker.addChunk(1, chunk1, Optional.of(LeaderInstallSnapshotState.INITIAL_LAST_CHUNK_HASH_CODE));
-            tracker.addChunk(2, chunk2, Optional.of(1));
+            tracker.addChunk(1, chunk1, OptionalInt.of(LeaderInstallSnapshotState.INITIAL_LAST_CHUNK_HASH_CODE));
+            tracker.addChunk(2, chunk2, OptionalInt.of(1));
         }
     }
 
     @Test(expected = IllegalStateException.class)
     public void testGetSnapshotBytesWhenNotSealed() throws IOException {
         try (SnapshotTracker tracker = new SnapshotTracker(LOG, 2, "leader", mockContext)) {
-            tracker.addChunk(1, chunk1, Optional.<Integer>absent());
+            tracker.addChunk(1, chunk1, OptionalInt.empty());
             tracker.getSnapshotBytes();
         }
     }
@@ -124,10 +121,8 @@ public class SnapshotTrackerTest {
         int start = offset;
         if (size > snapshotLength) {
             size = snapshotLength;
-        } else {
-            if (start + size > snapshotLength) {
-                size = snapshotLength - start;
-            }
+        } else if (start + size > snapshotLength) {
+            size = snapshotLength - start;
         }
 
         byte[] nextChunk = new byte[size];