2 * Copyright (c) 2015 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.raft;
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 import akka.actor.Props;
20 import akka.testkit.TestActorRef;
21 import com.google.common.collect.ImmutableMap;
22 import com.google.common.collect.Maps;
23 import java.util.Arrays;
24 import java.util.HashMap;
26 import org.junit.After;
27 import org.junit.Test;
28 import org.opendaylight.controller.cluster.NonPersistentDataProvider;
29 import org.opendaylight.controller.cluster.raft.ServerConfigurationPayload.ServerInfo;
30 import org.opendaylight.controller.cluster.raft.utils.DoNothingActor;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * Unit tests for RaftActorContextImpl.
37 * @author Thomas Pantelis
39 public class RaftActorContextImplTest extends AbstractActorTest {
40 private final TestActorFactory actorFactory = new TestActorFactory(getSystem());
42 private final TestActorRef<DoNothingActor> actor = actorFactory.createTestActor(
43 Props.create(DoNothingActor.class), actorFactory.generateActorId("actor"));
45 private final Logger log = LoggerFactory.getLogger(RaftActorContextImplTest.class);
48 public void tearDown() {
53 public void testGetPeerAddress() {
54 Map<String, String> peerMap = new HashMap<>();
55 peerMap.put("peer1", "peerAddress1");
56 peerMap.put("peer2", null);
57 DefaultConfigParamsImpl configParams = new DefaultConfigParamsImpl();
58 RaftActorContextImpl context = new RaftActorContextImpl(actor, actor.underlyingActor().getContext(),
59 "test", new ElectionTermImpl(new NonPersistentDataProvider(), "test", log), -1, -1,
60 peerMap, configParams, new NonPersistentDataProvider(), log);
62 assertEquals("getPeerAddress", "peerAddress1", context.getPeerAddress("peer1"));
63 assertEquals("getPeerAddress", null, context.getPeerAddress("peer2"));
65 PeerAddressResolver mockResolver = mock(PeerAddressResolver.class);
66 doReturn("peerAddress2").when(mockResolver).resolve("peer2");
67 doReturn("peerAddress3").when(mockResolver).resolve("peer3");
68 configParams.setPeerAddressResolver(mockResolver);
70 assertEquals("getPeerAddress", "peerAddress2", context.getPeerAddress("peer2"));
71 assertEquals("getPeerAddress", "peerAddress3", context.getPeerAddress("peer3"));
74 assertEquals("getPeerAddress", "peerAddress1", context.getPeerAddress("peer1"));
75 assertEquals("getPeerAddress", "peerAddress2", context.getPeerAddress("peer2"));
76 verify(mockResolver, never()).resolve(anyString());
80 public void testSetPeerAddress() {
81 DefaultConfigParamsImpl configParams = new DefaultConfigParamsImpl();
82 RaftActorContextImpl context = new RaftActorContextImpl(actor, actor.underlyingActor().getContext(),
83 "test", new ElectionTermImpl(new NonPersistentDataProvider(), "test", log), -1, -1,
84 Maps.newHashMap(ImmutableMap.<String, String>of("peer1", "peerAddress1")), configParams,
85 new NonPersistentDataProvider(), log);
87 context.setPeerAddress("peer1", "peerAddress1_1");
88 assertEquals("getPeerAddress", "peerAddress1_1", context.getPeerAddress("peer1"));
90 context.setPeerAddress("peer2", "peerAddress2");
91 assertEquals("getPeerAddress", null, context.getPeerAddress("peer2"));
95 public void testUpdatePeerIds() {
96 RaftActorContextImpl context = new RaftActorContextImpl(actor, actor.underlyingActor().getContext(),
97 "self", new ElectionTermImpl(new NonPersistentDataProvider(), "test", log), -1, -1,
98 Maps.newHashMap(ImmutableMap.<String, String>of("peer1", "peerAddress1")),
99 new DefaultConfigParamsImpl(), new NonPersistentDataProvider(), log);
101 context.updatePeerIds(new ServerConfigurationPayload(Arrays.asList(new ServerInfo("self", false),
102 new ServerInfo("peer2", true), new ServerInfo("peer3", false))));
103 verifyPeerInfo(context, "peer1", null);
104 verifyPeerInfo(context, "peer2", true);
105 verifyPeerInfo(context, "peer3", false);
106 assertEquals("isVotingMember", false, context.isVotingMember());
108 context.updatePeerIds(new ServerConfigurationPayload(Arrays.asList(new ServerInfo("self", true),
109 new ServerInfo("peer2", true), new ServerInfo("peer3", true))));
110 verifyPeerInfo(context, "peer2", true);
111 verifyPeerInfo(context, "peer3", true);
112 assertEquals("isVotingMember", true, context.isVotingMember());
114 context.updatePeerIds(new ServerConfigurationPayload(Arrays.asList(new ServerInfo("peer2", true),
115 new ServerInfo("peer3", true))));
116 verifyPeerInfo(context, "peer2", true);
117 verifyPeerInfo(context, "peer3", true);
118 assertEquals("isVotingMember", false, context.isVotingMember());
121 private void verifyPeerInfo(RaftActorContextImpl context, String peerId, Boolean voting) {
122 PeerInfo peerInfo = context.getPeerInfo(peerId);
124 assertNotNull("Expected peer " + peerId, peerInfo);
125 assertEquals("getVotingState for " + peerId, voting.booleanValue() ? VotingState.VOTING : VotingState.NON_VOTING,
126 peerInfo.getVotingState());
128 assertNull("Unexpected peer " + peerId, peerInfo);