Merge "BUG-4177: Li - flowMod result ignores errors from device"
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / rpc / 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.impl.rpc;
9
10 import static org.mockito.Mockito.times;
11
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.controller.sal.binding.api.BindingAwareBroker;
20 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
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.handlers.DeviceInitializationPhaseHandler;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeContext;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutputBuilder;
33 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
34 import org.opendaylight.yangtools.yang.binding.RpcService;
35
36 @RunWith(MockitoJUnitRunner.class)
37 public class RpcManagerImplTest {
38
39     private static final int AWAITED_NUM_OF_CALL_ADD_ROUTED_RPC = 9;
40
41     private RpcManagerImpl rpcManager;
42     @Mock
43     private ProviderContext rpcProviderRegistry;
44     @Mock
45     private DeviceContext deviceContext;
46     @Mock
47     private DeviceInitializationPhaseHandler deviceINitializationPhaseHandler;
48     @Mock
49     private ConnectionContext connectionContext;
50     @Mock
51     private BindingAwareBroker.RoutedRpcRegistration<RpcService> routedRpcRegistration;
52     @Mock
53     private DeviceState deviceState;
54     private KeyedInstanceIdentifier<Node, NodeKey> nodePath;
55
56     @Before
57     public void setUp() {
58         nodePath = KeyedInstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId("openflow-junit:1")));
59         rpcManager = new RpcManagerImpl(rpcProviderRegistry, 5);
60         rpcManager.setDeviceInitializationPhaseHandler(deviceINitializationPhaseHandler);
61         FeaturesReply features = new GetFeaturesOutputBuilder()
62                 .setVersion(OFConstants.OFP_VERSION_1_3)
63                 .build();
64         Mockito.when(connectionContext.getFeatures()).thenReturn(features);
65         Mockito.when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
66         Mockito.when(deviceContext.getDeviceState()).thenReturn(deviceState);
67         Mockito.when(deviceState.getNodeInstanceIdentifier()).thenReturn(nodePath);
68     }
69
70     @Test
71     public void testOnDeviceContextLevelUp() {
72
73         Mockito.when(rpcProviderRegistry.addRoutedRpcImplementation(
74                 Matchers.<Class<RpcService>>any(), Matchers.any(RpcService.class)))
75                 .thenReturn(routedRpcRegistration);
76
77         rpcManager.onDeviceContextLevelUp(deviceContext);
78
79         Mockito.verify(rpcProviderRegistry, times(AWAITED_NUM_OF_CALL_ADD_ROUTED_RPC)).addRoutedRpcImplementation(
80                 Matchers.<Class<RpcService>>any(), Matchers.any(RpcService.class));
81         Mockito.verify(routedRpcRegistration, times(AWAITED_NUM_OF_CALL_ADD_ROUTED_RPC)).registerPath(
82                 NodeContext.class, nodePath);
83         Mockito.verify(deviceINitializationPhaseHandler).onDeviceContextLevelUp(deviceContext);
84     }
85 }