4145ea8d79201af817aa385f65efd9fc25f34839
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / codec / xml / XmlDocumentUtilsTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.yangtools.yang.data.impl.codec.xml;
10
11 import com.google.common.base.Charsets;
12 import com.google.common.collect.Lists;
13 import com.google.common.io.ByteSource;
14 import java.io.ByteArrayInputStream;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import javax.xml.parsers.DocumentBuilder;
18 import javax.xml.parsers.DocumentBuilderFactory;
19 import javax.xml.parsers.ParserConfigurationException;
20 import org.junit.Before;
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.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
25 import org.w3c.dom.Document;
26 import org.xml.sax.SAXException;
27
28 public class XmlDocumentUtilsTest {
29
30     private static final DocumentBuilderFactory BUILDERFACTORY;
31
32     static {
33         final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
34         factory.setNamespaceAware(true);
35         factory.setCoalescing(true);
36         factory.setIgnoringElementContentWhitespace(true);
37         factory.setIgnoringComments(true);
38         BUILDERFACTORY = factory;
39     }
40
41     public static final String XML_CONTENT = "<input xmlns=\"urn:opendaylight:controller:rpc:test\">\n" +
42             "<a>value</a>\n" +
43             "<ref xmlns:ltha=\"urn:opendaylight:controller:rpc:test\">"+
44             "/ltha:cont/ltha:l[  ltha:id='id/foo/bar'  ]"+
45             "</ref>\n" +
46             "</input>";
47
48     public static final String RPC_REPLY = "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"m-1\">\n" +
49             " <ok/>\n" +
50             "</rpc-reply>";
51
52     private SchemaContext schema;
53     private RpcDefinition testRpc;
54
55     @Before
56     public void setUp() throws Exception {
57         final ByteSource byteSource = new ByteSource() {
58             @Override
59             public InputStream openStream() throws IOException {
60                 return XmlDocumentUtilsTest.this.getClass().getResourceAsStream("rpc-test.yang");
61             }
62         };
63         schema = new YangParserImpl().parseSources(Lists.newArrayList(byteSource));
64         final Module rpcTestModule = schema.getModules().iterator().next();
65         testRpc = rpcTestModule.getRpcs().iterator().next();
66     }
67
68     public static Document readXmlToDocument(final String xmlContent) throws SAXException, IOException {
69         return readXmlToDocument(new ByteArrayInputStream(xmlContent.getBytes(Charsets.UTF_8)));
70     }
71
72     public static Document readXmlToDocument(final InputStream xmlContent) throws SAXException, IOException {
73         final DocumentBuilder dBuilder;
74         try {
75             dBuilder = BUILDERFACTORY.newDocumentBuilder();
76         } catch (final ParserConfigurationException e) {
77             throw new IllegalStateException("Failed to parse XML document", e);
78         }
79         final Document doc = dBuilder.parse(xmlContent);
80
81         doc.getDocumentElement().normalize();
82         return doc;
83     }
84 }