Remove redundant exception declarations
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / services / ServiceMocking.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 package org.opendaylight.openflowplugin.impl.services;
9
10 import static org.mockito.Mockito.lenient;
11 import static org.mockito.Mockito.when;
12
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import java.math.BigInteger;
16 import org.junit.Before;
17 import org.junit.runner.RunWith;
18 import org.mockito.Matchers;
19 import org.mockito.Mock;
20 import org.mockito.junit.MockitoJUnitRunner;
21 import org.opendaylight.mdsal.binding.api.DataBroker;
22 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
23 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
24 import org.opendaylight.openflowplugin.api.OFConstants;
25 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
26 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
27 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
28 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
29 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
30 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
31 import org.opendaylight.openflowplugin.api.openflow.device.handlers.MultiMsgCollector;
32 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
33 import org.opendaylight.openflowplugin.extension.api.ConverterMessageToOFJava;
34 import org.opendaylight.openflowplugin.extension.api.ConvertorData;
35 import org.opendaylight.openflowplugin.extension.api.TypeVersionKey;
36 import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterProvider;
37 import org.opendaylight.openflowplugin.impl.device.DeviceContextImpl;
38 import org.opendaylight.openflowplugin.impl.registry.flow.DeviceFlowRegistryImpl;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ExperimenterId;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.experimenter.types.rev151020.experimenter.core.message.ExperimenterMessageOfChoice;
47 import org.opendaylight.yangtools.yang.binding.DataContainer;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
49 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
50 import org.opendaylight.yangtools.yang.common.RpcResult;
51 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
52
53 @RunWith(MockitoJUnitRunner.class)
54 public abstract class ServiceMocking {
55     protected static final BigInteger DUMMY_DATAPATH_ID = new BigInteger("444");
56     protected static final Short DUMMY_VERSION = OFConstants.OFP_VERSION_1_3;
57     protected static final Long DUMMY_XID_VALUE = 2121L;
58     protected static final Xid DUMMY_XID = new Xid(DUMMY_XID_VALUE);
59     protected static final long DUMMY_EXPERIMENTER_ID = 42L;
60
61     protected static final String DUMMY_NODE_ID = "444";
62     protected static final KeyedInstanceIdentifier<Node, NodeKey> DUMMY_NODE_II = InstanceIdentifier
63             .create(Nodes.class)
64             .child(Node.class, new NodeKey(new NodeId(DUMMY_NODE_ID)));
65
66     @Mock
67     protected RequestContextStack mockedRequestContextStack;
68     @Mock
69     protected ConnectionContext mockedPrimConnectionContext;
70     @Mock
71     protected FeaturesReply mockedFeatures;
72     @Mock
73     protected GetFeaturesOutput mockedFeaturesOutput;
74     @Mock
75     protected ConnectionAdapter mockedConnectionAdapter;
76     @Mock
77     protected MessageSpy mockedMessagSpy;
78     @Mock
79     protected DeviceContextImpl mockedDeviceContext;
80     @Mock
81     protected DeviceState mockedDeviceState;
82     @Mock
83     protected DeviceInfo mockedDeviceInfo;
84     @Mock
85     protected RequestContext mockedRequestContext;
86     @Mock
87     protected OutboundQueue mockedOutboundQueue;
88     @Mock
89     protected MultiMsgCollector multiMessageCollector;
90     @Mock
91     protected DataBroker dataBroker;
92     @Mock
93     protected ExtensionConverterProvider mockedExtensionConverterProvider;
94     @Mock
95     protected ConverterMessageToOFJava<ExperimenterMessageOfChoice, DataContainer,
96         ConvertorData> mockedExtensionConverter;
97
98     @Before
99     @SuppressWarnings("unchecked")
100     public void initialization() {
101         lenient().when(mockedExtensionConverter.getExperimenterId())
102                 .thenReturn(new ExperimenterId(DUMMY_EXPERIMENTER_ID));
103         lenient().when(mockedExtensionConverterProvider.getMessageConverter(Matchers.<TypeVersionKey>any()))
104                 .thenReturn(mockedExtensionConverter);
105         lenient().when(mockedRequestContextStack.createRequestContext()).thenReturn(mockedRequestContext);
106         lenient().when(mockedRequestContext.getXid()).thenReturn(DUMMY_XID);
107         lenient().when(mockedFeatures.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
108         lenient().when(mockedFeatures.getVersion()).thenReturn(DUMMY_VERSION);
109
110         lenient().when(mockedFeaturesOutput.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
111         lenient().when(mockedFeaturesOutput.getVersion()).thenReturn(DUMMY_VERSION);
112
113         lenient().when(mockedPrimConnectionContext.getFeatures()).thenReturn(mockedFeatures);
114         lenient().when(mockedPrimConnectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);
115         lenient().when(mockedPrimConnectionContext.getConnectionState())
116                 .thenReturn(ConnectionContext.CONNECTION_STATE.WORKING);
117         lenient().when(mockedPrimConnectionContext.getOutboundQueueProvider()).thenReturn(mockedOutboundQueue);
118
119         lenient().when(mockedDeviceInfo.getNodeInstanceIdentifier()).thenReturn(DUMMY_NODE_II);
120         when(mockedDeviceInfo.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
121         when(mockedDeviceInfo.getVersion()).thenReturn(DUMMY_VERSION);
122
123         lenient().when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimConnectionContext);
124         when(mockedDeviceContext.getMessageSpy()).thenReturn(mockedMessagSpy);
125         lenient().when(mockedDeviceContext.getDeviceFlowRegistry())
126                 .thenReturn(new DeviceFlowRegistryImpl(DUMMY_VERSION, dataBroker, DUMMY_NODE_II));
127         lenient().when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
128         when(mockedDeviceContext.getDeviceInfo()).thenReturn(mockedDeviceInfo);
129         lenient().when(mockedDeviceContext.getMultiMsgCollector(Matchers.any())).thenReturn(multiMessageCollector);
130
131         setup();
132     }
133
134     protected void setup() {
135         //NOOP - to be overloaded
136     }
137
138     protected  <T> void mockSuccessfulFuture() {
139         ListenableFuture<RpcResult<T>> dummySuccessfulFuture =
140                 Futures.immediateFuture(RpcResultBuilder.success((T) null).build());
141         when(mockedRequestContext.getFuture()).thenReturn(dummySuccessfulFuture);
142     }
143
144     protected  <T> void mockSuccessfulFuture(T result) {
145         ListenableFuture<RpcResult<T>> dummySuccessfulFuture = Futures.immediateFuture(RpcResultBuilder.success(result)
146                 .build());
147         when(mockedRequestContext.getFuture()).thenReturn(dummySuccessfulFuture);
148     }
149
150     protected ExperimenterMessageOfChoice mockExperimenter() {
151         return new DummyExperimenter();
152     }
153
154     public class DummyExperimenter implements ExperimenterMessageOfChoice {
155         @Override
156         public Class<? extends DataContainer> getImplementedInterface() {
157             return DummyExperimenter.class;
158         }
159     }
160 }