Migrate netconf to MD-SAL APIs
[netconf.git] / netconf / sal-netconf-connector / src / test / java / org / opendaylight / netconf / sal / connect / netconf / schema / mapping / SchemalessMessageTransformerTest.java
1 /*
2  * Copyright (c) 2016 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.schema.mapping;
9
10 import javax.xml.transform.dom.DOMSource;
11 import org.custommonkey.xmlunit.Diff;
12 import org.custommonkey.xmlunit.XMLUnit;
13 import org.junit.Assert;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.opendaylight.mdsal.dom.api.DOMNotification;
17 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
18 import org.opendaylight.netconf.api.NetconfMessage;
19 import org.opendaylight.netconf.api.xml.XmlUtil;
20 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
21 import org.opendaylight.netconf.sal.connect.util.MessageCounter;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
25 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
26 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
27 import org.w3c.dom.Document;
28 import org.w3c.dom.Node;
29
30 public class SchemalessMessageTransformerTest {
31
32     static {
33         XMLUnit.setIgnoreWhitespace(true);
34     }
35
36     private static final String EXP_REQUEST =
37             "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
38                     + "<test-rpc xmlns=\"test-ns\">\n"
39                     + "<input>aaa</input>\n"
40                     + "</test-rpc>\n"
41                     + "</rpc>";
42     private static final String EXP_REPLY =
43             "<rpc-reply message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
44                     + "<output xmlns=\"test-ns\">aaa</output>\n"
45                     + "</rpc-reply>";
46     private static final String OK_REPLY = "<rpc-reply message-id=\"101\"\n"
47             + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
48             + "<ok/>\n"
49             + "</rpc-reply>\n";
50
51     private SchemalessMessageTransformer transformer;
52     private static final QName TEST_RPC = QName.create("test-ns", "2016-10-13", "test-rpc");
53     private static final SchemaPath SCHEMA_PATH = SchemaPath.create(true, TEST_RPC);
54
55     @Before
56     public void setUp() throws Exception {
57         transformer = new SchemalessMessageTransformer(new MessageCounter());
58     }
59
60     @Test
61     public void toNotification() throws Exception {
62         final Document payload = XmlUtil.readXmlToDocument(getClass().getResourceAsStream("/notification-payload.xml"));
63         final NetconfMessage netconfMessage = new NetconfMessage(payload);
64         final DOMNotification domNotification = transformer.toNotification(netconfMessage);
65         Assert.assertEquals(domNotification.getType().getLastComponent(),
66                 SchemalessMessageTransformer.SCHEMALESS_NOTIFICATION_PAYLOAD.getNodeType());
67         final QName qName =
68                 QName.create("org:opendaylight:notification:test:ns:yang:user-notification", "user-visited-page");
69         final AnyXmlNode dataContainerChild =
70                 (AnyXmlNode) domNotification.getBody().getChild(new YangInstanceIdentifier.NodeIdentifier(qName)).get();
71         final Diff diff = XMLUnit.compareXML(payload, dataContainerChild.getValue().getNode().getOwnerDocument());
72         Assert.assertTrue(diff.toString(), diff.similar());
73
74     }
75
76     @Test
77     public void toRpcRequest() throws Exception {
78         final Node src = XmlUtil.readXmlToDocument("<test-rpc xmlns=\"test-ns\"><input>aaa</input></test-rpc>");
79         final AnyXmlNode input = Builders.anyXmlBuilder()
80                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TEST_RPC))
81                 .withValue(new DOMSource(src))
82                 .build();
83         final NetconfMessage netconfMessage = transformer.toRpcRequest(SCHEMA_PATH, input);
84         final Diff diff = XMLUnit.compareXML(XmlUtil.readXmlToDocument(EXP_REQUEST), netconfMessage.getDocument());
85         Assert.assertTrue(diff.toString(), diff.similar());
86     }
87
88     @Test
89     public void toRpcResult() throws Exception {
90         final Document doc = XmlUtil.readXmlToDocument(EXP_REPLY);
91         final NetconfMessage netconfMessage = new NetconfMessage(doc);
92         final DOMRpcResult result = transformer.toRpcResult(netconfMessage, SCHEMA_PATH);
93         final DOMSource value = (DOMSource) result.getResult().getValue();
94         Assert.assertNotNull(result.getResult());
95         final Document domSourceDoc = (Document) value.getNode();
96         final Diff diff = XMLUnit.compareXML(XmlUtil.readXmlToDocument(EXP_REPLY), domSourceDoc);
97         Assert.assertTrue(diff.toString(), diff.similar());
98     }
99
100     @Test
101     public void toEmptyRpcResult() throws Exception {
102         final Document doc = XmlUtil.readXmlToDocument(OK_REPLY);
103         final DOMRpcResult result = transformer.toRpcResult(
104                 new NetconfMessage(doc), SchemaPath.create(true, NetconfMessageTransformUtil.NETCONF_COMMIT_QNAME));
105         Assert.assertNull(result.getResult());
106     }
107
108 }