36385b3c145344d73f378479a988a423098736c5
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / role / RoleContextImplTest.java
1 /**
2  * Copyright (c) 2015 Cisco 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.openflowplugin.impl.role;
9
10
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;
31
32 @RunWith(MockitoJUnitRunner.class)
33 public class RoleContextImplTest {
34
35     private static final Logger LOG = LoggerFactory.getLogger(RoleContextImpl.class);
36
37     @Mock
38     private EntityOwnershipService entityOwnershipService;
39
40     @Mock
41     private EntityOwnershipCandidateRegistration entityOwnershipCandidateRegistration;
42
43     @Mock
44     private LifecycleConductor conductor;
45
46     @Mock
47     private DeviceInfo deviceInfo;
48
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;
53
54     @Before
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);
60     }
61
62     //@Test
63     //Run this test only if demanded because it takes 15s to run
64     public void testInitializationThreads() throws Exception {
65
66         /*Setting answer which will hold the answer for 5s*/
67         Mockito.when(entityOwnershipService.registerCandidate(entity)).thenAnswer(new Answer<EntityOwnershipService>() {
68             @Override
69             public EntityOwnershipService answer(final InvocationOnMock invocationOnMock) throws Throwable {
70                 LOG.info("Sleeping this thread for 14s");
71                 Thread.sleep(14000L);
72                 return null;
73             }
74         });
75
76         Thread t1 = new Thread(new Runnable() {
77             @Override
78             public void run() {
79                 LOG.info("Starting thread 1");
80                 Assert.assertTrue(roleContext.initialization());
81             }
82         });
83
84         Thread t2 = new Thread(new Runnable() {
85             @Override
86             public void run() {
87                 LOG.info("Starting thread 2");
88                 Assert.assertFalse(roleContext.initialization());
89             }
90         });
91
92         t1.start();
93         LOG.info("Sleeping main thread for 1s to prevent race condition.");
94         Thread.sleep(1000L);
95         t2.start();
96
97         while (t2.isAlive()) {
98             //Waiting
99         }
100
101     }
102
103     @Test
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());
111     }
112
113     @Test
114     public void testCreateRequestContext() throws Exception {
115         roleContext.createRequestContext();
116         Mockito.verify(conductor).reserveXidForDeviceMessage(nodeId);
117     }
118
119     @Test(expected = NullPointerException.class)
120     public void testSetSalRoleService() throws Exception {
121         roleContext.setSalRoleService(null);
122     }
123
124     @Test
125     public void testGetEntity() throws Exception {
126         Assert.assertTrue(roleContext.getEntity().equals(entity));
127     }
128
129     @Test
130     public void testGetTxEntity() throws Exception {
131         Assert.assertTrue(roleContext.getTxEntity().equals(txEntity));
132     }
133
134     @Test
135     public void testGetNodeId() throws Exception {
136         Assert.assertTrue(roleContext.getDeviceInfo().getNodeId().equals(nodeId));
137     }
138
139     @Test
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());
147     }
148 }