Merge "BUG-1521 Increase test coverage of netconf-netty-util"
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / test / java / org / opendaylight / controller / cluster / raft / RaftActorTest.java
1 package org.opendaylight.controller.cluster.raft;
2
3 import akka.actor.ActorRef;
4 import akka.actor.ActorSystem;
5 import akka.actor.Props;
6 import akka.event.Logging;
7 import akka.japi.Creator;
8 import akka.testkit.JavaTestKit;
9 import org.junit.Test;
10 import org.opendaylight.controller.cluster.raft.client.messages.FindLeader;
11 import org.opendaylight.controller.cluster.raft.client.messages.FindLeaderReply;
12
13 import java.util.Collections;
14 import java.util.Map;
15
16 import static junit.framework.TestCase.assertEquals;
17
18 public class RaftActorTest extends AbstractActorTest {
19
20
21     public static class MockRaftActor extends RaftActor {
22
23         public MockRaftActor(String id,
24             Map<String, String> peerAddresses) {
25             super(id, peerAddresses);
26         }
27
28         public static Props props(final String id, final Map<String, String> peerAddresses){
29             return Props.create(new Creator<MockRaftActor>(){
30
31                 @Override public MockRaftActor create() throws Exception {
32                     return new MockRaftActor(id, peerAddresses);
33                 }
34             });
35         }
36
37         @Override protected void applyState(ActorRef clientActor,
38             String identifier,
39             Object data) {
40         }
41
42         @Override protected Object createSnapshot() {
43             throw new UnsupportedOperationException("createSnapshot");
44         }
45
46         @Override protected void applySnapshot(Object snapshot) {
47             throw new UnsupportedOperationException("applySnapshot");
48         }
49
50         @Override protected void onStateChanged() {
51         }
52
53         @Override public String persistenceId() {
54             return this.getId();
55         }
56
57     }
58
59
60     private static class RaftActorTestKit extends JavaTestKit {
61         private final ActorRef raftActor;
62
63         public RaftActorTestKit(ActorSystem actorSystem, String actorName) {
64             super(actorSystem);
65
66             raftActor = this.getSystem()
67                 .actorOf(MockRaftActor.props(actorName,
68                     Collections.EMPTY_MAP), actorName);
69
70         }
71
72
73         public boolean waitForStartup(){
74             // Wait for a specific log message to show up
75             return
76                 new JavaTestKit.EventFilter<Boolean>(Logging.Info.class
77                 ) {
78                     protected Boolean run() {
79                         return true;
80                     }
81                 }.from(raftActor.path().toString())
82                     .message("Switching from state Candidate to Leader")
83                     .occurrences(1).exec();
84
85
86         }
87
88         public void findLeader(final String expectedLeader){
89
90
91             new Within(duration("1 seconds")) {
92                 protected void run() {
93
94                     raftActor.tell(new FindLeader(), getRef());
95
96                     String s = new ExpectMsg<String>(duration("1 seconds"),
97                         "findLeader") {
98                         // do not put code outside this method, will run afterwards
99                         protected String match(Object in) {
100                             if (in instanceof FindLeaderReply) {
101                                 return ((FindLeaderReply) in).getLeaderActor();
102                             } else {
103                                 throw noMatch();
104                             }
105                         }
106                     }.get();// this extracts the received message
107
108                     assertEquals(expectedLeader, s);
109
110                 }
111
112
113             };
114         }
115
116         public ActorRef getRaftActor() {
117             return raftActor;
118         }
119
120     }
121
122
123     @Test
124     public void testConstruction() {
125         boolean started = new RaftActorTestKit(getSystem(), "testConstruction").waitForStartup();
126         assertEquals(true, started);
127     }
128
129     @Test
130     public void testFindLeaderWhenLeaderIsSelf(){
131         RaftActorTestKit kit = new RaftActorTestKit(getSystem(), "testFindLeader");
132         kit.waitForStartup();
133         kit.findLeader(kit.getRaftActor().path().toString());
134     }
135
136
137 }