Coverage - ofp/impl/statistics/services
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / statistics / services / OpendaylightFlowTableStatisticsServiceImplTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.openflowplugin.impl.statistics.services;
10
11 import com.google.common.util.concurrent.FutureCallback;
12 import java.util.concurrent.Future;
13 import org.junit.Assert;
14 import org.junit.Test;
15 import org.mockito.ArgumentCaptor;
16 import org.mockito.Captor;
17 import org.mockito.Matchers;
18 import org.mockito.Mockito;
19 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
20 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
21 import org.opendaylight.openflowplugin.impl.rpc.AbstractRequestContext;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.GetFlowTablesStatisticsInputBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.GetFlowTablesStatisticsOutput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestTableCase;
28 import org.opendaylight.yangtools.yang.common.RpcResult;
29
30 /**
31  * Test for {@link OpendaylightFlowTableStatisticsServiceImpl}
32  */
33 public class OpendaylightFlowTableStatisticsServiceImplTest extends AbstractStatsServiceTest {
34
35     @Captor
36     private ArgumentCaptor<MultipartRequestInput> requestInput;
37
38     private RequestContext<Object> rqContext;
39
40     private OpendaylightFlowTableStatisticsServiceImpl flowTableStatisticsService;
41
42     public void setUp() {
43         flowTableStatisticsService = new OpendaylightFlowTableStatisticsServiceImpl(rqContextStack, deviceContext);
44
45         rqContext = new AbstractRequestContext<Object>(42L) {
46             @Override
47             public void close() {
48                 //NOOP
49             }
50         };
51         Mockito.when(rqContextStack.<Object>createRequestContext()).thenReturn(rqContext);
52     }
53
54     @Test
55     public void testGetFlowTablesStatistics() throws Exception {
56         Mockito.doAnswer(answerVoidToCallback).when(outboundQueueProvider)
57                 .commitEntry(Matchers.eq(42L), requestInput.capture(), Matchers.any(FutureCallback.class));
58
59         GetFlowTablesStatisticsInputBuilder input = new GetFlowTablesStatisticsInputBuilder()
60                 .setNode(createNodeRef("unitProt:123"));
61
62         final Future<RpcResult<GetFlowTablesStatisticsOutput>> resultFuture
63                 = flowTableStatisticsService.getFlowTablesStatistics(input.build());
64
65         Assert.assertTrue(resultFuture.isDone());
66         final RpcResult<GetFlowTablesStatisticsOutput> rpcResult = resultFuture.get();
67         Assert.assertTrue(rpcResult.isSuccessful());
68         Assert.assertEquals(MultipartType.OFPMPTABLE, requestInput.getValue().getType());
69     }
70
71     @Test
72     public void testBuildRequest() throws Exception {
73         Xid xid = new Xid(42L);
74         GetFlowTablesStatisticsInputBuilder input = new GetFlowTablesStatisticsInputBuilder()
75                 .setNode(createNodeRef("unitProt:123"));
76         final OfHeader request = flowTableStatisticsService.buildRequest(xid, input.build());
77         Assert.assertTrue(request instanceof MultipartRequestInput);
78         final MultipartRequestInput mpRequest = (MultipartRequestInput) request;
79         Assert.assertEquals(MultipartType.OFPMPTABLE, mpRequest.getType());
80         Assert.assertTrue(mpRequest.getMultipartRequestBody() instanceof MultipartRequestTableCase);
81         final MultipartRequestTableCase mpRequestBody = (MultipartRequestTableCase) (mpRequest.getMultipartRequestBody());
82         Assert.assertTrue(mpRequestBody.getMultipartRequestTable().isEmpty());
83     }
84 }