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