X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=openflowplugin-impl%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fopenflowplugin%2Fimpl%2Frole%2FRoleContextImplTest.java;h=dc3b6dccc4b428ff0118a50739d985c83dc36f5b;hb=2578bbbc093ee26fa014ed2c115f6be76af096e2;hp=b604a8e49964aee60cf30dd1fb164834c68726b0;hpb=3d85e02f9b4b514c3aec92da8a9900b95ba9defb;p=openflowplugin.git diff --git a/openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/role/RoleContextImplTest.java b/openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/role/RoleContextImplTest.java index b604a8e499..dc3b6dccc4 100644 --- a/openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/role/RoleContextImplTest.java +++ b/openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/role/RoleContextImplTest.java @@ -7,200 +7,60 @@ */ package org.opendaylight.openflowplugin.impl.role; -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 com.google.common.util.concurrent.SettableFuture; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; +import io.netty.util.HashedWheelTimer; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.ArgumentMatcher; -import org.mockito.Matchers; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.runners.MockitoJUnitRunner; -import org.opendaylight.controller.md.sal.binding.api.DataBroker; import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException; -import org.opendaylight.controller.md.sal.common.api.clustering.Entity; -import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService; -import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry; -import org.opendaylight.openflowplugin.api.OFConstants; -import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext; -import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext; -import org.opendaylight.openflowplugin.api.openflow.device.DeviceState; +import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo; +import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleService; +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.inventory.rev130819.nodes.Node; -import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput; -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.SetRoleOutput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleOutputBuilder; -import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier; -import org.opendaylight.yangtools.yang.common.RpcResult; -import org.opendaylight.yangtools.yang.common.RpcResultBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; -/** - * Created by kramesha on 9/1/15. - */ @RunWith(MockitoJUnitRunner.class) public class RoleContextImplTest { - public static final int FUTURE_SAFETY_TIMEOUT = 5; - @Mock - private EntityOwnershipService entityOwnershipService; + private static final Logger LOG = LoggerFactory.getLogger(RoleContextImpl.class); @Mock - private DataBroker dataBroker; - + HashedWheelTimer hashedWheelTimer; @Mock - private RpcProviderRegistry rpcProviderRegistry; - + private DeviceInfo deviceInfo; @Mock - private DeviceContext deviceContext; - + private RoleManager roleManager; @Mock - private ConnectionContext connectionContext; - - @Mock - private DeviceState deviceState; - - @Mock - private SalRoleService salRoleService; - - @Mock - private GetFeaturesOutput getFeaturesOutput; - - @Mock - private FeaturesReply featuresReply; + private LifecycleService lifecycleService; private final NodeId nodeId = NodeId.getDefaultInstance("openflow:1"); - private final KeyedInstanceIdentifier instanceIdentifier = DeviceStateUtil.createNodeInstanceIdentifier(nodeId); - private final Entity entity = new Entity(RoleManager.ENTITY_TYPE, nodeId.getValue()); - private final Entity txEntity = new Entity(RoleManager.TX_ENTITY_TYPE, nodeId.getValue()); - private RoleContextImpl roleContext; + private RoleContext roleContext; @Before public void setup() throws CandidateAlreadyRegisteredException { - when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext); - when(deviceContext.getDeviceState()).thenReturn(deviceState); - when(connectionContext.getNodeId()).thenReturn(nodeId); - when(deviceState.getNodeInstanceIdentifier()).thenReturn(instanceIdentifier); - when(deviceState.getNodeId()).thenReturn(nodeId); - when(rpcProviderRegistry.getRpcService(SalRoleService.class)).thenReturn(salRoleService); - when(deviceState.getFeatures()).thenReturn(getFeaturesOutput); - when(getFeaturesOutput.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3); - when(deviceContext.getPrimaryConnectionContext().getFeatures()).thenReturn(featuresReply); - when(deviceContext.getPrimaryConnectionContext().getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.WORKING); - when(deviceContext.onClusterRoleChange(Matchers.any())).thenReturn(Futures.immediateFuture((Void) null)); - - roleContext = new RoleContextImpl(deviceContext, entityOwnershipService, entity, txEntity); - roleContext.initialization(); + roleContext = new RoleContextImpl(deviceInfo, hashedWheelTimer, roleManager); + Mockito.when(deviceInfo.getNodeId()).thenReturn(nodeId); } @Test - public void testOnRoleChangedStartingMaster() throws InterruptedException, ExecutionException, TimeoutException { - final OfpRole oldRole = OfpRole.BECOMESLAVE; - final OfpRole newRole = OfpRole.BECOMEMASTER; - - final SettableFuture> future = SettableFuture.create(); - future.set(RpcResultBuilder.success().build()); - when(salRoleService.setRole(Matchers.argThat(new SetRoleInputMatcher(newRole, instanceIdentifier)))) - .thenReturn(future); - - roleContext.setSalRoleService(salRoleService); - - final ListenableFuture onRoleChanged = roleContext.onRoleChanged(oldRole, newRole); - onRoleChanged.get(FUTURE_SAFETY_TIMEOUT, TimeUnit.SECONDS); - - verify(deviceContext).onClusterRoleChange(newRole); + public void testCreateRequestContext() throws Exception { + roleContext.createRequestContext(); + Mockito.verify(deviceInfo).reserveXidForDeviceMessage(); } - @Test - public void testOnRoleChangedStartingSlave() throws InterruptedException, ExecutionException, TimeoutException { - final OfpRole oldRole = OfpRole.BECOMEMASTER; - final OfpRole newRole = OfpRole.BECOMESLAVE; - - final SettableFuture> future = SettableFuture.create(); - future.set(RpcResultBuilder.success().build()); - when(salRoleService.setRole(Matchers.argThat(new SetRoleInputMatcher(newRole, instanceIdentifier)))) - .thenReturn(future); - - roleContext.setSalRoleService(salRoleService); - - final ListenableFuture onRoleChanged = roleContext.onRoleChanged(oldRole, newRole); - onRoleChanged.get(5, TimeUnit.SECONDS); - - verify(deviceContext, Mockito.never()).onClusterRoleChange(newRole); - } - - @Test - public void testOnRoleChangedWorkingMaster() throws InterruptedException, ExecutionException, TimeoutException { - final OfpRole oldRole = OfpRole.BECOMESLAVE; - final OfpRole newRole = OfpRole.BECOMEMASTER; - - final ListenableFuture> future = - RpcResultBuilder.success(new SetRoleOutputBuilder().build()).buildFuture(); - when(salRoleService.setRole(Matchers.argThat(new SetRoleInputMatcher(newRole, instanceIdentifier)))) - .thenReturn(future); - - roleContext.setSalRoleService(salRoleService); - roleContext.promoteStateToWorking(); - - final ListenableFuture onRoleChanged = roleContext.onRoleChanged(oldRole, newRole); - onRoleChanged.get(5, TimeUnit.SECONDS); - - verify(deviceContext).onClusterRoleChange(newRole); + @Test(expected = NullPointerException.class) + public void testSetSalRoleService() throws Exception { + roleContext.setSalRoleService(null); } @Test - public void testOnRoleChangedWorkingSlave() throws InterruptedException, ExecutionException, TimeoutException { - final OfpRole oldRole = OfpRole.BECOMEMASTER; - final OfpRole newRole = OfpRole.BECOMESLAVE; - - final SettableFuture> future = SettableFuture.create(); - future.set(RpcResultBuilder.success().build()); - when(salRoleService.setRole(Matchers.argThat(new SetRoleInputMatcher(newRole, instanceIdentifier)))) - .thenReturn(future); - - roleContext.setSalRoleService(salRoleService); - roleContext.promoteStateToWorking(); - - final ListenableFuture onRoleChanged = roleContext.onRoleChanged(oldRole, newRole); - onRoleChanged.get(5, TimeUnit.SECONDS); - - verify(deviceContext).onClusterRoleChange(newRole); - } - - private class SetRoleInputMatcher extends ArgumentMatcher { - - private final OfpRole ofpRole; - private final NodeRef nodeRef; - - public SetRoleInputMatcher(final OfpRole ofpRole, final KeyedInstanceIdentifier instanceIdentifier) { - this.ofpRole = ofpRole; - nodeRef = new NodeRef(instanceIdentifier); - - } - - @Override - public boolean matches(final Object o) { - final SetRoleInput input = (SetRoleInput) o; - if (input.getControllerRole() == ofpRole && - input.getNode().equals(nodeRef)) { - return true; - } - return false; - } + public void testGetNodeId() throws Exception { + Assert.assertTrue(roleContext.getDeviceInfo().getNodeId().equals(nodeId)); } }