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