54b09bfce74bf2fb2880807faa2628d2db486924
[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.ArrayList;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.Map;
29 import org.junit.After;
30 import org.junit.AfterClass;
31 import org.junit.Before;
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34 import org.opendaylight.controller.remote.rpc.RemoteRpcProviderConfig;
35 import org.opendaylight.controller.remote.rpc.TerminationMonitor;
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         if (system != null)
56             system.shutdown();
57     }
58
59     @Before
60     public void createMocks(){
61         mockGossiper = spy(gossiper);
62     }
63
64     @After
65     public void resetMocks(){
66         reset(mockGossiper);
67
68     }
69
70     @Test
71     public void testReceiveGossipTick_WhenNoRemoteMemberShouldIgnore(){
72
73         mockGossiper.setClusterMembers(Collections.<Address>emptyList());
74         doNothing().when(mockGossiper).getLocalStatusAndSendTo(any(Address.class));
75         mockGossiper.receiveGossipTick();
76         verify(mockGossiper, times(0)).getLocalStatusAndSendTo(any(Address.class));
77     }
78
79     @Test
80     public void testReceiveGossipTick_WhenRemoteMemberExistsShouldSendStatus(){
81         List<Address> members = new ArrayList<>();
82         Address remote = new Address("tcp", "member");
83         members.add(remote);
84
85         mockGossiper.setClusterMembers(members);
86         doNothing().when(mockGossiper).getLocalStatusAndSendTo(any(Address.class));
87         mockGossiper.receiveGossipTick();
88         verify(mockGossiper, times(1)).getLocalStatusAndSendTo(any(Address.class));
89     }
90
91     @Test
92     public void testReceiveGossipStatus_WhenSenderIsNonMemberShouldIgnore(){
93
94         Address nonMember = new Address("tcp", "non-member");
95         GossipStatus remoteStatus = new GossipStatus(nonMember, mock(Map.class));
96
97         //add a member
98         List<Address> members = new ArrayList<>();
99         members.add(new Address("tcp", "member"));
100
101         mockGossiper.setClusterMembers(members);
102         mockGossiper.receiveGossipStatus(remoteStatus);
103         verify(mockGossiper, times(0)).getSender();
104     }
105
106     @Test
107     public void testReceiveGossip_WhenNotAddressedToSelfShouldIgnore(){
108         Address notSelf = new Address("tcp", "not-self");
109
110         GossipEnvelope envelope = new GossipEnvelope(notSelf, notSelf, mock(Map.class));
111         doNothing().when(mockGossiper).updateRemoteBuckets(anyMap());
112         mockGossiper.receiveGossip(envelope);
113         verify(mockGossiper, times(0)).updateRemoteBuckets(anyMap());
114     }
115
116     /**
117      * Create Gossiper actor and return the underlying instance of Gossiper class.
118      *
119      * @return instance of Gossiper class
120      */
121     private static Gossiper createGossiper(){
122
123         final Props props = Props.create(Gossiper.class, false, new RemoteRpcProviderConfig(system.settings().config()));
124         final TestActorRef<Gossiper> testRef = TestActorRef.create(system, props, "testGossiper");
125
126         return testRef.underlyingActor();
127     }
128 }