c5aea8f2ab666cb5e777e3f20d808910ab3308c6
[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.common.util.Rpcs;
29 import org.opendaylight.controller.sal.core.api.RpcProvisionRegistry;
30 import org.opendaylight.yangtools.yang.binding.DataObject;
31 import org.opendaylight.yangtools.yang.binding.RpcService;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.common.RpcError;
34 import org.opendaylight.yangtools.yang.common.RpcResult;
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         RpcResult<CompositeNode> result = Rpcs.getRpcResult(true, invokeRpcResult, errors);
102         futureCompNode = Futures.immediateFuture(result);
103         if( hasInput )
104         {
105             when(mockMappingService.toDataDom(inputForward)).thenReturn(toDataDomReturn);
106         }
107         when(mockbiRpcRegistry.invokeRpc(eq(mockQName), any(CompositeNode.class))).thenReturn(
108                 futureCompNode);
109         if (hasOutput) {
110             when(
111                     mockMappingService.dataObjectFromDataDom(eq(rpcInvocationStrategy
112                             .getOutputClass().get()), any(CompositeNode.class))).thenReturn(
113                     outputForward);
114         }
115
116     }
117
118     private void validateForwardToDomBroker(ListenableFuture<RpcResult<?>> forwardToDomBroker,
119             boolean expectedSuccess, DataObject expectedResult, int expectedErrorSize)
120             throws InterruptedException, ExecutionException {
121         assertNotNull(forwardToDomBroker);
122         assertEquals(expectedSuccess, forwardToDomBroker.get().isSuccessful());
123         assertEquals(expectedResult, forwardToDomBroker.get().getResult());
124         assertEquals(expectedErrorSize, forwardToDomBroker.get().getErrors().size());
125     }
126
127     private void setupTestMethod(String rpcName, String testMethodName, boolean hasInput)
128             throws NoSuchMethodException {
129         mockQName = new QName(urn, new Date(0L), new String("prefix"), new String(rpcName));
130         java.lang.reflect.Method rpcMethod = hasInput ? MockRpcService.class.getMethod(rpcName,
131                 DataObject.class) : MockRpcService.class.getMethod(rpcName);
132         rpcInvocationStrategy = new RpcInvocationStrategy(mockQName, rpcMethod, mockMappingService,
133                 mockbiRpcRegistry);
134     }
135
136     /*
137      * forwardToDomBroker tests
138      */
139     @Test
140     public void testForwardToDomBroker_WithInputNoOutput() throws Exception {
141         setupTestMethod("rpcnameWithInputNoOutput", "testForwardToDomBroker_WithInputNoOutput",
142                 true);
143         setupForForwardToDom(false, true, 0);
144         ListenableFuture<RpcResult<?>> forwardToDomBroker = rpcInvocationStrategy
145                 .forwardToDomBroker(inputForward);
146
147         validateForwardToDomBroker(forwardToDomBroker, true, null, 0);
148     }
149
150     @Test
151     public void testForwardToDomBroker_WithInputNoOutput_error() throws Exception {
152         setupTestMethod("rpcnameWithInputNoOutput",
153                 "testForwardToDomBroker_WithInputNoOutput_error", true);
154         setupForForwardToDom(false, true, 1);
155         ListenableFuture<RpcResult<?>> forwardToDomBroker = rpcInvocationStrategy
156                 .forwardToDomBroker(inputForward);
157
158         validateForwardToDomBroker(forwardToDomBroker, true, null, 1);
159     }
160
161     @Test
162     public void testForwardToDomBroker_WithInputWithOutput() throws Exception {
163         setupTestMethod("rpcnameWithInputWithOutput", "testForwardToDomBroker_WithInputWithOutput",
164                 true);
165         setupForForwardToDom(true, true, 0);
166         ListenableFuture<RpcResult<?>> forwardToDomBroker = rpcInvocationStrategy
167                 .forwardToDomBroker(inputForward);
168         validateForwardToDomBroker(forwardToDomBroker, true, outputForward, 0);
169     }
170
171     @Test
172     public void testForwardToDomBroker_NoInputWithOutput() throws Exception {
173         setupTestMethod("rpcnameNoInputWithOutput", "testForwardToDomBroker_NoInputWithOutput",
174                 false);
175         setupForForwardToDom(true, false, 0);
176         ListenableFuture<RpcResult<?>> forwardToDomBroker = rpcInvocationStrategy
177                 .forwardToDomBroker(null);
178         validateForwardToDomBroker(forwardToDomBroker, true, outputForward, 0);
179     }
180
181     @Test
182     public void testForwardToDomBroker_NoInputNoOutput() throws Exception {
183         setupTestMethod("rpcnameNoInputNoOutput", "testForwardToDomBroker_NoInputNoOutput", false);
184         setupForForwardToDom(false, false, 0);
185         ListenableFuture<RpcResult<?>> forwardToDomBroker = rpcInvocationStrategy
186                 .forwardToDomBroker(null);
187         validateForwardToDomBroker(forwardToDomBroker, true, null, 0);
188     }
189
190     /*
191      * invokeOn Tests
192      */
193     private void setupRpcResultsWithOutput(int expectedErrorSize) {
194         if (expectedErrorSize > 0)
195             errors.add(rpcError);
196         RpcResult<CompositeNode> resultCompNode = Rpcs.getRpcResult(true, inputInvokeOn, errors);
197         futureCompNode = Futures.immediateFuture(resultCompNode);
198         RpcResult<DataObject> resultDataObj = Rpcs.getRpcResult(true, toDataDomInput, errors);
199         futureDataObj = Futures.immediateFuture(resultDataObj);
200
201         when(mockMappingService.toDataDom(toDataDomInput)).thenReturn(outputInvokeOn);
202     }
203
204     private void setupRpcResultsNoOutput(int expectedErrorSize) {
205         if (expectedErrorSize > 0)
206             errors.add(rpcError);
207         RpcResult<CompositeNode> resultCompNode = Rpcs.getRpcResult(true, inputInvokeOn, errors);
208         futureCompNode = Futures.immediateFuture(resultCompNode);
209         RpcResult<DataObject> resultDataObj = Rpcs.getRpcResult(true, null, errors);
210         futureDataObj = Futures.immediateFuture(resultDataObj);
211     }
212
213     private void validateReturnedImmediateFuture(
214             ListenableFuture<RpcResult<CompositeNode>> immediateFuture, boolean expectedSuccess,
215             CompositeNode expectedReturn, int expectedErrorSize) throws InterruptedException,
216             ExecutionException {
217         assertNotNull(immediateFuture);
218         assertEquals(expectedSuccess, immediateFuture.get().isSuccessful());
219         assertEquals(expectedReturn, immediateFuture.get().getResult());
220         assertEquals(expectedErrorSize, immediateFuture.get().getErrors().size());
221     }
222
223     @Test
224     public void testInvokeOn_NoInputNoOutput() throws Exception {
225         setupTestMethod("rpcnameNoInputNoOutput", "testInvokeOn_NoInputNoOutput", false);
226         setupRpcResultsNoOutput(0);
227         ListenableFuture<RpcResult<CompositeNode>> immediateFuture = Futures
228                 .immediateFuture(rpcInvocationStrategy.invokeOn(mockRpcService, inputInvokeOn));
229         validateReturnedImmediateFuture(immediateFuture, true, null, 0);
230     }
231
232     @Test
233     public void testInvokeOn_NoInputNoOutput_errors() throws Exception {
234         setupTestMethod("rpcnameNoInputNoOutput", "testInvokeOn_NoInputNoOutput", false);
235         setupRpcResultsNoOutput(1);
236         ListenableFuture<RpcResult<CompositeNode>> immediateFuture = Futures
237                 .immediateFuture(rpcInvocationStrategy.invokeOn(mockRpcService, inputInvokeOn));
238         validateReturnedImmediateFuture(immediateFuture, true, null, 1);
239     }
240
241     @Test
242     public void testInvokeOn_WithInputNoOutput() throws Exception {
243         setupTestMethod("rpcnameWithInputNoOutput", "testInvokeOn_WithInputNoOutput", true);
244         setupRpcResultsNoOutput(0);
245         ListenableFuture<RpcResult<CompositeNode>> immediateFuture = Futures
246                 .immediateFuture(rpcInvocationStrategy.invokeOn(mockRpcService, inputInvokeOn));
247         validateReturnedImmediateFuture(immediateFuture, true, null, 0);
248     }
249
250     @Test
251     public void testInvokeOn_WithInputWithOutput() throws Exception {
252         setupTestMethod("rpcnameWithInputWithOutput", "testInvokeOn_WithInputWithOutput", true);
253         setupRpcResultsWithOutput(0);
254         ListenableFuture<RpcResult<CompositeNode>> immediateFuture = Futures
255                 .immediateFuture(rpcInvocationStrategy.invokeOn(mockRpcService, inputInvokeOn));
256         validateReturnedImmediateFuture(immediateFuture, true, outputInvokeOn, 0);
257     }
258
259     @Test
260     public void testInvokeOn_NoInputWithOutput() throws Exception {
261         setupTestMethod("rpcnameNoInputWithOutput", "testInvokeOn_NoInputWithOutput", false);
262         setupRpcResultsWithOutput(0);
263         ListenableFuture<RpcResult<CompositeNode>> immediateFuture = Futures
264                 .immediateFuture(rpcInvocationStrategy.invokeOn(mockRpcService, inputInvokeOn));
265         validateReturnedImmediateFuture(immediateFuture, true, outputInvokeOn, 0);
266     }
267 }

©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.