Merge "Prevent ConfigPusher from killing its thread"
[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  *
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.restconf.impl.test;
9
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;
16
17 import java.net.URI;
18 import java.net.URISyntaxException;
19 import java.util.Set;
20
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;
38
39 public class InvokeRpcMethodTest {
40
41     private static Set<Module> modules;
42
43     private class AnswerImpl implements Answer<RpcResult<CompositeNode>> {
44         @Override
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();
48         }
49     }
50
51     @BeforeClass
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);
57     }
58
59     /**
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).
64      */
65     @Test
66     public void invokeRpcMethodTest() {
67         ControllerContext contContext = ControllerContext.getInstance();
68         contContext.onGlobalContextUpdated(TestUtils.loadSchemaContext(modules));
69         try {
70             contContext.findModuleNameByNamespace(new URI("invoke:rpc:module"));
71         } catch (URISyntaxException e) {
72             assertTrue("Uri wasn't created sucessfuly", false);
73         }
74
75         BrokerFacade mockedBrokerFacade = mock(BrokerFacade.class);
76
77         RestconfImpl restconf = RestconfImpl.getInstance();
78         restconf.setBroker(mockedBrokerFacade);
79         restconf.setControllerContext(contContext);
80
81         when(mockedBrokerFacade.invokeRpc(any(QName.class), any(CompositeNode.class))).thenAnswer(new AnswerImpl());
82
83         StructuredData structData = restconf.invokeRpc("invoke-rpc-module:rpc-test", preparePayload());
84
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());
89
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;
95                 }
96             }
97         }
98         assertNotNull(cont);
99
100     }
101
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);
108         cont.init();
109
110         return cont;
111     }
112
113 }