896c0245393f22374f5077cacbe88680f58159cc
[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.TestActorRef;
24 import akka.testkit.javadsl.TestKit;
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.RemoteOpsProviderConfig;
33 import org.opendaylight.controller.remote.rpc.TerminationMonitor;
34
35
36 public class GossiperTest {
37
38     private static ActorSystem system;
39     private static Gossiper gossiper;
40
41     private Gossiper mockGossiper;
42
43     @BeforeClass
44     public static void setup() {
45         system = ActorSystem.create("opendaylight-rpc", ConfigFactory.load().getConfig("unit-test"));
46         system.actorOf(Props.create(TerminationMonitor.class), "termination-monitor");
47
48         gossiper = createGossiper();
49     }
50
51     @AfterClass
52     public static void teardown() {
53         TestKit.shutdownActorSystem(system);
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     @Test
67     public void testReceiveGossipTick_WhenNoRemoteMemberShouldIgnore() {
68         mockGossiper.setClusterMembers();
69         doNothing().when(mockGossiper).getLocalStatusAndSendTo(any(ActorSelection.class));
70         mockGossiper.receiveGossipTick();
71         verify(mockGossiper, times(0)).getLocalStatusAndSendTo(any(ActorSelection.class));
72     }
73
74     @Test
75     public void testReceiveGossipTick_WhenRemoteMemberExistsShouldSendStatus() {
76         mockGossiper.setClusterMembers(new Address("tcp", "member"));
77         doNothing().when(mockGossiper).getLocalStatusAndSendTo(any(ActorSelection.class));
78         mockGossiper.receiveGossipTick();
79         verify(mockGossiper, times(1)).getLocalStatusAndSendTo(any(ActorSelection.class));
80     }
81
82     @SuppressWarnings("unchecked")
83     @Test
84     public void testReceiveGossipStatus_WhenSenderIsNonMemberShouldIgnore() {
85         Address nonMember = new Address("tcp", "non-member");
86         GossipStatus remoteStatus = new GossipStatus(nonMember, mock(Map.class));
87
88         //add a member
89         mockGossiper.setClusterMembers(new Address("tcp", "member"));
90         mockGossiper.receiveGossipStatus(remoteStatus);
91         verify(mockGossiper, times(0)).getSender();
92     }
93
94     @SuppressWarnings("unchecked")
95     @Test
96     public void testReceiveGossipWhenNotAddressedToSelfShouldIgnore() {
97         doNothing().when(mockGossiper).updateRemoteBuckets(anyMap());
98         Address notSelf = new Address("tcp", "not-self");
99         mockGossiper.receiveGossip(new GossipEnvelope(notSelf, notSelf, mock(Map.class)));
100         verify(mockGossiper, times(0)).updateRemoteBuckets(anyMap());
101     }
102
103     /**
104      * Create Gossiper actor and return the underlying instance of Gossiper class.
105      *
106      * @return instance of Gossiper class
107      */
108     private static Gossiper createGossiper() {
109         final RemoteOpsProviderConfig config =
110                 new RemoteOpsProviderConfig.Builder("unit-test")
111                         .withConfigReader(ConfigFactory::load).build();
112         final Props props = Gossiper.testProps(config);
113         final TestActorRef<Gossiper> testRef = TestActorRef.create(system, props, "testGossiper");
114
115         return testRef.underlyingActor();
116     }
117 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.