Bug-5523 RoleManager changes
[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.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;
29
30 /**
31  * @author Jozef Bacigal
32  * Date: 4/19/16
33  * Time: 12:56
34  */
35 @RunWith(MockitoJUnitRunner.class)
36 public class RoleContextImplTest {
37
38     private static final Logger LOG = LoggerFactory.getLogger(RoleContextImpl.class);
39
40     @Mock
41     private EntityOwnershipService entityOwnershipService;
42
43     @Mock
44     private EntityOwnershipCandidateRegistration entityOwnershipCandidateRegistration;
45
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;
50
51     @Before
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);
56     }
57
58 //  @Test
59 //  Run this test only if demanded because it takes 15s to run
60     public void testInitializationThreads() throws Exception {
61
62         /*Setting answer which will hold the answer for 5s*/
63         Mockito.when(entityOwnershipService.registerCandidate(entity)).thenAnswer(new Answer<EntityOwnershipService>() {
64             @Override
65             public EntityOwnershipService answer(final InvocationOnMock invocationOnMock) throws Throwable {
66                 LOG.info("Sleeping this thread for 14s");
67                 Thread.sleep(14000L);
68                 return null;
69             }
70         });
71
72         Thread t1 = new Thread(new Runnable() {
73             @Override
74             public void run() {
75                 LOG.info("Starting thread 1");
76                 Assert.assertTrue(roleContext.initialization());
77             }
78         });
79
80         Thread t2 = new Thread(new Runnable() {
81             @Override
82             public void run() {
83                 LOG.info("Starting thread 2");
84                 Assert.assertFalse(roleContext.initialization());
85             }
86         });
87
88         t1.start();
89         LOG.info("Sleeping main thread for 1s to prevent race condition.");
90         Thread.sleep(1000L);
91         t2.start();
92
93         while (t2.isAlive()) {
94             //Waiting
95         }
96
97     }
98
99     @Test
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());
107     }
108
109     @Test
110     public void testCreateRequestContext() throws Exception {
111
112     }
113
114     @Test(expected = NullPointerException.class)
115     public void testSetSalRoleService() throws Exception {
116         roleContext.setSalRoleService(null);
117     }
118
119     @Test
120     public void testGetEntity() throws Exception {
121         Assert.assertTrue(roleContext.getEntity().equals(entity));
122     }
123
124     @Test
125     public void testGetTxEntity() throws Exception {
126         Assert.assertTrue(roleContext.getTxEntity().equals(txEntity));
127     }
128
129     @Test
130     public void testGetNodeId() throws Exception {
131         Assert.assertTrue(roleContext.getNodeId().equals(nodeId));
132     }
133
134     @Test
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());
142     }
143 }