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