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