2 * Copyright (c) 2015 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.openflowplugin.impl.role;
11 import org.junit.Assert;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.junit.runner.RunWith;
15 import org.mockito.Mock;
16 import org.mockito.Mockito;
17 import org.mockito.invocation.InvocationOnMock;
18 import org.mockito.runners.MockitoJUnitRunner;
19 import org.mockito.stubbing.Answer;
20 import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
21 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
22 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
23 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
24 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
25 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleConductor;
26 import org.opendaylight.openflowplugin.api.openflow.role.RoleContext;
27 import org.opendaylight.openflowplugin.api.openflow.role.RoleManager;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 @RunWith(MockitoJUnitRunner.class)
33 public class RoleContextImplTest {
35 private static final Logger LOG = LoggerFactory.getLogger(RoleContextImpl.class);
38 private EntityOwnershipService entityOwnershipService;
41 private EntityOwnershipCandidateRegistration entityOwnershipCandidateRegistration;
44 private LifecycleConductor conductor;
47 private DeviceInfo deviceInfo;
49 private final NodeId nodeId = NodeId.getDefaultInstance("openflow:1");
50 private final Entity entity = new Entity(RoleManager.ENTITY_TYPE, nodeId.getValue());
51 private final Entity txEntity = new Entity(RoleManager.TX_ENTITY_TYPE, nodeId.getValue());
52 private RoleContext roleContext;
55 public void setup() throws CandidateAlreadyRegisteredException {
56 roleContext = new RoleContextImpl(deviceInfo, entityOwnershipService, entity, txEntity, conductor);
57 Mockito.when(entityOwnershipService.registerCandidate(entity)).thenReturn(entityOwnershipCandidateRegistration);
58 Mockito.when(entityOwnershipService.registerCandidate(txEntity)).thenReturn(entityOwnershipCandidateRegistration);
59 Mockito.when(deviceInfo.getNodeId()).thenReturn(nodeId);
63 //Run this test only if demanded because it takes 15s to run
64 public void testInitializationThreads() throws Exception {
66 /*Setting answer which will hold the answer for 5s*/
67 Mockito.when(entityOwnershipService.registerCandidate(entity)).thenAnswer(new Answer<EntityOwnershipService>() {
69 public EntityOwnershipService answer(final InvocationOnMock invocationOnMock) throws Throwable {
70 LOG.info("Sleeping this thread for 14s");
76 Thread t1 = new Thread(new Runnable() {
79 LOG.info("Starting thread 1");
80 Assert.assertTrue(roleContext.initialization());
84 Thread t2 = new Thread(new Runnable() {
87 LOG.info("Starting thread 2");
88 Assert.assertFalse(roleContext.initialization());
93 LOG.info("Sleeping main thread for 1s to prevent race condition.");
97 while (t2.isAlive()) {
104 public void testTermination() throws Exception {
105 roleContext.registerCandidate(entity);
106 roleContext.registerCandidate(txEntity);
107 Assert.assertTrue(roleContext.isMainCandidateRegistered());
108 Assert.assertTrue(roleContext.isTxCandidateRegistered());
109 roleContext.unregisterAllCandidates();
110 Assert.assertFalse(roleContext.isMainCandidateRegistered());
114 public void testCreateRequestContext() throws Exception {
115 roleContext.createRequestContext();
116 Mockito.verify(conductor).reserveXidForDeviceMessage(deviceInfo);
119 @Test(expected = NullPointerException.class)
120 public void testSetSalRoleService() throws Exception {
121 roleContext.setSalRoleService(null);
125 public void testGetEntity() throws Exception {
126 Assert.assertTrue(roleContext.getEntity().equals(entity));
130 public void testGetTxEntity() throws Exception {
131 Assert.assertTrue(roleContext.getTxEntity().equals(txEntity));
135 public void testGetNodeId() throws Exception {
136 Assert.assertTrue(roleContext.getDeviceInfo().getNodeId().equals(nodeId));
140 public void testIsMaster() throws Exception {
141 Assert.assertTrue(roleContext.initialization());
142 Assert.assertFalse(roleContext.isMaster());
143 Assert.assertTrue(roleContext.registerCandidate(txEntity));
144 Assert.assertTrue(roleContext.isMaster());
145 Assert.assertTrue(roleContext.unregisterCandidate(entity));
146 Assert.assertFalse(roleContext.isMaster());