Merge "BUG-4117: add support for meter and group old Notif"
[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.assertNotNull;
11 import static org.junit.Assert.assertNull;
12 import static org.mockito.Mockito.verify;
13 import static org.mockito.Mockito.when;
14 import static org.junit.Assert.assertEquals;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.junit.runner.RunWith;
18 import org.mockito.Mock;
19 import org.mockito.Mockito;
20 import org.mockito.runners.MockitoJUnitRunner;
21 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
22 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
23 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
24 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
25 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
26 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
27 import org.opendaylight.openflowplugin.api.openflow.device.XidSequencer;
28 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
29 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeContext;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
37 import org.opendaylight.yangtools.yang.binding.RpcService;
38 import java.util.concurrent.Semaphore;
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 routedRpcReg;
59     @Mock
60     private NotificationPublishService notificationPublishService;
61     @Mock
62     private TestRpcService serviceInstance;
63
64     private KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier;
65
66     @Before
67     public void setup() {
68         final NodeId nodeId = new NodeId("openflow:1");
69         nodeInstanceIdentifier = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(nodeId));
70
71         when(deviceContext.getDeviceState()).thenReturn(deviceState);
72         when(deviceState.getNodeInstanceIdentifier()).thenReturn(nodeInstanceIdentifier);
73         when(deviceContext.getMessageSpy()).thenReturn(messageSpy);
74
75         rpcContext = new RpcContextImpl(rpcProviderRegistry,deviceContext, messageSpy, MAX_REQUESTS,nodeInstanceIdentifier);
76
77         when(rpcProviderRegistry.addRoutedRpcImplementation(TestRpcService.class, serviceInstance)).thenReturn(routedRpcReg);
78
79     }
80
81     @Test
82     public void testStoreOrFail() throws Exception {
83         try (final RpcContext rpcContext = new RpcContextImpl(rpcProviderRegistry, xidSequencer,
84                 messageSpy, 100, nodeInstanceIdentifier)) {
85             final RequestContext<?> requestContext = rpcContext.createRequestContext();
86             assertNotNull(requestContext);
87         }
88     }
89
90     @Test
91     public void testStoreOrFailThatFails() throws Exception {
92         try (final RpcContext rpcContext = new RpcContextImpl(rpcProviderRegistry, xidSequencer,
93                 messageSpy, 0, nodeInstanceIdentifier)) {
94             final RequestContext<?> requestContext = rpcContext.createRequestContext();
95             assertNull(requestContext);
96         }
97     }
98
99     @Test
100     public void testStoreAndCloseOrFail() throws Exception {
101         try (final RpcContext rpcContext = new RpcContextImpl(rpcProviderRegistry, deviceContext, messageSpy,
102                 100, nodeInstanceIdentifier)) {
103             final RequestContext<?> requestContext = rpcContext.createRequestContext();
104             assertNotNull(requestContext);
105             requestContext.close();
106             verify(messageSpy).spyMessage(RpcContextImpl.class, MessageSpy.STATISTIC_GROUP.REQUEST_STACK_FREED);
107         }
108     }
109
110     public void testRegisterRpcServiceImplementation() {
111         rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
112         verify(rpcProviderRegistry, Mockito.times(1)).addRoutedRpcImplementation(TestRpcService.class,serviceInstance);
113         verify(routedRpcReg,Mockito.times(1)).registerPath(NodeContext.class,nodeInstanceIdentifier);
114         assertEquals(rpcContext.isEmptyRpcRegistrations(), false);
115     }
116
117
118     @Test
119     public void testLookupRpcService() {
120         when(routedRpcReg.getInstance()).thenReturn(serviceInstance);
121         rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
122         TestRpcService temp = rpcContext.lookupRpcService(TestRpcService.class);
123         assertEquals(serviceInstance,temp);
124     }
125
126     @Test
127     public void testClose() {
128         rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
129         rpcContext.close();
130         assertEquals(rpcContext.isEmptyRpcRegistrations(), true);
131     }
132
133     /**
134      * When deviceContext.reserveXidForDeviceMessage returns null, null should be returned
135      * @throws InterruptedException
136      */
137     @Test
138     public void testCreateRequestContext1() throws InterruptedException {
139         when(deviceContext.reserveXidForDeviceMessage()).thenReturn(null);
140         assertEquals(rpcContext.createRequestContext(),null);
141     }
142
143     /**
144      * When deviceContext.reserveXidForDeviceMessage returns value, AbstractRequestContext should be returned
145      * @throws InterruptedException
146      */
147
148     @Test
149     public void testCreateRequestContext2() throws InterruptedException {
150         RequestContext temp = rpcContext.createRequestContext();
151         temp.close();
152         verify(messageSpy).spyMessage(RpcContextImpl.class,MessageSpy.STATISTIC_GROUP.REQUEST_STACK_FREED);
153     }
154
155     @Test
156     public void testUnregisterRpcServiceImpl() {
157         rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
158         assertEquals(rpcContext.isEmptyRpcRegistrations(), false);
159         rpcContext.unregisterRpcServiceImplementation(TestRpcService.class);
160         assertEquals(rpcContext.isEmptyRpcRegistrations(), true);
161     }
162
163     //Stub for RpcService class
164     public class TestRpcService implements RpcService {}
165 }