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
8 package org.opendaylight.openflowplugin.applications.frsync.impl.clustering;
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;
24 * Test for {@link DeviceMastershipManager}.
26 @RunWith(MockitoJUnitRunner.class)
27 public class DeviceMastershipManagerTest {
28 private static final NodeId NODE_ID = new NodeId("testNode");
29 private DeviceMastershipManager deviceMastershipManager;
31 private Registration registration;
33 private ClusterSingletonServiceProvider clusterSingletonService;
37 deviceMastershipManager = new DeviceMastershipManager(clusterSingletonService, new ReconciliationRegistry());
38 Mockito.when(clusterSingletonService
39 .registerClusterSingletonService(ArgumentMatchers.any()))
40 .thenReturn(registration);
44 public void testOnDeviceConnectedAndDisconnected() {
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));