Fix Mockito Generics
[openflowplugin.git] / openflowplugin / src / test / java / org / opendaylight / openflowplugin / openflow / md / core / sal / OFRpcTaskUtilTest.java
1 /*
2  * Copyright (c) 2014 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
9 package org.opendaylight.openflowplugin.openflow.md.core.sal;
10
11 import static org.junit.Assert.assertNotNull;
12 import static org.mockito.Mockito.when;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.ListeningExecutorService;
15 import java.util.Collection;
16 import java.util.concurrent.Callable;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.junit.runner.RunWith;
20 import org.mockito.Mock;
21 import org.mockito.Mockito;
22 import org.mockito.runners.MockitoJUnitRunner;
23 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
24 import org.opendaylight.openflowplugin.api.OFConstants;
25 import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
26 import org.opendaylight.openflowplugin.api.openflow.md.core.sal.NotificationComposer;
27 import org.opendaylight.openflowplugin.api.openflow.md.core.session.IMessageDispatchService;
28 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
35 import org.opendaylight.yangtools.yang.common.RpcError;
36 import org.opendaylight.yangtools.yang.common.RpcResult;
37
38 @RunWith(MockitoJUnitRunner.class)
39 public class OFRpcTaskUtilTest {
40
41     @Mock
42     private OFRpcTaskContext taskContext;
43     @Mock
44     private SwitchConnectionDistinguisher connectionDistinguisher;
45     @Mock
46     private SessionContext sessionContext;
47     @Mock
48     private IMessageDispatchService messageDispatchService;
49     @Mock
50     private GetFeaturesOutput featuresOutput;
51     @Mock
52     private ListenableFuture<RpcResult<BarrierOutput>> resultListenableFuture;
53     @Mock
54     private ListenableFuture<RpcResult<UpdateFlowOutput>> updateFlowRpcResultListenableFuture;
55     @Mock
56     private OFRpcTaskContext ofRpcTaskContext;
57     @Mock
58     private NotificationProviderService notificationProviderService;
59     @Mock
60     private NotificationComposer<?> notificationComposer;
61     @Mock
62     ListeningExecutorService executorService;
63
64
65     @Before
66     public void setup() {
67         when(taskContext.getSession()).thenReturn(sessionContext);
68         when(taskContext.getMessageService()).thenReturn(messageDispatchService);
69         when(sessionContext.getNextXid()).thenReturn(new Long(10));
70         when(sessionContext.getFeatures()).thenReturn(featuresOutput);
71         when(featuresOutput.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
72         when(messageDispatchService.barrier(Mockito.any(BarrierInput.class), Mockito.any(SwitchConnectionDistinguisher.class))).thenReturn(resultListenableFuture);
73         when(ofRpcTaskContext.getRpcPool()).thenReturn(executorService);
74         when(executorService.submit(Mockito.<Callable<RpcResult<UpdateFlowOutput>>> any())).thenReturn(updateFlowRpcResultListenableFuture);
75     }
76
77
78     @Test
79     public void testManageBarrier() throws Exception {
80         final Collection<RpcError> rpcErrors = OFRpcTaskUtil.manageBarrier(taskContext, true, connectionDistinguisher);
81         assertNotNull(rpcErrors);
82     }
83
84     @Test
85     public void testHookFutureNotification() throws Exception {
86         final AddFlowInputBuilder flowInputBuilder = new AddFlowInputBuilder();
87         final OFRpcTask<AddFlowInput, RpcResult<UpdateFlowOutput>> addFlowInputRpcResultOFRpcTask = OFRpcTaskFactory.createAddFlowTask(ofRpcTaskContext, flowInputBuilder.build(), connectionDistinguisher);
88         OFRpcTaskUtil.hookFutureNotification(addFlowInputRpcResultOFRpcTask, updateFlowRpcResultListenableFuture, notificationProviderService, notificationComposer);
89     }
90
91     @Test
92     public void testChainFutureBarrier() throws Exception {
93         final AddFlowInputBuilder flowInputBuilder = new AddFlowInputBuilder();
94         flowInputBuilder.setBarrier(true);
95         final OFRpcTask<AddFlowInput, RpcResult<UpdateFlowOutput>> addFlowInputRpcResultOFRpcTask = OFRpcTaskFactory.createAddFlowTask(ofRpcTaskContext, flowInputBuilder.build(), connectionDistinguisher);
96         OFRpcTaskUtil.chainFutureBarrier(addFlowInputRpcResultOFRpcTask, updateFlowRpcResultListenableFuture);
97     }
98 }