Merge "Fix checkstyle warnings in netty-threadgroup-config."
[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 akka.actor.ActorRef;
5 import akka.actor.Props;
6 import akka.testkit.JavaTestKit;
7 import akka.testkit.TestActorRef;
8 import org.junit.Test;
9 import org.opendaylight.controller.cluster.datastore.utils.MessageCollectorActor;
10 import org.opendaylight.controller.cluster.notifications.RegisterRoleChangeListener;
11 import org.opendaylight.controller.cluster.notifications.RegisterRoleChangeListenerReply;
12 import org.opendaylight.controller.cluster.notifications.RoleChangeNotification;
13 import org.opendaylight.controller.cluster.notifications.RoleChangeNotifier;
14 import org.opendaylight.controller.cluster.notifications.RoleChanged;
15 import org.opendaylight.controller.cluster.raft.RaftState;
16 import static org.junit.Assert.assertEquals;
17 import static org.junit.Assert.assertNotNull;
18 import static org.junit.Assert.assertNull;
19
20 public class RoleChangeNotifierTest extends AbstractActorTest  {
21
22     @Test
23     public void testHandleRegisterRoleChangeListener() throws Exception {
24         new JavaTestKit(getSystem()) {{
25             String memberId = "testHandleRegisterRoleChangeListener";
26             ActorRef listenerActor =  getSystem().actorOf(Props.create(MessageCollectorActor.class));
27
28             TestActorRef<RoleChangeNotifier> notifierTestActorRef = TestActorRef.create(
29                 getSystem(), RoleChangeNotifier.getProps(memberId), memberId);
30
31             notifierTestActorRef.tell(new RegisterRoleChangeListener(), listenerActor);
32
33             RegisterRoleChangeListenerReply reply = (RegisterRoleChangeListenerReply)
34                 MessageCollectorActor.getFirstMatching(listenerActor, RegisterRoleChangeListenerReply.class);
35             assertNotNull(reply);
36
37             RoleChangeNotification notification = (RoleChangeNotification)
38                 MessageCollectorActor.getFirstMatching(listenerActor, RoleChangeNotification.class);
39             assertNull(notification);
40         }};
41
42     }
43
44     @Test
45     public void testHandleRaftRoleChanged() throws Exception {
46         new JavaTestKit(getSystem()) {{
47             String memberId = "testHandleRegisterRoleChangeListenerWithNotificationSet";
48             ActorRef listenerActor =  getSystem().actorOf(Props.create(MessageCollectorActor.class));
49             ActorRef shardActor =  getTestActor();
50
51             TestActorRef<RoleChangeNotifier> notifierTestActorRef = TestActorRef.create(
52                 getSystem(), RoleChangeNotifier.getProps(memberId), memberId);
53
54             RoleChangeNotifier roleChangeNotifier = notifierTestActorRef.underlyingActor();
55
56             notifierTestActorRef.tell(new RoleChanged(memberId, RaftState.Candidate.name(), RaftState.Leader.name()), shardActor);
57
58             // no notification should be sent as listener has not yet registered
59             assertNull(MessageCollectorActor.getFirstMatching(listenerActor, RoleChangeNotification.class));
60
61             // listener registers after role has been changed, ensure we sent the latest role change after a reply
62             notifierTestActorRef.tell(new RegisterRoleChangeListener(), listenerActor);
63
64             RegisterRoleChangeListenerReply reply = (RegisterRoleChangeListenerReply)
65                 MessageCollectorActor.getFirstMatching(listenerActor, RegisterRoleChangeListenerReply.class);
66             assertNotNull(reply);
67
68             RoleChangeNotification notification = (RoleChangeNotification)
69                 MessageCollectorActor.getFirstMatching(listenerActor, RoleChangeNotification.class);
70             assertNotNull(notification);
71             assertEquals(RaftState.Candidate.name(), notification.getOldRole());
72             assertEquals(RaftState.Leader.name(), notification.getNewRole());
73
74         }};
75
76     }
77 }
78
79