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