OPNFLWPLUG-1032: Neon-MRI: Bump odlparent, yangtools, mdsal
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / rpc / RpcContextImplTest.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.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.mockito.Mockito.verify;
14 import static org.mockito.Mockito.when;
15
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19 import org.mockito.Mock;
20 import org.mockito.Mockito;
21 import org.mockito.runners.MockitoJUnitRunner;
22 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
23 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
24 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
25 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
26 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
27 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
28 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
29 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
30 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
31 import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterProvider;
32 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeContext;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
40 import org.opendaylight.yangtools.yang.binding.RpcService;
41
42 @RunWith(MockitoJUnitRunner.class)
43 public class RpcContextImplTest {
44
45     private static final int MAX_REQUESTS = 5;
46     private RpcContextImpl rpcContext;
47
48
49     @Mock
50     private RpcProviderRegistry rpcProviderRegistry;
51     @Mock
52     private DeviceState deviceState;
53     @Mock
54     private MessageSpy messageSpy;
55     @Mock
56     private DeviceContext deviceContext;
57     @Mock
58     private BindingAwareBroker.RoutedRpcRegistration<TestRpcService> routedRpcReg;
59
60     @Mock
61     private NotificationPublishService notificationPublishService;
62     @Mock
63     private TestRpcService serviceInstance;
64     @Mock
65     private DeviceInfo deviceInfo;
66     @Mock
67     private ExtensionConverterProvider extensionConverterProvider;
68     @Mock
69     private ConvertorExecutor convertorExecutor;
70
71     private KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier;
72
73     @Before
74     public void setup() {
75         final NodeId nodeId = new NodeId("openflow:1");
76         nodeInstanceIdentifier = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(nodeId));
77
78         when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(nodeInstanceIdentifier);
79         when(deviceContext.getMessageSpy()).thenReturn(messageSpy);
80         when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
81         when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(nodeInstanceIdentifier);
82
83         rpcContext = new RpcContextImpl(
84                 rpcProviderRegistry,
85                 MAX_REQUESTS,
86                 deviceContext,
87                 extensionConverterProvider,
88                 convertorExecutor,
89                 notificationPublishService, true);
90
91         when(rpcProviderRegistry.addRoutedRpcImplementation(TestRpcService.class, serviceInstance))
92                 .thenReturn(routedRpcReg);
93     }
94
95     @Test
96     public void testStoreOrFail() throws Exception {
97         try (RpcContext rpcContext = new RpcContextImpl(
98                 rpcProviderRegistry,
99                 100,
100                 deviceContext,
101                 extensionConverterProvider,
102                 convertorExecutor,
103                 notificationPublishService, true)) {
104             final RequestContext<?> requestContext = rpcContext.createRequestContext();
105             assertNotNull(requestContext);
106         }
107     }
108
109     @Test
110     public void testStoreOrFailThatFails() throws Exception {
111         try (RpcContext rpcContext = new RpcContextImpl(
112                 rpcProviderRegistry,
113                 0,
114                 deviceContext,
115                 extensionConverterProvider,
116                 convertorExecutor,
117                 notificationPublishService, true)) {
118             final RequestContext<?> requestContext = rpcContext.createRequestContext();
119             assertNull(requestContext);
120         }
121     }
122
123     @Test
124     public void testStoreAndCloseOrFail() throws Exception {
125         try (RpcContext rpcContext = new RpcContextImpl(
126                 rpcProviderRegistry,
127                 100,
128                 deviceContext,
129                 extensionConverterProvider,
130                 convertorExecutor,
131                 notificationPublishService, true)) {
132             final RequestContext<?> requestContext = rpcContext.createRequestContext();
133             assertNotNull(requestContext);
134             requestContext.close();
135             verify(messageSpy).spyMessage(RpcContextImpl.class, MessageSpy.StatisticsGroup.REQUEST_STACK_FREED);
136         }
137     }
138
139     public void testRegisterRpcServiceImplementation() {
140         rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
141         verify(rpcProviderRegistry, Mockito.times(1)).addRoutedRpcImplementation(TestRpcService.class,serviceInstance);
142         verify(routedRpcReg,Mockito.times(1)).registerPath(NodeContext.class,nodeInstanceIdentifier);
143         assertEquals(rpcContext.isEmptyRpcRegistrations(), false);
144     }
145
146     @Test
147     public void testLookupRpcService() {
148         when(routedRpcReg.getInstance()).thenReturn(serviceInstance);
149         rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
150         TestRpcService temp = rpcContext.lookupRpcService(TestRpcService.class);
151         assertEquals(serviceInstance,temp);
152     }
153
154     @Test
155     public void testClose() {
156         Class<TestRpcService> serviceClass = TestRpcService.class;
157         when(routedRpcReg.getServiceType()).thenReturn(serviceClass);
158         rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
159         rpcContext.close();
160         assertEquals(rpcContext.isEmptyRpcRegistrations(), true);
161     }
162
163     /**
164      * When deviceContext.reserveXidForDeviceMessage returns null, null should be returned.
165      */
166     @Test
167     public void testCreateRequestContext1() throws InterruptedException {
168         when(deviceInfo.reserveXidForDeviceMessage()).thenReturn(null);
169         assertEquals(rpcContext.createRequestContext(),null);
170     }
171
172     /**
173      * When deviceContext.reserveXidForDeviceMessage returns value, AbstractRequestContext should be returned.
174      */
175
176     @Test
177     public void testCreateRequestContext2() throws InterruptedException {
178         RequestContext temp = rpcContext.createRequestContext();
179         temp.close();
180         verify(messageSpy).spyMessage(RpcContextImpl.class, MessageSpy.StatisticsGroup.REQUEST_STACK_FREED);
181     }
182
183     @Test
184     public void testUnregisterRpcServiceImpl() {
185         rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
186         assertEquals(rpcContext.isEmptyRpcRegistrations(), false);
187         rpcContext.unregisterRpcServiceImplementation(TestRpcService.class);
188         assertEquals(rpcContext.isEmptyRpcRegistrations(), true);
189     }
190
191     //Stub for RpcService class.
192     public class TestRpcService implements RpcService {}
193 }