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