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