Merge "BUG-4177: Li - flowMod result ignores errors from device"
[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 java.math.BigInteger;
8 import java.util.concurrent.ExecutionException;
9 import java.util.concurrent.Future;
10 import junit.framework.TestCase;
11 import org.junit.Before;
12 import org.junit.Test;
13 import org.junit.runner.RunWith;
14 import org.mockito.Mock;
15 import org.mockito.runners.MockitoJUnitRunner;
16 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
17 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
18 import org.opendaylight.openflowplugin.api.OFConstants;
19 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
20 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
21 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
22 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
23 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
24 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
25 import org.opendaylight.openflowplugin.impl.registry.flow.DeviceFlowRegistryImpl;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlow;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlow;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
34 import org.opendaylight.yangtools.yang.binding.DataObject;
35 import org.opendaylight.yangtools.yang.common.RpcResult;
36 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
37
38 @RunWith(MockitoJUnitRunner.class)
39 public class SalFlowServiceImplTest extends TestCase {
40
41     private static final BigInteger DUMMY_DATAPATH_ID = new BigInteger("444");
42     private static final Short DUMMY_VERSION = OFConstants.OFP_VERSION_1_3;
43
44     @Mock
45     private RequestContextStack mockedRequestContextStack;
46     @Mock
47     private DeviceContext mockedDeviceContext;
48     @Mock
49     private ConnectionContext mockedPrimConnectionContext;
50     @Mock
51     private FeaturesReply mockedFeatures;
52     @Mock
53     private ConnectionAdapter mockedConnectionAdapter;
54     @Mock
55     private MessageSpy mockedMessagSpy;
56     @Mock
57     private RequestContext<Object> requestContext;
58     @Mock
59     private OutboundQueue outboundQueue;
60     @Mock
61     private Match match;
62     private SalFlowServiceImpl salFlowService;
63
64
65     @Before
66     public void initialization() {
67         when(mockedFeatures.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
68         when(mockedFeatures.getVersion()).thenReturn(DUMMY_VERSION);
69
70         when(mockedPrimConnectionContext.getFeatures()).thenReturn(mockedFeatures);
71         when(mockedPrimConnectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);
72         when(mockedPrimConnectionContext.getOutboundQueueProvider()).thenReturn(outboundQueue);
73
74         when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimConnectionContext);
75
76         when(mockedDeviceContext.getMessageSpy()).thenReturn(mockedMessagSpy);
77         when(mockedDeviceContext.getDeviceFlowRegistry()).thenReturn(new DeviceFlowRegistryImpl());
78         when(mockedRequestContextStack.createRequestContext()).thenReturn(requestContext);
79
80         when(requestContext.getXid()).thenReturn(new Xid(84L));
81         when(requestContext.getFuture()).thenReturn(RpcResultBuilder.success().buildFuture());
82
83         salFlowService = new SalFlowServiceImpl(mockedRequestContextStack, mockedDeviceContext);
84     }
85
86     @Test
87     public void testAddFlow() throws Exception {
88         final AddFlowInput mockedAddFlowInput = createFlowMock(AddFlowInput.class);
89
90         verifyOutput(salFlowService.addFlow(mockedAddFlowInput));
91     }
92
93     @Test
94     public void testRemoveFlow() throws Exception {
95         final RemoveFlowInput mockedRemoveFlowInput = createFlowMock(RemoveFlowInput.class);
96
97         verifyOutput(salFlowService.removeFlow(mockedRemoveFlowInput));
98     }
99
100     @Test
101     public void testUpdateFlow() throws Exception {
102         final UpdateFlowInput mockedUpdateFlowInput = mock(UpdateFlowInput.class);
103
104         final UpdatedFlow mockedUpdateFlow = createFlowMock(UpdatedFlow.class);
105         when(mockedUpdateFlowInput.getUpdatedFlow()).thenReturn(mockedUpdateFlow);
106
107         final OriginalFlow mockedOriginalFlow = createFlowMock(OriginalFlow.class);
108         when(mockedUpdateFlowInput.getOriginalFlow()).thenReturn(mockedOriginalFlow);
109
110         verifyOutput(salFlowService.updateFlow(mockedUpdateFlowInput));
111     }
112
113     private <T extends DataObject> void verifyOutput(Future<RpcResult<T>> rpcResultFuture) throws ExecutionException, InterruptedException {
114         assertNotNull(rpcResultFuture);
115         final RpcResult<?> addFlowOutputRpcResult = rpcResultFuture.get();
116         assertNotNull(addFlowOutputRpcResult);
117         assertTrue(addFlowOutputRpcResult.isSuccessful());
118     }
119
120     private <T extends Flow> T createFlowMock(Class<T> flowClazz) {
121         T mockedFlow = mock(flowClazz);
122         when(mockedFlow.getMatch()).thenReturn(match);
123         return mockedFlow;
124     }
125 }