ClusterSingletonService cleaning FRM/FRS
[openflowplugin.git] / applications / forwardingrules-sync / src / test / java / org / opendaylight / openflowplugin / applications / frsync / impl / clustering / DeviceMastershipManagerTest.java
1 /**
2  * Copyright (c) 2016 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
9 package org.opendaylight.openflowplugin.applications.frsync.impl.clustering;
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.openflowplugin.applications.frsync.util.ReconciliationRegistry;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
24
25 /**
26  * Test for {@link DeviceMastershipManager}.
27  */
28 @RunWith(MockitoJUnitRunner.class)
29 public class DeviceMastershipManagerTest {
30     private static final NodeId NODE_ID = new NodeId("testNode");
31     private DeviceMastershipManager deviceMastershipManager;
32     @Mock
33     private ClusterSingletonServiceRegistration registration;
34     @Mock
35     private ClusterSingletonServiceProvider clusterSingletonService;
36
37     @Before
38     public void setUp() throws Exception {
39         deviceMastershipManager = new DeviceMastershipManager(clusterSingletonService, new ReconciliationRegistry());
40         Mockito.when(clusterSingletonService.registerClusterSingletonService(Matchers.<ClusterSingletonService>any()))
41                 .thenReturn(registration);
42     }
43
44     @Test
45     public void testOnDeviceConnectedAndDisconnected() throws Exception {
46         // no context
47         Assert.assertNull(deviceMastershipManager.getDeviceMasterships().get(NODE_ID));
48         // create context - register
49         deviceMastershipManager.onDeviceConnected(NODE_ID);
50         DeviceMastership serviceInstance = deviceMastershipManager.getDeviceMasterships().get(NODE_ID);
51         Assert.assertNotNull(serviceInstance);
52         Mockito.verify(clusterSingletonService).registerClusterSingletonService(serviceInstance);
53         // destroy context - unregister
54         deviceMastershipManager.onDeviceDisconnected(NODE_ID);
55         Assert.assertNull(deviceMastershipManager.getDeviceMasterships().get(NODE_ID));
56         Mockito.verify(registration).close();
57     }
58
59     @Test
60     public void testIsDeviceMasteredOrSlaved() {
61         // no context
62         Assert.assertFalse(deviceMastershipManager.isDeviceMastered(NODE_ID));
63         deviceMastershipManager.onDeviceConnected(NODE_ID);
64         // is master
65         deviceMastershipManager.getDeviceMasterships().get(NODE_ID).instantiateServiceInstance();
66         Assert.assertTrue(deviceMastershipManager.isDeviceMastered(NODE_ID));
67         // is not master
68         deviceMastershipManager.getDeviceMasterships().get(NODE_ID).closeServiceInstance();
69         Assert.assertFalse(deviceMastershipManager.isDeviceMastered(NODE_ID));
70     }
71
72 }