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