Use partial mocking for FollowerTest 68/36568/1
authorRobert Varga <rovarga@cisco.com>
Tue, 22 Mar 2016 17:57:58 +0000 (18:57 +0100)
committerRobert Varga <rovarga@cisco.com>
Tue, 22 Mar 2016 17:57:58 +0000 (18:57 +0100)
This patch removes the need for a subclass and some explicit casts.

Change-Id: I19a30266f8690ffcc68f70e949d88101db6552ae
Signed-off-by: Robert Varga <rovarga@cisco.com>
opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/behaviors/FollowerTest.java

index 86caf2a..d5d9e88 100644 (file)
@@ -13,9 +13,11 @@ import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
+import static org.mockito.Matchers.any;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.verify;
 import akka.actor.ActorRef;
 import akka.actor.Props;
@@ -56,7 +58,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest {
     private final TestActorRef<MessageCollectorActor> leaderActor = actorFactory.createTestActor(
             Props.create(MessageCollectorActor.class), actorFactory.generateActorId("leader"));
 
-    private RaftActorBehavior follower;
+    private Follower follower;
 
     private final short payloadVersion = 5;
 
@@ -71,8 +73,8 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest {
     }
 
     @Override
-    protected RaftActorBehavior createBehavior(RaftActorContext actorContext) {
-        return new TestFollower(actorContext);
+    protected Follower createBehavior(RaftActorContext actorContext) {
+        return spy(new Follower(actorContext));
     }
 
     @Override
@@ -87,13 +89,6 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest {
         return context;
     }
 
-    private static int getElectionTimeoutCount(RaftActorBehavior follower){
-        if(follower instanceof TestFollower){
-            return ((TestFollower) follower).getElectionTimeoutCount();
-        }
-        return -1;
-    }
-
     @Test
     public void testThatAnElectionTimeoutIsTriggered(){
         MockRaftActorContext actorContext = createActorContext();
@@ -130,7 +125,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest {
 
         assertEquals("isVoteGranted", true, reply.isVoteGranted());
         assertEquals("getTerm", term, reply.getTerm());
-        assertEquals("schedule election", 1, getElectionTimeoutCount(follower));
+        verify(follower).scheduleElection(any(FiniteDuration.class));
     }
 
     @Test
@@ -148,7 +143,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest {
         RequestVoteReply reply = MessageCollectorActor.expectFirstMatching(leaderActor, RequestVoteReply.class);
 
         assertEquals("isVoteGranted", false, reply.isVoteGranted());
-        assertEquals("schedule election", 0, getElectionTimeoutCount(follower));
+        verify(follower, never()).scheduleElection(any(FiniteDuration.class));
     }
 
 
@@ -799,7 +794,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest {
             assertEquals("getFollowerId", context.getId(), reply.getFollowerId());
         }
 
-        assertNull("Expected null SnapshotTracker", ((Follower) follower).getSnapshotTracker());
+        assertNull("Expected null SnapshotTracker", follower.getSnapshotTracker());
     }
 
 
@@ -824,7 +819,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest {
         int lastIncludedIndex = 1;
 
         // Check that snapshot installation is not in progress
-        assertNull(((Follower) follower).getSnapshotTracker());
+        assertNull(follower.getSnapshotTracker());
 
         // Make sure that we have more than 1 chunk to send
         assertTrue(totalChunks > 1);
@@ -835,7 +830,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest {
                 chunkData, 1, totalChunks));
 
         // Check if snapshot installation is in progress now
-        assertNotNull(((Follower) follower).getSnapshotTracker());
+        assertNotNull(follower.getSnapshotTracker());
 
         // Send an append entry
         AppendEntries appendEntries = mock(AppendEntries.class);
@@ -927,7 +922,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest {
         assertEquals("getTerm", 1, reply.getTerm());
         assertEquals("getFollowerId", context.getId(), reply.getFollowerId());
 
-        assertNull("Expected null SnapshotTracker", ((Follower) follower).getSnapshotTracker());
+        assertNull("Expected null SnapshotTracker", follower.getSnapshotTracker());
     }
 
     @Test
@@ -974,7 +969,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest {
                 return 100;
             }
         });
-        assertEquals("schedule election", 1, getElectionTimeoutCount(follower));
+        verify(follower).scheduleElection(any(FiniteDuration.class));
     }
 
     @Test
@@ -982,7 +977,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest {
         MockRaftActorContext context = createActorContext();
         follower = createBehavior(context);
         follower.handleMessage(leaderActor, "non-raft-rpc");
-        assertEquals("schedule election", 0, getElectionTimeoutCount(follower));
+        verify(follower, never()).scheduleElection(any(FiniteDuration.class));
     }
 
     public byte[] getNextChunk (ByteString bs, int offset, int chunkSize){
@@ -1053,23 +1048,4 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest {
         AppendEntriesReply reply = MessageCollectorActor.expectFirstMatching(replyActor, AppendEntriesReply.class);
         assertEquals("isSuccess", true, reply.isSuccess());
     }
-
-    private static class TestFollower extends Follower {
-
-        int electionTimeoutCount = 0;
-
-        public TestFollower(RaftActorContext context) {
-            super(context);
-        }
-
-        @Override
-        protected void scheduleElection(FiniteDuration interval) {
-            electionTimeoutCount++;
-            super.scheduleElection(interval);
-        }
-
-        public int getElectionTimeoutCount() {
-            return electionTimeoutCount;
-        }
-    }
 }