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