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

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