Bug 6110: Fixed bugs in statistics manager due to race condition.
[openflowplugin.git] / applications / forwardingrules-manager / src / test / java / org / opendaylight / openflowplugin / applications / frm / impl / DeviceMastershipManagerTest.java
1 /**
2  * Copyright (c) 2016 Pantheon Technologies s.r.o. 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
9 package org.opendaylight.openflowplugin.applications.frm.impl;
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.Matchers;
16 import org.mockito.Mock;
17 import org.mockito.Mockito;
18 import org.mockito.runners.MockitoJUnitRunner;
19 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
20 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
21 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
23
24 /**
25  * Test for {@link DeviceMastershipManager}.
26  */
27 @RunWith(MockitoJUnitRunner.class)
28 public class DeviceMastershipManagerTest {
29     private static final NodeId NODE_ID = new NodeId("testNode");
30     private DeviceMastershipManager deviceMastershipManager;
31     @Mock
32     private ClusterSingletonServiceRegistration registration;
33     @Mock
34     private ClusterSingletonServiceProvider clusterSingletonService;
35
36     @Before
37     public void setUp() throws Exception {
38         deviceMastershipManager = new DeviceMastershipManager(clusterSingletonService);
39         Mockito.when(clusterSingletonService.registerClusterSingletonService(Matchers.<ClusterSingletonService>any()))
40                 .thenReturn(registration);
41     }
42
43     @Test
44     public void testOnDeviceConnectedAndDisconnected() throws Exception {
45         // no context
46         Assert.assertNull(deviceMastershipManager.getDeviceMasterships().get(NODE_ID));
47         // create context - register
48         deviceMastershipManager.onDeviceConnected(NODE_ID);
49         DeviceMastership serviceInstance = deviceMastershipManager.getDeviceMasterships().get(NODE_ID);
50         Assert.assertNotNull(serviceInstance);
51         Mockito.verify(clusterSingletonService).registerClusterSingletonService(serviceInstance);
52         // destroy context - unregister
53         deviceMastershipManager.onDeviceDisconnected(NODE_ID);
54         Assert.assertNull(deviceMastershipManager.getDeviceMasterships().get(NODE_ID));
55         Mockito.verify(registration).close();
56     }
57
58     @Test
59     public void testIsDeviceMasteredOrSlaved() {
60         // no context
61         Assert.assertFalse(deviceMastershipManager.isDeviceMastered(NODE_ID));
62         deviceMastershipManager.onDeviceConnected(NODE_ID);
63         // is master
64         deviceMastershipManager.getDeviceMasterships().get(NODE_ID).instantiateServiceInstance();
65         Assert.assertTrue(deviceMastershipManager.isDeviceMastered(NODE_ID));
66         // is not master
67         deviceMastershipManager.getDeviceMasterships().get(NODE_ID).closeServiceInstance();
68         Assert.assertFalse(deviceMastershipManager.isDeviceMastered(NODE_ID));
69     }
70
71 }