Merge "Add NodeConfiguratorImpl enqueue trace"
[openflowplugin.git] / applications / forwardingrules-manager / src / test / java / org / opendaylight / openflowplugin / applications / frm / impl / DeviceMastershipManagerTest.java
1 /*
2  * Copyright (c) 2016, 2017 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 package org.opendaylight.openflowplugin.applications.frm.impl;
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.binding.api.DataBroker;
19 import org.opendaylight.mdsal.binding.api.RpcProviderService;
20 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
21 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
22 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
23 import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeServiceManager;
24 import org.opendaylight.openflowplugin.applications.frm.FlowNodeReconciliation;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.frm.reconciliation.service.rev180227.FrmReconciliationService;
27
28 /**
29  * Test for {@link DeviceMastershipManager}.
30  */
31 @RunWith(MockitoJUnitRunner.class)
32 public class DeviceMastershipManagerTest {
33     private DeviceMastershipManager deviceMastershipManager;
34     @Mock
35     private ClusterSingletonServiceRegistration registration;
36     @Mock
37     private ClusterSingletonServiceProvider clusterSingletonService;
38     @Mock
39     private FlowNodeReconciliation reconciliationAgent;
40     @Mock
41     private DataBroker dataBroker;
42     @Mock
43     private MastershipChangeServiceManager mastershipChangeServiceManager;
44     @Mock
45     private DeviceInfo deviceInfo;
46     @Mock
47     private NodeId nodeId;
48     @Mock
49     private RpcProviderService rpcProviderService;
50     @Mock
51     private FrmReconciliationService reconciliationService;
52
53     @Before
54     public void setUp() {
55         deviceMastershipManager = new DeviceMastershipManager(clusterSingletonService, reconciliationAgent, dataBroker,
56                 mastershipChangeServiceManager, rpcProviderService, reconciliationService);
57         Mockito.lenient().when(clusterSingletonService
58                 .registerClusterSingletonService(ArgumentMatchers.any()))
59                 .thenReturn(registration);
60         Mockito.when(deviceInfo.getNodeId()).thenReturn(nodeId);
61         Mockito.when(nodeId.getValue()).thenReturn("dummyValue");
62     }
63
64     @Test
65     public void testOnDeviceConnectedAndDisconnected() {
66         // no context
67         Assert.assertNull(deviceMastershipManager.getDeviceMasterships().get(deviceInfo.getNodeId()));
68         deviceMastershipManager.onBecomeOwner(deviceInfo);
69         DeviceMastership serviceInstance = deviceMastershipManager.getDeviceMasterships().get(deviceInfo.getNodeId());
70         Assert.assertNotNull(serviceInstance);
71         // destroy context - unregister
72         Assert.assertNotNull(deviceMastershipManager.getDeviceMasterships().get(deviceInfo.getNodeId()));
73         deviceMastershipManager.onLoseOwnership(deviceInfo);
74         Assert.assertNull(deviceMastershipManager.getDeviceMasterships().get(deviceInfo.getNodeId()));
75     }
76
77     @Test
78     public void testIsDeviceMasteredOrSlaved() {
79         // no context
80         Assert.assertFalse(deviceMastershipManager.isDeviceMastered(deviceInfo.getNodeId()));
81         deviceMastershipManager.onBecomeOwner(deviceInfo);
82         // is master
83         deviceMastershipManager.getDeviceMasterships().get(deviceInfo.getNodeId()).instantiateServiceInstance();
84         Assert.assertTrue(deviceMastershipManager.isDeviceMastered(deviceInfo.getNodeId()));
85         // is not master
86         deviceMastershipManager.getDeviceMasterships().get(deviceInfo.getNodeId()).closeServiceInstance();
87         Assert.assertFalse(deviceMastershipManager.isDeviceMastered(deviceInfo.getNodeId()));
88     }
89 }