Fix codestyle
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / statistics / services / OpendaylightFlowStatisticsServiceImpl2Test.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.Collections;
13 import java.util.List;
14 import java.util.concurrent.Future;
15 import org.junit.Assert;
16 import org.junit.Test;
17 import org.mockito.ArgumentCaptor;
18 import org.mockito.Captor;
19 import org.mockito.Matchers;
20 import org.mockito.Mock;
21 import org.mockito.Mockito;
22 import org.mockito.invocation.InvocationOnMock;
23 import org.mockito.stubbing.Answer;
24 import org.opendaylight.openflowplugin.api.OFConstants;
25 import org.opendaylight.openflowplugin.api.openflow.device.MessageTranslator;
26 import org.opendaylight.openflowplugin.api.openflow.md.core.TranslatorKey;
27 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier;
28 import org.opendaylight.openflowplugin.impl.rpc.AbstractRequestContext;
29 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager;
30 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManagerFactory;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForGivenMatchInputBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.get.aggregate.flow.statistics.from.flow.table._for.given.match.output.AggregatedFlowStatistics;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.get.aggregate.flow.statistics.from.flow.table._for.given.match.output.AggregatedFlowStatisticsBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessageBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInput;
39 import org.opendaylight.yangtools.yang.common.RpcResult;
40 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
41
42 /**
43  * Test for {@link OpendaylightFlowStatisticsServiceImpl} - only not delegated method.
44  */
45 public class OpendaylightFlowStatisticsServiceImpl2Test extends AbstractStatsServiceTest {
46
47     @Captor
48     private ArgumentCaptor<MultipartRequestInput> requestInput;
49     @Mock
50     private MessageTranslator<MultipartReply, AggregatedFlowStatistics> translator;
51
52     private AbstractRequestContext<List<MultipartReply>> rqContextMp;
53
54     private OpendaylightFlowStatisticsServiceImpl flowStatisticsService;
55
56
57     public void setUp() {
58         final ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
59         flowStatisticsService = OpendaylightFlowStatisticsServiceImpl.createWithOook(rqContextStack,
60                                                                                      deviceContext,
61                                                                                      convertorManager);
62
63         rqContextMp = new AbstractRequestContext<List<MultipartReply>>(42L) {
64             @Override
65             public void close() {
66                 //NOOP
67             }
68         };
69         Mockito.when(rqContextStack.<List<MultipartReply>>createRequestContext()).thenReturn(rqContextMp);
70         Mockito.when(translatorLibrary
71                 .<MultipartReply, AggregatedFlowStatistics>lookupTranslator(Matchers.any(TranslatorKey.class)))
72                 .thenReturn(translator);
73     }
74
75     @Test
76     public void testGetAggregateFlowStatisticsFromFlowTableForGivenMatch() throws Exception {
77         Mockito.doAnswer(answerVoidToCallback).when(outboundQueueProvider)
78                 .commitEntry(Matchers.eq(42L), requestInput.capture(), Matchers.any(FutureCallback.class));
79         Mockito.doAnswer((Answer<Void>) invocation -> {
80             final MultipartReplyMessageBuilder messageBuilder = new MultipartReplyMessageBuilder()
81                     .setVersion(OFConstants.OFP_VERSION_1_3);
82
83             rqContextMp.setResult(RpcResultBuilder
84                     .success(Collections.<MultipartReply>singletonList(messageBuilder.build()))
85                     .build());
86             return null;
87         }).when(multiMsgCollector).endCollecting(Matchers.any(EventIdentifier.class));
88         Mockito.when(translator.translate(
89                         Matchers.any(MultipartReply.class), Matchers.same(deviceInfo), Matchers.isNull())
90         ).thenReturn(new AggregatedFlowStatisticsBuilder().build());
91
92         GetAggregateFlowStatisticsFromFlowTableForGivenMatchInputBuilder input =
93                 new GetAggregateFlowStatisticsFromFlowTableForGivenMatchInputBuilder()
94                         .setNode(createNodeRef("unitProt:123"))
95                         .setPriority(5)
96                         .setTableId((short) 1);
97
98         final Future<RpcResult<GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput>> resultFuture
99                 = flowStatisticsService.getAggregateFlowStatisticsFromFlowTableForGivenMatch(input.build());
100
101         Assert.assertTrue(resultFuture.isDone());
102         final RpcResult<GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput> rpcResult = resultFuture.get();
103         Assert.assertTrue(rpcResult.isSuccessful());
104         Assert.assertEquals(1, rpcResult.getResult().getAggregatedFlowStatistics().size());
105         Assert.assertEquals(MultipartType.OFPMPAGGREGATE, requestInput.getValue().getType());
106     }
107 }