BUG 4017: Notification publish service is not available from provider context
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / test / java / org / opendaylight / controller / cluster / raft / RaftActorRecoverySupportTest.java
index ddc8bed42a452767b6229d8996c84e06ac56e1b1..2c130314718d9ab092f5dde8546164c61ddd71b4 100644 (file)
@@ -8,8 +8,11 @@
 package org.opendaylight.controller.cluster.raft;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
 import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.anyInt;
+import static org.mockito.Mockito.doNothing;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.verify;
@@ -75,13 +78,20 @@ public class RaftActorRecoverySupportTest {
     public void setup() {
         MockitoAnnotations.initMocks(this);
 
+        doNothing().when(mockCohort).startLogRecoveryBatch(any(int.class));
+        doNothing().when(mockCohort).applyCurrentLogRecoveryBatch();
+        doNothing().when(mockCohort).applyRecoverySnapshot(any(byte[].class));
+        doNothing().when(mockCohort).appendRecoveredLogEntry(any(Payload.class));
+        doReturn(null).when(mockCohort).getRestoreFromSnapshot();
+        doReturn(true).when(mockPersistence).isRecoveryApplicable();
+        doNothing().when(mockPersistence).deleteMessages(any(long.class));
+        doNothing().when(mockPersistentProvider).deleteMessages(any(long.class));
+        doNothing().when(mockPersistentProvider).deleteSnapshots(any(SnapshotSelectionCriteria.class));
+        doNothing().when(mockPersistentProvider).persist(any(Object.class), any(Procedure.class));
+
         context = new RaftActorContextImpl(null, null, localId, new ElectionTermImpl(mockPersistentProvider, "test", LOG),
                 -1, -1, Collections.<String,String>emptyMap(), configParams, mockPersistence, LOG);
-
         support = new RaftActorRecoverySupport(context, mockBehavior , mockCohort);
-
-        doReturn(true).when(mockPersistence).isRecoveryApplicable();
-
         context.setReplicatedLog(ReplicatedLogImpl.newInstance(context, mockBehavior));
     }
 
@@ -213,6 +223,7 @@ public class RaftActorRecoverySupportTest {
         assertEquals("Snapshot index", lastAppliedDuringSnapshotCapture, context.getReplicatedLog().getSnapshotIndex());
         assertEquals("Election term", electionTerm, context.getTermInformation().getCurrentTerm());
         assertEquals("Election votedFor", electionVotedFor, context.getTermInformation().getVotedFor());
+        assertFalse("Dynamic server configuration", context.isDynamicServerConfigurationInUse());
 
         verify(mockCohort).applyRecoverySnapshot(snapshotBytes);
     }
@@ -395,6 +406,7 @@ public class RaftActorRecoverySupportTest {
         sendMessageToSupport(new MockRaftActorContext.MockReplicatedLogEntry(1, 0, obj));
 
         //verify new peers
+        assertTrue("Dynamic server configuration", context.isDynamicServerConfigurationInUse());
         assertEquals("New peer Ids", Sets.newHashSet(follower1, follower2, follower3),
                 Sets.newHashSet(context.getPeerIds()));
         assertEquals("follower1 isVoting", true, context.getPeerInfo(follower1).isVoting());
@@ -415,6 +427,7 @@ public class RaftActorRecoverySupportTest {
         sendMessageToSupport(new MockRaftActorContext.MockReplicatedLogEntry(1, 1, obj));
 
         //verify new peers
+        assertTrue("Dynamic server configuration", context.isDynamicServerConfigurationInUse());
         assertEquals("New peer Ids", Sets.newHashSet(follower2, follower3), Sets.newHashSet(context.getPeerIds()));
     }
 
@@ -431,4 +444,29 @@ public class RaftActorRecoverySupportTest {
         //verify new peers
         assertEquals("New peer Ids", Sets.newHashSet(follower), Sets.newHashSet(context.getPeerIds()));
     }
+
+    @Test
+    public void testOnSnapshotOfferWithServerConfiguration() {
+        long electionTerm = 2;
+        String electionVotedFor = "member-2";
+        ServerConfigurationPayload serverPayload = new ServerConfigurationPayload(Arrays.asList(
+                                                        new ServerInfo(localId, true),
+                                                        new ServerInfo("follower1", true),
+                                                        new ServerInfo("follower2", true)));
+
+        Snapshot snapshot = Snapshot.create(new byte[]{1}, Collections.<ReplicatedLogEntry>emptyList(),
+                -1, -1, -1, -1, electionTerm, electionVotedFor, serverPayload);
+
+        SnapshotMetadata metadata = new SnapshotMetadata("test", 6, 12345);
+        SnapshotOffer snapshotOffer = new SnapshotOffer(metadata , snapshot);
+
+        sendMessageToSupport(snapshotOffer);
+
+        assertEquals("Journal log size", 0, context.getReplicatedLog().size());
+        assertEquals("Election term", electionTerm, context.getTermInformation().getCurrentTerm());
+        assertEquals("Election votedFor", electionVotedFor, context.getTermInformation().getVotedFor());
+        assertTrue("Dynamic server configuration", context.isDynamicServerConfigurationInUse());
+        assertEquals("Peer List", Sets.newHashSet("follower1", "follower2"),
+            Sets.newHashSet(context.getPeerIds()));
+    }
 }