2 * Copyright (c) 2014 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
9 package org.opendaylight.yangtools.yang.data.impl.codec.xml;
11 import com.google.common.base.Charsets;
12 import com.google.common.base.Optional;
13 import com.google.common.collect.Lists;
14 import com.google.common.io.ByteSource;
15 import java.io.ByteArrayInputStream;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import javax.activation.UnsupportedDataTypeException;
19 import javax.xml.parsers.DocumentBuilder;
20 import javax.xml.parsers.DocumentBuilderFactory;
21 import javax.xml.parsers.ParserConfigurationException;
22 import junit.framework.Assert;
23 import org.custommonkey.xmlunit.XMLUnit;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
27 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.Module;
31 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
34 import org.w3c.dom.Document;
35 import org.w3c.dom.Element;
36 import org.xml.sax.SAXException;
38 public class XmlDocumentUtilsTest {
40 private static final DocumentBuilderFactory BUILDERFACTORY;
43 final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
44 factory.setNamespaceAware(true);
45 factory.setCoalescing(true);
46 factory.setIgnoringElementContentWhitespace(true);
47 factory.setIgnoringComments(true);
48 BUILDERFACTORY = factory;
51 public static final String XML_CONTENT = "<input xmlns=\"urn:opendaylight:controller:rpc:test\">\n" +
53 "<ref xmlns:ltha=\"urn:opendaylight:controller:rpc:test\">/ltha:cont/ltha:l[ltha:id='id']</ref>\n" +
56 private SchemaContext schema;
57 private RpcDefinition testRpc;
60 public void setUp() throws Exception {
61 final ByteSource byteSource = new ByteSource() {
63 public InputStream openStream() throws IOException {
64 return XmlDocumentUtilsTest.this.getClass().getResourceAsStream("rpc-test.yang");
67 schema = new YangParserImpl().parseSources(Lists.newArrayList(byteSource));
68 final Module rpcTestModule = schema.getModules().iterator().next();
69 testRpc = rpcTestModule.getRpcs().iterator().next();
73 public void testRpcInputTransform() throws Exception {
75 final Document inputDocument = readXmlToDocument(XML_CONTENT);
76 final Element input = inputDocument.getDocumentElement();
78 final CompositeNode node = inputXmlToCompositeNode(input);
79 final SimpleNode<?> refParsed = node.getSimpleNodesByName("ref").iterator().next();
80 Assert.assertEquals(YangInstanceIdentifier.class, refParsed.getValue().getClass());
81 final Document serializedDocument = inputCompositeNodeToXml(node);
83 XMLUnit.compareXML(inputDocument, serializedDocument);
86 public static Document readXmlToDocument(final String xmlContent) throws SAXException, IOException {
87 return readXmlToDocument(new ByteArrayInputStream(xmlContent.getBytes(Charsets.UTF_8)));
90 public static Document readXmlToDocument(final InputStream xmlContent) throws SAXException, IOException {
91 final DocumentBuilder dBuilder;
93 dBuilder = BUILDERFACTORY.newDocumentBuilder();
94 } catch (final ParserConfigurationException e) {
95 throw new IllegalStateException("Failed to parse XML document", e);
97 final Document doc = dBuilder.parse(xmlContent);
99 doc.getDocumentElement().normalize();
103 public Document inputCompositeNodeToXml(final CompositeNode cNode)
104 throws UnsupportedDataTypeException {
105 return XmlDocumentUtils.toDocument(cNode, testRpc.getInput(), XmlDocumentUtils.defaultValueCodecProvider());
108 public CompositeNode inputXmlToCompositeNode(final Element e) {
109 return (CompositeNode) XmlDocumentUtils.toDomNode(e, Optional.<DataSchemaNode>of(testRpc.getInput()),
110 Optional.of(XmlDocumentUtils.defaultValueCodecProvider()), Optional.of(schema));