1ab03b216cdf09075d5747b7a76bf7e579bb9c38
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / RoleChangeNotifierTest.java
1 package org.opendaylight.controller.cluster.datastore;
2
3
4 import static org.junit.Assert.assertEquals;
5 import static org.junit.Assert.assertNotNull;
6 import static org.junit.Assert.assertNull;
7 import akka.actor.ActorRef;
8 import akka.actor.Props;
9 import akka.testkit.JavaTestKit;
10 import akka.testkit.TestActorRef;
11 import org.junit.Test;
12 import org.opendaylight.controller.cluster.datastore.utils.MessageCollectorActor;
13 import org.opendaylight.controller.cluster.notifications.LeaderStateChanged;
14 import org.opendaylight.controller.cluster.notifications.RegisterRoleChangeListener;
15 import org.opendaylight.controller.cluster.notifications.RegisterRoleChangeListenerReply;
16 import org.opendaylight.controller.cluster.notifications.RoleChangeNotification;
17 import org.opendaylight.controller.cluster.notifications.RoleChangeNotifier;
18 import org.opendaylight.controller.cluster.notifications.RoleChanged;
19 import org.opendaylight.controller.cluster.raft.RaftState;
20
21 public class RoleChangeNotifierTest extends AbstractActorTest  {
22
23     @Test
24     public void testHandleRegisterRoleChangeListener() throws Exception {
25         new JavaTestKit(getSystem()) {{
26             String memberId = "testHandleRegisterRoleChangeListener";
27             ActorRef listenerActor =  getSystem().actorOf(Props.create(MessageCollectorActor.class));
28
29             TestActorRef<RoleChangeNotifier> notifierTestActorRef = TestActorRef.create(
30                 getSystem(), RoleChangeNotifier.getProps(memberId), memberId);
31
32             notifierTestActorRef.tell(new RegisterRoleChangeListener(), listenerActor);
33
34             RegisterRoleChangeListenerReply reply = (RegisterRoleChangeListenerReply)
35                 MessageCollectorActor.getFirstMatching(listenerActor, RegisterRoleChangeListenerReply.class);
36             assertNotNull(reply);
37
38             RoleChangeNotification notification = (RoleChangeNotification)
39                 MessageCollectorActor.getFirstMatching(listenerActor, RoleChangeNotification.class);
40             assertNull(notification);
41         }};
42
43     }
44
45     @Test
46     public void testHandleRaftRoleChanged() throws Exception {
47         new JavaTestKit(getSystem()) {{
48             String memberId = "testHandleRegisterRoleChangeListenerWithNotificationSet";
49             ActorRef listenerActor =  getSystem().actorOf(Props.create(MessageCollectorActor.class));
50             ActorRef shardActor =  getTestActor();
51
52             TestActorRef<RoleChangeNotifier> notifierTestActorRef = TestActorRef.create(
53                 getSystem(), RoleChangeNotifier.getProps(memberId), memberId);
54
55             notifierTestActorRef.tell(new RoleChanged(memberId, RaftState.Candidate.name(), RaftState.Leader.name()), shardActor);
56
57             // no notification should be sent as listener has not yet registered
58             assertNull(MessageCollectorActor.getFirstMatching(listenerActor, RoleChangeNotification.class));
59
60             // listener registers after role has been changed, ensure we sent the latest role change after a reply
61             notifierTestActorRef.tell(new RegisterRoleChangeListener(), listenerActor);
62
63             RegisterRoleChangeListenerReply reply = (RegisterRoleChangeListenerReply)
64                 MessageCollectorActor.getFirstMatching(listenerActor, RegisterRoleChangeListenerReply.class);
65             assertNotNull(reply);
66
67             RoleChangeNotification notification = (RoleChangeNotification)
68                 MessageCollectorActor.getFirstMatching(listenerActor, RoleChangeNotification.class);
69             assertNotNull(notification);
70             assertEquals(RaftState.Candidate.name(), notification.getOldRole());
71             assertEquals(RaftState.Leader.name(), notification.getNewRole());
72
73         }};
74
75     }
76
77     @Test
78     public void testHandleLeaderStateChanged() throws Exception {
79         new JavaTestKit(getSystem()) {{
80             String actorId = "testHandleLeaderStateChanged";
81             TestActorRef<RoleChangeNotifier> notifierTestActorRef = TestActorRef.create(
82                 getSystem(), RoleChangeNotifier.getProps(actorId), actorId);
83
84             notifierTestActorRef.tell(new LeaderStateChanged("member1", "leader1"), ActorRef.noSender());
85
86             // listener registers after the sate has been changed, ensure we sent the latest state change after a reply
87             notifierTestActorRef.tell(new RegisterRoleChangeListener(), getRef());
88
89             expectMsgClass(RegisterRoleChangeListenerReply.class);
90
91             LeaderStateChanged leaderStateChanged = expectMsgClass(LeaderStateChanged.class);
92             assertEquals("getMemberId", "member1", leaderStateChanged.getMemberId());
93             assertEquals("getLeaderId", "leader1", leaderStateChanged.getLeaderId());
94
95             notifierTestActorRef.tell(new LeaderStateChanged("member1", "leader2"), ActorRef.noSender());
96
97             leaderStateChanged = expectMsgClass(LeaderStateChanged.class);
98             assertEquals("getMemberId", "member1", leaderStateChanged.getMemberId());
99             assertEquals("getLeaderId", "leader2", leaderStateChanged.getLeaderId());
100         }};
101     }
102 }
103
104