Merge "Coverage - ofp/impl/statistics/services"
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / statistics / services / AbstractStatsServiceTest.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 org.junit.Before;
13 import org.junit.runner.RunWith;
14 import org.mockito.Matchers;
15 import org.mockito.Mock;
16 import org.mockito.Mockito;
17 import org.mockito.invocation.InvocationOnMock;
18 import org.mockito.runners.MockitoJUnitRunner;
19 import org.mockito.stubbing.Answer;
20 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
21 import org.opendaylight.openflowplugin.api.OFConstants;
22 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
23 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
24 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
25 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
26 import org.opendaylight.openflowplugin.api.openflow.device.TranslatorLibrary;
27 import org.opendaylight.openflowplugin.api.openflow.device.handlers.MultiMsgCollector;
28 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
29 import org.opendaylight.openflowplugin.openflow.md.util.OpenflowPortsUtil;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38
39 /**
40  * Created by mirehak on 7/23/15.
41  */
42 @RunWith(MockitoJUnitRunner.class)
43 public abstract class AbstractStatsServiceTest {
44     @Mock
45     RequestContextStack rqContextStack;
46     @Mock
47     DeviceContext deviceContext;
48     @Mock
49     ConnectionContext connectionContext;
50     @Mock
51     FeaturesReply features;
52     @Mock
53     MessageSpy messageSpy;
54     @Mock
55     OutboundQueue outboundQueueProvider;
56     @Mock
57     MultiMsgCollector multiMsgCollector;
58     @Mock
59     TranslatorLibrary translatorLibrary;
60
61     final Answer<Void> answerVoidToCallback = new Answer<Void>() {
62         @Override
63         public Void answer(InvocationOnMock invocation) throws Throwable {
64             final FutureCallback<OfHeader> callback = (FutureCallback<OfHeader>) (invocation.getArguments()[2]);
65             callback.onSuccess(null);
66             return null;
67         }
68     };
69
70     /**
71      * default ctor
72      */
73     public AbstractStatsServiceTest() {
74         OpenflowPortsUtil.init();
75     }
76
77     @Before
78     public void init() throws Exception {
79         Mockito.when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
80         Mockito.when(deviceContext.getMessageSpy()).thenReturn(messageSpy);
81         Mockito.when(deviceContext.getMultiMsgCollector(Matchers.any(RequestContext.class))).thenReturn(multiMsgCollector);
82         Mockito.when(deviceContext.oook()).thenReturn(translatorLibrary);
83         Mockito.when(connectionContext.getFeatures()).thenReturn(features);
84         Mockito.when(connectionContext.getOutboundQueueProvider()).thenReturn(outboundQueueProvider);
85         Mockito.when(features.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
86
87         setUp();
88     }
89
90     protected void setUp() {
91         //NOOP
92     }
93
94     static NodeRef createNodeRef(String nodeIdValue) {
95         InstanceIdentifier<Node> nodePath = InstanceIdentifier.create(Nodes.class)
96                 .child(Node.class, new NodeKey(new NodeId(nodeIdValue)));
97         return new NodeRef(nodePath);
98     }
99 }