Decompose RPC implementation classes
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / services / sal / SalFlowServiceImplTest.java
index 4f1fd3c62669d42370a40873ee5f5fa87cf0b50f..9a32c10df6166632dbbc5e7c23b86d65d4ed98c8 100644 (file)
@@ -7,21 +7,21 @@
  */
 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.math.BigInteger;
+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.Matchers;
 import org.mockito.Mock;
-import org.mockito.Mockito;
-import org.mockito.runners.MockitoJUnitRunner;
+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;
@@ -33,10 +33,7 @@ import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
 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.registry.flow.FlowDescriptor;
-import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
-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;
@@ -46,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;
@@ -58,6 +53,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowRef;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
 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;
@@ -68,20 +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;
-
-@RunWith(MockitoJUnitRunner.class)
-public class SalFlowServiceImplTest extends TestCase {
-
-    private static final BigInteger DUMMY_DATAPATH_ID = new BigInteger("444");
+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.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 Uint8 DUMMY_TABLE_ID = Uint8.ZERO;
+
+    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<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 static final KeyedInstanceIdentifier<Table, TableKey> TABLE_II
-            = NODE_II.augmentation(FlowCapableNode.class).child(Table.class, new TableKey(DUMMY_TABLE_ID));
+    private final NodeRef noderef = new NodeRef(NODE_II);
 
     @Mock
     private RequestContextStack mockedRequestContextStack;
@@ -113,35 +114,47 @@ public class SalFlowServiceImplTest extends TestCase {
 
     @Before
     public void initialization() {
-        when(mockedFeatures.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
-        when(mockedFeaturesOutput.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
 
-        when(mockedPrimConnectionContext.getFeatures()).thenReturn(mockedFeatures);
-        when(mockedPrimConnectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);
         when(mockedPrimConnectionContext.getOutboundQueueProvider()).thenReturn(outboundQueue);
 
         when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimConnectionContext);
         when(mockedDeviceContext.getMessageSpy()).thenReturn(mockedMessagSpy);
         when(mockedDeviceContext.getDeviceFlowRegistry()).thenReturn(deviceFlowRegistry);
 
-        when(requestContext.getXid()).thenReturn(new Xid(84L));
+        when(requestContext.getXid()).thenReturn(new Xid(Uint32.valueOf(84L)));
         when(requestContext.getFuture()).thenReturn(RpcResultBuilder.success().buildFuture());
         when(mockedRequestContextStack.createRequestContext()).thenReturn(requestContext);
 
-        when(mockedDeviceInfo.getNodeInstanceIdentifier()).thenReturn(NODE_II);
         when(mockedDeviceInfo.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
 
-        when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
         when(mockedDeviceContext.getDeviceInfo()).thenReturn(mockedDeviceInfo);
     }
 
-    private SalFlowServiceImpl mockSalFlowService(final short version) {
+    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 ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
-        return new SalFlowServiceImpl(mockedRequestContextStack, mockedDeviceContext, convertorManager);
+        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 var convertorManager = ConvertorManagerFactory.createDefaultManager();
+        return new UpdateFlowImpl(mockedRequestContextStack, mockedDeviceContext, convertorManager);
     }
 
     @Test
@@ -160,21 +173,20 @@ public class SalFlowServiceImplTest extends TestCase {
         addFlowFailCallback(OFConstants.OFP_VERSION_1_3);
     }
 
-    private void addFlowFailCallback(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();
 
-        mockingFlowRegistryLookup();
-        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());
     }
@@ -189,20 +201,20 @@ public class SalFlowServiceImplTest extends TestCase {
         removeFlowFailCallback(OFConstants.OFP_VERSION_1_3);
     }
 
-    private void removeFlowFailCallback(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());
     }
@@ -213,16 +225,15 @@ public class SalFlowServiceImplTest extends TestCase {
         addFlow(OFConstants.OFP_VERSION_1_3);
     }
 
-    private void addFlow(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);
 
-        mockingFlowRegistryLookup();
-        verifyOutput(salFlowService.addFlow(mockedAddFlowInput));
+        verifyOutput(addFlow.invoke(mockedAddFlowInput));
     }
 
     @Test
@@ -237,14 +248,15 @@ public class SalFlowServiceImplTest extends TestCase {
         removeFlow(OFConstants.OFP_VERSION_1_3);
     }
 
-    private void removeFlow(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
@@ -259,64 +271,53 @@ public class SalFlowServiceImplTest extends TestCase {
         updateFlow(OFConstants.OFP_VERSION_1_3);
     }
 
-    private void updateFlow(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);
-
-        verifyOutput(salFlowService.updateFlow(mockedUpdateFlowInput));
-        verifyOutput(salFlowService.updateFlow(mockedUpdateFlowInput1));
-    }
-
-    private void mockingFlowRegistryLookup() {
-        FlowDescriptor mockedFlowDescriptor = mock(FlowDescriptor.class);
-        FlowId flowId = new FlowId(DUMMY_FLOW_ID);
-        when(mockedFlowDescriptor.getFlowId()).thenReturn(flowId);
-        when(mockedFlowDescriptor.getTableKey()).thenReturn(new TableKey(DUMMY_TABLE_ID));
-
-        when(deviceFlowRegistry
-                .retrieveDescriptor(Matchers.any(FlowRegistryKey.class))).thenReturn(mockedFlowDescriptor);
+        final var updateFlow = mockUpdateFlow(version);
+        verifyOutput(updateFlow.invoke(mockedUpdateFlowInput));
+        verifyOutput(updateFlow.invoke(mockedUpdateFlowInput1));
     }
 
-    private <T extends DataObject> void verifyOutput(Future<RpcResult<T>> rpcResultFuture) throws ExecutionException,
-                                                                                                  InterruptedException {
+    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());
     }