BUG-4340: raise test coverage to 80% - services related
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / services / SalFlowServiceImplTest.java
1 package org.opendaylight.openflowplugin.impl.services;
2
3
4 import static org.mockito.Mockito.mock;
5 import static org.mockito.Mockito.when;
6
7 import com.google.common.util.concurrent.Futures;
8 import java.math.BigInteger;
9 import java.util.concurrent.ExecutionException;
10 import java.util.concurrent.Future;
11 import junit.framework.TestCase;
12 import org.junit.Before;
13 import org.junit.Test;
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.runners.MockitoJUnitRunner;
19 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
20 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
21 import org.opendaylight.openflowplugin.api.OFConstants;
22 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
23 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
24 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
25 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
26 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
27 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
28 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
29 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowDescriptor;
30 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
31 import org.opendaylight.openflowplugin.api.openflow.rpc.listener.ItemLifecycleListener;
32 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowOutput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlow;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlow;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowRef;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
53 import org.opendaylight.yangtools.yang.binding.DataObject;
54 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
55 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
56 import org.opendaylight.yangtools.yang.common.RpcResult;
57 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
58
59 @RunWith(MockitoJUnitRunner.class)
60 public class SalFlowServiceImplTest extends TestCase {
61
62     private static final BigInteger DUMMY_DATAPATH_ID = new BigInteger("444");
63     private static final Short DUMMY_VERSION = OFConstants.OFP_VERSION_1_3;
64     private static final String DUMMY_NODE_ID = "dummyNodeID";
65     private static final String DUMMY_FLOW_ID = "dummyFlowID";
66     private static final Short DUMMY_TABLE_ID = (short) 0;
67
68     private static final KeyedInstanceIdentifier<Node, NodeKey> NODE_II
69             = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(DUMMY_NODE_ID)));
70
71     private static final KeyedInstanceIdentifier<Table, TableKey> TABLE_II
72             = NODE_II.augmentation(FlowCapableNode.class).child(Table.class, new TableKey(DUMMY_TABLE_ID));
73
74     @Mock
75     private RequestContextStack mockedRequestContextStack;
76     @Mock
77     private DeviceContext mockedDeviceContext;
78     @Mock
79     private ConnectionContext mockedPrimConnectionContext;
80     @Mock
81     private FeaturesReply mockedFeatures;
82     @Mock
83     private ConnectionAdapter mockedConnectionAdapter;
84     @Mock
85     private MessageSpy mockedMessagSpy;
86     @Mock
87     private RequestContext<Object> requestContext;
88     @Mock
89     private OutboundQueue outboundQueue;
90     @Mock
91     private Match match;
92     private SalFlowServiceImpl salFlowService;
93
94     @Mock
95     DeviceState mockedDeviceState;
96     @Mock
97     private DeviceFlowRegistry deviceFlowRegistry;
98
99     @Before
100     public void initialization() {
101         when(mockedFeatures.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
102         when(mockedFeatures.getVersion()).thenReturn(DUMMY_VERSION);
103
104         when(mockedPrimConnectionContext.getFeatures()).thenReturn(mockedFeatures);
105         when(mockedPrimConnectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);
106         when(mockedPrimConnectionContext.getOutboundQueueProvider()).thenReturn(outboundQueue);
107
108         when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimConnectionContext);
109
110         when(mockedDeviceContext.getMessageSpy()).thenReturn(mockedMessagSpy);
111         when(mockedDeviceContext.getDeviceFlowRegistry()).thenReturn(deviceFlowRegistry);
112         when(mockedRequestContextStack.createRequestContext()).thenReturn(requestContext);
113
114         when(requestContext.getXid()).thenReturn(new Xid(84L));
115         when(requestContext.getFuture()).thenReturn(RpcResultBuilder.success().buildFuture());
116
117         salFlowService = new SalFlowServiceImpl(mockedRequestContextStack, mockedDeviceContext);
118
119
120         when(mockedDeviceState.getNodeInstanceIdentifier()).thenReturn(NODE_II);
121         when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
122     }
123
124     @Test
125     public void testAddFlow() throws Exception {
126         addFlow(null);
127     }
128
129     @Test
130     public void testAddFlowFailCallback() throws Exception {
131         AddFlowInput mockedAddFlowInput = createFlowMock(AddFlowInput.class);
132         Mockito.doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
133                 .when(requestContext).getFuture();
134
135         final Future<RpcResult<AddFlowOutput>> rpcResultFuture = salFlowService.addFlow(mockedAddFlowInput);
136
137         assertNotNull(rpcResultFuture);
138         final RpcResult<?> addFlowOutputRpcResult = rpcResultFuture.get();
139         assertNotNull(addFlowOutputRpcResult);
140         assertFalse(addFlowOutputRpcResult.isSuccessful());
141     }
142
143     @Test
144     public void testRemoveFlowFailCallback() throws Exception {
145         RemoveFlowInput mockedRemoveFlowInput = createFlowMock(RemoveFlowInput.class);
146         Mockito.doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
147                 .when(requestContext).getFuture();
148
149         final Future<RpcResult<RemoveFlowOutput>> rpcResultFuture = salFlowService.removeFlow(mockedRemoveFlowInput);
150
151         assertNotNull(rpcResultFuture);
152         final RpcResult<?> removeFlowOutputRpcResult = rpcResultFuture.get();
153         assertNotNull(removeFlowOutputRpcResult);
154         assertFalse(removeFlowOutputRpcResult.isSuccessful());
155     }
156
157     @Test
158     public void testAddFlowWithItemLifecycle() throws Exception {
159         addFlow(mock(ItemLifecycleListener.class));
160     }
161
162     private void addFlow(final ItemLifecycleListener itemLifecycleListener) throws ExecutionException, InterruptedException {
163         AddFlowInput mockedAddFlowInput = createFlowMock(AddFlowInput.class);
164         salFlowService.setItemLifecycleListener(itemLifecycleListener);
165
166         verifyOutput(salFlowService.addFlow(mockedAddFlowInput));
167         if (itemLifecycleListener != null) {
168             Mockito.verify(itemLifecycleListener).onAdded(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any(), Matchers.<Flow>any());
169         }
170     }
171
172     @Test
173     public void testRemoveFlow() throws Exception {
174         removeFlow(null);
175     }
176
177     @Test
178     public void testRemoveFlowWithItemLifecycle() throws Exception {
179         removeFlow(mock(ItemLifecycleListener.class));
180     }
181
182     private void removeFlow(final ItemLifecycleListener itemLifecycleListener) throws Exception {
183         RemoveFlowInput mockedRemoveFlowInput = createFlowMock(RemoveFlowInput.class);
184
185         if (itemLifecycleListener != null) {
186             salFlowService.setItemLifecycleListener(itemLifecycleListener);
187             mockingFlowRegistryLookup();
188
189         }
190
191         verifyOutput(salFlowService.removeFlow(mockedRemoveFlowInput));
192         if (itemLifecycleListener != null) {
193             Mockito.verify(itemLifecycleListener).onRemoved(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any());
194         }
195
196     }
197
198     @Test
199     public void testUpdateFlow() throws Exception {
200         updateFlow(null);
201     }
202
203     @Test
204     public void testUpdateFlowWithItemLifecycle() throws Exception {
205         updateFlow(mock(ItemLifecycleListener.class));
206     }
207
208     private void updateFlow(final ItemLifecycleListener itemLifecycleListener) throws Exception {
209         UpdateFlowInput mockedUpdateFlowInput = mock(UpdateFlowInput.class);
210
211         UpdatedFlow mockedUpdateFlow = createFlowMock(UpdatedFlow.class);
212         when(mockedUpdateFlowInput.getUpdatedFlow()).thenReturn(mockedUpdateFlow);
213
214         FlowRef mockedFlowRef = mock(FlowRef.class);
215         Mockito.doReturn(TABLE_II.child(Flow.class, new FlowKey(new FlowId(DUMMY_FLOW_ID)))).when(mockedFlowRef).getValue();
216         when(mockedUpdateFlowInput.getFlowRef()).thenReturn(mockedFlowRef);
217
218         OriginalFlow mockedOriginalFlow = createFlowMock(OriginalFlow.class);
219         when(mockedUpdateFlowInput.getOriginalFlow()).thenReturn(mockedOriginalFlow);
220
221         if (itemLifecycleListener != null) {
222             salFlowService.setItemLifecycleListener(itemLifecycleListener);
223             mockingFlowRegistryLookup();
224         }
225
226         verifyOutput(salFlowService.updateFlow(mockedUpdateFlowInput));
227
228         if (itemLifecycleListener != null) {
229             Mockito.verify(itemLifecycleListener).onAdded(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any(), Matchers.<Flow>any());
230             Mockito.verify(itemLifecycleListener).onRemoved(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any());
231         }
232
233     }
234
235     private void mockingFlowRegistryLookup() {
236         FlowDescriptor mockedFlowDescriptor = mock(FlowDescriptor.class);
237         when(mockedFlowDescriptor.getFlowId()).thenReturn(new FlowId(DUMMY_FLOW_ID));
238         when(mockedFlowDescriptor.getTableKey()).thenReturn(new TableKey(DUMMY_TABLE_ID));
239
240         when(deviceFlowRegistry.retrieveIdForFlow(Matchers.any(FlowRegistryKey.class))).thenReturn(mockedFlowDescriptor);
241     }
242
243     private <T extends DataObject> void verifyOutput(Future<RpcResult<T>> rpcResultFuture) throws ExecutionException, InterruptedException {
244         assertNotNull(rpcResultFuture);
245         final RpcResult<?> addFlowOutputRpcResult = rpcResultFuture.get();
246         assertNotNull(addFlowOutputRpcResult);
247         assertTrue(addFlowOutputRpcResult.isSuccessful());
248     }
249
250     private <T extends org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow> T createFlowMock(Class<T> flowClazz) {
251         T mockedFlow = mock(flowClazz);
252         when(mockedFlow.getMatch()).thenReturn(match);
253         return mockedFlow;
254     }
255 }