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