Fix warnings in sal-akka-raft test classes
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / test / java / org / opendaylight / controller / cluster / raft / RaftActorContextImplTest.java
1 /*
2  * Copyright (c) 2015 Brocade Communications 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.cluster.raft;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.mockito.Matchers.anyString;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.never;
17 import static org.mockito.Mockito.reset;
18 import static org.mockito.Mockito.verify;
19
20 import akka.actor.Props;
21 import akka.testkit.TestActorRef;
22 import com.google.common.collect.ImmutableMap;
23 import com.google.common.collect.Maps;
24 import java.util.Arrays;
25 import java.util.HashMap;
26 import java.util.Map;
27 import org.junit.After;
28 import org.junit.Test;
29 import org.opendaylight.controller.cluster.NonPersistentDataProvider;
30 import org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPayload;
31 import org.opendaylight.controller.cluster.raft.persisted.ServerInfo;
32 import org.opendaylight.controller.cluster.raft.utils.DoNothingActor;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * Unit tests for RaftActorContextImpl.
38  *
39  * @author Thomas Pantelis
40  */
41 public class RaftActorContextImplTest extends AbstractActorTest {
42     private final TestActorFactory actorFactory = new TestActorFactory(getSystem());
43
44     private final TestActorRef<DoNothingActor> actor = actorFactory.createTestActor(
45             Props.create(DoNothingActor.class), actorFactory.generateActorId("actor"));
46
47     private final Logger log = LoggerFactory.getLogger(RaftActorContextImplTest.class);
48
49     @After
50     public void tearDown() {
51         actorFactory.close();
52     }
53
54     @Test
55     public void testGetPeerAddress() {
56         Map<String, String> peerMap = new HashMap<>();
57         peerMap.put("peer1", "peerAddress1");
58         peerMap.put("peer2", null);
59         DefaultConfigParamsImpl configParams = new DefaultConfigParamsImpl();
60         RaftActorContextImpl context = new RaftActorContextImpl(actor, actor.underlyingActor().getContext(),
61                 "test", new ElectionTermImpl(new NonPersistentDataProvider(), "test", log), -1, -1,
62                 peerMap, configParams, new NonPersistentDataProvider(), log);
63
64         assertEquals("getPeerAddress", "peerAddress1", context.getPeerAddress("peer1"));
65         assertEquals("getPeerAddress", null, context.getPeerAddress("peer2"));
66
67         PeerAddressResolver mockResolver = mock(PeerAddressResolver.class);
68         doReturn("peerAddress2").when(mockResolver).resolve("peer2");
69         doReturn("peerAddress3").when(mockResolver).resolve("peer3");
70         configParams.setPeerAddressResolver(mockResolver);
71
72         assertEquals("getPeerAddress", "peerAddress2", context.getPeerAddress("peer2"));
73         assertEquals("getPeerAddress", "peerAddress3", context.getPeerAddress("peer3"));
74
75         reset(mockResolver);
76         assertEquals("getPeerAddress", "peerAddress1", context.getPeerAddress("peer1"));
77         assertEquals("getPeerAddress", "peerAddress2", context.getPeerAddress("peer2"));
78         verify(mockResolver, never()).resolve(anyString());
79     }
80
81     @Test
82     public void testSetPeerAddress() {
83         DefaultConfigParamsImpl configParams = new DefaultConfigParamsImpl();
84         RaftActorContextImpl context = new RaftActorContextImpl(actor, actor.underlyingActor().getContext(),
85                 "test", new ElectionTermImpl(new NonPersistentDataProvider(), "test", log), -1, -1,
86                 Maps.newHashMap(ImmutableMap.<String, String>of("peer1", "peerAddress1")), configParams,
87                 new NonPersistentDataProvider(), log);
88
89         context.setPeerAddress("peer1", "peerAddress1_1");
90         assertEquals("getPeerAddress", "peerAddress1_1", context.getPeerAddress("peer1"));
91
92         context.setPeerAddress("peer2", "peerAddress2");
93         assertEquals("getPeerAddress", null, context.getPeerAddress("peer2"));
94     }
95
96     @Test
97     public void testUpdatePeerIds() {
98         RaftActorContextImpl context = new RaftActorContextImpl(actor, actor.underlyingActor().getContext(),
99                 "self", new ElectionTermImpl(new NonPersistentDataProvider(), "test", log), -1, -1,
100                 Maps.newHashMap(ImmutableMap.<String, String>of("peer1", "peerAddress1")),
101                 new DefaultConfigParamsImpl(), new NonPersistentDataProvider(), log);
102
103         context.updatePeerIds(new ServerConfigurationPayload(Arrays.asList(new ServerInfo("self", false),
104                 new ServerInfo("peer2", true), new ServerInfo("peer3", false))));
105         verifyPeerInfo(context, "peer1", null);
106         verifyPeerInfo(context, "peer2", true);
107         verifyPeerInfo(context, "peer3", false);
108         assertEquals("isVotingMember", false, context.isVotingMember());
109
110         context.updatePeerIds(new ServerConfigurationPayload(Arrays.asList(new ServerInfo("self", true),
111                 new ServerInfo("peer2", true), new ServerInfo("peer3", true))));
112         verifyPeerInfo(context, "peer2", true);
113         verifyPeerInfo(context, "peer3", true);
114         assertEquals("isVotingMember", true, context.isVotingMember());
115
116         context.updatePeerIds(new ServerConfigurationPayload(Arrays.asList(new ServerInfo("peer2", true),
117                 new ServerInfo("peer3", true))));
118         verifyPeerInfo(context, "peer2", true);
119         verifyPeerInfo(context, "peer3", true);
120         assertEquals("isVotingMember", false, context.isVotingMember());
121     }
122
123     private static void verifyPeerInfo(RaftActorContextImpl context, String peerId, Boolean voting) {
124         PeerInfo peerInfo = context.getPeerInfo(peerId);
125         if (voting != null) {
126             assertNotNull("Expected peer " + peerId, peerInfo);
127             assertEquals("getVotingState for " + peerId, voting.booleanValue()
128                     ? VotingState.VOTING : VotingState.NON_VOTING, peerInfo.getVotingState());
129         } else {
130             assertNull("Unexpected peer " + peerId, peerInfo);
131         }
132     }
133 }

©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.