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.role.RoleContext;
25 import org.opendaylight.openflowplugin.api.openflow.role.RoleManager;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * @author Jozef Bacigal
35 @RunWith(MockitoJUnitRunner.class)
36 public class RoleContextImplTest {
38 private static final Logger LOG = LoggerFactory.getLogger(RoleContextImpl.class);
41 private EntityOwnershipService entityOwnershipService;
44 private EntityOwnershipCandidateRegistration entityOwnershipCandidateRegistration;
46 private final NodeId nodeId = NodeId.getDefaultInstance("openflow:1");
47 private final Entity entity = new Entity(RoleManager.ENTITY_TYPE, nodeId.getValue());
48 private final Entity txEntity = new Entity(RoleManager.TX_ENTITY_TYPE, nodeId.getValue());
49 private RoleContext roleContext;
52 public void setup() throws CandidateAlreadyRegisteredException {
53 roleContext = new RoleContextImpl(nodeId, entityOwnershipService, entity, txEntity);
54 Mockito.when(entityOwnershipService.registerCandidate(entity)).thenReturn(entityOwnershipCandidateRegistration);
55 Mockito.when(entityOwnershipService.registerCandidate(txEntity)).thenReturn(entityOwnershipCandidateRegistration);
59 // Run this test only if demanded because it takes 15s to run
60 public void testInitializationThreads() throws Exception {
62 /*Setting answer which will hold the answer for 5s*/
63 Mockito.when(entityOwnershipService.registerCandidate(entity)).thenAnswer(new Answer<EntityOwnershipService>() {
65 public EntityOwnershipService answer(final InvocationOnMock invocationOnMock) throws Throwable {
66 LOG.info("Sleeping this thread for 14s");
72 Thread t1 = new Thread(new Runnable() {
75 LOG.info("Starting thread 1");
76 Assert.assertTrue(roleContext.initialization());
80 Thread t2 = new Thread(new Runnable() {
83 LOG.info("Starting thread 2");
84 Assert.assertFalse(roleContext.initialization());
89 LOG.info("Sleeping main thread for 1s to prevent race condition.");
93 while (t2.isAlive()) {
100 public void testTermination() throws Exception {
101 roleContext.registerCandidate(entity);
102 roleContext.registerCandidate(txEntity);
103 Assert.assertTrue(roleContext.isMainCandidateRegistered());
104 Assert.assertTrue(roleContext.isTxCandidateRegistered());
105 roleContext.unregisterAllCandidates();
106 Assert.assertFalse(roleContext.isMainCandidateRegistered());
110 public void testCreateRequestContext() throws Exception {
114 @Test(expected = NullPointerException.class)
115 public void testSetSalRoleService() throws Exception {
116 roleContext.setSalRoleService(null);
120 public void testGetEntity() throws Exception {
121 Assert.assertTrue(roleContext.getEntity().equals(entity));
125 public void testGetTxEntity() throws Exception {
126 Assert.assertTrue(roleContext.getTxEntity().equals(txEntity));
130 public void testGetNodeId() throws Exception {
131 Assert.assertTrue(roleContext.getNodeId().equals(nodeId));
135 public void testIsMaster() throws Exception {
136 Assert.assertTrue(roleContext.initialization());
137 Assert.assertFalse(roleContext.isMaster());
138 Assert.assertTrue(roleContext.registerCandidate(txEntity));
139 Assert.assertTrue(roleContext.isMaster());
140 Assert.assertTrue(roleContext.unregisterCandidate(entity));
141 Assert.assertFalse(roleContext.isMaster());