OPNFLWPLUG-1032: Neon-MRI: Bump odlparent, yangtools, mdsal
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / role / RoleContextImplTest.java
index 4234aa992a005ad44e3934312d2cb73b766eec7c..fa491d38ce8ad3f9be27aad91112e23c09d91215 100644 (file)
-/**
- * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
+/*
+ * Copyright (c) 2017 Pantheon Technologies s.r.o. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
+
 package org.opendaylight.openflowplugin.impl.role;
 
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
 
 import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
 import io.netty.util.HashedWheelTimer;
-import org.junit.Assert;
+import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Mock;
-import org.mockito.Mockito;
 import org.mockito.runners.MockitoJUnitRunner;
-import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
 import org.opendaylight.openflowplugin.api.OFConstants;
+import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
-import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleService;
+import org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChainMastershipState;
+import org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChainMastershipWatcher;
 import org.opendaylight.openflowplugin.api.openflow.role.RoleContext;
-import org.opendaylight.openflowplugin.api.openflow.role.RoleManager;
 import org.opendaylight.openflowplugin.impl.util.DeviceStateUtil;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.OpenflowProviderConfig;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SalRoleService;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleInputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleOutput;
+import org.opendaylight.yangtools.yang.common.RpcResult;
 
 @RunWith(MockitoJUnitRunner.class)
 public class RoleContextImplTest {
-
     @Mock
-    HashedWheelTimer hashedWheelTimer;
+    private SalRoleService roleService;
+    @Mock
+    private ContextChainMastershipWatcher contextChainMastershipWatcher;
     @Mock
     private DeviceInfo deviceInfo;
     @Mock
-    private RoleManager roleManager;
+    private DeviceContext deviceContext;
     @Mock
-    private LifecycleService lifecycleService;
+    private ListenableFuture<RpcResult<SetRoleOutput>> setRoleFuture;
     @Mock
-    private SalRoleService salRoleService;
-
-    private final NodeId nodeId = NodeId.getDefaultInstance("openflow:1");
+    private OpenflowProviderConfig config;
     private RoleContext roleContext;
-    private RoleContextImpl roleContextSpy;
 
     @Before
-    public void setup() throws CandidateAlreadyRegisteredException {
-        roleContext = new RoleContextImpl(deviceInfo, hashedWheelTimer, roleManager, lifecycleService);
-        roleContext.setSalRoleService(salRoleService);
-        Mockito.when(deviceInfo.getNodeId()).thenReturn(nodeId);
-        Mockito.when(salRoleService.setRole(Mockito.<SetRoleInput>any())).thenReturn(Futures.immediateFuture(null));
-        Mockito.when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(DeviceStateUtil.createNodeInstanceIdentifier(nodeId));
-        roleContextSpy = Mockito.spy((RoleContextImpl) roleContext);
-    }
-
-    @Test
-    public void testCreateRequestContext() throws Exception {
-        roleContext.createRequestContext();
-        Mockito.verify(deviceInfo).reserveXidForDeviceMessage();
-    }
-
-    @Test(expected = NullPointerException.class)
-    public void testSetSalRoleService() throws Exception {
-        roleContext.setSalRoleService(null);
-    }
-
-    @Test
-    public void testGetNodeId() throws Exception {
-        Assert.assertTrue(roleContext.getDeviceInfo().getNodeId().equals(nodeId));
-    }
-
-    @Test
-    public void startupClusterServices() throws Exception {
-        roleContextSpy.startupClusterServices();
-        Mockito.verify(roleContextSpy).sendRoleChangeToDevice(OfpRole.BECOMEMASTER);
+    public void setUp() throws Exception {
+        when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(DeviceStateUtil
+                .createNodeInstanceIdentifier(new NodeId("openflow:1")));
+        when(deviceInfo.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
+        when(roleService.setRole(any())).thenReturn(Futures.immediateFuture(null));
+
+        roleContext = new RoleContextImpl(deviceInfo, new HashedWheelTimer(), 20000, config);
+        roleContext.registerMastershipWatcher(contextChainMastershipWatcher);
+        roleContext.setRoleService(roleService);
     }
 
-    @Test
-    public void startupClusterServicesVersion10() throws Exception {
-        Mockito.when(deviceInfo.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_0);
-        roleContextSpy.startupClusterServices();
-        Mockito.verify(roleContextSpy).sendRoleChangeToDevice(OfpRole.BECOMEMASTER);
-    }
-
-    @Test
-    public void startupClusterServicesVersion13() throws Exception {
-        Mockito.when(deviceInfo.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
-        roleContextSpy.startupClusterServices();
-        Mockito.verify(roleContextSpy).sendRoleChangeToDevice(OfpRole.BECOMEMASTER);
-    }
-
-    @Test
-    public void stopClusterServicesNotDisconnected() throws Exception {
-        roleContextSpy.stopClusterServices(false);
-        Mockito.verify(roleContextSpy).sendRoleChangeToDevice(OfpRole.BECOMESLAVE);
-        Mockito.verify(roleManager, Mockito.never()).removeDeviceFromOperationalDS(Mockito.<DeviceInfo>any());
+    @After
+    public void tearDown() throws Exception {
+        roleContext.close();
     }
 
     @Test
-    public void stopClusterServicesDisconnected() throws Exception {
-        roleContextSpy.stopClusterServices(true);
-        Mockito.verify(roleManager, Mockito.atLeastOnce()).removeDeviceFromOperationalDS(Mockito.<DeviceInfo>any());
+    public void instantiateServiceInstance() throws Exception {
+        roleContext.instantiateServiceInstance();
+        verify(roleService).setRole(new SetRoleInputBuilder()
+                .setControllerRole(OfpRole.BECOMEMASTER)
+                .setNode(new NodeRef(deviceInfo.getNodeInstanceIdentifier()))
+                .build());
+        verify(contextChainMastershipWatcher).onMasterRoleAcquired(
+                deviceInfo,
+                ContextChainMastershipState.MASTER_ON_DEVICE);
     }
 
     @Test
-    public void makeDeviceSlave() throws Exception {
-        roleContextSpy.makeDeviceSlave();
-        Mockito.verify(roleContextSpy).sendRoleChangeToDevice(OfpRole.BECOMESLAVE);
+    public void closeServiceInstance() throws Exception {
+        when(setRoleFuture.isCancelled()).thenReturn(false);
+        when(setRoleFuture.isDone()).thenReturn(false);
+        when(roleService.setRole(any())).thenReturn(setRoleFuture);
+        roleContext.instantiateServiceInstance();
+        roleContext.closeServiceInstance().get();
+        verify(setRoleFuture).cancel(true);
     }
 
 }