6850c36b72dd8cc368f56d126cb6c364062ae9af
[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.Ignore;
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         when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
86         when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(nodeInstanceIdentifier);
87
88         rpcContext = new RpcContextImpl(
89                 rpcProviderRegistry,
90                 MAX_REQUESTS,
91                 deviceContext,
92                 extensionConverterProvider,
93                 convertorExecutor,
94                 notificationPublishService, true);
95
96         when(rpcProviderRegistry.addRoutedRpcImplementation(TestRpcService.class, serviceInstance)).thenReturn(routedRpcReg);
97
98     }
99
100     @Test
101     public void testStoreOrFail() throws Exception {
102         try (final RpcContext rpcContext = new RpcContextImpl(
103                 rpcProviderRegistry,
104                 100,
105                 deviceContext,
106                 extensionConverterProvider,
107                 convertorExecutor,
108                 notificationPublishService, true)){
109             final RequestContext<?> requestContext = rpcContext.createRequestContext();
110             assertNotNull(requestContext);
111         }
112     }
113
114     @Test
115     public void testStoreOrFailThatFails() throws Exception {
116         try (final RpcContext rpcContext = new RpcContextImpl(
117                 rpcProviderRegistry,
118                 0,
119                 deviceContext,
120                 extensionConverterProvider,
121                 convertorExecutor,
122                 notificationPublishService, true)){
123             final RequestContext<?> requestContext = rpcContext.createRequestContext();
124             assertNull(requestContext);
125         }
126     }
127
128     @Test
129     public void testStoreAndCloseOrFail() throws Exception {
130         try (final RpcContext rpcContext = new RpcContextImpl(
131                 rpcProviderRegistry,
132                 100,
133                 deviceContext,
134                 extensionConverterProvider,
135                 convertorExecutor,
136                 notificationPublishService, true)){
137             final RequestContext<?> requestContext = rpcContext.createRequestContext();
138             assertNotNull(requestContext);
139             requestContext.close();
140             verify(messageSpy).spyMessage(RpcContextImpl.class, MessageSpy.STATISTIC_GROUP.REQUEST_STACK_FREED);
141         }
142     }
143
144     public void testRegisterRpcServiceImplementation() {
145         rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
146         verify(rpcProviderRegistry, Mockito.times(1)).addRoutedRpcImplementation(TestRpcService.class,serviceInstance);
147         verify(routedRpcReg,Mockito.times(1)).registerPath(NodeContext.class,nodeInstanceIdentifier);
148         assertEquals(rpcContext.isEmptyRpcRegistrations(), false);
149     }
150
151
152     @Test
153     public void testLookupRpcService() {
154         when(routedRpcReg.getInstance()).thenReturn(serviceInstance);
155         rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
156         TestRpcService temp = rpcContext.lookupRpcService(TestRpcService.class);
157         assertEquals(serviceInstance,temp);
158     }
159
160     //TODO: RPCServices should be removed on event from singleton service
161     @Test
162     @Ignore
163     public void testClose() {
164         serviceClass = TestRpcService.class;
165         when(routedRpcReg.getServiceType()).thenReturn(serviceClass);
166         rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
167         rpcContext.close();
168         assertEquals(rpcContext.isEmptyRpcRegistrations(), true);
169     }
170
171     /**
172      * When deviceContext.reserveXidForDeviceMessage returns null, null should be returned
173      * @throws InterruptedException
174      */
175     @Test
176     public void testCreateRequestContext1() throws InterruptedException {
177         when(deviceInfo.reserveXidForDeviceMessage()).thenReturn(null);
178         assertEquals(rpcContext.createRequestContext(),null);
179     }
180
181     /**
182      * When deviceContext.reserveXidForDeviceMessage returns value, AbstractRequestContext should be returned
183      * @throws InterruptedException
184      */
185
186     @Test
187     public void testCreateRequestContext2() throws InterruptedException {
188         RequestContext temp = rpcContext.createRequestContext();
189         temp.close();
190         verify(messageSpy).spyMessage(RpcContextImpl.class,MessageSpy.STATISTIC_GROUP.REQUEST_STACK_FREED);
191     }
192
193     @Test
194     public void testUnregisterRpcServiceImpl() {
195         rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
196         assertEquals(rpcContext.isEmptyRpcRegistrations(), false);
197         rpcContext.unregisterRpcServiceImplementation(TestRpcService.class);
198         assertEquals(rpcContext.isEmptyRpcRegistrations(), true);
199     }
200
201     //Stub for RpcService class
202     public class TestRpcService implements RpcService {}
203 }