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