2 * Copyright (c) 2016 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.netconf.sal.connect.netconf.schema.mapping;
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;
30 public class SchemalessMessageTransformerTest {
33 XMLUnit.setIgnoreWhitespace(true);
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"
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"
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"
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);
56 public void setUp() throws Exception {
57 transformer = new SchemalessMessageTransformer(new MessageCounter());
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());
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());
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))
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());
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());
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());