Switch to MD-SAL APIs
[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.ArgumentMatchers.anySet;
14 import static org.mockito.ArgumentMatchers.eq;
15 import static org.mockito.Mockito.verify;
16 import static org.mockito.Mockito.when;
17
18 import com.google.common.collect.ImmutableSet;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.mockito.Mock;
23 import org.mockito.Mockito;
24 import org.mockito.junit.MockitoJUnitRunner;
25 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
26 import org.opendaylight.mdsal.binding.api.RpcProviderService;
27 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
28 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
29 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
30 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
31 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
32 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
33 import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterProvider;
34 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
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.concepts.ObjectRegistration;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
42 import org.opendaylight.yangtools.yang.binding.RpcService;
43
44 @RunWith(MockitoJUnitRunner.class)
45 public class RpcContextImplTest {
46
47     private static final int MAX_REQUESTS = 5;
48     private RpcContextImpl rpcContext;
49
50
51     @Mock
52     private RpcProviderService rpcProviderRegistry;
53     @Mock
54     private DeviceState deviceState;
55     @Mock
56     private MessageSpy messageSpy;
57     @Mock
58     private DeviceContext deviceContext;
59     @Mock
60     private ObjectRegistration<TestRpcService> routedRpcReg;
61
62     @Mock
63     private NotificationPublishService notificationPublishService;
64     @Mock
65     private TestRpcService serviceInstance;
66     @Mock
67     private DeviceInfo deviceInfo;
68     @Mock
69     private ExtensionConverterProvider extensionConverterProvider;
70     @Mock
71     private ConvertorExecutor convertorExecutor;
72
73     private KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier;
74
75     @Before
76     public void setup() {
77         final NodeId nodeId = new NodeId("openflow:1");
78         nodeInstanceIdentifier = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(nodeId));
79
80         when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(nodeInstanceIdentifier);
81         when(deviceContext.getMessageSpy()).thenReturn(messageSpy);
82         when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
83         when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(nodeInstanceIdentifier);
84
85         rpcContext = new RpcContextImpl(
86                 rpcProviderRegistry,
87                 MAX_REQUESTS,
88                 deviceContext,
89                 extensionConverterProvider,
90                 convertorExecutor,
91                 notificationPublishService, true);
92
93         when(rpcProviderRegistry.registerRpcImplementation(eq(TestRpcService.class), eq(serviceInstance), anySet()))
94                 .thenReturn(routedRpcReg);
95     }
96
97     @Test
98     public void testStoreOrFail() throws Exception {
99         try (RpcContext rpcContext = new RpcContextImpl(
100                 rpcProviderRegistry,
101                 100,
102                 deviceContext,
103                 extensionConverterProvider,
104                 convertorExecutor,
105                 notificationPublishService, true)) {
106             final RequestContext<?> requestContext = rpcContext.createRequestContext();
107             assertNotNull(requestContext);
108         }
109     }
110
111     @Test
112     public void testStoreOrFailThatFails() throws Exception {
113         try (RpcContext rpcContext = new RpcContextImpl(
114                 rpcProviderRegistry,
115                 0,
116                 deviceContext,
117                 extensionConverterProvider,
118                 convertorExecutor,
119                 notificationPublishService, true)) {
120             final RequestContext<?> requestContext = rpcContext.createRequestContext();
121             assertNull(requestContext);
122         }
123     }
124
125     @Test
126     public void testStoreAndCloseOrFail() throws Exception {
127         try (RpcContext rpcContext = new RpcContextImpl(
128                 rpcProviderRegistry,
129                 100,
130                 deviceContext,
131                 extensionConverterProvider,
132                 convertorExecutor,
133                 notificationPublishService, true)) {
134             final RequestContext<?> requestContext = rpcContext.createRequestContext();
135             assertNotNull(requestContext);
136             requestContext.close();
137             verify(messageSpy).spyMessage(RpcContextImpl.class, MessageSpy.StatisticsGroup.REQUEST_STACK_FREED);
138         }
139     }
140
141     public void testRegisterRpcServiceImplementation() {
142         rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
143         verify(rpcProviderRegistry, Mockito.times(1)).registerRpcImplementation(TestRpcService.class, serviceInstance,
144             ImmutableSet.of(nodeInstanceIdentifier));
145         assertEquals(rpcContext.isEmptyRpcRegistrations(), false);
146     }
147
148     @Test
149     public void testLookupRpcService() {
150         when(routedRpcReg.getInstance()).thenReturn(serviceInstance);
151         rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
152         TestRpcService temp = rpcContext.lookupRpcService(TestRpcService.class);
153         assertEquals(serviceInstance,temp);
154     }
155
156     @Test
157     public void testClose() {
158         when(routedRpcReg.getInstance()).thenReturn(serviceInstance);
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 }