2 * Copyright (c) 2016 Cisco 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.lispflowmapping.clustering;
10 import static org.opendaylight.lispflowmapping.clustering.util.ClusteringUtil.LISPFLOWMAPPING_ENTITY_NAME;
11 import static org.opendaylight.lispflowmapping.clustering.util.ClusteringUtil.LISPFLOWMAPPING_ENTITY_TYPE;
13 import com.google.common.base.Optional;
14 import org.junit.Assert;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.mockito.Matchers;
18 import org.mockito.Mockito;
19 import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
20 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
21 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipChange;
22 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
23 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipState;
24 import org.opendaylight.lispflowmapping.clustering.api.ClusterNodeModuleSwitcher;
26 public class ClusterNodeModulSwitcherImplTest {
28 private EntityOwnershipService entityOwnershipServiceMocked;
29 private ClusterNodeModulSwitcherImpl clusterNodeModulSwitcherImpl;
30 private ClusterNodeModuleSwitcher module;
31 private Entity entity = new Entity(LISPFLOWMAPPING_ENTITY_NAME, LISPFLOWMAPPING_ENTITY_TYPE);
35 entityOwnershipServiceMocked = Mockito.mock(EntityOwnershipService.class);
36 clusterNodeModulSwitcherImpl = new ClusterNodeModulSwitcherImpl(entityOwnershipServiceMocked);
37 module = Mockito.mock(ClusterNodeModuleSwitcher.class);
41 public void constructorCallTest() throws CandidateAlreadyRegisteredException {
42 Mockito.verify(entityOwnershipServiceMocked).registerListener(Matchers.eq(LISPFLOWMAPPING_ENTITY_NAME),
43 Matchers.any(ClusterNodeModulSwitcherImpl.class));
44 Mockito.verify(entityOwnershipServiceMocked).registerCandidate(Matchers.eq(entity));
48 public void ownershipChangedTest() {
50 ownershipChanged(false);
51 Mockito.verify(module).stopModule();
53 ownershipChanged(true);
54 Mockito.verify(module).startModule();
58 public void isMasterTest_OptionalAbsent() {
59 Mockito.when(entityOwnershipServiceMocked.getOwnershipState(Matchers.eq(entity))).thenReturn(Optional
62 Assert.assertFalse(clusterNodeModulSwitcherImpl.isMaster());
66 public void isMasterTest_True() {
67 Mockito.when(entityOwnershipServiceMocked.getOwnershipState(Matchers.eq(entity))).thenReturn(Optional
68 .of(new EntityOwnershipState(true, true)));
69 Assert.assertTrue(clusterNodeModulSwitcherImpl.isMaster());
73 public void isMasterTest_False() {
74 Mockito.when(entityOwnershipServiceMocked.getOwnershipState(Matchers.eq(entity))).thenReturn(Optional
75 .of(new EntityOwnershipState(false, true)));
76 Assert.assertFalse(clusterNodeModulSwitcherImpl.isMaster());
80 public void switchModuleByEntityOwnershipTest() {
81 Mockito.when(entityOwnershipServiceMocked.getOwnershipState(Matchers.eq(entity))).thenReturn(Optional
82 .of(new EntityOwnershipState(false, true)));
83 clusterNodeModulSwitcherImpl.switchModuleByEntityOwnership();
86 private void ownershipChanged(boolean isOwner) {
87 final EntityOwnershipChange entityOwnershipChangeMock = Mockito.mock(EntityOwnershipChange.class);
88 clusterNodeModulSwitcherImpl.setModule(module);
89 Mockito.when(entityOwnershipChangeMock.isOwner()).thenReturn(isOwner);
90 clusterNodeModulSwitcherImpl.ownershipChanged(entityOwnershipChangeMock);