Merge "Fixed the log level."
[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 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(rpcProviderRegistry,deviceContext, messageSpy, MAX_REQUESTS,nodeInstanceIdentifier);
78
79         when(rpcProviderRegistry.addRoutedRpcImplementation(TestRpcService.class, serviceInstance)).thenReturn(routedRpcReg);
80
81     }
82
83     @Test
84     public void testStoreOrFail() throws Exception {
85         try (final RpcContext rpcContext = new RpcContextImpl(rpcProviderRegistry, xidSequencer,
86                 messageSpy, 100, nodeInstanceIdentifier)) {
87             final RequestContext<?> requestContext = rpcContext.createRequestContext();
88             assertNotNull(requestContext);
89         }
90     }
91
92     @Test
93     public void testStoreOrFailThatFails() throws Exception {
94         try (final RpcContext rpcContext = new RpcContextImpl(rpcProviderRegistry, xidSequencer,
95                 messageSpy, 0, nodeInstanceIdentifier)) {
96             final RequestContext<?> requestContext = rpcContext.createRequestContext();
97             assertNull(requestContext);
98         }
99     }
100
101     @Test
102     public void testStoreAndCloseOrFail() throws Exception {
103         try (final RpcContext rpcContext = new RpcContextImpl(rpcProviderRegistry, deviceContext, messageSpy,
104                 100, nodeInstanceIdentifier)) {
105             final RequestContext<?> requestContext = rpcContext.createRequestContext();
106             assertNotNull(requestContext);
107             requestContext.close();
108             verify(messageSpy).spyMessage(RpcContextImpl.class, MessageSpy.STATISTIC_GROUP.REQUEST_STACK_FREED);
109         }
110     }
111
112     public void testRegisterRpcServiceImplementation() {
113         rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
114         verify(rpcProviderRegistry, Mockito.times(1)).addRoutedRpcImplementation(TestRpcService.class,serviceInstance);
115         verify(routedRpcReg,Mockito.times(1)).registerPath(NodeContext.class,nodeInstanceIdentifier);
116         assertEquals(rpcContext.isEmptyRpcRegistrations(), false);
117     }
118
119
120     @Test
121     public void testLookupRpcService() {
122         when(routedRpcReg.getInstance()).thenReturn(serviceInstance);
123         rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
124         TestRpcService temp = rpcContext.lookupRpcService(TestRpcService.class);
125         assertEquals(serviceInstance,temp);
126     }
127
128     @Test
129     public void testClose() {
130         rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
131         rpcContext.close();
132         assertEquals(rpcContext.isEmptyRpcRegistrations(), true);
133     }
134
135     /**
136      * When deviceContext.reserveXidForDeviceMessage returns null, null should be returned
137      * @throws InterruptedException
138      */
139     @Test
140     public void testCreateRequestContext1() throws InterruptedException {
141         when(deviceContext.reserveXidForDeviceMessage()).thenReturn(null);
142         assertEquals(rpcContext.createRequestContext(),null);
143     }
144
145     /**
146      * When deviceContext.reserveXidForDeviceMessage returns value, AbstractRequestContext should be returned
147      * @throws InterruptedException
148      */
149
150     @Test
151     public void testCreateRequestContext2() throws InterruptedException {
152         RequestContext temp = rpcContext.createRequestContext();
153         temp.close();
154         verify(messageSpy).spyMessage(RpcContextImpl.class,MessageSpy.STATISTIC_GROUP.REQUEST_STACK_FREED);
155     }
156
157     @Test
158     public void testUnregisterRpcServiceImpl() {
159         rpcContext.registerRpcServiceImplementation(TestRpcService.class, serviceInstance);
160         assertEquals(rpcContext.isEmptyRpcRegistrations(), false);
161         rpcContext.unregisterRpcServiceImplementation(TestRpcService.class);
162         assertEquals(rpcContext.isEmptyRpcRegistrations(), true);
163     }
164
165     //Stub for RpcService class
166     public class TestRpcService implements RpcService {}
167 }