Switch to MD-SAL APIs
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / statistics / StatisticsContextImplTest.java
index 2006ca210f5920b19a1e61192a2dc7b33d5da920..02962122f490fe56ac34bcbb683c118f7ecfff96 100644 (file)
@@ -1,41 +1,38 @@
 /*
+ * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
  *
- *  * Copyright (c) 2015 Cisco Systems, Inc. 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
- *
- *
+ * 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.statistics;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
-import static org.mockito.Mockito.mock;
+import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.times;
 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.MoreExecutors;
 import java.util.Collections;
-import java.util.concurrent.ExecutionException;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
-import org.mockito.Matchers;
+import org.mockito.Mock;
 import org.mockito.Mockito;
-import org.mockito.runners.MockitoJUnitRunner;
-import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
+import org.mockito.junit.MockitoJUnitRunner;
 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
+import org.opendaylight.openflowplugin.impl.datastore.MultipartWriterProviderFactory;
+import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager;
+import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManagerFactory;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.NonZeroUint32Type;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.OpenflowProviderConfig;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
 import org.slf4j.Logger;
@@ -47,17 +44,38 @@ public class StatisticsContextImplTest extends StatisticsContextImpMockInitiatio
     private static final Logger LOG = LoggerFactory.getLogger(StatisticsContextImplTest.class);
 
     private static final Long TEST_XID = 55L;
-    private StatisticsContextImpl statisticsContext;
+    private StatisticsContextImpl<MultipartReply> statisticsContext;
+    private ConvertorManager convertorManager;
+    @Mock
+    private final OpenflowProviderConfig config =
+            Mockito.mock(OpenflowProviderConfig.class);
 
     @Before
     public void setUp() throws Exception {
-        when(mockedDeviceContext.reserveXidForDeviceMessage()).thenReturn(TEST_XID);
-        when(mockConductor.getDeviceContext(Mockito.<NodeId>any())).thenReturn(mockedDeviceContext);
+        convertorManager = ConvertorManagerFactory.createDefaultManager();
+        when(mockedDeviceInfo.reserveXidForDeviceMessage()).thenReturn(TEST_XID);
+        Mockito.when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
+        Mockito.when(config.isIsTableStatisticsPollingOn()).thenReturn(true);
+        Mockito.when(config.isIsFlowStatisticsPollingOn()).thenReturn(true);
+        Mockito.when(config.isIsGroupStatisticsPollingOn()).thenReturn(true);
+        Mockito.when(config.isIsMeterStatisticsPollingOn()).thenReturn(true);
+        Mockito.when(config.isIsPortStatisticsPollingOn()).thenReturn(true);
+        Mockito.when(config.isIsQueueStatisticsPollingOn()).thenReturn(true);
+        Mockito.when(config.getBasicTimerDelay()).thenReturn(new NonZeroUint32Type(3000L));
+        Mockito.when(config.getMaximumTimerDelay()).thenReturn(new NonZeroUint32Type(50000L));
+
         initStatisticsContext();
     }
 
     private void initStatisticsContext() {
-        statisticsContext = new StatisticsContextImpl(mockedDeviceInfo, false, mockConductor);
+        statisticsContext = new StatisticsContextImpl<>(mockedDeviceContext, convertorManager,
+                MultipartWriterProviderFactory
+                        .createDefaultProvider(mockedDeviceContext),
+                MoreExecutors.newDirectExecutorService(),
+                config,
+                true,
+                false);
+
         statisticsContext.setStatisticsGatheringService(mockedStatisticsGatheringService);
         statisticsContext.setStatisticsGatheringOnTheFlyService(mockedStatisticsOnFlyGatheringService);
     }
@@ -71,11 +89,21 @@ public class StatisticsContextImplTest extends StatisticsContextImpMockInitiatio
     }
 
     /**
-     * There is nothing to check in close method
+     * There is nothing to check in close method.
      */
     @Test
+    @SuppressWarnings("checkstyle:IllegalCatch")
     public void testClose() throws Exception {
-        final StatisticsContextImpl statisticsContext = new StatisticsContextImpl(mockedDeviceInfo, false, mockConductor);
+        statisticsContext =
+                new StatisticsContextImpl<>(mockedDeviceContext,
+                        convertorManager,
+                        MultipartWriterProviderFactory
+                                .createDefaultProvider(mockedDeviceContext),
+                        MoreExecutors.newDirectExecutorService(),
+                        config,
+                        true,
+                        false);
+
         final RequestContext<Object> requestContext = statisticsContext.createRequestContext();
         statisticsContext.close();
         try {
@@ -91,78 +119,40 @@ public class StatisticsContextImplTest extends StatisticsContextImpMockInitiatio
 
     @Test
     public void testGatherDynamicData_none() throws Exception {
-        final ListenableFuture<Boolean> gatheringResult = statisticsContext.gatherDynamicData();
-        Assert.assertTrue(gatheringResult.isDone());
-        Assert.assertTrue(gatheringResult.get());
+        statisticsContext.instantiateServiceInstance();
         Mockito.verifyNoMoreInteractions(mockedStatisticsGatheringService, mockedStatisticsOnFlyGatheringService);
     }
 
     @Test
     public void testGatherDynamicData_all() throws Exception {
         Mockito.reset(mockedDeviceState);
-        when(mockedDeviceState.getFeatures()).thenReturn(mock(GetFeaturesOutput.class));
         when(mockedDeviceState.isTableStatisticsAvailable()).thenReturn(Boolean.TRUE);
         when(mockedDeviceState.isFlowStatisticsAvailable()).thenReturn(Boolean.TRUE);
         when(mockedDeviceState.isGroupAvailable()).thenReturn(Boolean.TRUE);
         when(mockedDeviceState.isMetersAvailable()).thenReturn(Boolean.TRUE);
         when(mockedDeviceState.isPortStatisticsAvailable()).thenReturn(Boolean.TRUE);
         when(mockedDeviceState.isQueueStatisticsAvailable()).thenReturn(Boolean.TRUE);
-        when(mockedDeviceState.getNodeInstanceIdentifier()).thenReturn(dummyNodeII);
+        when(mockedDeviceInfo.getNodeInstanceIdentifier()).thenReturn(DUMMY_NODE_ID);
         initStatisticsContext();
 
-        when(mockedStatisticsGatheringService.getStatisticsOfType(Matchers.any(EventIdentifier.class), Matchers.any(MultipartType.class)))
-                .thenReturn(
-                        Futures.immediateFuture(RpcResultBuilder.success(Collections.<MultipartReply>emptyList()).build())
-                );
-        when(mockedStatisticsOnFlyGatheringService.getStatisticsOfType(Matchers.any(EventIdentifier.class), Matchers.any(MultipartType.class)))
-                .thenReturn(
-                        Futures.immediateFuture(RpcResultBuilder.success(Collections.<MultipartReply>emptyList()).build())
-                );
-
-        final ListenableFuture<Boolean> gatheringResult = statisticsContext.gatherDynamicData();
-        Assert.assertTrue(gatheringResult.isDone());
-        Assert.assertTrue(gatheringResult.get());
+        when(mockedStatisticsGatheringService
+                     .getStatisticsOfType(any(EventIdentifier.class), any(MultipartType.class)))
+                .thenReturn(Futures.immediateFuture(
+                        RpcResultBuilder.success(Collections.<MultipartReply>emptyList()).build()));
+        when(mockedStatisticsOnFlyGatheringService
+                     .getStatisticsOfType(any(EventIdentifier.class), any(MultipartType.class)))
+                .thenReturn(Futures.immediateFuture(
+                        RpcResultBuilder.success(Collections.<MultipartReply>emptyList()).build()));
+
+        statisticsContext.registerMastershipWatcher(mockedMastershipWatcher);
+        statisticsContext.setStatisticsGatheringService(mockedStatisticsGatheringService);
+        statisticsContext.setStatisticsGatheringOnTheFlyService(mockedStatisticsOnFlyGatheringService);
+        statisticsContext.instantiateServiceInstance();
+
         verify(mockedStatisticsGatheringService, times(7))
-                .getStatisticsOfType(Matchers.any(EventIdentifier.class), Matchers.any(MultipartType.class));
+                .getStatisticsOfType(any(EventIdentifier.class), any(MultipartType.class));
         verify(mockedStatisticsOnFlyGatheringService)
-                .getStatisticsOfType(Matchers.any(EventIdentifier.class), Matchers.any(MultipartType.class));
+                .getStatisticsOfType(any(EventIdentifier.class), any(MultipartType.class));
         Mockito.verifyNoMoreInteractions(mockedStatisticsGatheringService, mockedStatisticsOnFlyGatheringService);
     }
-
-    @Test
-    public void testDeviceConnectionCheck_WORKING() throws Exception {
-        final ListenableFuture<Boolean> deviceConnectionCheckResult = statisticsContext.deviceConnectionCheck();
-        Assert.assertNull(deviceConnectionCheckResult);
-    }
-
-    @Test
-    public void testDeviceConnectionCheck_RIP() throws Exception {
-        Mockito.reset(mockedConnectionContext);
-        when(mockedConnectionContext.getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.RIP);
-        final ListenableFuture<Boolean> deviceConnectionCheckResult = statisticsContext.deviceConnectionCheck();
-        Assert.assertNotNull(deviceConnectionCheckResult);
-        Assert.assertTrue(deviceConnectionCheckResult.isDone());
-        try {
-            deviceConnectionCheckResult.get();
-            Assert.fail("connection in state RIP should have caused exception here");
-        } catch (final Exception e) {
-            LOG.debug("expected behavior for RIP connection achieved");
-            Assert.assertTrue(e instanceof ExecutionException);
-        }
-    }
-
-    @Test
-    public void testDeviceConnectionCheck_HANSHAKING() throws Exception {
-        Mockito.reset(mockedConnectionContext);
-        when(mockedConnectionContext.getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.HANDSHAKING);
-        final ListenableFuture<Boolean> deviceConnectionCheckResult = statisticsContext.deviceConnectionCheck();
-        Assert.assertNotNull(deviceConnectionCheckResult);
-        Assert.assertTrue(deviceConnectionCheckResult.isDone());
-        try {
-            final Boolean checkPositive = deviceConnectionCheckResult.get();
-            Assert.assertTrue(checkPositive);
-        } catch (final Exception e) {
-            Assert.fail("connection in state HANDSHAKING should NOT have caused exception here");
-        }
-    }
 }