Improve cleanup after device disconnected event
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / rpc / RpcContextImplTest.java
index 75b20cb7e578d7745da7692ee6aae09b014658df..68e3dfdc91c50d0a8ddc286da492fa74925bf44e 100644 (file)
@@ -7,35 +7,45 @@
  */
 package org.opendaylight.openflowplugin.impl.rpc;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
+import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
+
 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.binding.api.NotificationPublishService;
 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
+import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
 import org.opendaylight.openflowplugin.api.openflow.device.XidSequencer;
 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
+import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterProvider;
+import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeContext;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
 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.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
+import org.opendaylight.yangtools.yang.binding.RpcService;
 
-/**
- * @author joe
- */
 @RunWith(MockitoJUnitRunner.class)
 public class RpcContextImplTest {
 
+    private static final int MAX_REQUESTS = 5;
+    private RpcContextImpl rpcContext;
+
+
     @Mock
     private BindingAwareBroker.ProviderContext rpcProviderRegistry;
     @Mock
@@ -44,6 +54,22 @@ public class RpcContextImplTest {
     private XidSequencer xidSequencer;
     @Mock
     private MessageSpy messageSpy;
+    @Mock
+    private DeviceContext deviceContext;
+    @Mock
+    private BindingAwareBroker.RoutedRpcRegistration<TestRpcService> routedRpcReg;
+
+    private Class<TestRpcService> serviceClass;
+    @Mock
+    private NotificationPublishService notificationPublishService;
+    @Mock
+    private TestRpcService serviceInstance;
+    @Mock
+    private DeviceInfo deviceInfo;
+    @Mock
+    private ExtensionConverterProvider extensionConverterProvider;
+    @Mock
+    private ConvertorExecutor convertorExecutor;
 
     private KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier;
 
@@ -51,12 +77,38 @@ public class RpcContextImplTest {
     public void setup() {
         final NodeId nodeId = new NodeId("openflow:1");
         nodeInstanceIdentifier = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(nodeId));
+
+        when(deviceContext.getDeviceState()).thenReturn(deviceState);
+        when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(nodeInstanceIdentifier);
+        when(deviceContext.getMessageSpy()).thenReturn(messageSpy);
+
+        rpcContext = new RpcContextImpl(
+                deviceInfo,
+                rpcProviderRegistry,
+                messageSpy,
+                MAX_REQUESTS,
+                nodeInstanceIdentifier,
+                deviceContext,
+                extensionConverterProvider,
+                convertorExecutor,
+                notificationPublishService);
+
+        when(rpcProviderRegistry.addRoutedRpcImplementation(TestRpcService.class, serviceInstance)).thenReturn(routedRpcReg);
+
     }
 
     @Test
     public void testStoreOrFail() throws Exception {
-        try (final RpcContext rpcContext = new RpcContextImpl(rpcProviderRegistry, xidSequencer,
-                messageSpy, 100, nodeInstanceIdentifier)) {
+        try (final RpcContext rpcContext = new RpcContextImpl(
+                deviceInfo,
+                rpcProviderRegistry,
+                messageSpy,
+                100,
+                nodeInstanceIdentifier,
+                deviceContext,
+                extensionConverterProvider,
+                convertorExecutor,
+                notificationPublishService)){
             final RequestContext<?> requestContext = rpcContext.createRequestContext();
             assertNotNull(requestContext);
         }
@@ -64,10 +116,95 @@ public class RpcContextImplTest {
 
     @Test
     public void testStoreOrFailThatFails() throws Exception {
-        try (final RpcContext rpcContext = new RpcContextImpl(rpcProviderRegistry, xidSequencer,
-                messageSpy, 0, nodeInstanceIdentifier)) {
+        try (final RpcContext rpcContext = new RpcContextImpl(
+                deviceInfo,
+                rpcProviderRegistry,
+                messageSpy,
+                0,
+                nodeInstanceIdentifier,
+                deviceContext,
+                extensionConverterProvider,
+                convertorExecutor,
+                notificationPublishService)){
             final RequestContext<?> requestContext = rpcContext.createRequestContext();
             assertNull(requestContext);
         }
     }
+
+    @Test
+    public void testStoreAndCloseOrFail() throws Exception {
+        try (final RpcContext rpcContext = new RpcContextImpl(
+                deviceInfo,
+                rpcProviderRegistry,
+                messageSpy,
+                100,
+                nodeInstanceIdentifier,
+                deviceContext,
+                extensionConverterProvider,
+                convertorExecutor,
+                notificationPublishService)){
+            final RequestContext<?> requestContext = rpcContext.createRequestContext();
+            assertNotNull(requestContext);
+            requestContext.close();
+            verify(messageSpy).spyMessage(RpcContextImpl.class, MessageSpy.STATISTIC_GROUP.REQUEST_STACK_FREED);
+        }
+    }
+
+    public void testRegisterRpcServiceImplementation() {
+        rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
+        verify(rpcProviderRegistry, Mockito.times(1)).addRoutedRpcImplementation(TestRpcService.class,serviceInstance);
+        verify(routedRpcReg,Mockito.times(1)).registerPath(NodeContext.class,nodeInstanceIdentifier);
+        assertEquals(rpcContext.isEmptyRpcRegistrations(), false);
+    }
+
+
+    @Test
+    public void testLookupRpcService() {
+        when(routedRpcReg.getInstance()).thenReturn(serviceInstance);
+        rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
+        TestRpcService temp = rpcContext.lookupRpcService(TestRpcService.class);
+        assertEquals(serviceInstance,temp);
+    }
+
+    @Test
+    public void testClose() {
+        serviceClass = TestRpcService.class;
+        when(routedRpcReg.getServiceType()).thenReturn(serviceClass);
+        rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
+        rpcContext.close();
+        assertEquals(rpcContext.isEmptyRpcRegistrations(), true);
+    }
+
+    /**
+     * When deviceContext.reserveXidForDeviceMessage returns null, null should be returned
+     * @throws InterruptedException
+     */
+    @Test
+    public void testCreateRequestContext1() throws InterruptedException {
+        when(deviceInfo.reserveXidForDeviceMessage()).thenReturn(null);
+        assertEquals(rpcContext.createRequestContext(),null);
+    }
+
+    /**
+     * When deviceContext.reserveXidForDeviceMessage returns value, AbstractRequestContext should be returned
+     * @throws InterruptedException
+     */
+
+    @Test
+    public void testCreateRequestContext2() throws InterruptedException {
+        RequestContext temp = rpcContext.createRequestContext();
+        temp.close();
+        verify(messageSpy).spyMessage(RpcContextImpl.class,MessageSpy.STATISTIC_GROUP.REQUEST_STACK_FREED);
+    }
+
+    @Test
+    public void testUnregisterRpcServiceImpl() {
+        rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
+        assertEquals(rpcContext.isEmptyRpcRegistrations(), false);
+        rpcContext.unregisterRpcServiceImplementation(TestRpcService.class);
+        assertEquals(rpcContext.isEmptyRpcRegistrations(), true);
+    }
+
+    //Stub for RpcService class
+    public class TestRpcService implements RpcService {}
 }