Bump upstream versions
[netconf.git] / netconf / sal-netconf-connector / src / test / java / org / opendaylight / netconf / sal / connect / netconf / NetconfToRpcRequestTest.java
1 /*
2  * Copyright (c) 2014, 2015 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.netconf.sal.connect.netconf;
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.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.toId;
14
15 import java.util.Collection;
16 import org.junit.Before;
17 import org.junit.BeforeClass;
18 import org.junit.Test;
19 import org.opendaylight.netconf.api.NetconfMessage;
20 import org.opendaylight.netconf.api.xml.XmlUtil;
21 import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.NetconfMessageTransformer;
22 import org.opendaylight.yangtools.rfc8528.data.util.EmptyMountPointContext;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
25 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
26 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
27 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
28 import org.opendaylight.yangtools.yang.model.api.Module;
29 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
30 import org.w3c.dom.Document;
31
32 public class NetconfToRpcRequestTest extends AbstractBaseSchemasTest {
33
34     private static final String TEST_MODEL_NAMESPACE = "urn:opendaylight:params:xml:ns:yang:controller:md:sal:rpc-test";
35     private static final String REVISION = "2014-07-14";
36     private static final QName STREAM_NAME = QName.create(TEST_MODEL_NAMESPACE, REVISION, "stream-name");
37     private static final QName SUBSCRIBE_RPC_NAME = QName.create(TEST_MODEL_NAMESPACE, REVISION, "subscribe");
38
39     private static final String CONFIG_TEST_NAMESPACE =
40             "urn:opendaylight:params:xml:ns:yang:controller:md:sal:test:rpc:config:defs";
41     private static final String CONFIG_TEST_REVISION = "2014-07-21";
42     private static final QName EDIT_CONFIG_QNAME =
43             QName.create(CONFIG_TEST_NAMESPACE, CONFIG_TEST_REVISION, "edit-config");
44
45     static EffectiveModelContext cfgCtx;
46
47     private  NetconfMessageTransformer messageTransformer;
48
49     @BeforeClass
50     public static void setup() {
51         final Collection<? extends Module> notifModules = YangParserTestUtils.parseYangResource(
52             "/schemas/rpc-notification-subscription.yang").getModules();
53         assertTrue(!notifModules.isEmpty());
54
55         cfgCtx = YangParserTestUtils.parseYangResources(NetconfToRpcRequestTest.class,
56             "/schemas/config-test-rpc.yang", "/schemas/rpc-notification-subscription.yang");
57     }
58
59     @Before
60     public void before() {
61         messageTransformer = new NetconfMessageTransformer(new EmptyMountPointContext(cfgCtx), true,
62             BASE_SCHEMAS.getBaseSchema());
63     }
64
65     @Test
66     public void testUserDefinedRpcCall() throws Exception {
67         final ContainerNode root = Builders.containerBuilder()
68                 .withNodeIdentifier(toId(SUBSCRIBE_RPC_NAME))
69                 .withChild(ImmutableNodes.leafNode(STREAM_NAME, "NETCONF"))
70                 .build();
71
72         final NetconfMessage message = messageTransformer.toRpcRequest(SUBSCRIBE_RPC_NAME, root);
73         assertNotNull(message);
74
75         final Document xmlDoc = message.getDocument();
76         final org.w3c.dom.Node rpcChild = xmlDoc.getFirstChild();
77         assertEquals(rpcChild.getLocalName(), "rpc");
78
79         final org.w3c.dom.Node subscribeName = rpcChild.getFirstChild();
80         assertEquals(subscribeName.getLocalName(), "subscribe");
81
82         final org.w3c.dom.Node streamName = subscribeName.getFirstChild();
83         assertEquals(streamName.getLocalName(), "stream-name");
84
85     }
86
87     // The edit config defined in yang has no output
88     @Test(expected = IllegalArgumentException.class)
89     public void testRpcResponse() throws Exception {
90         final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
91                 "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"m-5\">\n"
92                         + "<data xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">"
93                         + "module schema"
94                         + "</data>\n"
95                         + "</rpc-reply>\n"
96         ));
97
98         messageTransformer.toRpcResult(response, EDIT_CONFIG_QNAME);
99     }
100
101 }