Merge "Bug 1372 - toString methods in generated classes"
[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 static org.junit.Assert.assertEquals;
12
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;
17
18 import java.io.ByteArrayInputStream;
19 import java.io.IOException;
20 import java.io.InputStream;
21
22 import javax.activation.UnsupportedDataTypeException;
23 import javax.xml.parsers.DocumentBuilder;
24 import javax.xml.parsers.DocumentBuilderFactory;
25 import javax.xml.parsers.ParserConfigurationException;
26
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;
41
42 public class XmlDocumentUtilsTest {
43
44     private static final DocumentBuilderFactory BUILDERFACTORY;
45
46     static {
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;
53     }
54
55     public static final String XML_CONTENT = "<input xmlns=\"urn:opendaylight:controller:rpc:test\">\n" +
56             "<a>value</a>\n" +
57             "<ref xmlns:ltha=\"urn:opendaylight:controller:rpc:test\">/ltha:cont/ltha:l[ltha:id='id']</ref>\n" +
58             "</input>";
59
60     private SchemaContext schema;
61     private RpcDefinition testRpc;
62
63     @Before
64     public void setUp() throws Exception {
65         final ByteSource byteSource = new ByteSource() {
66             @Override
67             public InputStream openStream() throws IOException {
68                 return XmlDocumentUtilsTest.this.getClass().getResourceAsStream("rpc-test.yang");
69             }
70         };
71         schema = new YangParserImpl().parseSources(Lists.newArrayList(byteSource));
72         final Module rpcTestModule = schema.getModules().iterator().next();
73         testRpc = rpcTestModule.getRpcs().iterator().next();
74     }
75
76     @Test
77     public void testRpcInputTransform() throws Exception {
78
79         final Document inputDocument = readXmlToDocument(XML_CONTENT);
80         final Element input = inputDocument.getDocumentElement();
81
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);
86
87         XMLUnit.compareXML(inputDocument, serializedDocument);
88     }
89
90     public static Document readXmlToDocument(final String xmlContent) throws SAXException, IOException {
91         return readXmlToDocument(new ByteArrayInputStream(xmlContent.getBytes(Charsets.UTF_8)));
92     }
93
94     public static Document readXmlToDocument(final InputStream xmlContent) throws SAXException, IOException {
95         final DocumentBuilder dBuilder;
96         try {
97             dBuilder = BUILDERFACTORY.newDocumentBuilder();
98         } catch (final ParserConfigurationException e) {
99             throw new IllegalStateException("Failed to parse XML document", e);
100         }
101         final Document doc = dBuilder.parse(xmlContent);
102
103         doc.getDocumentElement().normalize();
104         return doc;
105     }
106
107     public Document inputCompositeNodeToXml(final CompositeNode cNode)
108             throws UnsupportedDataTypeException {
109         return XmlDocumentUtils.toDocument(cNode, testRpc.getInput(), XmlDocumentUtils.defaultValueCodecProvider());
110     }
111
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));
115     }
116 }