Merge "Technical Debt part 1"
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / services / SalFlatBatchServiceImplTest.java
index 51fa0691d084fd3dc40514b4dbffe64b4ff51d0f..889ee00e5c844c8db4c000eee0f43beb9474767e 100644 (file)
@@ -28,14 +28,15 @@ import org.mockito.Mock;
 import org.mockito.Mockito;
 import org.mockito.runners.MockitoJUnitRunner;
 import org.opendaylight.openflowplugin.impl.services.batch.BatchPlanStep;
+import org.opendaylight.openflowplugin.impl.services.batch.BatchStepJob;
 import org.opendaylight.openflowplugin.impl.services.batch.BatchStepType;
+import org.opendaylight.openflowplugin.impl.util.FlatBatchUtil;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.ProcessFlatBatchInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.ProcessFlatBatchInputBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.ProcessFlatBatchOutput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.ProcessFlatBatchOutputBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.Batch;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.BatchBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchAddFlowCase;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchAddFlowCaseBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchAddGroupCaseBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchAddMeterCaseBuilder;
@@ -117,10 +118,6 @@ public class SalFlatBatchServiceImplTest {
     private SalGroupsBatchService salGroupsBatchService;
     @Mock
     private SalMetersBatchService salMetersBatchService;
-    @Mock
-    private AsyncFunction<RpcResult<ProcessFlatBatchOutput>, RpcResult<ProcessFlatBatchOutput>> chainElement1;
-    @Mock
-    private AsyncFunction<RpcResult<ProcessFlatBatchOutput>, RpcResult<ProcessFlatBatchOutput>> chainElement2;
     @Captor
     private ArgumentCaptor<AddFlowsBatchInput> addFlowsBatchInputCpt;
 
@@ -234,6 +231,8 @@ public class SalFlatBatchServiceImplTest {
         final InOrder inOrder = Mockito.inOrder(salFlowsBatchService, salGroupsBatchService, salMetersBatchService);
         inOrder.verify(salFlowsBatchService).addFlowsBatch(Matchers.<AddFlowsBatchInput>any());
         inOrder.verify(salFlowsBatchService).removeFlowsBatch(Matchers.<RemoveFlowsBatchInput>any());
+        inOrder.verify(salFlowsBatchService).updateFlowsBatch(Matchers.<UpdateFlowsBatchInput>any());
+        inOrder.verify(salGroupsBatchService).addGroupsBatch(Matchers.<AddGroupsBatchInput>any());
     }
 
     @Test
@@ -436,22 +435,32 @@ public class SalFlatBatchServiceImplTest {
 
     @Test
     public void testExecuteBatchPlan() throws Exception {
-        final ListenableFuture<RpcResult<ProcessFlatBatchOutput>> succeededChainOutput =
-                RpcResultBuilder.<ProcessFlatBatchOutput>success().buildFuture();
+        BatchStepJob batchStepJob1 = Mockito.mock(BatchStepJob.class);
+        BatchStepJob batchStepJob2 = Mockito.mock(BatchStepJob.class);
+        AsyncFunction<RpcResult<ProcessFlatBatchOutput>, RpcResult<ProcessFlatBatchOutput>> function1 = Mockito.mock(AsyncFunction.class);
+        AsyncFunction<RpcResult<ProcessFlatBatchOutput>, RpcResult<ProcessFlatBatchOutput>> function2 = Mockito.mock(AsyncFunction.class);
+        Mockito.when(batchStepJob1.getStepFunction()).thenReturn(function1);
+        Mockito.when(batchStepJob2.getStepFunction()).thenReturn(function2);
+        BatchPlanStep batchPlanStep1 = new BatchPlanStep(BatchStepType.GROUP_ADD);
+        batchPlanStep1.setBarrierAfter(true);
+        BatchPlanStep batchPlanStep2 = new BatchPlanStep(BatchStepType.FLOW_ADD);
+        batchPlanStep1.setBarrierAfter(false);
+        Mockito.when(batchStepJob1.getPlanStep()).thenReturn(batchPlanStep1);
+        Mockito.when(batchStepJob2.getPlanStep()).thenReturn(batchPlanStep2);
+
+        final ListenableFuture<RpcResult<ProcessFlatBatchOutput>> succeededChainOutput = FlatBatchUtil.createEmptyRpcBatchResultFuture(true);
         final ListenableFuture<RpcResult<ProcessFlatBatchOutput>> failedChainOutput =
                 RpcResultBuilder.<ProcessFlatBatchOutput>failed()
                         .withError(RpcError.ErrorType.APPLICATION, "ut-chainError")
-                        .withResult(createFlatBatchOutput(
-                                createFlowBatchFailure(0, "f1"), createFlowBatchFailure(1, "f2")))
+                        .withResult(createFlatBatchOutput(createFlowBatchFailure(0, "f1"), createFlowBatchFailure(1, "f2")))
                         .buildFuture();
 
-        Mockito.when(chainElement1.apply(Matchers.<RpcResult<ProcessFlatBatchOutput>>any()))
+        Mockito.when(batchStepJob1.getStepFunction().apply(Matchers.<RpcResult<ProcessFlatBatchOutput>>any()))
                 .thenReturn(succeededChainOutput);
-        Mockito.when(chainElement2.apply(Matchers.<RpcResult<ProcessFlatBatchOutput>>any()))
+        Mockito.when(batchStepJob2.getStepFunction().apply(Matchers.<RpcResult<ProcessFlatBatchOutput>>any()))
                 .thenReturn(failedChainOutput);
 
-        final List<AsyncFunction<RpcResult<ProcessFlatBatchOutput>, RpcResult<ProcessFlatBatchOutput>>> batchChainElements =
-                Lists.newArrayList(chainElement1, chainElement2);
+        final List<BatchStepJob> batchChainElements = Lists.newArrayList(batchStepJob1, batchStepJob2);
         final Future<RpcResult<ProcessFlatBatchOutput>> rpcResultFuture = salFlatBatchService.executeBatchPlan(batchChainElements);
 
         Assert.assertTrue(rpcResultFuture.isDone());
@@ -479,17 +488,14 @@ public class SalFlatBatchServiceImplTest {
 
     @Test
     public void testPrepareBatchPlan_success() throws Exception {
-        final FlatBatchAddFlowCase flatBatchAddFlowCase = new FlatBatchAddFlowCaseBuilder()
-                .setFlatBatchAddFlow(Collections.singletonList(new FlatBatchAddFlowBuilder()
-                        .setFlowId(new FlowId("f1"))
-                        .build()))
+        final FlatBatchAddFlow flatBatchAddFlow = new FlatBatchAddFlowBuilder()
+                .setFlowId(new FlowId("f1"))
                 .build();
-        final BatchPlanStep batchPlanStep =
-                new BatchPlanStep(BatchStepType.FLOW_ADD);
+        final BatchPlanStep batchPlanStep = new BatchPlanStep(BatchStepType.FLOW_ADD);
+        batchPlanStep.getTaskBag().addAll(Lists.newArrayList(flatBatchAddFlow, flatBatchAddFlow));
         final List<BatchPlanStep> batchPlan = Lists.newArrayList(batchPlanStep);
 
-        final List<AsyncFunction<RpcResult<ProcessFlatBatchOutput>, RpcResult<ProcessFlatBatchOutput>>> batchChain =
-                salFlatBatchService.prepareBatchChain(batchPlan, NODE_REF, true);
+        final List<BatchStepJob> batchChain = salFlatBatchService.prepareBatchChain(batchPlan, NODE_REF, true);
 
         Assert.assertEquals(1, batchChain.size());
 
@@ -513,16 +519,12 @@ public class SalFlatBatchServiceImplTest {
         final FlatBatchAddFlow flatBatchAddFlow = new FlatBatchAddFlowBuilder()
                 .setFlowId(new FlowId("f1"))
                 .build();
-        final BatchPlanStep batchPlanStep =
-                new BatchPlanStep(BatchStepType.FLOW_ADD);
-        batchPlanStep.getTaskBag().addAll(Lists.newArrayList(
-                flatBatchAddFlow,
-                flatBatchAddFlow));
+        final BatchPlanStep batchPlanStep = new BatchPlanStep(BatchStepType.FLOW_ADD);
+        batchPlanStep.getTaskBag().addAll(Lists.newArrayList(flatBatchAddFlow, flatBatchAddFlow));
 
         final List<BatchPlanStep> batchPlan = Lists.newArrayList(batchPlanStep, batchPlanStep);
 
-        final List<AsyncFunction<RpcResult<ProcessFlatBatchOutput>, RpcResult<ProcessFlatBatchOutput>>> batchChain =
-                salFlatBatchService.prepareBatchChain(batchPlan, NODE_REF, true);
+        final List<BatchStepJob> batchChain = salFlatBatchService.prepareBatchChain(batchPlan, NODE_REF, true);
 
         Assert.assertEquals(2, batchChain.size());
 
@@ -547,10 +549,10 @@ public class SalFlatBatchServiceImplTest {
         Assert.assertTrue(rpcResultFuture.isDone());
         final RpcResult<ProcessFlatBatchOutput> rpcResult = rpcResultFuture.get();
         Assert.assertFalse(rpcResult.isSuccessful());
-        Assert.assertEquals(1, rpcResult.getErrors().size());
-        Assert.assertEquals(2, rpcResult.getResult().getBatchFailure().size());
+        Assert.assertEquals(2, rpcResult.getErrors().size());
+        Assert.assertEquals(4, rpcResult.getResult().getBatchFailure().size());
 
-        Mockito.verify(salFlowsBatchService).addFlowsBatch(addFlowsBatchInputCpt.capture());
+        Mockito.verify(salFlowsBatchService, Mockito.times(2)).addFlowsBatch(addFlowsBatchInputCpt.capture());
         Assert.assertEquals(2, addFlowsBatchInputCpt.getValue().getBatchAddFlows().size());
     }
 }
\ No newline at end of file