Reduce mocking in FlowDirectStatisticsServiceTest
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / statistics / services / direct / multilayer / FlowDirectStatisticsServiceTest.java
index 6f598f8737912d8828f4974b44a108873221cbec..3716d6f78eb42f2d4ff1898656cab5f028c64bc4 100644 (file)
@@ -5,30 +5,26 @@
  * 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.services.direct.multilayer;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
-import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.eq;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
-import java.math.BigInteger;
-import java.util.Collections;
 import java.util.List;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
 import org.opendaylight.openflowplugin.impl.registry.flow.FlowDescriptorFactory;
 import org.opendaylight.openflowplugin.impl.statistics.services.direct.AbstractDirectStatisticsServiceTest;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetFlowStatisticsInput;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetFlowStatisticsOutput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetFlowStatisticsInputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetFlowStatisticsOutputBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowAndStatisticsMap;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapListBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlags;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
@@ -36,18 +32,19 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyFlowCase;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.MultipartReplyFlow;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.multipart.reply.flow.FlowStats;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.multipart.reply.flow.FlowStatsBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestFlowCase;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.flow._case.MultipartRequestFlow;
+import org.opendaylight.yangtools.yang.common.Uint32;
+import org.opendaylight.yangtools.yang.common.Uint64;
+import org.opendaylight.yangtools.yang.common.Uint8;
 
 public class FlowDirectStatisticsServiceTest extends AbstractDirectStatisticsServiceTest {
-    static final Short TABLE_NO = 1;
-    private FlowDirectStatisticsService service;
+    static final Uint8 TABLE_NO = Uint8.ONE;
+    private MultiGetFlowStatistics service;
 
     @Override
-    public void setUp() throws Exception {
-        service = new FlowDirectStatisticsService(requestContextStack,
+    public void setUp() {
+        service = new MultiGetFlowStatistics(requestContextStack,
                                                   deviceContext,
                                                   convertorManager,
                                                   multipartWriterProvider);
@@ -57,65 +54,53 @@ public class FlowDirectStatisticsServiceTest extends AbstractDirectStatisticsSer
     }
 
     @Override
-    public void testBuildRequestBody() throws Exception {
-        final GetFlowStatisticsInput input = mock(GetFlowStatisticsInput.class);
-
-        when(input.getNode()).thenReturn(createNodeRef(NODE_ID));
-        when(input.getTableId()).thenReturn(TABLE_NO);
-
-        final MultipartRequestFlowCase body = (MultipartRequestFlowCase) ((MultipartRequestInput) service
-            .buildRequest(new Xid(42L), input))
+    public void testBuildRequestBody() {
+        final var body = (MultipartRequestFlowCase) ((MultipartRequestInput) service
+            .buildRequest(new Xid(Uint32.valueOf(42L)), new GetFlowStatisticsInputBuilder()
+                .setTableId(TABLE_NO)
+                .build()))
             .getMultipartRequestBody();
 
-        final MultipartRequestFlow flow = body.getMultipartRequestFlow();
+        final var flow = body.getMultipartRequestFlow();
 
         assertEquals(TABLE_NO, flow.getTableId());
     }
 
     @Override
-    public void testBuildReply() throws Exception {
+    public void testBuildReply() {
         final MultipartReply reply = mock(MultipartReply.class);
         final MultipartReplyFlowCase flowCase = mock(MultipartReplyFlowCase.class);
         final MultipartReplyFlow flow = mock(MultipartReplyFlow.class);
-        final FlowStats flowStat = new FlowStatsBuilder()
-                .setDurationSec(1L)
-                .setDurationNsec(1L)
-                .setTableId(TABLE_NO)
-                .setByteCount(BigInteger.ONE)
-                .setPacketCount(BigInteger.ONE)
-                .setFlags(mock(FlowModFlags.class))
-                .setMatch(new org.opendaylight.yang.gen.v1.urn
-                        .opendaylight.openflow.oxm.rev150225.match.grouping.MatchBuilder()
-                        .setMatchEntry(Collections.emptyList())
-                        .build())
-                .build();
 
-        final List<FlowStats> flowStats = Collections.singletonList(flowStat);
-        final List<MultipartReply> input = Collections.singletonList(reply);
-
-        when(flow.getFlowStats()).thenReturn(flowStats);
+        when(flow.getFlowStats()).thenReturn(List.of(new FlowStatsBuilder()
+            .setDurationSec(Uint32.ONE)
+            .setDurationNsec(Uint32.ONE)
+            .setTableId(TABLE_NO)
+            .setByteCount(Uint64.ONE)
+            .setPacketCount(Uint64.ONE)
+            .setFlags(mock(FlowModFlags.class))
+            .setMatch(new org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.grouping
+                .MatchBuilder().setMatchEntry(List.of()).build())
+            .build()));
         when(flowCase.getMultipartReplyFlow()).thenReturn(flow);
         when(reply.getMultipartReplyBody()).thenReturn(flowCase);
 
-        final GetFlowStatisticsOutput output = service.buildReply(input, true);
-        assertTrue(output.getFlowAndStatisticsMapList().size() > 0);
+        final var output = service.buildReply(List.of(reply), true);
+        assertTrue(output.nonnullFlowAndStatisticsMapList().size() > 0);
 
-        final FlowAndStatisticsMap stats = output.getFlowAndStatisticsMapList().get(0);
+        final var stats = output.nonnullFlowAndStatisticsMapList().iterator().next();
 
         assertEquals(stats.getTableId(), TABLE_NO);
     }
 
     @Override
-    public void testStoreStatistics() throws Exception {
-        final FlowAndStatisticsMapList stat = mock(FlowAndStatisticsMapList.class);
-        when(stat.getTableId()).thenReturn(TABLE_NO);
-        when(stat.getMatch()).thenReturn(new MatchBuilder().build());
-
-        final List<FlowAndStatisticsMapList> stats = Collections.singletonList(stat);
-        final GetFlowStatisticsOutput output = mock(GetFlowStatisticsOutput.class);
-        when(output.getFlowAndStatisticsMapList()).thenReturn(stats);
-
-        multipartWriterProvider.lookup(MultipartType.OFPMPFLOW).get().write(output, true);
+    public void testStoreStatistics() {
+        multipartWriterProvider.lookup(MultipartType.OFPMPFLOW).orElseThrow().write(new GetFlowStatisticsOutputBuilder()
+            .setFlowAndStatisticsMapList(List.of(new FlowAndStatisticsMapListBuilder()
+                .setTableId(TABLE_NO)
+                .setMatch(new MatchBuilder().build())
+                .build()))
+            .build(), true);
         verify(deviceContext).writeToTransactionWithParentsSlow(eq(LogicalDatastoreType.OPERATIONAL), any(), any());
     }
 }