2 * Copyright (c) 2016 Cisco Systems, Inc. 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.frsync.impl.clustering;
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;
26 * Test for {@link DeviceMastershipManager}.
28 @RunWith(MockitoJUnitRunner.class)
29 public class DeviceMastershipManagerTest {
30 private static final NodeId NODE_ID = new NodeId("testNode");
31 private DeviceMastershipManager deviceMastershipManager;
33 private ClusterSingletonServiceRegistration registration;
35 private ClusterSingletonServiceProvider clusterSingletonService;
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);
46 public void testOnDeviceConnectedAndDisconnected() throws Exception {
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();
61 public void testIsDeviceMasteredOrSlaved() {
63 Assert.assertFalse(deviceMastershipManager.isDeviceMastered(NODE_ID));
64 deviceMastershipManager.onDeviceConnected(NODE_ID);
66 deviceMastershipManager.getDeviceMasterships().get(NODE_ID).instantiateServiceInstance();
67 Assert.assertTrue(deviceMastershipManager.isDeviceMastered(NODE_ID));
69 deviceMastershipManager.getDeviceMasterships().get(NODE_ID).closeServiceInstance();
70 Assert.assertFalse(deviceMastershipManager.isDeviceMastered(NODE_ID));