Bug 5596 Created lifecycle service
[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(() -> {
77             LOG.info("Starting thread 1");
78             Assert.assertTrue(roleContext.initialization());
79         });
80
81         Thread t2 = new Thread(() -> {
82             LOG.info("Starting thread 2");
83             Assert.assertFalse(roleContext.initialization());
84         });
85
86         t1.start();
87         LOG.info("Sleeping main thread for 1s to prevent race condition.");
88         Thread.sleep(1000L);
89         t2.start();
90
91         while (t2.isAlive()) {
92             //Waiting
93         }
94
95     }
96
97     @Test
98     public void testTermination() throws Exception {
99         roleContext.registerCandidate(entity);
100         roleContext.registerCandidate(txEntity);
101         Assert.assertTrue(roleContext.isMainCandidateRegistered());
102         Assert.assertTrue(roleContext.isTxCandidateRegistered());
103         roleContext.unregisterAllCandidates();
104         Assert.assertFalse(roleContext.isMainCandidateRegistered());
105     }
106
107     @Test
108     public void testCreateRequestContext() throws Exception {
109         roleContext.createRequestContext();
110         Mockito.verify(conductor).reserveXidForDeviceMessage(deviceInfo);
111     }
112
113     @Test(expected = NullPointerException.class)
114     public void testSetSalRoleService() throws Exception {
115         roleContext.setSalRoleService(null);
116     }
117
118     @Test
119     public void testGetEntity() throws Exception {
120         Assert.assertTrue(roleContext.getEntity().equals(entity));
121     }
122
123     @Test
124     public void testGetTxEntity() throws Exception {
125         Assert.assertTrue(roleContext.getTxEntity().equals(txEntity));
126     }
127
128     @Test
129     public void testGetNodeId() throws Exception {
130         Assert.assertTrue(roleContext.getDeviceInfo().getNodeId().equals(nodeId));
131     }
132
133     @Test
134     public void testIsMaster() throws Exception {
135         Assert.assertTrue(roleContext.initialization());
136         Assert.assertFalse(roleContext.isMaster());
137         Assert.assertTrue(roleContext.registerCandidate(txEntity));
138         Assert.assertTrue(roleContext.isMaster());
139         Assert.assertTrue(roleContext.unregisterCandidate(entity));
140         Assert.assertFalse(roleContext.isMaster());
141     }
142 }