Decompose RPC implementation classes
[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.ArgumentMatchers.any;
14 import static org.mockito.ArgumentMatchers.eq;
15 import static org.mockito.Mockito.verify;
16 import static org.mockito.Mockito.when;
17
18 import com.google.common.collect.ClassToInstanceMap;
19 import java.util.Set;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.mockito.ArgumentCaptor;
24 import org.mockito.Captor;
25 import org.mockito.Mock;
26 import org.mockito.junit.MockitoJUnitRunner;
27 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
28 import org.opendaylight.mdsal.binding.api.RpcProviderService;
29 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
30 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
31 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
32 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
33 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
34 import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterProvider;
35 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
40 import org.opendaylight.yangtools.concepts.ObjectRegistration;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
43 import org.opendaylight.yangtools.yang.binding.Rpc;
44 import org.opendaylight.yangtools.yang.binding.RpcService;
45 import org.opendaylight.yangtools.yang.common.Uint32;
46 import org.opendaylight.yangtools.yang.common.Uint8;
47
48 @RunWith(MockitoJUnitRunner.class)
49 public class RpcContextImplTest {
50     @Mock
51     private RpcProviderService rpcProviderRegistry;
52     @Mock
53     private DeviceState deviceState;
54     @Mock
55     private MessageSpy messageSpy;
56     @Mock
57     private DeviceContext deviceContext;
58     @Mock
59     private ConnectionContext connectionContext;
60     @Mock
61     private ObjectRegistration<RpcService> registration;
62     @Mock
63     private NotificationPublishService notificationPublishService;
64     @Mock
65     private DeviceInfo deviceInfo;
66     @Mock
67     private ExtensionConverterProvider extensionConverterProvider;
68     @Mock
69     private ConvertorExecutor convertorExecutor;
70     @Captor
71     private ArgumentCaptor<ClassToInstanceMap<Rpc<?, ?>>> captor;
72
73     private KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier;
74     private RpcContextImpl rpcContext;
75
76     @Before
77     public void setup() {
78         final NodeId nodeId = new NodeId("openflow:1");
79         nodeInstanceIdentifier = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(nodeId));
80
81         when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(nodeInstanceIdentifier);
82         when(deviceContext.getMessageSpy()).thenReturn(messageSpy);
83         when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
84         when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(nodeInstanceIdentifier);
85         when(deviceInfo.reserveXidForDeviceMessage()).thenReturn(Uint32.TWO);
86         when(deviceInfo.getVersion()).thenReturn(Uint8.ONE);
87         when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
88
89         rpcContext = new RpcContextImpl(
90                 rpcProviderRegistry,
91                 5,
92                 deviceContext,
93                 extensionConverterProvider,
94                 convertorExecutor,
95                 notificationPublishService, true);
96     }
97
98     @Test
99     public void testStoreOrFail() {
100         try (var rpcContext = new RpcContextImpl(
101                 rpcProviderRegistry,
102                 100,
103                 deviceContext,
104                 extensionConverterProvider,
105                 convertorExecutor,
106                 notificationPublishService, true)) {
107             assertNotNull(rpcContext.createRequestContext());
108         }
109     }
110
111     @Test
112     public void testStoreOrFailThatFails() {
113         try (var rpcContext = new RpcContextImpl(
114                 rpcProviderRegistry,
115                 0,
116                 deviceContext,
117                 extensionConverterProvider,
118                 convertorExecutor,
119                 notificationPublishService, true)) {
120             assertNull(rpcContext.createRequestContext());
121         }
122     }
123
124     @Test
125     public void testStoreAndCloseOrFail() {
126         try (var rpcContext = new RpcContextImpl(
127                 rpcProviderRegistry,
128                 100,
129                 deviceContext,
130                 extensionConverterProvider,
131                 convertorExecutor,
132                 notificationPublishService, true)) {
133             try (var requestContext = rpcContext.createRequestContext()) {
134                 assertNotNull(requestContext);
135             }
136             verify(messageSpy).spyMessage(RpcContextImpl.class, MessageSpy.StatisticsGroup.REQUEST_STACK_FREED);
137         }
138     }
139
140     /**
141      * When deviceContext.reserveXidForDeviceMessage returns null, null should be returned.
142      */
143     @Test
144     public void testCreateRequestContext1() {
145         when(deviceInfo.reserveXidForDeviceMessage()).thenReturn(null);
146         assertNull(rpcContext.createRequestContext());
147     }
148
149     /**
150      * When deviceContext.reserveXidForDeviceMessage returns value, AbstractRequestContext should be returned.
151      */
152     @Test
153     public void testCreateRequestContext2() {
154         try (var temp = rpcContext.createRequestContext()) {
155             // nothing
156         }
157         verify(messageSpy).spyMessage(RpcContextImpl.class, MessageSpy.StatisticsGroup.REQUEST_STACK_FREED);
158     }
159
160     @Test
161     public void testInstantiateServiceInstance() {
162         when(rpcProviderRegistry.registerRpcImplementations(any(),
163             eq(Set.of(nodeInstanceIdentifier)))).thenReturn(registration);
164
165         rpcContext.instantiateServiceInstance();
166
167         verify(rpcProviderRegistry).registerRpcImplementations(captor.capture(), eq(Set.of(nodeInstanceIdentifier)));
168
169         final var map = captor.getValue();
170         assertEquals(46, map.size());
171     }
172 }