Revert "Add mockito-configuration to tests"
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / test / java / org / opendaylight / controller / cluster / raft / RaftActorLeadershipTransferCohortTest.java
1 /*
2  * Copyright (c) 2015 Brocade Communications 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.mockito.Mockito.mock;
11 import static org.mockito.Mockito.never;
12 import static org.mockito.Mockito.timeout;
13 import static org.mockito.Mockito.verify;
14 import akka.dispatch.Dispatchers;
15 import com.google.common.base.Function;
16 import org.junit.After;
17 import org.junit.Test;
18 import org.opendaylight.controller.cluster.raft.RaftActorLeadershipTransferCohort.OnComplete;
19
20 /**
21  * Unit tests for RaftActorLeadershipTransferCohort.
22  *
23  * @author Thomas Pantelis
24  */
25 public class RaftActorLeadershipTransferCohortTest extends AbstractActorTest {
26     private final TestActorFactory factory = new TestActorFactory(getSystem());
27     private MockRaftActor mockRaftActor;
28     private RaftActorLeadershipTransferCohort cohort;
29     private final OnComplete onComplete = mock(OnComplete.class);
30     private final DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
31     private Function<Runnable, Void> pauseLeaderFunction;
32
33     @After
34     public void tearDown() {
35         factory.close();
36     }
37
38     private void setup() {
39         String persistenceId = factory.generateActorId("leader-");
40         mockRaftActor = factory.<MockRaftActor>createTestActor(MockRaftActor.builder().id(persistenceId).config(
41                 config).pauseLeaderFunction(pauseLeaderFunction).props().withDispatcher(Dispatchers.DefaultDispatcherId()),
42                 persistenceId).underlyingActor();
43         cohort = new RaftActorLeadershipTransferCohort(mockRaftActor, null);
44         cohort.addOnComplete(onComplete);
45         mockRaftActor.waitForInitializeBehaviorComplete();
46     }
47
48     @Test
49     public void testOnNewLeader() {
50         setup();
51         cohort.setNewLeaderTimeoutInMillis(20000);
52
53         cohort.onNewLeader("new-leader");
54         verify(onComplete, never()).onSuccess(mockRaftActor.self(), null);
55
56         cohort.transferComplete();
57
58         cohort.onNewLeader(null);
59         verify(onComplete, never()).onSuccess(mockRaftActor.self(), null);
60
61         cohort.onNewLeader("new-leader");
62         verify(onComplete).onSuccess(mockRaftActor.self(), null);
63     }
64
65     @Test
66     public void testNewLeaderTimeout() {
67         setup();
68         cohort.setNewLeaderTimeoutInMillis(200);
69         cohort.transferComplete();
70         verify(onComplete, timeout(3000)).onSuccess(mockRaftActor.self(), null);
71     }
72
73     @Test
74     public void testNotLeaderOnRun() {
75         config.setElectionTimeoutFactor(10000);
76         setup();
77         cohort.doTransfer();
78         verify(onComplete).onSuccess(mockRaftActor.self(), null);
79     }
80
81     @Test
82     public void testAbortTransfer() {
83         setup();
84         cohort.abortTransfer();
85         verify(onComplete).onFailure(mockRaftActor.self(), null);
86     }
87
88     @Test
89     public void testPauseLeaderTimeout() {
90         pauseLeaderFunction = new Function<Runnable, Void>() {
91             @Override
92             public Void apply(Runnable input) {
93                 return null;
94             }
95         };
96
97         setup();
98         cohort.init();
99         verify(onComplete, timeout(2000)).onFailure(mockRaftActor.self(), null);
100     }
101 }