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