Add getPeerIds to RaftActorContext
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / test / java / org / opendaylight / controller / cluster / raft / SnapshotManagerTest.java
index 0078c2bd848d6677efd7c0d241e1c083c07807e4..ef1a92641613c1df24b6316c2ff7bb30f68fdc65 100644 (file)
@@ -25,9 +25,7 @@ import akka.actor.ActorRef;
 import akka.japi.Procedure;
 import akka.persistence.SnapshotSelectionCriteria;
 import akka.testkit.TestActorRef;
-import com.google.common.collect.ImmutableMap;
 import java.util.Arrays;
-import java.util.HashMap;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -62,6 +60,9 @@ public class SnapshotManagerTest extends AbstractActorTest {
     @Mock
     private Procedure<Void> mockProcedure;
 
+    @Mock
+    private ElectionTerm mockElectionTerm;
+
     private SnapshotManager snapshotManager;
 
     private TestActorFactory factory;
@@ -72,7 +73,7 @@ public class SnapshotManagerTest extends AbstractActorTest {
     public void setUp(){
         MockitoAnnotations.initMocks(this);
 
-        doReturn(new HashMap<>()).when(mockRaftActorContext).getPeerAddresses();
+        doReturn(false).when(mockRaftActorContext).hasFollowers();
         doReturn(mockConfigParams).when(mockRaftActorContext).getConfigParams();
         doReturn(10L).when(mockConfigParams).getSnapshotBatchCount();
         doReturn(70).when(mockConfigParams).getSnapshotDataThresholdPercentage();
@@ -81,9 +82,9 @@ public class SnapshotManagerTest extends AbstractActorTest {
         doReturn(mockDataPersistenceProvider).when(mockRaftActorContext).getPersistenceProvider();
         doReturn("123").when(mockRaftActorBehavior).getLeaderId();
 
-        ElectionTerm mockElectionTerm = mock(ElectionTerm.class);
         doReturn(mockElectionTerm).when(mockRaftActorContext).getTermInformation();
         doReturn(5L).when(mockElectionTerm).getCurrentTerm();
+        doReturn("member5").when(mockElectionTerm).getVotedFor();
 
         snapshotManager = new SnapshotManager(mockRaftActorContext, LoggerFactory.getLogger(this.getClass()));
         factory = new TestActorFactory(getSystem());
@@ -160,6 +161,35 @@ public class SnapshotManagerTest extends AbstractActorTest {
 
     }
 
+    @Test
+    public void testCaptureWithNullLastLogEntry() throws Exception {
+        boolean capture = snapshotManager.capture(null, 1);
+
+        assertTrue(capture);
+
+        assertEquals(true, snapshotManager.isCapturing());
+
+        verify(mockProcedure).apply(null);
+
+        CaptureSnapshot captureSnapshot = snapshotManager.getCaptureSnapshot();
+
+        System.out.println(captureSnapshot);
+
+        // LastIndex and LastTerm are picked up from the lastLogEntry
+        assertEquals(-1L, captureSnapshot.getLastIndex());
+        assertEquals(-1L, captureSnapshot.getLastTerm());
+
+        // Since the actor does not have any followers (no peer addresses) lastApplied will be from lastLogEntry
+        assertEquals(-1L, captureSnapshot.getLastAppliedIndex());
+        assertEquals(-1L, captureSnapshot.getLastAppliedTerm());
+
+        //
+        assertEquals(-1L, captureSnapshot.getReplicatedToAllIndex());
+        assertEquals(-1L, captureSnapshot.getReplicatedToAllTerm());
+        actorRef.underlyingActor().clear();
+
+    }
+
     @Test
     public void testCaptureWithCreateProcedureError () throws Exception {
         doThrow(new Exception("mock")).when(mockProcedure).apply(null);
@@ -199,7 +229,7 @@ public class SnapshotManagerTest extends AbstractActorTest {
         doReturn(7L).when(mockReplicatedLog).getSnapshotIndex();
         doReturn(1L).when(mockReplicatedLog).getSnapshotTerm();
 
-        doReturn(ImmutableMap.builder().put("follower-1", "").build()).when(mockRaftActorContext).getPeerAddresses();
+        doReturn(true).when(mockRaftActorContext).hasFollowers();
 
         doReturn(8L).when(mockRaftActorContext).getLastApplied();
 
@@ -229,6 +259,8 @@ public class SnapshotManagerTest extends AbstractActorTest {
         assertEquals("getLastAppliedIndex", 8L, snapshot.getLastAppliedIndex());
         assertArrayEquals("getState", bytes, snapshot.getState());
         assertEquals("getUnAppliedEntries", Arrays.asList(lastLogEntry), snapshot.getUnAppliedEntries());
+        assertEquals("electionTerm", mockElectionTerm.getCurrentTerm(), snapshot.getElectionTerm());
+        assertEquals("electionVotedFor", mockElectionTerm.getVotedFor(), snapshot.getElectionVotedFor());
 
         verify(mockReplicatedLog).snapshotPreCommit(7L, 1L);
     }