2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.impl.rpc;
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;
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.openflowplugin.api.openflow.device.DeviceContext;
25 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
26 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
27 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
28 import org.opendaylight.openflowplugin.api.openflow.device.XidSequencer;
29 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
30 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeContext;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
38 import org.opendaylight.yangtools.yang.binding.RpcService;
40 @RunWith(MockitoJUnitRunner.class)
41 public class RpcContextImplTest {
43 private static final int MAX_REQUESTS = 5;
44 private RpcContextImpl rpcContext;
48 private BindingAwareBroker.ProviderContext rpcProviderRegistry;
50 private DeviceState deviceState;
52 private XidSequencer xidSequencer;
54 private MessageSpy messageSpy;
56 private DeviceContext deviceContext;
58 private BindingAwareBroker.RoutedRpcRegistration<TestRpcService> routedRpcReg;
60 private NotificationPublishService notificationPublishService;
62 private TestRpcService serviceInstance;
64 private DeviceInfo deviceInfo;
66 private KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier;
70 final NodeId nodeId = new NodeId("openflow:1");
71 nodeInstanceIdentifier = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(nodeId));
73 when(deviceContext.getDeviceState()).thenReturn(deviceState);
74 when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(nodeInstanceIdentifier);
75 when(deviceContext.getMessageSpy()).thenReturn(messageSpy);
77 rpcContext = new RpcContextImpl(
83 nodeInstanceIdentifier);
85 when(rpcProviderRegistry.addRoutedRpcImplementation(TestRpcService.class, serviceInstance)).thenReturn(routedRpcReg);
90 public void testStoreOrFail() throws Exception {
91 try (final RpcContext rpcContext = new RpcContextImpl(
97 nodeInstanceIdentifier)) {
98 final RequestContext<?> requestContext = rpcContext.createRequestContext();
99 assertNotNull(requestContext);
104 public void testStoreOrFailThatFails() throws Exception {
105 try (final RpcContext rpcContext = new RpcContextImpl(
111 nodeInstanceIdentifier)) {
112 final RequestContext<?> requestContext = rpcContext.createRequestContext();
113 assertNull(requestContext);
118 public void testStoreAndCloseOrFail() throws Exception {
119 try (final RpcContext rpcContext = new RpcContextImpl(
125 nodeInstanceIdentifier)) {
126 final RequestContext<?> requestContext = rpcContext.createRequestContext();
127 assertNotNull(requestContext);
128 requestContext.close();
129 verify(messageSpy).spyMessage(RpcContextImpl.class, MessageSpy.STATISTIC_GROUP.REQUEST_STACK_FREED);
133 public void testRegisterRpcServiceImplementation() {
134 rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
135 verify(rpcProviderRegistry, Mockito.times(1)).addRoutedRpcImplementation(TestRpcService.class,serviceInstance);
136 verify(routedRpcReg,Mockito.times(1)).registerPath(NodeContext.class,nodeInstanceIdentifier);
137 assertEquals(rpcContext.isEmptyRpcRegistrations(), false);
142 public void testLookupRpcService() {
143 when(routedRpcReg.getInstance()).thenReturn(serviceInstance);
144 rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
145 TestRpcService temp = rpcContext.lookupRpcService(TestRpcService.class);
146 assertEquals(serviceInstance,temp);
150 public void testClose() {
151 rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
153 assertEquals(rpcContext.isEmptyRpcRegistrations(), true);
157 * When deviceContext.reserveXidForDeviceMessage returns null, null should be returned
158 * @throws InterruptedException
161 public void testCreateRequestContext1() throws InterruptedException {
162 when(deviceContext.reserveXidForDeviceMessage()).thenReturn(null);
163 assertEquals(rpcContext.createRequestContext(),null);
167 * When deviceContext.reserveXidForDeviceMessage returns value, AbstractRequestContext should be returned
168 * @throws InterruptedException
172 public void testCreateRequestContext2() throws InterruptedException {
173 RequestContext temp = rpcContext.createRequestContext();
175 verify(messageSpy).spyMessage(RpcContextImpl.class,MessageSpy.STATISTIC_GROUP.REQUEST_STACK_FREED);
179 public void testUnregisterRpcServiceImpl() {
180 rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
181 assertEquals(rpcContext.isEmptyRpcRegistrations(), false);
182 rpcContext.unregisterRpcServiceImplementation(TestRpcService.class);
183 assertEquals(rpcContext.isEmptyRpcRegistrations(), true);
186 //Stub for RpcService class
187 public class TestRpcService implements RpcService {}