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