a1ba78f65795c47008c7e234944214b4b33f6fbe
[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.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.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 BindingAwareBroker.ProviderContext rpcProviderRegistry;
51     @Mock
52     private DeviceState deviceState;
53     @Mock
54     private XidSequencer xidSequencer;
55     @Mock
56     private MessageSpy messageSpy;
57     @Mock
58     private DeviceContext deviceContext;
59     @Mock
60     private BindingAwareBroker.RoutedRpcRegistration<TestRpcService> routedRpcReg;
61     @Mock
62     private NotificationPublishService notificationPublishService;
63     @Mock
64     private TestRpcService serviceInstance;
65     @Mock
66     private DeviceInfo deviceInfo;
67     @Mock
68     private ExtensionConverterProvider extensionConverterProvider;
69     @Mock
70     private ConvertorExecutor convertorExecutor;
71
72     private KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier;
73
74     @Before
75     public void setup() {
76         final NodeId nodeId = new NodeId("openflow:1");
77         nodeInstanceIdentifier = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(nodeId));
78
79         when(deviceContext.getDeviceState()).thenReturn(deviceState);
80         when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(nodeInstanceIdentifier);
81         when(deviceContext.getMessageSpy()).thenReturn(messageSpy);
82
83         rpcContext = new RpcContextImpl(
84                 deviceInfo,
85                 rpcProviderRegistry,
86                 deviceContext,
87                 messageSpy,
88                 MAX_REQUESTS,
89                 nodeInstanceIdentifier,
90                 deviceContext,
91                 extensionConverterProvider,
92                 convertorExecutor,
93                 notificationPublishService);
94
95         when(rpcProviderRegistry.addRoutedRpcImplementation(TestRpcService.class, serviceInstance)).thenReturn(routedRpcReg);
96
97     }
98
99     @Test
100     public void testStoreOrFail() throws Exception {
101         try (final RpcContext rpcContext = new RpcContextImpl(
102                 deviceInfo,
103                 rpcProviderRegistry,
104                 xidSequencer,
105                 messageSpy,
106                 100,
107                 nodeInstanceIdentifier,
108                 deviceContext,
109                 extensionConverterProvider,
110                 convertorExecutor,
111                 notificationPublishService)){
112             final RequestContext<?> requestContext = rpcContext.createRequestContext();
113             assertNotNull(requestContext);
114         }
115     }
116
117     @Test
118     public void testStoreOrFailThatFails() throws Exception {
119         try (final RpcContext rpcContext = new RpcContextImpl(
120                 deviceInfo,
121                 rpcProviderRegistry,
122                 xidSequencer,
123                 messageSpy,
124                 0,
125                 nodeInstanceIdentifier,
126                 deviceContext,
127                 extensionConverterProvider,
128                 convertorExecutor,
129                 notificationPublishService)){
130             final RequestContext<?> requestContext = rpcContext.createRequestContext();
131             assertNull(requestContext);
132         }
133     }
134
135     @Test
136     public void testStoreAndCloseOrFail() throws Exception {
137         try (final RpcContext rpcContext = new RpcContextImpl(
138                 deviceInfo,
139                 rpcProviderRegistry,
140                 deviceContext,
141                 messageSpy,
142                 100,
143                 nodeInstanceIdentifier,
144                 deviceContext,
145                 extensionConverterProvider,
146                 convertorExecutor,
147                 notificationPublishService)){
148             final RequestContext<?> requestContext = rpcContext.createRequestContext();
149             assertNotNull(requestContext);
150             requestContext.close();
151             verify(messageSpy).spyMessage(RpcContextImpl.class, MessageSpy.STATISTIC_GROUP.REQUEST_STACK_FREED);
152         }
153     }
154
155     public void testRegisterRpcServiceImplementation() {
156         rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
157         verify(rpcProviderRegistry, Mockito.times(1)).addRoutedRpcImplementation(TestRpcService.class,serviceInstance);
158         verify(routedRpcReg,Mockito.times(1)).registerPath(NodeContext.class,nodeInstanceIdentifier);
159         assertEquals(rpcContext.isEmptyRpcRegistrations(), false);
160     }
161
162
163     @Test
164     public void testLookupRpcService() {
165         when(routedRpcReg.getInstance()).thenReturn(serviceInstance);
166         rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
167         TestRpcService temp = rpcContext.lookupRpcService(TestRpcService.class);
168         assertEquals(serviceInstance,temp);
169     }
170
171     @Test
172     public void testClose() {
173         rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
174         rpcContext.close();
175         assertEquals(rpcContext.isEmptyRpcRegistrations(), true);
176     }
177
178     /**
179      * When deviceContext.reserveXidForDeviceMessage returns null, null should be returned
180      * @throws InterruptedException
181      */
182     @Test
183     public void testCreateRequestContext1() throws InterruptedException {
184         when(deviceContext.reserveXidForDeviceMessage()).thenReturn(null);
185         assertEquals(rpcContext.createRequestContext(),null);
186     }
187
188     /**
189      * When deviceContext.reserveXidForDeviceMessage returns value, AbstractRequestContext should be returned
190      * @throws InterruptedException
191      */
192
193     @Test
194     public void testCreateRequestContext2() throws InterruptedException {
195         RequestContext temp = rpcContext.createRequestContext();
196         temp.close();
197         verify(messageSpy).spyMessage(RpcContextImpl.class,MessageSpy.STATISTIC_GROUP.REQUEST_STACK_FREED);
198     }
199
200     @Test
201     public void testUnregisterRpcServiceImpl() {
202         rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
203         assertEquals(rpcContext.isEmptyRpcRegistrations(), false);
204         rpcContext.unregisterRpcServiceImplementation(TestRpcService.class);
205         assertEquals(rpcContext.isEmptyRpcRegistrations(), true);
206     }
207
208     //Stub for RpcService class
209     public class TestRpcService implements RpcService {}
210 }