OPNFLWPLUG-1032: Neon-MRI: Bump odlparent, yangtools, mdsal
[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.ArgumentMatchers;
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
41                 .registerClusterSingletonService(ArgumentMatchers.<ClusterSingletonService>any()))
42                 .thenReturn(registration);
43     }
44
45     @Test
46     public void testOnDeviceConnectedAndDisconnected() throws Exception {
47         // no context
48         Assert.assertNull(deviceMastershipManager.getDeviceMasterships().get(NODE_ID));
49         // create context - register
50         deviceMastershipManager.onDeviceConnected(NODE_ID);
51         DeviceMastership serviceInstance = deviceMastershipManager.getDeviceMasterships().get(NODE_ID);
52         Assert.assertNotNull(serviceInstance);
53         Mockito.verify(clusterSingletonService).registerClusterSingletonService(serviceInstance);
54         // destroy context - unregister
55         deviceMastershipManager.onDeviceDisconnected(NODE_ID);
56         Assert.assertNull(deviceMastershipManager.getDeviceMasterships().get(NODE_ID));
57         Mockito.verify(registration).close();
58     }
59
60     @Test
61     public void testIsDeviceMasteredOrSlaved() {
62         // no context
63         Assert.assertFalse(deviceMastershipManager.isDeviceMastered(NODE_ID));
64         deviceMastershipManager.onDeviceConnected(NODE_ID);
65         // is master
66         deviceMastershipManager.getDeviceMasterships().get(NODE_ID).instantiateServiceInstance();
67         Assert.assertTrue(deviceMastershipManager.isDeviceMastered(NODE_ID));
68         // is not master
69         deviceMastershipManager.getDeviceMasterships().get(NODE_ID).closeServiceInstance();
70         Assert.assertFalse(deviceMastershipManager.isDeviceMastered(NODE_ID));
71     }
72
73 }