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