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