Set to non-voting if not in server confguration
[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 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;
25 import java.util.Map;
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;
33
34 /**
35  * Unit tests for RaftActorContextImpl.
36  *
37  * @author Thomas Pantelis
38  */
39 public class RaftActorContextImplTest extends AbstractActorTest {
40     private final TestActorFactory actorFactory = new TestActorFactory(getSystem());
41
42     private final TestActorRef<DoNothingActor> actor = actorFactory.createTestActor(
43             Props.create(DoNothingActor.class), actorFactory.generateActorId("actor"));
44
45     private final Logger log = LoggerFactory.getLogger(RaftActorContextImplTest.class);
46
47     @After
48     public void tearDown() {
49         actorFactory.close();
50     }
51
52     @Test
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);
61
62         assertEquals("getPeerAddress", "peerAddress1", context.getPeerAddress("peer1"));
63         assertEquals("getPeerAddress", null, context.getPeerAddress("peer2"));
64
65         PeerAddressResolver mockResolver = mock(PeerAddressResolver.class);
66         doReturn("peerAddress2").when(mockResolver).resolve("peer2");
67         doReturn("peerAddress3").when(mockResolver).resolve("peer3");
68         configParams.setPeerAddressResolver(mockResolver);
69
70         assertEquals("getPeerAddress", "peerAddress2", context.getPeerAddress("peer2"));
71         assertEquals("getPeerAddress", "peerAddress3", context.getPeerAddress("peer3"));
72
73         reset(mockResolver);
74         assertEquals("getPeerAddress", "peerAddress1", context.getPeerAddress("peer1"));
75         assertEquals("getPeerAddress", "peerAddress2", context.getPeerAddress("peer2"));
76         verify(mockResolver, never()).resolve(anyString());
77     }
78
79     @Test
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);
86
87         context.setPeerAddress("peer1", "peerAddress1_1");
88         assertEquals("getPeerAddress", "peerAddress1_1", context.getPeerAddress("peer1"));
89
90         context.setPeerAddress("peer2", "peerAddress2");
91         assertEquals("getPeerAddress", null, context.getPeerAddress("peer2"));
92     }
93
94     @Test
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);
100
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());
107
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());
113
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());
119     }
120
121     private void verifyPeerInfo(RaftActorContextImpl context, String peerId, Boolean voting) {
122         PeerInfo peerInfo = context.getPeerInfo(peerId);
123         if(voting != null) {
124             assertNotNull("Expected peer " + peerId, peerInfo);
125             assertEquals("getVotingState for " + peerId, voting.booleanValue() ? VotingState.VOTING : VotingState.NON_VOTING,
126                     peerInfo.getVotingState());
127         } else {
128             assertNull("Unexpected peer " + peerId, peerInfo);
129         }
130     }
131 }