Add exists method on DOMStoreReadTransaction and DOMDataReadTransaction
[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         Thread.sleep(1000);//give some time for previous test to stop the system. Netty port conflict arises otherwise.
49         system = ActorSystem.create("opendaylight-rpc", ConfigFactory.load().getConfig("unit-test"));
50         system.actorOf(Props.create(TerminationMonitor.class), "termination-monitor");
51
52         gossiper = createGossiper();
53     }
54
55     @AfterClass
56     public static void teardown() {
57         if (system != null)
58             system.shutdown();
59     }
60
61     @Before
62     public void createMocks(){
63         mockGossiper = spy(gossiper);
64     }
65
66     @After
67     public void resetMocks(){
68         reset(mockGossiper);
69
70     }
71
72     @Test
73     public void testReceiveGossipTick_WhenNoRemoteMemberShouldIgnore(){
74
75         mockGossiper.setClusterMembers(Collections.EMPTY_LIST);
76         doNothing().when(mockGossiper).getLocalStatusAndSendTo(any(Address.class));
77         mockGossiper.receiveGossipTick();
78         verify(mockGossiper, times(0)).getLocalStatusAndSendTo(any(Address.class));
79     }
80
81     @Test
82     public void testReceiveGossipTick_WhenRemoteMemberExistsShouldSendStatus(){
83         List<Address> members = new ArrayList<>();
84         Address remote = new Address("tcp", "member");
85         members.add(remote);
86
87         mockGossiper.setClusterMembers(members);
88         doNothing().when(mockGossiper).getLocalStatusAndSendTo(any(Address.class));
89         mockGossiper.receiveGossipTick();
90         verify(mockGossiper, times(1)).getLocalStatusAndSendTo(any(Address.class));
91     }
92
93     @Test
94     public void testReceiveGossipStatus_WhenSenderIsNonMemberShouldIgnore(){
95
96         Address nonMember = new Address("tcp", "non-member");
97         GossipStatus remoteStatus = new GossipStatus(nonMember, mock(Map.class));
98
99         //add a member
100         List<Address> members = new ArrayList<>();
101         members.add(new Address("tcp", "member"));
102
103         mockGossiper.setClusterMembers(members);
104         mockGossiper.receiveGossipStatus(remoteStatus);
105         verify(mockGossiper, times(0)).getSender();
106     }
107
108     @Test
109     public void testReceiveGossip_WhenNotAddressedToSelfShouldIgnore(){
110         Address notSelf = new Address("tcp", "not-self");
111
112         GossipEnvelope envelope = new GossipEnvelope(notSelf, notSelf, mock(Map.class));
113         doNothing().when(mockGossiper).updateRemoteBuckets(anyMap());
114         mockGossiper.receiveGossip(envelope);
115         verify(mockGossiper, times(0)).updateRemoteBuckets(anyMap());
116     }
117
118     @Test
119     public void testUpdateRemoteBuckets_WhenNoBucketShouldIgnore(){
120
121         mockGossiper.updateRemoteBuckets(null);
122         verify(mockGossiper, times(0)).getContext();
123
124         Map<Address, Bucket> empty = Collections.emptyMap();
125         mockGossiper.updateRemoteBuckets(empty);
126         verify(mockGossiper, times(0)).getContext();
127     }
128
129     /**
130      * Create Gossiper actor and return the underlying instance of Gossiper class.
131      *
132      * @return instance of Gossiper class
133      */
134     private static Gossiper createGossiper(){
135
136         final Props props = Props.create(Gossiper.class, false);
137         final TestActorRef<Gossiper> testRef = TestActorRef.create(system, props, "testGossiper");
138
139         return testRef.underlyingActor();
140     }
141 }