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