BUG-461 Test transformation of rpc input using XmlDocumentUtils
[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.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.InstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
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;
37
38 public class XmlDocumentUtilsTest {
39
40     private static final DocumentBuilderFactory BUILDERFACTORY;
41
42     static {
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;
49     }
50
51     public static final String XML_CONTENT = "<input xmlns=\"urn:opendaylight:controller:rpc:test\">\n" +
52             "<a>value</a>\n" +
53             "<ref xmlns:ltha=\"urn:opendaylight:controller:rpc:test\">/ltha:cont/ltha:l[ltha:id='id']</ref>\n" +
54             "</input>";
55
56     private SchemaContext schema;
57     private RpcDefinition testRpc;
58
59     @Before
60     public void setUp() throws Exception {
61         final ByteSource byteSource = new ByteSource() {
62             @Override
63             public InputStream openStream() throws IOException {
64                 return XmlDocumentUtilsTest.this.getClass().getResourceAsStream("rpc-test.yang");
65             }
66         };
67         schema = new YangParserImpl().parseSources(Lists.newArrayList(byteSource));
68         final Module rpcTestModule = schema.getModules().iterator().next();
69         testRpc = rpcTestModule.getRpcs().iterator().next();
70     }
71
72     @Test
73     public void testRpcInputTransform() throws Exception {
74
75         final Document inputDocument = readXmlToDocument(XML_CONTENT);
76         final Element input = inputDocument.getDocumentElement();
77
78         final CompositeNode node = inputXmlToCompositeNode(input);
79         final SimpleNode<?> refParsed = node.getSimpleNodesByName("ref").iterator().next();
80         Assert.assertEquals(InstanceIdentifier.class, refParsed.getValue().getClass());
81         final Document serializedDocument = inputCompositeNodeToXml(node);
82
83         XMLUnit.compareXML(inputDocument, serializedDocument);
84     }
85
86     public static Document readXmlToDocument(final String xmlContent) throws SAXException, IOException {
87         return readXmlToDocument(new ByteArrayInputStream(xmlContent.getBytes(Charsets.UTF_8)));
88     }
89
90     public static Document readXmlToDocument(final InputStream xmlContent) throws SAXException, IOException {
91         final DocumentBuilder dBuilder;
92         try {
93             dBuilder = BUILDERFACTORY.newDocumentBuilder();
94         } catch (final ParserConfigurationException e) {
95             throw new IllegalStateException("Failed to parse XML document", e);
96         }
97         final Document doc = dBuilder.parse(xmlContent);
98
99         doc.getDocumentElement().normalize();
100         return doc;
101     }
102
103     public Document inputCompositeNodeToXml(final CompositeNode cNode)
104             throws UnsupportedDataTypeException {
105         return XmlDocumentUtils.toDocument(cNode, testRpc.getInput(), XmlDocumentUtils.defaultValueCodecProvider());
106     }
107
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));
111     }
112 }