/* * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.netconf.sal.connect.netconf.schema.mapping; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import javax.xml.transform.dom.DOMSource; import org.custommonkey.xmlunit.Diff; import org.custommonkey.xmlunit.XMLUnit; import org.junit.Before; import org.junit.Test; import org.opendaylight.mdsal.dom.api.DOMNotification; import org.opendaylight.mdsal.dom.api.DOMRpcResult; import org.opendaylight.netconf.api.NetconfMessage; import org.opendaylight.netconf.api.xml.XmlUtil; import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil; import org.opendaylight.netconf.sal.connect.util.MessageCounter; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier; import org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode; import org.opendaylight.yangtools.yang.data.impl.schema.Builders; import org.w3c.dom.Document; import org.w3c.dom.Node; public class SchemalessMessageTransformerTest { static { XMLUnit.setIgnoreWhitespace(true); } private static final String EXP_REQUEST = "\n" + "\n" + "aaa\n" + "\n" + ""; private static final String EXP_REPLY = "\n" + "aaa\n" + ""; private static final String OK_REPLY = "\n" + "\n" + "\n"; private static final QName TEST_RPC = QName.create("test-ns", "2016-10-13", "test-rpc"); private SchemalessMessageTransformer transformer; @Before public void setUp() throws Exception { transformer = new SchemalessMessageTransformer(new MessageCounter()); } @Test public void toNotification() throws Exception { final Document payload = XmlUtil.readXmlToDocument(getClass().getResourceAsStream("/notification-payload.xml")); final NetconfMessage netconfMessage = new NetconfMessage(payload); final DOMNotification domNotification = transformer.toNotification(netconfMessage); assertEquals(domNotification.getType().lastNodeIdentifier(), SchemalessMessageTransformer.SCHEMALESS_NOTIFICATION_PAYLOAD.getNodeType()); final QName qName = QName.create("org:opendaylight:notification:test:ns:yang:user-notification", "user-visited-page"); final DOMSourceAnyxmlNode dataContainerChild = (DOMSourceAnyxmlNode) domNotification.getBody() .findChildByArg(new NodeIdentifier(qName)).get(); final Diff diff = XMLUnit.compareXML(payload, dataContainerChild.body().getNode().getOwnerDocument()); assertTrue(diff.toString(), diff.similar()); } @Test public void toRpcRequest() throws Exception { final Node src = XmlUtil.readXmlToDocument("aaa"); final DOMSourceAnyxmlNode input = Builders.anyXmlBuilder() .withNodeIdentifier(new NodeIdentifier(TEST_RPC)) .withValue(new DOMSource(src)) .build(); final NetconfMessage netconfMessage = transformer.toRpcRequest(TEST_RPC, input); final Diff diff = XMLUnit.compareXML(XmlUtil.readXmlToDocument(EXP_REQUEST), netconfMessage.getDocument()); assertTrue(diff.toString(), diff.similar()); } @Test public void toRpcResult() throws Exception { final Document doc = XmlUtil.readXmlToDocument(EXP_REPLY); final NetconfMessage netconfMessage = new NetconfMessage(doc); final DOMRpcResult result = transformer.toRpcResult(netconfMessage, TEST_RPC); final DOMSource value = (DOMSource) result.getResult().body(); assertNotNull(result.getResult()); final Document domSourceDoc = (Document) value.getNode(); final Diff diff = XMLUnit.compareXML(XmlUtil.readXmlToDocument(EXP_REPLY), domSourceDoc); assertTrue(diff.toString(), diff.similar()); } @Test public void toEmptyRpcResult() throws Exception { final Document doc = XmlUtil.readXmlToDocument(OK_REPLY); final DOMRpcResult result = transformer.toRpcResult( new NetconfMessage(doc), NetconfMessageTransformUtil.NETCONF_COMMIT_QNAME); assertNull(result.getResult()); } }