Add RoleManager and RoleContext
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / lifecycle / ContextChainImplTest.java
index 28166586edc925b36d9a64cc232640c9ff5491c4..c61a6029374f9c368bd1dd56419c5a0c340f8240 100644 (file)
@@ -8,6 +8,7 @@
 package org.opendaylight.openflowplugin.impl.lifecycle;
 
 import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.MoreExecutors;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
@@ -16,18 +17,23 @@ import org.mockito.Mock;
 import org.mockito.Mockito;
 import org.mockito.runners.MockitoJUnitRunner;
 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
+import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
+import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
+import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceRemovedHandler;
 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChain;
-import org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChainState;
-import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleService;
+import org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChainMastershipWatcher;
 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
 
 @RunWith(MockitoJUnitRunner.class)
 public class ContextChainImplTest {
 
+    private static final String TEST_NODE = "test node";
+    private static final ServiceGroupIdentifier SERVICE_GROUP_IDENTIFIER = ServiceGroupIdentifier.create(TEST_NODE);
+
     @Mock
     private StatisticsContext statisticsContext;
     @Mock
@@ -35,127 +41,86 @@ public class ContextChainImplTest {
     @Mock
     private DeviceContext deviceContext;
     @Mock
-    private LifecycleService lifecycleService;
-    @Mock
     private DeviceInfo deviceInfo;
     @Mock
     private ConnectionContext connectionContext;
     @Mock
-    private ConnectionContext secondaryConnectionContext;
-    @Mock
     private ClusterSingletonServiceProvider clusterSingletonServiceProvider;
+    @Mock
+    private ClusterSingletonServiceRegistration clusterSingletonServiceRegistration;
+    @Mock
+    private ContextChainMastershipWatcher contextChainMastershipWatcher;
+    @Mock
+    private DeviceRemovedHandler deviceRemovedHandler;
 
     private ContextChain contextChain;
 
     @Before
     public void setUp() throws Exception {
-
         Mockito.when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
-        Mockito.when(deviceContext.stopClusterServices(Mockito.anyBoolean()))
-                .thenReturn(Futures.immediateFuture(null));
-        Mockito.when(rpcContext.stopClusterServices()).thenReturn(Futures.immediateFuture(null));
-        Mockito.when(statisticsContext.stopClusterServices()).thenReturn(Futures.immediateFuture(null));
-        Mockito.when(statisticsContext.initialGatherDynamicData()).thenReturn(Futures.immediateFuture(null));
-
-        contextChain = new ContextChainImpl(connectionContext);
+        Mockito.when(deviceInfo.getServiceIdentifier()).thenReturn(SERVICE_GROUP_IDENTIFIER);
+        Mockito.when(deviceContext.closeServiceInstance()).thenReturn(Futures.immediateFuture(null));
+        Mockito.when(rpcContext.closeServiceInstance()).thenReturn(Futures.immediateFuture(null));
+        Mockito.when(statisticsContext.closeServiceInstance()).thenReturn(Futures.immediateFuture(null));
+        Mockito.when(statisticsContext.gatherDynamicData()).thenReturn(Futures.immediateFuture(null));
+        Mockito.when(connectionContext.getDeviceInfo()).thenReturn(deviceInfo);
+        Mockito.when(connectionContext.getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.WORKING);
+        Mockito.when(clusterSingletonServiceProvider.registerClusterSingletonService(Mockito.any()))
+                .thenReturn(clusterSingletonServiceRegistration);
+
+        contextChain = new ContextChainImpl(contextChainMastershipWatcher, connectionContext,
+                MoreExecutors.newDirectExecutorService());
         contextChain.addContext(statisticsContext);
         contextChain.addContext(rpcContext);
         contextChain.addContext(deviceContext);
-        contextChain.addLifecycleService(lifecycleService);
-
-    }
-
-    @Test
-    public void stopChain() throws Exception {
-        contextChain.stopChain(true);
-        Mockito.verify(deviceContext).stopClusterServices(Mockito.anyBoolean());
-        Mockito.verify(rpcContext).stopClusterServices();
-        Mockito.verify(statisticsContext).stopClusterServices();
-    }
-
-    @Test
-    public void startChain() throws Exception {
-        Assert.assertSame(contextChain.getContextChainState(), ContextChainState.INITIALIZED);
-        contextChain.startChain();
-        Mockito.verify(statisticsContext).initialGatherDynamicData();
-        Assert.assertSame(contextChain.getContextChainState(), ContextChainState.WORKING_MASTER);
-    }
-
-    @Test
-    public void startChainTwoTimes() throws Exception {
-        Assert.assertSame(contextChain.getContextChainState(), ContextChainState.INITIALIZED);
-        contextChain.startChain();
-        contextChain.startChain();
-        Assert.assertSame(contextChain.getContextChainState(), ContextChainState.WORKING_MASTER);
-        Mockito.verify(statisticsContext, Mockito.times(1)).initialGatherDynamicData();
+        contextChain.registerServices(clusterSingletonServiceProvider);
     }
 
     @Test
-    public void startChainThreeTimes() throws Exception {
-        Assert.assertSame(contextChain.getContextChainState(), ContextChainState.INITIALIZED);
-        contextChain.startChain();
-        contextChain.startChain();
-        Assert.assertSame(contextChain.getContextChainState(), ContextChainState.WORKING_MASTER);
-        Mockito.verify(statisticsContext, Mockito.times(1)).initialGatherDynamicData();
+    public void closeServiceInstance() throws Exception {
+        contextChain.closeServiceInstance();
+        Mockito.verify(deviceContext).closeServiceInstance();
+        Mockito.verify(rpcContext).closeServiceInstance();
+        Mockito.verify(statisticsContext).closeServiceInstance();
     }
 
     @Test
     public void close() throws Exception {
+        contextChain.registerDeviceRemovedHandler(deviceRemovedHandler);
         contextChain.close();
         Mockito.verify(statisticsContext).close();
         Mockito.verify(deviceContext).close();
         Mockito.verify(rpcContext).close();
-        Mockito.verify(lifecycleService).close();
-    }
-
-    @Test
-    public void changePrimaryConnection() throws Exception {
-        Assert.assertSame(contextChain.getPrimaryConnectionContext(), connectionContext);
-        contextChain.changePrimaryConnection(secondaryConnectionContext);
-        Assert.assertSame(contextChain.getPrimaryConnectionContext(), secondaryConnectionContext);
-        Mockito.verify(deviceContext).replaceConnection(Mockito.any(ConnectionContext.class));
-        Mockito.verify(rpcContext).replaceConnection(Mockito.any(ConnectionContext.class));
-        Mockito.verify(statisticsContext).replaceConnection(Mockito.any(ConnectionContext.class));
-    }
-
-    @Test
-    public void connectionDropped() throws Exception {
-        contextChain.startChain();
-        Mockito.verify(statisticsContext).initialGatherDynamicData();
-        Assert.assertSame(contextChain.getContextChainState(), ContextChainState.WORKING_MASTER);
-        contextChain.connectionDropped();
-        Mockito.verify(deviceContext).stopClusterServices(Mockito.anyBoolean());
-        Mockito.verify(rpcContext).stopClusterServices();
-        Mockito.verify(statisticsContext).stopClusterServices();
+        Mockito.verify(deviceRemovedHandler).onDeviceRemoved(Mockito.any(DeviceInfo.class));
     }
 
     @Test
-    public void sleepTheChainAndDropConnection() throws Exception {
-        contextChain.sleepTheChainAndDropConnection();
-        Assert.assertSame(contextChain.getContextChainState(), ContextChainState.SLEEPING);
-        Mockito.verify(connectionContext).closeConnection(Mockito.anyBoolean());
+    public void closeTwoTimes() throws Exception {
+        contextChain.registerDeviceRemovedHandler(deviceRemovedHandler);
+        contextChain.close();
+        contextChain.close();
+        Mockito.verify(deviceRemovedHandler, Mockito.times(1))
+                .onDeviceRemoved(Mockito.any(DeviceInfo.class));
     }
 
     @Test
-    public void registerServices() throws Exception {
-        contextChain.registerServices(clusterSingletonServiceProvider);
-        Assert.assertSame(contextChain.getContextChainState(), ContextChainState.INITIALIZED);
-        Mockito.verify(lifecycleService).registerService(
-                Mockito.any(ClusterSingletonServiceProvider.class),
-                Mockito.any(DeviceContext.class));
+    public void closeThreeTimes() throws Exception {
+        contextChain.registerDeviceRemovedHandler(deviceRemovedHandler);
+        contextChain.close();
+        contextChain.close();
+        Mockito.verify(deviceRemovedHandler, Mockito.times(1))
+                .onDeviceRemoved(Mockito.any(DeviceInfo.class));
     }
 
     @Test
-    public void makeDeviceSlave() throws Exception {
-        contextChain.makeDeviceSlave();
-        Mockito.verify(lifecycleService).makeDeviceSlave(Mockito.any(DeviceContext.class));
+    public void getIdentifier() throws Exception {
+        Assert.assertEquals(contextChain.getIdentifier(), SERVICE_GROUP_IDENTIFIER);
     }
 
     @Test
-    public void closePrimaryConnection() throws Exception {
-        contextChain.closePrimaryConnection();
-        Mockito.verify(connectionContext).closeConnection(Mockito.anyBoolean());
+    public void instantiateServiceInstanceFail() throws Exception {
+        Mockito.doThrow(new IllegalStateException()).when(deviceContext).instantiateServiceInstance();
+        contextChain.instantiateServiceInstance();
+        Mockito.verify(contextChainMastershipWatcher).onNotAbleToStartMastershipMandatory(Mockito.any(DeviceInfo.class), Mockito.anyString());
     }
-
 }
-