Bug 2525 : remove zombie registrations of notification listener
[openflowplugin.git] / openflowplugin / src / test / java / org / opendaylight / openflowplugin / openflow / md / core / sal / SalRegistrationManagerTest.java
index e2cc4cdcc0aacc45e623f121f45f43ac116cd931..2047cb5047840d0589614aa01b63959bf16bd528 100644 (file)
@@ -10,12 +10,14 @@ package org.opendaylight.openflowplugin.openflow.md.core.sal;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
 
 import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListeningExecutorService;
 import java.math.BigInteger;
 import java.util.Collections;
 import java.util.Set;
-import java.util.concurrent.ExecutorService;
+import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -24,17 +26,17 @@ import org.mockito.Mock;
 import org.mockito.Mockito;
 import org.mockito.runners.MockitoJUnitRunner;
 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
-import org.opendaylight.controller.sal.binding.api.NotificationListener;
 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
 import org.opendaylight.controller.sal.common.util.Rpcs;
-import org.opendaylight.openflowplugin.openflow.md.ModelDrivenSwitch;
-import org.opendaylight.openflowplugin.openflow.md.OFConstants;
-import org.opendaylight.openflowplugin.openflow.md.core.ConnectionConductor;
-import org.opendaylight.openflowplugin.openflow.md.core.SwitchConnectionDistinguisher;
-import org.opendaylight.openflowplugin.openflow.md.core.session.IMessageDispatchService;
-import org.opendaylight.openflowplugin.openflow.md.core.session.SessionContext;
-import org.opendaylight.openflowplugin.openflow.md.core.session.SwitchSessionKeyOF;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
+import org.opendaylight.openflowplugin.api.OFConstants;
+import org.opendaylight.openflowplugin.api.openflow.md.ModelDrivenSwitch;
+import org.opendaylight.openflowplugin.api.openflow.md.core.ConnectionConductor;
+import org.opendaylight.openflowplugin.api.openflow.md.core.NotificationEnqueuer;
+import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
+import org.opendaylight.openflowplugin.api.openflow.md.core.session.IMessageDispatchService;
+import org.opendaylight.openflowplugin.api.openflow.md.core.session.SwitchSessionKeyOF;
+import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
+import org.opendaylight.openflowplugin.openflow.md.core.session.SessionContextOFImpl;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutputBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
@@ -43,10 +45,8 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.N
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
 import org.opendaylight.yangtools.concepts.CompositeObjectRegistration;
-import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
-import org.opendaylight.yangtools.yang.binding.Notification;
 import org.opendaylight.yangtools.yang.common.RpcError;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 
@@ -60,8 +60,8 @@ public class SalRegistrationManagerTest {
     private static final BigInteger dataPathId = BigInteger.ONE;
 
     private SalRegistrationManager salRegistrationManager;
-    @Mock
-    private SessionContext context;
+
+    private SessionContextOFImpl context;
     @Mock
     private ConnectionConductor conductor;
     @Mock
@@ -70,27 +70,34 @@ public class SalRegistrationManagerTest {
     private GetFeaturesOutput features;
     @Mock
     private BindingAwareBroker.ProviderContext providerContext;
+    @Mock
+    private NotificationEnqueuer notificationEnqueuer;
+    @Mock
+    private ListeningExecutorService rpcPool;
+    @Mock
+    private NotificationProviderService notificationProviderService;
 
     private ModelDrivenSwitch mdSwitchOF13;
 
     CompositeObjectRegistration<ModelDrivenSwitch> registration;
 
+
     @Before
     public void setUp() {
-
-
-        Mockito.when(context.getPrimaryConductor()).thenReturn(conductor);
-        Mockito.when(context.getMessageDispatchService()).thenReturn(messageDispatchService);
+        OFSessionUtil.getSessionManager().setRpcPool(rpcPool);
         Mockito.when(conductor.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_0)
                 .thenReturn(OFConstants.OFP_VERSION_1_3);
-        Mockito.when(context.getFeatures()).thenReturn(features);
+        context = new SessionContextOFImpl();
+        context.setPrimaryConductor(conductor);
+        Mockito.when(features.getDatapathId()).thenReturn(BigInteger.valueOf(1));
+        Mockito.when(features.getVersion()).thenReturn((short) 1);
+        context.setFeatures(features);
+        context.setNotificationEnqueuer(notificationEnqueuer);
 
         mdSwitchOF13 = new ModelDrivenSwitchImpl(null, null, context);
         registration = new CompositeObjectRegistration<>(mdSwitchOF13, Collections.EMPTY_LIST);
+        context.setProviderRegistration(registration);
 
-        Mockito.when(context.getProviderRegistration()).thenReturn(registration);
-        Mockito.when(features.getDatapathId()).thenReturn(BigInteger.valueOf(1));
-        Mockito.when(features.getVersion()).thenReturn((short) 1);
 
         Set<RpcError> errorSet = Collections.emptySet();
         UpdateFlowOutputBuilder updateFlowOutput = new UpdateFlowOutputBuilder();
@@ -102,7 +109,16 @@ public class SalRegistrationManagerTest {
 
         salRegistrationManager = new SalRegistrationManager();
         salRegistrationManager.onSessionInitiated(providerContext);
-        salRegistrationManager.setPublishService(new MockNotificationProviderService());
+        salRegistrationManager.setPublishService(notificationProviderService);
+
+    }
+
+    /**
+     * free sesion manager
+     */
+    @After
+    public void tearDown() {
+        OFSessionUtil.releaseSessionManager();
     }
 
     /**
@@ -145,49 +161,21 @@ public class SalRegistrationManagerTest {
 
 
     /**
-     * Test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SalRegistrationManager#onSessionRemoved(org.opendaylight.openflowplugin.openflow.md.core.session.SessionContext)}
+     * Test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SalRegistrationManager#onSessionRemoved(org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext)}
      */
     @Test
     public void testOnSessionRemoved() {
+        assertNotNull(context.getProviderRegistration());
         salRegistrationManager.onSessionRemoved(context);
+        assertNull(context.getProviderRegistration());
     }
 
     /**
-     * Test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SalRegistrationManager#onSessionAdded(org.opendaylight.openflowplugin.openflow.md.core.session.SwitchSessionKeyOF, org.opendaylight.openflowplugin.openflow.md.core.session.SessionContext)}
+     * Test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SalRegistrationManager#onSessionAdded(org.opendaylight.openflowplugin.api.openflow.md.core.session.SwitchSessionKeyOF, org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext)}
      */
     public void testOnAdded() {
         SwitchSessionKeyOF switchSessionKeyOF = new SwitchSessionKeyOF();
         salRegistrationManager.onSessionAdded(switchSessionKeyOF, context);
     }
-
-
-    private class MockNotificationProviderService implements NotificationProviderService {
-
-        @Override
-        public void publish(Notification notification) {
-
-        }
-
-        @Override
-        public void publish(Notification notification, ExecutorService executorService) {
-
-        }
-
-        @Override
-        public ListenerRegistration<NotificationInterestListener> registerInterestListener(NotificationInterestListener notificationInterestListener) {
-            return null;
-        }
-
-        @Override
-        public <T extends Notification> ListenerRegistration<NotificationListener<T>> registerNotificationListener(Class<T> tClass, NotificationListener<T> tNotificationListener) {
-            return null;
-        }
-
-        @Override
-        public ListenerRegistration<org.opendaylight.yangtools.yang.binding.NotificationListener> registerNotificationListener(org.opendaylight.yangtools.yang.binding.NotificationListener notificationListener) {
-            return null;
-        }
-    }
-
 }