Fix warnings in sal-akka-raft test classes
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / test / java / org / opendaylight / controller / cluster / raft / RecoveryIntegrationSingleNodeTest.java
1 /*
2  * Copyright (c) 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;
9
10 import static org.junit.Assert.assertEquals;
11
12 import akka.actor.ActorRef;
13 import akka.persistence.SaveSnapshotSuccess;
14 import akka.testkit.TestActorRef;
15 import com.google.common.collect.ImmutableMap;
16 import com.google.common.collect.Lists;
17 import java.util.List;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.opendaylight.controller.cluster.raft.persisted.ApplyJournalEntries;
21 import org.opendaylight.controller.cluster.raft.utils.InMemoryJournal;
22 import org.opendaylight.controller.cluster.raft.utils.InMemorySnapshotStore;
23 import org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * Recovery Integration Test for single node.
29  */
30 public class RecoveryIntegrationSingleNodeTest extends AbstractRaftActorIntegrationTest {
31
32     static final Logger LOG = LoggerFactory.getLogger(RecoveryIntegrationSingleNodeTest.class);
33
34     @Before
35     public void setup() {
36         leaderConfigParams = newLeaderConfigParams();
37     }
38
39
40     @Test
41     public void testJournalReplayAfterSnapshotWithSingleNode() throws Exception {
42
43         String persistenceId = factory.generateActorId("singleNode");
44         TestActorRef<AbstractRaftActorIntegrationTest.TestRaftActor> singleNodeActorRef =
45                 newTestRaftActor(persistenceId, ImmutableMap.<String, String>builder().build(), leaderConfigParams);
46
47         waitUntilLeader(singleNodeActorRef);
48
49         ActorRef singleNodeCollectorActor = singleNodeActorRef.underlyingActor().collectorActor();
50         final RaftActorContext singleNodeContext = singleNodeActorRef.underlyingActor().getRaftActorContext();
51
52
53         final MockRaftActorContext.MockPayload payload0 = sendPayloadData(singleNodeActorRef, "zero");
54         final MockRaftActorContext.MockPayload payload1 = sendPayloadData(singleNodeActorRef, "one");
55         final MockRaftActorContext.MockPayload payload2 = sendPayloadData(singleNodeActorRef, "two");
56
57         MessageCollectorActor.expectMatching(singleNodeCollectorActor, ApplyJournalEntries.class, 3);
58
59         // this should trigger a snapshot
60         final MockRaftActorContext.MockPayload payload3 = sendPayloadData(singleNodeActorRef, "three");
61
62         MessageCollectorActor.expectMatching(singleNodeCollectorActor, ApplyJournalEntries.class, 4);
63
64         //add 2 more
65         final MockRaftActorContext.MockPayload payload4 = sendPayloadData(singleNodeActorRef, "four");
66         final MockRaftActorContext.MockPayload payload5 = sendPayloadData(singleNodeActorRef, "five");
67
68
69         // Wait for snapshot complete.
70         MessageCollectorActor.expectFirstMatching(singleNodeCollectorActor, SaveSnapshotSuccess.class);
71
72         MessageCollectorActor.expectMatching(singleNodeCollectorActor, ApplyJournalEntries.class, 6);
73
74         assertEquals("Last applied", 5, singleNodeContext.getLastApplied());
75
76         assertEquals("Incorrect State after snapshot success is received ", Lists.newArrayList(payload0, payload1,
77                 payload2, payload3, payload4, payload5), singleNodeActorRef.underlyingActor().getState());
78
79         // we get 2 log entries (4 and 5 indexes) and 3 ApplyJournalEntries (for 3, 4, and 5 indexes)
80         assertEquals(5, InMemoryJournal.get(persistenceId).size());
81
82         List<Snapshot> persistedSnapshots = InMemorySnapshotStore.getSnapshots(persistenceId, Snapshot.class);
83         assertEquals(1, persistedSnapshots.size());
84
85         @SuppressWarnings("unchecked")
86         List<Object> snapshottedState = (List<Object>)MockRaftActor.toObject(persistedSnapshots.get(0).getState());
87         assertEquals("Incorrect Snapshot", Lists.newArrayList(payload0, payload1, payload2, payload3),
88                 snapshottedState);
89
90         //recovery logic starts
91         killActor(singleNodeActorRef);
92
93         singleNodeActorRef = newTestRaftActor(persistenceId,
94                 ImmutableMap.<String, String>builder().build(), leaderConfigParams);
95
96         singleNodeActorRef.underlyingActor().waitForRecoveryComplete();
97
98         assertEquals("Incorrect State after Recovery ", Lists.newArrayList(payload0, payload1, payload2, payload3,
99                 payload4, payload5), singleNodeActorRef.underlyingActor().getState());
100
101     }
102 }

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