Use ByteBuf.readRetainedSlice()
[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.api.ClusterSingletonServiceProvider;
21 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
22 import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeServiceManager;
23 import org.opendaylight.openflowplugin.applications.frm.FlowNodeReconciliation;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
25 import org.opendaylight.yangtools.concepts.Registration;
26
27 /**
28  * Test for {@link DeviceMastershipManager}.
29  */
30 @RunWith(MockitoJUnitRunner.class)
31 public class DeviceMastershipManagerTest {
32     private DeviceMastershipManager deviceMastershipManager;
33     @Mock
34     private Registration registration;
35     @Mock
36     private ClusterSingletonServiceProvider clusterSingletonService;
37     @Mock
38     private FlowNodeReconciliation reconciliationAgent;
39     @Mock
40     private DataBroker dataBroker;
41     @Mock
42     private MastershipChangeServiceManager mastershipChangeServiceManager;
43     @Mock
44     private DeviceInfo deviceInfo;
45     @Mock
46     private NodeId nodeId;
47     @Mock
48     private RpcProviderService rpcProviderService;
49
50     @Before
51     public void setUp() {
52         deviceMastershipManager = new DeviceMastershipManager(reconciliationAgent, dataBroker,
53                 mastershipChangeServiceManager, rpcProviderService);
54         Mockito.lenient().when(clusterSingletonService
55                 .registerClusterSingletonService(ArgumentMatchers.any()))
56                 .thenReturn(registration);
57         Mockito.when(deviceInfo.getNodeId()).thenReturn(nodeId);
58         Mockito.when(nodeId.getValue()).thenReturn("dummyValue");
59     }
60
61     @Test
62     public void testOnDeviceConnectedAndDisconnected() {
63         // no context
64         Assert.assertNull(deviceMastershipManager.getDeviceMasterships().get(deviceInfo.getNodeId()));
65         deviceMastershipManager.onBecomeOwner(deviceInfo);
66         DeviceMastership serviceInstance = deviceMastershipManager.getDeviceMasterships().get(deviceInfo.getNodeId());
67         Assert.assertNotNull(serviceInstance);
68         // destroy context - unregister
69         Assert.assertNotNull(deviceMastershipManager.getDeviceMasterships().get(deviceInfo.getNodeId()));
70         deviceMastershipManager.onLoseOwnership(deviceInfo);
71         Assert.assertNull(deviceMastershipManager.getDeviceMasterships().get(deviceInfo.getNodeId()));
72     }
73
74     @Test
75     public void testIsDeviceMasteredOrSlaved() {
76         // no context
77         Assert.assertFalse(deviceMastershipManager.isDeviceMastered(deviceInfo.getNodeId()));
78         deviceMastershipManager.onBecomeOwner(deviceInfo);
79         // is master
80         deviceMastershipManager.getDeviceMasterships().get(deviceInfo.getNodeId()).instantiateServiceInstance();
81         Assert.assertTrue(deviceMastershipManager.isDeviceMastered(deviceInfo.getNodeId()));
82         // is not master
83         deviceMastershipManager.getDeviceMasterships().get(deviceInfo.getNodeId()).closeServiceInstance();
84         Assert.assertFalse(deviceMastershipManager.isDeviceMastered(deviceInfo.getNodeId()));
85     }
86 }