Deprecated Rpcs and RpcErrors helper classes
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / test / java / org / opendaylight / controller / sal / binding / impl / connect / dom / RpcInvocationStrategyTest.java
1 /*
2 * Copyright (c) 2014 Brocade Communications 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.controller.sal.binding.impl.connect.dom;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Matchers.eq;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.when;
16
17 import java.net.URI;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.Date;
21 import java.util.concurrent.ExecutionException;
22 import java.util.concurrent.Future;
23
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.Mock;
27 import org.mockito.MockitoAnnotations;
28 import org.opendaylight.controller.sal.core.api.RpcProvisionRegistry;
29 import org.opendaylight.yangtools.yang.binding.DataObject;
30 import org.opendaylight.yangtools.yang.binding.RpcService;
31 import org.opendaylight.yangtools.yang.common.QName;
32 import org.opendaylight.yangtools.yang.common.RpcError;
33 import org.opendaylight.yangtools.yang.common.RpcResult;
34 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
35 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
36 import org.opendaylight.yangtools.yang.data.impl.codec.BindingIndependentMappingService;
37
38 import com.google.common.util.concurrent.Futures;
39 import com.google.common.util.concurrent.ListenableFuture;
40
41 public class RpcInvocationStrategyTest {
42
43     @Mock
44     private BindingIndependentMappingService mockMappingService;
45     @Mock
46     private RpcProvisionRegistry mockbiRpcRegistry;
47
48     private RpcInvocationStrategy rpcInvocationStrategy;
49     private ListenableFuture<RpcResult<DataObject>> futureDataObj;
50     private ListenableFuture<RpcResult<CompositeNode>> futureCompNode;
51     private final RpcError rpcError = mock(RpcError.class);
52     private final Collection<RpcError> errors = new ArrayList<RpcError>();
53
54     private final CompositeNode inputInvokeOn = mock(CompositeNode.class);
55     private final CompositeNode outputInvokeOn = mock(CompositeNode.class);
56
57     private final DataObject toDataDomInput = mock(DataObject.class);
58     private final CompositeNode toDataDomReturn = mock(CompositeNode.class);
59     private final CompositeNode invokeRpcResult = mock(CompositeNode.class);
60
61     private final DataObject inputForward = mock(DataObject.class);
62     private final DataObject outputForward = mock(DataObject.class);
63
64     private QName mockQName;
65     private URI urn;
66
67     private final MockRpcService mockRpcService = new MockRpcService();
68
69     public class MockRpcService implements RpcService {
70
71         public Future<?> rpcnameWithInputNoOutput(DataObject input) {
72             return futureDataObj;
73         }
74
75         public Future<RpcResult<DataObject>> rpcnameWithInputWithOutput(DataObject input) {
76             return futureDataObj;
77         }
78
79         public Future<RpcResult<DataObject>> rpcnameNoInputWithOutput() {
80             return futureDataObj;
81         }
82
83         public Future<?> rpcnameNoInputNoOutput() {
84             return futureDataObj;
85         }
86     }
87
88     public RpcInvocationStrategyTest() {
89         MockitoAnnotations.initMocks(this);
90     }
91
92     @Before
93     public void testInit() throws Exception {
94         urn = new URI(new String("urn:a:valid:urn"));
95     }
96
97     private void setupForForwardToDom(boolean hasOutput, boolean hasInput, int expectedErrorSize) {
98
99         if (expectedErrorSize > 0) {
100             errors.add(rpcError);
101         }
102         RpcResult<CompositeNode> result = RpcResultBuilder.<CompositeNode>success(invokeRpcResult)
103                                                             .withRpcErrors( errors ).build();
104         futureCompNode = Futures.immediateFuture(result);
105         if( hasInput )
106         {
107             when(mockMappingService.toDataDom(inputForward)).thenReturn(toDataDomReturn);
108         }
109         when(mockbiRpcRegistry.invokeRpc(eq(mockQName), any(CompositeNode.class))).thenReturn(
110                 futureCompNode);
111         if (hasOutput) {
112             when(
113                     mockMappingService.dataObjectFromDataDom(eq(rpcInvocationStrategy
114                             .getOutputClass().get()), any(CompositeNode.class))).thenReturn(
115                     outputForward);
116         }
117
118     }
119
120     private void validateForwardToDomBroker(ListenableFuture<RpcResult<?>> forwardToDomBroker,
121             boolean expectedSuccess, DataObject expectedResult, int expectedErrorSize)
122             throws InterruptedException, ExecutionException {
123         assertNotNull(forwardToDomBroker);
124         assertEquals(expectedSuccess, forwardToDomBroker.get().isSuccessful());
125         assertEquals(expectedResult, forwardToDomBroker.get().getResult());
126         assertEquals(expectedErrorSize, forwardToDomBroker.get().getErrors().size());
127     }
128
129     private void setupTestMethod(String rpcName, String testMethodName, boolean hasInput)
130             throws NoSuchMethodException {
131         mockQName = new QName(urn, new Date(0L), new String("prefix"), new String(rpcName));
132         java.lang.reflect.Method rpcMethod = hasInput ? MockRpcService.class.getMethod(rpcName,
133                 DataObject.class) : MockRpcService.class.getMethod(rpcName);
134         rpcInvocationStrategy = new RpcInvocationStrategy(mockQName, rpcMethod, mockMappingService,
135                 mockbiRpcRegistry);
136     }
137
138     /*
139      * forwardToDomBroker tests
140      */
141     @Test
142     public void testForwardToDomBroker_WithInputNoOutput() throws Exception {
143         setupTestMethod("rpcnameWithInputNoOutput", "testForwardToDomBroker_WithInputNoOutput",
144                 true);
145         setupForForwardToDom(false, true, 0);
146         ListenableFuture<RpcResult<?>> forwardToDomBroker = rpcInvocationStrategy
147                 .forwardToDomBroker(inputForward);
148
149         validateForwardToDomBroker(forwardToDomBroker, true, null, 0);
150     }
151
152     @Test
153     public void testForwardToDomBroker_WithInputNoOutput_error() throws Exception {
154         setupTestMethod("rpcnameWithInputNoOutput",
155                 "testForwardToDomBroker_WithInputNoOutput_error", true);
156         setupForForwardToDom(false, true, 1);
157         ListenableFuture<RpcResult<?>> forwardToDomBroker = rpcInvocationStrategy
158                 .forwardToDomBroker(inputForward);
159
160         validateForwardToDomBroker(forwardToDomBroker, true, null, 1);
161     }
162
163     @Test
164     public void testForwardToDomBroker_WithInputWithOutput() throws Exception {
165         setupTestMethod("rpcnameWithInputWithOutput", "testForwardToDomBroker_WithInputWithOutput",
166                 true);
167         setupForForwardToDom(true, true, 0);
168         ListenableFuture<RpcResult<?>> forwardToDomBroker = rpcInvocationStrategy
169                 .forwardToDomBroker(inputForward);
170         validateForwardToDomBroker(forwardToDomBroker, true, outputForward, 0);
171     }
172
173     @Test
174     public void testForwardToDomBroker_NoInputWithOutput() throws Exception {
175         setupTestMethod("rpcnameNoInputWithOutput", "testForwardToDomBroker_NoInputWithOutput",
176                 false);
177         setupForForwardToDom(true, false, 0);
178         ListenableFuture<RpcResult<?>> forwardToDomBroker = rpcInvocationStrategy
179                 .forwardToDomBroker(null);
180         validateForwardToDomBroker(forwardToDomBroker, true, outputForward, 0);
181     }
182
183     @Test
184     public void testForwardToDomBroker_NoInputNoOutput() throws Exception {
185         setupTestMethod("rpcnameNoInputNoOutput", "testForwardToDomBroker_NoInputNoOutput", false);
186         setupForForwardToDom(false, false, 0);
187         ListenableFuture<RpcResult<?>> forwardToDomBroker = rpcInvocationStrategy
188                 .forwardToDomBroker(null);
189         validateForwardToDomBroker(forwardToDomBroker, true, null, 0);
190     }
191
192     /*
193      * invokeOn Tests
194      */
195     private void setupRpcResultsWithOutput(int expectedErrorSize) {
196         if (expectedErrorSize > 0) {
197             errors.add(rpcError);
198         }
199         RpcResult<CompositeNode> resultCompNode = RpcResultBuilder.<CompositeNode>success(inputInvokeOn)
200                                                                         .withRpcErrors(errors).build();
201         futureCompNode = Futures.immediateFuture(resultCompNode);
202         RpcResult<DataObject> resultDataObj = RpcResultBuilder.<DataObject>success(toDataDomInput)
203                                                                            .withRpcErrors(errors).build();
204         futureDataObj = Futures.immediateFuture(resultDataObj);
205
206         when(mockMappingService.toDataDom(toDataDomInput)).thenReturn(outputInvokeOn);
207     }
208
209     private void setupRpcResultsNoOutput(int expectedErrorSize) {
210         if (expectedErrorSize > 0) {
211             errors.add(rpcError);
212         }
213         RpcResult<CompositeNode> resultCompNode = RpcResultBuilder.<CompositeNode>success(inputInvokeOn)
214                                                                           .withRpcErrors(errors).build();
215         futureCompNode = Futures.immediateFuture(resultCompNode);
216         RpcResult<DataObject> resultDataObj = RpcResultBuilder.<DataObject>success()
217                                                                           .withRpcErrors(errors).build();
218         futureDataObj = Futures.immediateFuture(resultDataObj);
219     }
220
221     private void validateReturnedImmediateFuture(
222             ListenableFuture<RpcResult<CompositeNode>> immediateFuture, boolean expectedSuccess,
223             CompositeNode expectedReturn, int expectedErrorSize) throws InterruptedException,
224             ExecutionException {
225         assertNotNull(immediateFuture);
226         assertEquals(expectedSuccess, immediateFuture.get().isSuccessful());
227         assertEquals(expectedReturn, immediateFuture.get().getResult());
228         assertEquals(expectedErrorSize, immediateFuture.get().getErrors().size());
229     }
230
231     @Test
232     public void testInvokeOn_NoInputNoOutput() throws Exception {
233         setupTestMethod("rpcnameNoInputNoOutput", "testInvokeOn_NoInputNoOutput", false);
234         setupRpcResultsNoOutput(0);
235         ListenableFuture<RpcResult<CompositeNode>> immediateFuture = Futures
236                 .immediateFuture(rpcInvocationStrategy.invokeOn(mockRpcService, inputInvokeOn));
237         validateReturnedImmediateFuture(immediateFuture, true, null, 0);
238     }
239
240     @Test
241     public void testInvokeOn_NoInputNoOutput_errors() throws Exception {
242         setupTestMethod("rpcnameNoInputNoOutput", "testInvokeOn_NoInputNoOutput", false);
243         setupRpcResultsNoOutput(1);
244         ListenableFuture<RpcResult<CompositeNode>> immediateFuture = Futures
245                 .immediateFuture(rpcInvocationStrategy.invokeOn(mockRpcService, inputInvokeOn));
246         validateReturnedImmediateFuture(immediateFuture, true, null, 1);
247     }
248
249     @Test
250     public void testInvokeOn_WithInputNoOutput() throws Exception {
251         setupTestMethod("rpcnameWithInputNoOutput", "testInvokeOn_WithInputNoOutput", true);
252         setupRpcResultsNoOutput(0);
253         ListenableFuture<RpcResult<CompositeNode>> immediateFuture = Futures
254                 .immediateFuture(rpcInvocationStrategy.invokeOn(mockRpcService, inputInvokeOn));
255         validateReturnedImmediateFuture(immediateFuture, true, null, 0);
256     }
257
258     @Test
259     public void testInvokeOn_WithInputWithOutput() throws Exception {
260         setupTestMethod("rpcnameWithInputWithOutput", "testInvokeOn_WithInputWithOutput", true);
261         setupRpcResultsWithOutput(0);
262         ListenableFuture<RpcResult<CompositeNode>> immediateFuture = Futures
263                 .immediateFuture(rpcInvocationStrategy.invokeOn(mockRpcService, inputInvokeOn));
264         validateReturnedImmediateFuture(immediateFuture, true, outputInvokeOn, 0);
265     }
266
267     @Test
268     public void testInvokeOn_NoInputWithOutput() throws Exception {
269         setupTestMethod("rpcnameNoInputWithOutput", "testInvokeOn_NoInputWithOutput", false);
270         setupRpcResultsWithOutput(0);
271         ListenableFuture<RpcResult<CompositeNode>> immediateFuture = Futures
272                 .immediateFuture(rpcInvocationStrategy.invokeOn(mockRpcService, inputInvokeOn));
273         validateReturnedImmediateFuture(immediateFuture, true, outputInvokeOn, 0);
274     }
275 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.