85124809af545b8f3285809aaf562be266a3d852
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / services / ServiceMocking.java
1 package org.opendaylight.openflowplugin.impl.services;
2
3 import static org.mockito.Mockito.when;
4
5 import com.google.common.util.concurrent.Futures;
6 import com.google.common.util.concurrent.ListenableFuture;
7 import java.math.BigInteger;
8 import org.junit.Before;
9 import org.junit.runner.RunWith;
10 import org.mockito.Matchers;
11 import org.mockito.Mock;
12 import org.mockito.runners.MockitoJUnitRunner;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
15 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
16 import org.opendaylight.openflowplugin.api.OFConstants;
17 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
18 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
19 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
20 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
21 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
22 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
23 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
24 import org.opendaylight.openflowplugin.api.openflow.device.handlers.MultiMsgCollector;
25 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
26 import org.opendaylight.openflowplugin.impl.device.DeviceContextImpl;
27 import org.opendaylight.openflowplugin.impl.registry.flow.DeviceFlowRegistryImpl;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
36 import org.opendaylight.yangtools.yang.common.RpcResult;
37 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
38
39 @RunWith(MockitoJUnitRunner.class)
40 public abstract class ServiceMocking {
41     private static final BigInteger DUMMY_DATAPATH_ID = new BigInteger("444");
42     private static final Short DUMMY_VERSION = OFConstants.OFP_VERSION_1_3;
43     private static final Long DUMMY_XID_VALUE = 2121L;
44     private static final Xid DUMMY_XID = new Xid(DUMMY_XID_VALUE);
45
46     protected static final String DUMMY_NODE_ID = "dummyNodeID";
47     private static final KeyedInstanceIdentifier<Node, NodeKey> NODE_II
48             = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(DUMMY_NODE_ID)));
49
50     @Mock
51     protected RequestContextStack mockedRequestContextStack;
52     @Mock
53     protected ConnectionContext mockedPrimConnectionContext;
54     @Mock
55     protected FeaturesReply mockedFeatures;
56     @Mock
57     protected GetFeaturesOutput mockedFeaturesOutput;
58     @Mock
59     protected ConnectionAdapter mockedConnectionAdapter;
60     @Mock
61     protected MessageSpy mockedMessagSpy;
62     @Mock
63     protected DeviceContextImpl mockedDeviceContext;
64     @Mock
65     protected DeviceState mockedDeviceState;
66     @Mock
67     protected DeviceInfo mockedDeviceInfo;
68     @Mock
69     protected DeviceInitializationPhaseHandler mockedDevicePhaseHandler;
70     @Mock
71     protected RequestContext mockedRequestContext;
72     @Mock
73     protected OutboundQueue mockedOutboundQueue;
74     @Mock
75     protected MultiMsgCollector multiMessageCollector;
76     @Mock
77     protected DataBroker dataBroker;
78
79     @Before
80     public void initialization() {
81         when(mockedRequestContextStack.createRequestContext()).thenReturn(mockedRequestContext);
82         when(mockedRequestContext.getXid()).thenReturn(DUMMY_XID);
83
84         when(mockedFeatures.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
85         when(mockedFeatures.getVersion()).thenReturn(DUMMY_VERSION);
86
87         when(mockedFeaturesOutput.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
88         when(mockedFeaturesOutput.getVersion()).thenReturn(DUMMY_VERSION);
89
90         when(mockedPrimConnectionContext.getFeatures()).thenReturn(mockedFeatures);
91         when(mockedPrimConnectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);
92         when(mockedPrimConnectionContext.getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.WORKING);
93         when(mockedPrimConnectionContext.getOutboundQueueProvider()).thenReturn(mockedOutboundQueue);
94
95         when(mockedDeviceInfo.getNodeInstanceIdentifier()).thenReturn(NODE_II);
96         when(mockedDeviceInfo.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
97         when(mockedDeviceInfo.getVersion()).thenReturn(DUMMY_VERSION);
98
99         when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimConnectionContext);
100         when(mockedDeviceContext.getMessageSpy()).thenReturn(mockedMessagSpy);
101         when(mockedDeviceContext.getDeviceFlowRegistry()).thenReturn(new DeviceFlowRegistryImpl(dataBroker, NODE_II));
102         when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
103         when(mockedDeviceContext.getDeviceInfo()).thenReturn(mockedDeviceInfo);
104         when(mockedDeviceContext.getMultiMsgCollector(Matchers.any())).thenReturn(multiMessageCollector);
105
106         setup();
107     }
108
109     protected void setup() {
110         //NOOP - to be overloaded
111     }
112
113
114     protected  <T> void mockSuccessfulFuture() {
115         ListenableFuture<RpcResult<T>> dummySuccessfulFuture = Futures.immediateFuture(RpcResultBuilder.success((T) null).build());
116         when(mockedRequestContext.getFuture()).thenReturn(dummySuccessfulFuture);
117     }
118
119 }