BUG-3128: cache ActorSelections
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / test / java / org / opendaylight / controller / remote / rpc / registry / gossip / GossiperTest.java
1 /*
2  * Copyright (c) 2014 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 package org.opendaylight.controller.remote.rpc.registry.gossip;
9
10 import static org.mockito.Matchers.any;
11 import static org.mockito.Matchers.anyMap;
12 import static org.mockito.Mockito.doNothing;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.reset;
15 import static org.mockito.Mockito.spy;
16 import static org.mockito.Mockito.times;
17 import static org.mockito.Mockito.verify;
18
19 import akka.actor.ActorSelection;
20 import akka.actor.ActorSystem;
21 import akka.actor.Address;
22 import akka.actor.Props;
23 import akka.testkit.JavaTestKit;
24 import akka.testkit.TestActorRef;
25 import com.typesafe.config.ConfigFactory;
26 import java.util.Map;
27 import org.junit.After;
28 import org.junit.AfterClass;
29 import org.junit.Before;
30 import org.junit.BeforeClass;
31 import org.junit.Test;
32 import org.opendaylight.controller.remote.rpc.RemoteRpcProviderConfig;
33 import org.opendaylight.controller.remote.rpc.TerminationMonitor;
34 import org.opendaylight.controller.remote.rpc.registry.gossip.Messages.GossiperMessages.GossipEnvelope;
35 import org.opendaylight.controller.remote.rpc.registry.gossip.Messages.GossiperMessages.GossipStatus;
36
37
38 public class GossiperTest {
39
40     private static ActorSystem system;
41     private static Gossiper gossiper;
42
43     private Gossiper mockGossiper;
44
45     @BeforeClass
46     public static void setup() throws InterruptedException {
47         system = ActorSystem.create("opendaylight-rpc", ConfigFactory.load().getConfig("unit-test"));
48         system.actorOf(Props.create(TerminationMonitor.class), "termination-monitor");
49
50         gossiper = createGossiper();
51     }
52
53     @AfterClass
54     public static void teardown() {
55         JavaTestKit.shutdownActorSystem(system);
56     }
57
58     @Before
59     public void createMocks() {
60         mockGossiper = spy(gossiper);
61     }
62
63     @After
64     public void resetMocks() {
65         reset(mockGossiper);
66
67     }
68
69     @Test
70     public void testReceiveGossipTick_WhenNoRemoteMemberShouldIgnore() {
71         mockGossiper.setClusterMembers();
72         doNothing().when(mockGossiper).getLocalStatusAndSendTo(any(ActorSelection.class));
73         mockGossiper.receiveGossipTick();
74         verify(mockGossiper, times(0)).getLocalStatusAndSendTo(any(ActorSelection.class));
75     }
76
77     @Test
78     public void testReceiveGossipTick_WhenRemoteMemberExistsShouldSendStatus() {
79         mockGossiper.setClusterMembers(new Address("tcp", "member"));
80         doNothing().when(mockGossiper).getLocalStatusAndSendTo(any(ActorSelection.class));
81         mockGossiper.receiveGossipTick();
82         verify(mockGossiper, times(1)).getLocalStatusAndSendTo(any(ActorSelection.class));
83     }
84
85     @SuppressWarnings("unchecked")
86     @Test
87     public void testReceiveGossipStatus_WhenSenderIsNonMemberShouldIgnore() {
88
89         Address nonMember = new Address("tcp", "non-member");
90         GossipStatus remoteStatus = new GossipStatus(nonMember, mock(Map.class));
91
92         //add a member
93         mockGossiper.setClusterMembers(new Address("tcp", "member"));
94         mockGossiper.receiveGossipStatus(remoteStatus);
95         verify(mockGossiper, times(0)).getSender();
96     }
97
98     @SuppressWarnings({ "unchecked", "rawtypes" })
99     @Test
100     public void testReceiveGossipWhenNotAddressedToSelfShouldIgnore() {
101         Address notSelf = new Address("tcp", "not-self");
102
103         GossipEnvelope envelope = new GossipEnvelope(notSelf, notSelf, mock(Map.class));
104         doNothing().when(mockGossiper).updateRemoteBuckets(anyMap());
105         mockGossiper.receiveGossip(envelope);
106         verify(mockGossiper, times(0)).updateRemoteBuckets(anyMap());
107     }
108
109     /**
110      * Create Gossiper actor and return the underlying instance of Gossiper class.
111      *
112      * @return instance of Gossiper class
113      */
114     private static Gossiper createGossiper() {
115         final Props props = Gossiper.testProps(new RemoteRpcProviderConfig(system.settings().config()));
116         final TestActorRef<Gossiper> testRef = TestActorRef.create(system, props, "testGossiper");
117
118         return testRef.underlyingActor();
119     }
120 }