2 * Copyright (c) 2016 Pantheon Technologies s.r.o. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.applications.frm.impl;
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.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
25 * Test for {@link DeviceMastershipManager}.
27 @RunWith(MockitoJUnitRunner.class)
28 public class DeviceMastershipManagerTest {
29 private static final NodeId NODE_ID = new NodeId("testNode");
30 private DeviceMastershipManager deviceMastershipManager;
32 private ClusterSingletonServiceRegistration registration;
34 private ClusterSingletonServiceProvider clusterSingletonService;
37 public void setUp() throws Exception {
38 deviceMastershipManager = new DeviceMastershipManager(clusterSingletonService);
39 Mockito.when(clusterSingletonService.registerClusterSingletonService(Matchers.<ClusterSingletonService>any()))
40 .thenReturn(registration);
44 public void testOnDeviceConnectedAndDisconnected() throws Exception {
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();
59 public void testIsDeviceMasteredOrSlaved() {
61 Assert.assertFalse(deviceMastershipManager.isDeviceMastered(NODE_ID));
62 deviceMastershipManager.onDeviceConnected(NODE_ID);
64 deviceMastershipManager.getDeviceMasterships().get(NODE_ID).instantiateServiceInstance();
65 Assert.assertTrue(deviceMastershipManager.isDeviceMastered(NODE_ID));
67 deviceMastershipManager.getDeviceMasterships().get(NODE_ID).closeServiceInstance();
68 Assert.assertFalse(deviceMastershipManager.isDeviceMastered(NODE_ID));