2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.restconf.impl.test;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Matchers.any;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.when;
18 import java.net.URISyntaxException;
21 import org.junit.BeforeClass;
22 import org.junit.Test;
23 import org.mockito.invocation.InvocationOnMock;
24 import org.mockito.stubbing.Answer;
25 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
26 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
27 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
28 import org.opendaylight.controller.sal.restconf.impl.StructuredData;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.common.RpcResult;
31 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
32 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
33 import org.opendaylight.yangtools.yang.data.api.MutableCompositeNode;
34 import org.opendaylight.yangtools.yang.data.api.MutableSimpleNode;
35 import org.opendaylight.yangtools.yang.data.api.Node;
36 import org.opendaylight.yangtools.yang.data.impl.NodeFactory;
37 import org.opendaylight.yangtools.yang.model.api.Module;
39 public class InvokeRpcMethodTest {
41 private static Set<Module> modules;
43 private class AnswerImpl implements Answer<RpcResult<CompositeNode>> {
45 public RpcResult<CompositeNode> answer(InvocationOnMock invocation) throws Throwable {
46 CompositeNode compNode = (CompositeNode) invocation.getArguments()[1];
47 return new DummyRpcResult.Builder<CompositeNode>().result(compNode).isSuccessful(true).build();
52 public static void initialization() {
53 modules = TestUtils.loadModulesFrom("/invoke-rpc");
54 assertEquals(1, modules.size());
55 Module module = TestUtils.resolveModule("invoke-rpc-module", modules);
56 assertNotNull(module);
60 * Test method invokeRpc in RestconfImpl class tests if composite node as
61 * input parameter of method invokeRpc (second argument) is wrapped to
62 * parent composite node which has QName equals to QName of rpc (resolved
63 * from string - first argument).
66 public void invokeRpcMethodTest() {
67 ControllerContext contContext = ControllerContext.getInstance();
68 contContext.onGlobalContextUpdated(TestUtils.loadSchemaContext(modules));
70 contContext.findModuleNameByNamespace(new URI("invoke:rpc:module"));
71 } catch (URISyntaxException e) {
72 assertTrue("Uri wasn't created sucessfuly", false);
75 BrokerFacade mockedBrokerFacade = mock(BrokerFacade.class);
77 RestconfImpl restconf = RestconfImpl.getInstance();
78 restconf.setBroker(mockedBrokerFacade);
79 restconf.setControllerContext(contContext);
81 when(mockedBrokerFacade.invokeRpc(any(QName.class), any(CompositeNode.class))).thenAnswer(new AnswerImpl());
83 StructuredData structData = restconf.invokeRpc("invoke-rpc-module:rpc-test", preparePayload());
85 CompositeNode rpcCompNode = structData.getData();
86 CompositeNode cont = null;
87 assertEquals("invoke:rpc:module", rpcCompNode.getNodeType().getNamespace().toString());
88 assertEquals("rpc-test", rpcCompNode.getNodeType().getLocalName());
90 for (Node<?> node : rpcCompNode.getChildren()) {
91 if (node.getNodeType().getLocalName().equals("cont")
92 && node.getNodeType().getNamespace().toString().equals("nmspc")) {
93 if (node instanceof CompositeNode) {
94 cont = (CompositeNode) node;
102 private CompositeNode preparePayload() {
103 MutableCompositeNode cont = NodeFactory.createMutableCompositeNode(
104 TestUtils.buildQName("cont", "nmspc", "2013-12-04"), null, null, ModifyAction.CREATE, null);
105 MutableSimpleNode<?> lf = NodeFactory.createMutableSimpleNode(
106 TestUtils.buildQName("lf", "nmspc", "2013-12-04"), cont, "any value", ModifyAction.CREATE, null);
107 cont.getChildren().add(lf);