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