5a6e1055206f02699f8043458a5f224c03817583
[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 package org.opendaylight.openflowplugin.applications.frsync.impl.clustering;
9
10 import org.junit.Assert;
11 import org.junit.Before;
12 import org.junit.Test;
13 import org.junit.runner.RunWith;
14 import org.mockito.ArgumentMatchers;
15 import org.mockito.Mock;
16 import org.mockito.Mockito;
17 import org.mockito.junit.MockitoJUnitRunner;
18 import org.opendaylight.mdsal.singleton.api.ClusterSingletonServiceProvider;
19 import org.opendaylight.openflowplugin.applications.frsync.util.ReconciliationRegistry;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
21 import org.opendaylight.yangtools.concepts.Registration;
22
23 /**
24  * Test for {@link DeviceMastershipManager}.
25  */
26 @RunWith(MockitoJUnitRunner.class)
27 public class DeviceMastershipManagerTest {
28     private static final NodeId NODE_ID = new NodeId("testNode");
29     private DeviceMastershipManager deviceMastershipManager;
30     @Mock
31     private Registration registration;
32     @Mock
33     private ClusterSingletonServiceProvider clusterSingletonService;
34
35     @Before
36     public void setUp() {
37         deviceMastershipManager = new DeviceMastershipManager(clusterSingletonService, new ReconciliationRegistry());
38         Mockito.when(clusterSingletonService
39                 .registerClusterSingletonService(ArgumentMatchers.any()))
40                 .thenReturn(registration);
41     }
42
43     @Test
44     public void testOnDeviceConnectedAndDisconnected() {
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 }