Implementation of Rpc interfaces
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / api / openflow / device / RpcManagerImplTest.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.api.openflow.device;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.mockito.Mockito.mock;
12 import static org.mockito.Mockito.times;
13 import static org.mockito.Mockito.verify;
14 import static org.mockito.Mockito.when;
15
16 import java.util.concurrent.ExecutionException;
17 import java.util.concurrent.Future;
18 import org.junit.Test;
19 import org.mockito.Matchers;
20 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
21 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
22 import org.opendaylight.openflowplugin.impl.rpc.RpcContextImpl;
23 import org.opendaylight.openflowplugin.impl.rpc.RpcManagerImpl;
24 import org.opendaylight.openflowplugin.impl.services.SalFlowServiceImpl;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
29 import org.opendaylight.yangtools.yang.binding.RpcService;
30 import org.opendaylight.yangtools.yang.common.RpcResult;
31
32 public class RpcManagerImplTest {
33
34     private static final int AWAITED_NUM_OF_CALL_ADD_ROUTED_RPC = 12;
35
36     private static final int AWAITED_NUM_OF_CALL_ADD_NEW_REQUEST = 1;
37
38     final ProviderContext mockedProviderContext = mock(ProviderContext.class);
39     final RpcManagerImpl rpcManager = new RpcManagerImpl(mockedProviderContext);
40     final DeviceContext mockedRequestContext = mock(DeviceContext.class);
41
42     @Test
43     public void deviceConnectedTest() {
44
45         rpcManager.deviceConnected(mockedRequestContext);
46
47         verify(mockedProviderContext, times(AWAITED_NUM_OF_CALL_ADD_ROUTED_RPC)).addRoutedRpcImplementation(
48                 Matchers.any(Class.class), Matchers.any(RpcService.class));
49     }
50
51     final RpcContext mockedRpcContext = mock(RpcContext.class);
52     final AddFlowInput mockedFlowInput = prepareTestingAddFlow();
53     final DeviceContext mockedDeviceContext = mock(DeviceContext.class);
54
55     /**
56      * Tests behavior of RpcContextImpl when calling rpc from MD-SAL
57      */
58     @Test
59     public void invokeRpcTestExistsCapacityTest() throws InterruptedException, ExecutionException {
60         invokeRpcTestExistsCapacity(10, true);
61         invokeRpcTestExistsCapacity(0, false);
62     }
63
64     private void invokeRpcTestExistsCapacity(final int capacity, final boolean result) throws InterruptedException,
65             ExecutionException {
66         // TODO: how to invoke service remotely?
67         final RpcContextImpl rpcContext = new RpcContextImpl(mockedProviderContext, mockedDeviceContext);
68         when(mockedProviderContext.getRpcService(SalFlowService.class)).thenReturn(new SalFlowServiceImpl(rpcContext));
69         rpcContext.setRequestContextQuota(capacity);
70
71         final SalFlowService salFlowService = mockedProviderContext.getRpcService(SalFlowService.class);
72         final Future<RpcResult<AddFlowOutput>> addedFlow = salFlowService.addFlow(prepareTestingAddFlow());
73     }
74
75     /**
76      * @return
77      */
78     private AddFlowInput prepareTestingAddFlow() {
79         final AddFlowInputBuilder builder = new AddFlowInputBuilder();
80         builder.setFlowName("dummy flow");
81         builder.setHardTimeout(10000);
82
83         return builder.build();
84     }
85 }