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

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