X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2FRoleChangeNotifierTest.java;h=bec679c16327789106d0f8938166f6c80bf0836c;hb=6313c088fc7db266cc25b691e0cd909300fc8425;hp=4e61260550cdc9ab4f5b7e9409e295337e0d47b8;hpb=191ad68d71f30f6ad6258ac458c60c663e5b1b85;p=controller.git diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/RoleChangeNotifierTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/RoleChangeNotifierTest.java index 4e61260550..bec679c163 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/RoleChangeNotifierTest.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/RoleChangeNotifierTest.java @@ -1,21 +1,30 @@ +/* + * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + package org.opendaylight.controller.cluster.datastore; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import akka.actor.ActorRef; import akka.actor.Props; import akka.testkit.JavaTestKit; import akka.testkit.TestActorRef; import org.junit.Test; -import org.opendaylight.controller.cluster.datastore.utils.MessageCollectorActor; +import org.opendaylight.controller.cluster.notifications.LeaderStateChanged; import org.opendaylight.controller.cluster.notifications.RegisterRoleChangeListener; import org.opendaylight.controller.cluster.notifications.RegisterRoleChangeListenerReply; import org.opendaylight.controller.cluster.notifications.RoleChangeNotification; import org.opendaylight.controller.cluster.notifications.RoleChangeNotifier; import org.opendaylight.controller.cluster.notifications.RoleChanged; import org.opendaylight.controller.cluster.raft.RaftState; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; +import org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor; public class RoleChangeNotifierTest extends AbstractActorTest { @@ -30,11 +39,11 @@ public class RoleChangeNotifierTest extends AbstractActorTest { notifierTestActorRef.tell(new RegisterRoleChangeListener(), listenerActor); - RegisterRoleChangeListenerReply reply = (RegisterRoleChangeListenerReply) + RegisterRoleChangeListenerReply reply = MessageCollectorActor.getFirstMatching(listenerActor, RegisterRoleChangeListenerReply.class); assertNotNull(reply); - RoleChangeNotification notification = (RoleChangeNotification) + RoleChangeNotification notification = MessageCollectorActor.getFirstMatching(listenerActor, RoleChangeNotification.class); assertNull(notification); }}; @@ -51,8 +60,6 @@ public class RoleChangeNotifierTest extends AbstractActorTest { TestActorRef notifierTestActorRef = TestActorRef.create( getSystem(), RoleChangeNotifier.getProps(memberId), memberId); - RoleChangeNotifier roleChangeNotifier = notifierTestActorRef.underlyingActor(); - notifierTestActorRef.tell(new RoleChanged(memberId, RaftState.Candidate.name(), RaftState.Leader.name()), shardActor); // no notification should be sent as listener has not yet registered @@ -61,11 +68,11 @@ public class RoleChangeNotifierTest extends AbstractActorTest { // listener registers after role has been changed, ensure we sent the latest role change after a reply notifierTestActorRef.tell(new RegisterRoleChangeListener(), listenerActor); - RegisterRoleChangeListenerReply reply = (RegisterRoleChangeListenerReply) + RegisterRoleChangeListenerReply reply = MessageCollectorActor.getFirstMatching(listenerActor, RegisterRoleChangeListenerReply.class); assertNotNull(reply); - RoleChangeNotification notification = (RoleChangeNotification) + RoleChangeNotification notification = MessageCollectorActor.getFirstMatching(listenerActor, RoleChangeNotification.class); assertNotNull(notification); assertEquals(RaftState.Candidate.name(), notification.getOldRole()); @@ -74,6 +81,34 @@ public class RoleChangeNotifierTest extends AbstractActorTest { }}; } + + @Test + public void testHandleLeaderStateChanged() throws Exception { + new JavaTestKit(getSystem()) {{ + String actorId = "testHandleLeaderStateChanged"; + TestActorRef notifierTestActorRef = TestActorRef.create( + getSystem(), RoleChangeNotifier.getProps(actorId), actorId); + + notifierTestActorRef.tell(new LeaderStateChanged("member1", "leader1", (short)5), ActorRef.noSender()); + + // listener registers after the sate has been changed, ensure we sent the latest state change after a reply + notifierTestActorRef.tell(new RegisterRoleChangeListener(), getRef()); + + expectMsgClass(RegisterRoleChangeListenerReply.class); + + LeaderStateChanged leaderStateChanged = expectMsgClass(LeaderStateChanged.class); + assertEquals("getMemberId", "member1", leaderStateChanged.getMemberId()); + assertEquals("getLeaderId", "leader1", leaderStateChanged.getLeaderId()); + assertEquals("getLeaderPayloadVersion", 5, leaderStateChanged.getLeaderPayloadVersion()); + + notifierTestActorRef.tell(new LeaderStateChanged("member1", "leader2", (short)6), ActorRef.noSender()); + + leaderStateChanged = expectMsgClass(LeaderStateChanged.class); + assertEquals("getMemberId", "member1", leaderStateChanged.getMemberId()); + assertEquals("getLeaderId", "leader2", leaderStateChanged.getLeaderId()); + assertEquals("getLeaderPayloadVersion", 6, leaderStateChanged.getLeaderPayloadVersion()); + }}; + } }