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