2 * Copyright (c) 2015 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.retest;
11 import com.google.common.base.Charsets;
12 import java.io.ByteArrayInputStream;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import javax.xml.parsers.DocumentBuilder;
17 import javax.xml.parsers.DocumentBuilderFactory;
18 import javax.xml.parsers.ParserConfigurationException;
19 import org.junit.Before;
20 import org.opendaylight.yangtools.yang.data.impl.RetestUtils;
21 import org.opendaylight.yangtools.yang.model.api.Module;
22 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 import org.w3c.dom.Document;
25 import org.xml.sax.SAXException;
27 public class XmlDocumentUtilsTest {
29 private static final DocumentBuilderFactory BUILDERFACTORY;
32 final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
33 factory.setNamespaceAware(true);
34 factory.setCoalescing(true);
35 factory.setIgnoringElementContentWhitespace(true);
36 factory.setIgnoringComments(true);
37 BUILDERFACTORY = factory;
40 public static final String XML_CONTENT = "<input xmlns=\"urn:opendaylight:controller:rpc:test\">\n"
41 + "<a>value</a>\n" + "<ref xmlns:ltha=\"urn:opendaylight:controller:rpc:test\">"
42 + "/ltha:cont/ltha:l[ ltha:id='id/foo/bar' ]" + "</ref>\n" + "</input>";
44 public static final String RPC_REPLY = "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"m-1\">\n"
45 + " <ok/>\n" + "</rpc-reply>";
47 private SchemaContext schema;
48 private RpcDefinition testRpc;
51 public void setUp() throws Exception {
52 File rpcTestYang = new File(getClass().getResource("/rpc-test.yang").toURI());
53 schema = RetestUtils.parseYangSources(rpcTestYang);
54 final Module rpcTestModule = schema.getModules().iterator().next();
55 testRpc = rpcTestModule.getRpcs().iterator().next();
58 public static Document readXmlToDocument(final String xmlContent) throws SAXException, IOException {
59 return readXmlToDocument(new ByteArrayInputStream(xmlContent.getBytes(Charsets.UTF_8)));
62 public static Document readXmlToDocument(final InputStream xmlContent) throws SAXException, IOException {
63 final DocumentBuilder dBuilder;
65 dBuilder = BUILDERFACTORY.newDocumentBuilder();
66 } catch (final ParserConfigurationException e) {
67 throw new IllegalStateException("Failed to parse XML document", e);
69 final Document doc = dBuilder.parse(xmlContent);
71 doc.getDocumentElement().normalize();