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