Deprecated Rpcs and RpcErrors helper classes
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / InvokeRpcMethodTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  * Copyright (c) 2014 Brocade Communications Systems, Inc.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9 package org.opendaylight.controller.sal.restconf.impl.test;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertSame;
14 import static org.junit.Assert.assertTrue;
15 import static org.junit.Assert.fail;
16 import static org.mockito.Matchers.any;
17 import static org.mockito.Matchers.eq;
18 import static org.mockito.Mockito.mock;
19 import static org.mockito.Mockito.spy;
20 import static org.mockito.Mockito.when;
21
22 import com.google.common.base.Optional;
23 import com.google.common.util.concurrent.Futures;
24 import com.google.common.util.concurrent.ListenableFuture;
25 import java.io.FileNotFoundException;
26 import java.net.URI;
27 import java.net.URISyntaxException;
28 import java.util.Arrays;
29 import java.util.Collections;
30 import java.util.HashSet;
31 import java.util.List;
32 import java.util.Set;
33 import javax.ws.rs.core.MultivaluedHashMap;
34 import javax.ws.rs.core.MultivaluedMap;
35 import javax.ws.rs.core.UriInfo;
36 import org.junit.Before;
37 import org.junit.BeforeClass;
38 import org.junit.Test;
39
40 import org.opendaylight.controller.sal.core.api.mount.MountInstance;
41 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
42 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
43 import org.opendaylight.controller.sal.restconf.impl.InstanceIdWithSchemaNode;
44 import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException;
45 import org.opendaylight.controller.sal.restconf.impl.RestconfError;
46 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorTag;
47 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorType;
48 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
49 import org.opendaylight.controller.sal.restconf.impl.StructuredData;
50 import org.opendaylight.yangtools.yang.common.QName;
51 import org.opendaylight.yangtools.yang.common.RpcError;
52 import org.opendaylight.yangtools.yang.common.RpcResult;
53 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
54 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
55 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
56 import org.opendaylight.yangtools.yang.data.api.MutableCompositeNode;
57 import org.opendaylight.yangtools.yang.data.api.MutableSimpleNode;
58 import org.opendaylight.yangtools.yang.data.impl.NodeFactory;
59 import org.opendaylight.yangtools.yang.model.api.Module;
60 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
61 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
62
63 public class InvokeRpcMethodTest {
64
65     private RestconfImpl restconfImpl = null;
66     private static ControllerContext controllerContext = null;
67     private static UriInfo uriInfo;
68
69     @BeforeClass
70     public static void init() throws FileNotFoundException {
71         Set<Module> allModules = new HashSet<Module>(TestUtils.loadModulesFrom("/full-versions/yangs"));
72         allModules.addAll(TestUtils.loadModulesFrom("/invoke-rpc"));
73         assertNotNull(allModules);
74         Module module = TestUtils.resolveModule("invoke-rpc-module", allModules);
75         assertNotNull(module);
76         SchemaContext schemaContext = TestUtils.loadSchemaContext(allModules);
77         controllerContext = spy(ControllerContext.getInstance());
78         controllerContext.setSchemas(schemaContext);
79         uriInfo = mock(UriInfo.class);
80         MultivaluedMap<String, String> map = new MultivaluedHashMap<>();
81         map.put("prettyPrint", Collections.singletonList("true"));
82         when(uriInfo.getQueryParameters(any(Boolean.class))).thenReturn(map);
83     }
84
85     @Before
86     public void initMethod() {
87         restconfImpl = RestconfImpl.getInstance();
88         restconfImpl.setControllerContext(controllerContext);
89     }
90
91     /**
92      * Test method invokeRpc in RestconfImpl class tests if composite node as input parameter of method invokeRpc
93      * (second argument) is wrapped to parent composite node which has QName equals to QName of rpc (resolved from
94      * string - first argument).
95      */
96     @Test
97     public void invokeRpcMtethodTest() {
98         ControllerContext contContext = controllerContext;
99         try {
100             contContext.findModuleNameByNamespace(new URI("invoke:rpc:module"));
101         } catch (URISyntaxException e) {
102             assertTrue("Uri wasn't created sucessfuly", false);
103         }
104
105         BrokerFacade mockedBrokerFacade = mock(BrokerFacade.class);
106
107         RestconfImpl restconf = RestconfImpl.getInstance();
108         restconf.setBroker(mockedBrokerFacade);
109         restconf.setControllerContext(contContext);
110
111         CompositeNode payload = preparePayload();
112
113         when(mockedBrokerFacade.invokeRpc(any(QName.class), any(CompositeNode.class))).thenReturn(
114                 Futures.<RpcResult<CompositeNode>> immediateFuture(RpcResultBuilder.<CompositeNode>success().build()));
115
116         StructuredData structData = restconf.invokeRpc("invoke-rpc-module:rpc-test", payload, uriInfo);
117         assertTrue(structData == null);
118
119     }
120
121     private CompositeNode preparePayload() {
122         MutableCompositeNode cont = NodeFactory.createMutableCompositeNode(
123                 TestUtils.buildQName("cont", "nmspc", "2013-12-04"), null, null, ModifyAction.CREATE, null);
124         MutableSimpleNode<?> lf = NodeFactory.createMutableSimpleNode(
125                 TestUtils.buildQName("lf", "nmspc", "2013-12-04"), cont, "any value", ModifyAction.CREATE, null);
126         cont.getValue().add(lf);
127         cont.init();
128
129         return cont;
130     }
131
132     @Test
133     public void testInvokeRpcWithNoPayloadRpc_FailNoErrors() {
134         RpcResult<CompositeNode> rpcResult = RpcResultBuilder.<CompositeNode>failed().build();
135
136         BrokerFacade brokerFacade = mock(BrokerFacade.class);
137         when(
138                 brokerFacade.invokeRpc(
139                         eq(QName.create("(http://netconfcentral.org/ns/toaster?revision=2009-11-20)cancel-toast")),
140                         any(CompositeNode.class))).thenReturn(
141                 Futures.<RpcResult<CompositeNode>> immediateFuture(rpcResult));
142
143         restconfImpl.setBroker(brokerFacade);
144
145         try {
146             restconfImpl.invokeRpc("toaster:cancel-toast", "", uriInfo);
147             fail("Expected an exception to be thrown.");
148         } catch (RestconfDocumentedException e) {
149             verifyRestconfDocumentedException(e, 0, ErrorType.RPC, ErrorTag.OPERATION_FAILED,
150                     Optional.<String> absent(), Optional.<String> absent());
151         }
152     }
153
154     void verifyRestconfDocumentedException(final RestconfDocumentedException e, final int index,
155             final ErrorType expErrorType, final ErrorTag expErrorTag, final Optional<String> expErrorMsg,
156             final Optional<String> expAppTag) {
157         RestconfError actual = null;
158         try {
159             actual = e.getErrors().get(index);
160         } catch (ArrayIndexOutOfBoundsException ex) {
161             fail("RestconfError not found at index " + index);
162         }
163
164         assertEquals("getErrorType", expErrorType, actual.getErrorType());
165         assertEquals("getErrorTag", expErrorTag, actual.getErrorTag());
166         assertNotNull("getErrorMessage is null", actual.getErrorMessage());
167
168         if (expErrorMsg.isPresent()) {
169             assertEquals("getErrorMessage", expErrorMsg.get(), actual.getErrorMessage());
170         }
171
172         if (expAppTag.isPresent()) {
173             assertEquals("getErrorAppTag", expAppTag.get(), actual.getErrorAppTag());
174         }
175     }
176
177     @Test
178     public void testInvokeRpcWithNoPayloadRpc_FailWithRpcError() {
179         List<RpcError> rpcErrors = Arrays.asList(
180             RpcResultBuilder.newError( RpcError.ErrorType.TRANSPORT, "bogusTag", "foo" ),
181             RpcResultBuilder.newWarning( RpcError.ErrorType.RPC, "in-use", "bar",
182                                          "app-tag", null, null ) );
183
184         RpcResult<CompositeNode> rpcResult = RpcResultBuilder.<CompositeNode>failed()
185                                                               .withRpcErrors(rpcErrors).build();
186
187         BrokerFacade brokerFacade = mock(BrokerFacade.class);
188         when(
189                 brokerFacade.invokeRpc(
190                         eq(QName.create("(http://netconfcentral.org/ns/toaster?revision=2009-11-20)cancel-toast")),
191                         any(CompositeNode.class))).thenReturn(
192                 Futures.<RpcResult<CompositeNode>> immediateFuture(rpcResult));
193
194         restconfImpl.setBroker(brokerFacade);
195
196         try {
197             restconfImpl.invokeRpc("toaster:cancel-toast", "", uriInfo);
198             fail("Expected an exception to be thrown.");
199         } catch (RestconfDocumentedException e) {
200             verifyRestconfDocumentedException(e, 0, ErrorType.TRANSPORT, ErrorTag.OPERATION_FAILED, Optional.of("foo"),
201                     Optional.<String> absent());
202             verifyRestconfDocumentedException(e, 1, ErrorType.RPC, ErrorTag.IN_USE, Optional.of("bar"),
203                     Optional.of("app-tag"));
204         }
205     }
206
207     @Test
208     public void testInvokeRpcWithNoPayload_Success() {
209         RpcResult<CompositeNode> rpcResult = RpcResultBuilder.<CompositeNode>success().build();
210
211         BrokerFacade brokerFacade = mock(BrokerFacade.class);
212         when(
213                 brokerFacade.invokeRpc(
214                         eq(QName.create("(http://netconfcentral.org/ns/toaster?revision=2009-11-20)cancel-toast")),
215                         any(CompositeNode.class))).thenReturn(
216                 Futures.<RpcResult<CompositeNode>> immediateFuture(rpcResult));
217
218         restconfImpl.setBroker(brokerFacade);
219
220         StructuredData output = restconfImpl.invokeRpc("toaster:cancel-toast", "", uriInfo);
221         assertEquals(null, output);
222         // additional validation in the fact that the restconfImpl does not
223         // throw an exception.
224     }
225
226     @Test
227     public void testInvokeRpcMethodExpectingNoPayloadButProvidePayload() {
228         try {
229             restconfImpl.invokeRpc("toaster:cancel-toast", " a payload ", uriInfo);
230             fail("Expected an exception");
231         } catch (RestconfDocumentedException e) {
232             verifyRestconfDocumentedException(e, 0, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE,
233                     Optional.<String> absent(), Optional.<String> absent());
234         }
235     }
236
237     @Test
238     public void testInvokeRpcMethodWithBadMethodName() {
239         try {
240             restconfImpl.invokeRpc("toaster:bad-method", "", uriInfo);
241             fail("Expected an exception");
242         } catch (RestconfDocumentedException e) {
243             verifyRestconfDocumentedException(e, 0, ErrorType.RPC, ErrorTag.UNKNOWN_ELEMENT,
244                     Optional.<String> absent(), Optional.<String> absent());
245         }
246     }
247
248     @Test
249     public void testInvokeRpcMethodWithInput() {
250         RpcResult<CompositeNode> rpcResult = RpcResultBuilder.<CompositeNode>success().build();
251
252         CompositeNode payload = mock(CompositeNode.class);
253
254         BrokerFacade brokerFacade = mock(BrokerFacade.class);
255         when(
256                 brokerFacade.invokeRpc(
257                         eq(QName.create("(http://netconfcentral.org/ns/toaster?revision=2009-11-20)make-toast")),
258                         any(CompositeNode.class))).thenReturn(
259                 Futures.<RpcResult<CompositeNode>> immediateFuture(rpcResult));
260
261         restconfImpl.setBroker(brokerFacade);
262
263         StructuredData output = restconfImpl.invokeRpc("toaster:make-toast", payload, uriInfo);
264         assertEquals(null, output);
265         // additional validation in the fact that the restconfImpl does not
266         // throw an exception.
267     }
268
269     @Test
270     public void testThrowExceptionWhenSlashInModuleName() {
271         try {
272             restconfImpl.invokeRpc("toaster/slash", "", uriInfo);
273             fail("Expected an exception.");
274         } catch (RestconfDocumentedException e) {
275             verifyRestconfDocumentedException(e, 0, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE,
276                     Optional.<String> absent(), Optional.<String> absent());
277         }
278     }
279
280     @Test
281     public void testInvokeRpcWithNoPayloadWithOutput_Success() {
282         CompositeNode compositeNode = mock(CompositeNode.class);
283         RpcResult<CompositeNode> rpcResult =
284                                   RpcResultBuilder.<CompositeNode>success(compositeNode).build();
285
286         BrokerFacade brokerFacade = mock(BrokerFacade.class);
287         when(
288                 brokerFacade.invokeRpc(
289                         eq(QName.create("(http://netconfcentral.org/ns/toaster?revision=2009-11-20)testOutput")),
290                         any(CompositeNode.class))).thenReturn(
291                 Futures.<RpcResult<CompositeNode>> immediateFuture(rpcResult));
292
293         restconfImpl.setBroker(brokerFacade);
294
295         StructuredData output = restconfImpl.invokeRpc("toaster:testOutput", "", uriInfo);
296         assertNotNull(output);
297         assertSame(compositeNode, output.getData());
298         assertNotNull(output.getSchema());
299     }
300
301     @Test
302     public void testMountedRpcCallNoPayload_Success() throws Exception {
303         RpcResult<CompositeNode> rpcResult = RpcResultBuilder.<CompositeNode>success().build();
304
305         ListenableFuture<RpcResult<CompositeNode>> mockListener = mock(ListenableFuture.class);
306         when(mockListener.get()).thenReturn(rpcResult);
307
308         QName cancelToastQName = QName.create("namespace", "2014-05-28", "cancelToast");
309
310         RpcDefinition mockRpc = mock(RpcDefinition.class);
311         when(mockRpc.getQName()).thenReturn(cancelToastQName);
312
313         MountInstance mockMountPoint = mock(MountInstance.class);
314         when(mockMountPoint.rpc(eq(cancelToastQName), any(CompositeNode.class))).thenReturn(mockListener);
315
316         InstanceIdWithSchemaNode mockedInstanceId = mock(InstanceIdWithSchemaNode.class);
317         when(mockedInstanceId.getMountPoint()).thenReturn(mockMountPoint);
318
319         ControllerContext mockedContext = mock(ControllerContext.class);
320         String cancelToastStr = "toaster:cancel-toast";
321         when(mockedContext.urlPathArgDecode(cancelToastStr)).thenReturn(cancelToastStr);
322         when(mockedContext.getRpcDefinition(cancelToastStr)).thenReturn(mockRpc);
323         when(
324                 mockedContext.toMountPointIdentifier("opendaylight-inventory:nodes/node/"
325                         + "REMOTE_HOST/yang-ext:mount/toaster:cancel-toast")).thenReturn(mockedInstanceId);
326
327         restconfImpl.setControllerContext(mockedContext);
328         StructuredData output = restconfImpl.invokeRpc(
329                 "opendaylight-inventory:nodes/node/REMOTE_HOST/yang-ext:mount/toaster:cancel-toast", "", uriInfo);
330         assertEquals(null, output);
331
332         // additional validation in the fact that the restconfImpl does not
333         // throw an exception.
334     }
335 }

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