Decompose RPC implementation classes
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / services / sal / SalFlowServiceImplTest.java
index 97350e440b5d80895d8635fc57588672da4fd618..9a32c10df6166632dbbc5e7c23b86d65d4ed98c8 100644 (file)
@@ -7,29 +7,24 @@
  */
 package org.opendaylight.openflowplugin.impl.services.sal;
 
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
 import com.google.common.util.concurrent.Futures;
-import java.time.LocalDateTime;
-import java.util.HashMap;
-import java.util.LinkedList;
-import java.util.Map;
-import java.util.Queue;
+import com.google.common.util.concurrent.ListenableFuture;
 import java.util.concurrent.ExecutionException;
-import java.util.concurrent.Future;
-import junit.framework.TestCase;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Mock;
-import org.mockito.Mockito;
 import org.mockito.junit.MockitoJUnitRunner;
 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
 import org.opendaylight.openflowplugin.api.OFConstants;
-import org.opendaylight.openflowplugin.api.openflow.FlowGroupCache;
-import org.opendaylight.openflowplugin.api.openflow.FlowGroupStatus;
 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
@@ -39,8 +34,6 @@ import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
-import org.opendaylight.openflowplugin.impl.services.cache.FlowGroupCacheManagerImpl;
-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.flow.inventory.rev130819.FlowCapableNode;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
@@ -50,10 +43,8 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.ta
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInputBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowOutput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlow;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlowBuilder;
@@ -73,44 +64,25 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
+import org.opendaylight.yangtools.yang.common.Uint16;
 import org.opendaylight.yangtools.yang.common.Uint32;
 import org.opendaylight.yangtools.yang.common.Uint64;
 import org.opendaylight.yangtools.yang.common.Uint8;
 
-@RunWith(MockitoJUnitRunner.class)
-public class SalFlowServiceImplTest extends TestCase {
-
+@RunWith(MockitoJUnitRunner.StrictStubs.class)
+public class SalFlowServiceImplTest {
     private static final Uint64 DUMMY_DATAPATH_ID = Uint64.valueOf(444);
     private static final String DUMMY_NODE_ID = "dummyNodeID";
     private static final String DUMMY_FLOW_ID = "dummyFlowID";
-    private static final Short DUMMY_TABLE_ID = (short) 0;
-
-    private static final KeyedInstanceIdentifier<Node, NodeKey> NODE_II
-            = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(DUMMY_NODE_ID)));
-
-    private static final KeyedInstanceIdentifier<Table, TableKey> TABLE_II
-            = NODE_II.augmentation(FlowCapableNode.class).child(Table.class, new TableKey(DUMMY_TABLE_ID));
-
-    private NodeRef noderef = new NodeRef(NODE_II);
-    private static final String KEY = "0";
-    private static FlowGroupCache flowcache =
-            new FlowGroupCache("0","mock class", FlowGroupStatus.ADDED, LocalDateTime.MAX);
+    private static final Uint8 DUMMY_TABLE_ID = Uint8.ZERO;
 
-    private static Queue<FlowGroupCache> caches() {
-        Queue<FlowGroupCache> cache = new LinkedList<>();
-        cache.add(flowcache);
-        return cache;
-    }
-
-    private static final Queue<FlowGroupCache> CACHE = caches();
+    private static final KeyedInstanceIdentifier<Node, NodeKey> NODE_II =
+        InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(DUMMY_NODE_ID)));
 
-    private static Map<String, Queue<FlowGroupCache>> createMap() {
-        Map<String,Queue<FlowGroupCache>> myMap = new HashMap<>();
-        myMap.put(KEY, CACHE);
-        return myMap;
-    }
+    private static final KeyedInstanceIdentifier<Table, TableKey> TABLE_II =
+        NODE_II.augmentation(FlowCapableNode.class).child(Table.class, new TableKey(DUMMY_TABLE_ID));
 
-    private static final Map<String, Queue<FlowGroupCache>> MYMAP = createMap();
+    private final NodeRef noderef = new NodeRef(NODE_II);
 
     @Mock
     private RequestContextStack mockedRequestContextStack;
@@ -139,8 +111,6 @@ public class SalFlowServiceImplTest extends TestCase {
     private DeviceFlowRegistry deviceFlowRegistry;
     @Mock
     private GetFeaturesOutput mockedFeaturesOutput;
-    @Mock
-    private FlowGroupCacheManagerImpl flowGroupCacheManager;
 
     @Before
     public void initialization() {
@@ -158,18 +128,33 @@ public class SalFlowServiceImplTest extends TestCase {
         when(mockedDeviceInfo.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
 
         when(mockedDeviceContext.getDeviceInfo()).thenReturn(mockedDeviceInfo);
-        when(flowGroupCacheManager.getAllNodesFlowGroupCache()).thenReturn(MYMAP);
     }
 
-    private SalFlowServiceImpl mockSalFlowService(final short version) {
-        Uint8 ver = Uint8.valueOf(version);
-        when(mockedFeatures.getVersion()).thenReturn(ver);
-        when(mockedFeaturesOutput.getVersion()).thenReturn(ver);
+    private AddFlowImpl mockAddFlow(final Uint8 version) {
+        when(mockedFeatures.getVersion()).thenReturn(version);
+        when(mockedFeaturesOutput.getVersion()).thenReturn(version);
+        when(mockedDeviceInfo.getVersion()).thenReturn(version);
+
+        final var convertorManager = ConvertorManagerFactory.createDefaultManager();
+        return new AddFlowImpl(mockedRequestContextStack, mockedDeviceContext, convertorManager);
+    }
+
+    private RemoveFlowImpl mockRemoveFlow(final Uint8 version) {
+        when(mockedFeatures.getVersion()).thenReturn(version);
+        when(mockedFeaturesOutput.getVersion()).thenReturn(version);
+        when(mockedDeviceInfo.getVersion()).thenReturn(version);
+
+        final var convertorManager = ConvertorManagerFactory.createDefaultManager();
+        return new RemoveFlowImpl(mockedRequestContextStack, mockedDeviceContext, convertorManager);
+    }
+
+    private UpdateFlowImpl mockUpdateFlow(final Uint8 version) {
+        when(mockedFeatures.getVersion()).thenReturn(version);
+        when(mockedFeaturesOutput.getVersion()).thenReturn(version);
         when(mockedDeviceInfo.getVersion()).thenReturn(version);
 
-        final ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
-        return new SalFlowServiceImpl(mockedRequestContextStack, mockedDeviceContext, convertorManager,
-                flowGroupCacheManager);
+        final var convertorManager = ConvertorManagerFactory.createDefaultManager();
+        return new UpdateFlowImpl(mockedRequestContextStack, mockedDeviceContext, convertorManager);
     }
 
     @Test
@@ -188,21 +173,20 @@ public class SalFlowServiceImplTest extends TestCase {
         addFlowFailCallback(OFConstants.OFP_VERSION_1_3);
     }
 
-    private void addFlowFailCallback(final short version) throws InterruptedException, ExecutionException {
+    private void addFlowFailCallback(final Uint8 version) throws InterruptedException, ExecutionException {
         AddFlowInput mockedAddFlowInput = new AddFlowInputBuilder()
                 .setMatch(match)
-                .setTableId((short)1)
+                .setTableId(Uint8.ONE)
                 .setNode(noderef)
                 .build();
 
-        Mockito.doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
+        doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
                 .when(requestContext).getFuture();
 
-        final Future<RpcResult<AddFlowOutput>> rpcResultFuture =
-                mockSalFlowService(version).addFlow(mockedAddFlowInput);
+        final var rpcResultFuture = mockAddFlow(version).invoke(mockedAddFlowInput);
 
         assertNotNull(rpcResultFuture);
-        final RpcResult<?> addFlowOutputRpcResult = rpcResultFuture.get();
+        final var addFlowOutputRpcResult = rpcResultFuture.get();
         assertNotNull(addFlowOutputRpcResult);
         assertFalse(addFlowOutputRpcResult.isSuccessful());
     }
@@ -217,21 +201,20 @@ public class SalFlowServiceImplTest extends TestCase {
         removeFlowFailCallback(OFConstants.OFP_VERSION_1_3);
     }
 
-    private void removeFlowFailCallback(final short version) throws InterruptedException, ExecutionException {
+    private void removeFlowFailCallback(final Uint8 version) throws InterruptedException, ExecutionException {
         RemoveFlowInput mockedRemoveFlowInput = new RemoveFlowInputBuilder()
-                .setTableId((short)1)
+                .setTableId(Uint8.ONE)
                 .setMatch(match)
                 .setNode(noderef)
                 .build();
 
-        Mockito.doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
+        doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
                 .when(requestContext).getFuture();
 
-        final Future<RpcResult<RemoveFlowOutput>> rpcResultFuture =
-                mockSalFlowService(version).removeFlow(mockedRemoveFlowInput);
+        final var rpcResultFuture = mockRemoveFlow(version).invoke(mockedRemoveFlowInput);
 
         assertNotNull(rpcResultFuture);
-        final RpcResult<?> removeFlowOutputRpcResult = rpcResultFuture.get();
+        final var removeFlowOutputRpcResult = rpcResultFuture.get();
         assertNotNull(removeFlowOutputRpcResult);
         assertFalse(removeFlowOutputRpcResult.isSuccessful());
     }
@@ -242,15 +225,15 @@ public class SalFlowServiceImplTest extends TestCase {
         addFlow(OFConstants.OFP_VERSION_1_3);
     }
 
-    private void addFlow(final short version) throws ExecutionException, InterruptedException {
+    private void addFlow(final Uint8 version) throws ExecutionException, InterruptedException {
         AddFlowInput mockedAddFlowInput = new AddFlowInputBuilder()
                 .setMatch(match)
-                .setTableId((short)1)
+                .setTableId(Uint8.ONE)
                 .setNode(noderef)
                 .build();
-        SalFlowServiceImpl salFlowService = mockSalFlowService(version);
+        var addFlow = mockAddFlow(version);
 
-        verifyOutput(salFlowService.addFlow(mockedAddFlowInput));
+        verifyOutput(addFlow.invoke(mockedAddFlowInput));
     }
 
     @Test
@@ -265,15 +248,15 @@ public class SalFlowServiceImplTest extends TestCase {
         removeFlow(OFConstants.OFP_VERSION_1_3);
     }
 
-    private void removeFlow(final short version) throws Exception {
+    private void removeFlow(final Uint8 version) throws Exception {
         RemoveFlowInput mockedRemoveFlowInput = new RemoveFlowInputBuilder()
                 .setMatch(match)
-                .setTableId((short)1)
+                .setTableId(Uint8.ONE)
                 .setNode(noderef)
                 .build();
 
-        SalFlowServiceImpl salFlowService = mockSalFlowService(version);
-        verifyOutput(salFlowService.removeFlow(mockedRemoveFlowInput));
+        final var removeFlow = mockRemoveFlow(version);
+        verifyOutput(removeFlow.invoke(mockedRemoveFlowInput));
     }
 
     @Test
@@ -288,55 +271,53 @@ public class SalFlowServiceImplTest extends TestCase {
         updateFlow(OFConstants.OFP_VERSION_1_3);
     }
 
-    private void updateFlow(final short version) throws Exception {
+    private void updateFlow(final Uint8 version) throws Exception {
         UpdateFlowInput mockedUpdateFlowInput = mock(UpdateFlowInput.class);
         UpdateFlowInput mockedUpdateFlowInput1 = mock(UpdateFlowInput.class);
 
         UpdatedFlow mockedUpdateFlow = new UpdatedFlowBuilder()
                 .setMatch(match)
-                .setTableId((short)1)
+                .setTableId(Uint8.ONE)
                 .build();
 
         UpdatedFlow mockedUpdateFlow1 = new UpdatedFlowBuilder()
                 .setMatch(match)
-                .setTableId((short)1)
-                .setPriority(1)
+                .setTableId(Uint8.ONE)
+                .setPriority(Uint16.ONE)
                 .build();
 
         when(mockedUpdateFlowInput.getUpdatedFlow()).thenReturn(mockedUpdateFlow);
         when(mockedUpdateFlowInput1.getUpdatedFlow()).thenReturn(mockedUpdateFlow1);
 
         FlowRef mockedFlowRef = mock(FlowRef.class);
-        Mockito.doReturn(TABLE_II.child(Flow.class,
+        doReturn(TABLE_II.child(Flow.class,
                          new FlowKey(new FlowId(DUMMY_FLOW_ID)))).when(mockedFlowRef).getValue();
         when(mockedUpdateFlowInput.getFlowRef()).thenReturn(mockedFlowRef);
         when(mockedUpdateFlowInput1.getFlowRef()).thenReturn(mockedFlowRef);
 
         OriginalFlow mockedOriginalFlow = new OriginalFlowBuilder()
                 .setMatch(match)
-                .setTableId((short)1)
+                .setTableId(Uint8.ONE)
                 .build();
 
         OriginalFlow mockedOriginalFlow1 = new OriginalFlowBuilder()
                 .setMatch(match)
-                .setTableId((short)1)
-                .setPriority(2)
+                .setTableId(Uint8.ONE)
+                .setPriority(Uint16.TWO)
                 .build();
 
         when(mockedUpdateFlowInput.getOriginalFlow()).thenReturn(mockedOriginalFlow);
         when(mockedUpdateFlowInput1.getOriginalFlow()).thenReturn(mockedOriginalFlow1);
 
-        SalFlowServiceImpl salFlowService = mockSalFlowService(version);
-        when(mockedUpdateFlowInput.getNode()).thenReturn(noderef);
-        when(mockedUpdateFlowInput1.getNode()).thenReturn(noderef);
-        verifyOutput(salFlowService.updateFlow(mockedUpdateFlowInput));
-        verifyOutput(salFlowService.updateFlow(mockedUpdateFlowInput1));
+        final var updateFlow = mockUpdateFlow(version);
+        verifyOutput(updateFlow.invoke(mockedUpdateFlowInput));
+        verifyOutput(updateFlow.invoke(mockedUpdateFlowInput1));
     }
 
-    private static <T extends DataObject> void verifyOutput(final Future<RpcResult<T>> rpcResultFuture)
+    private static <T extends DataObject> void verifyOutput(final ListenableFuture<RpcResult<T>> rpcResultFuture)
             throws ExecutionException, InterruptedException {
         assertNotNull(rpcResultFuture);
-        final RpcResult<?> addFlowOutputRpcResult = rpcResultFuture.get();
+        final var addFlowOutputRpcResult = rpcResultFuture.get();
         assertNotNull(addFlowOutputRpcResult);
         assertTrue(addFlowOutputRpcResult.isSuccessful());
     }