Remove JournalWriter.getLastEntry()
[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.ArgumentMatchers.any;
11 import static org.mockito.ArgumentMatchers.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 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() {
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         TestKit.shutdownActorSystem(system);
53     }
54
55     @Before
56     public void createMocks() {
57         mockGossiper = spy(gossiper);
58     }
59
60     @After
61     public void resetMocks() {
62         reset(mockGossiper);
63     }
64
65     @Test
66     public void testReceiveGossipTick_WhenNoRemoteMemberShouldIgnore() {
67         mockGossiper.setClusterMembers();
68         doNothing().when(mockGossiper).getLocalStatusAndSendTo(any(ActorSelection.class));
69         mockGossiper.receiveGossipTick();
70         verify(mockGossiper, times(0)).getLocalStatusAndSendTo(any(ActorSelection.class));
71     }
72
73     @Test
74     public void testReceiveGossipTick_WhenRemoteMemberExistsShouldSendStatus() {
75         mockGossiper.setClusterMembers(new Address("tcp", "member"));
76         doNothing().when(mockGossiper).getLocalStatusAndSendTo(any(ActorSelection.class));
77         mockGossiper.receiveGossipTick();
78         verify(mockGossiper, times(1)).getLocalStatusAndSendTo(any(ActorSelection.class));
79     }
80
81     @SuppressWarnings("unchecked")
82     @Test
83     public void testReceiveGossipStatus_WhenSenderIsNonMemberShouldIgnore() {
84         Address nonMember = new Address("tcp", "non-member");
85         GossipStatus remoteStatus = new GossipStatus(nonMember, mock(Map.class));
86
87         //add a member
88         mockGossiper.setClusterMembers(new Address("tcp", "member"));
89         mockGossiper.receiveGossipStatus(remoteStatus);
90         verify(mockGossiper, times(0)).getSender();
91     }
92
93     @SuppressWarnings("unchecked")
94     @Test
95     public void testReceiveGossipWhenNotAddressedToSelfShouldIgnore() {
96         doNothing().when(mockGossiper).updateRemoteBuckets(anyMap());
97         Address notSelf = new Address("tcp", "not-self");
98         mockGossiper.receiveGossip(new GossipEnvelope(notSelf, notSelf, mock(Map.class)));
99         verify(mockGossiper, times(0)).updateRemoteBuckets(anyMap());
100     }
101
102     /**
103      * Create Gossiper actor and return the underlying instance of Gossiper class.
104      *
105      * @return instance of Gossiper class
106      */
107     private static Gossiper createGossiper() {
108         final RemoteOpsProviderConfig config =
109                 new RemoteOpsProviderConfig.Builder("unit-test")
110                         .withConfigReader(ConfigFactory::load).build();
111         final Props props = Gossiper.testProps(config);
112         final TestActorRef<Gossiper> testRef = TestActorRef.create(system, props, "testGossiper");
113
114         return testRef.underlyingActor();
115     }
116 }