Merge "Added missing parent tag to pom files"
[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 import static org.junit.Assert.assertTrue;
13
14 import com.google.common.base.Charsets;
15 import com.google.common.base.Optional;
16 import com.google.common.collect.Lists;
17 import com.google.common.io.ByteSource;
18 import java.io.ByteArrayInputStream;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import javax.activation.UnsupportedDataTypeException;
22 import javax.xml.parsers.DocumentBuilder;
23 import javax.xml.parsers.DocumentBuilderFactory;
24 import javax.xml.parsers.ParserConfigurationException;
25 import org.custommonkey.xmlunit.XMLUnit;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.opendaylight.yangtools.yang.common.QName;
29 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
30 import org.opendaylight.yangtools.yang.data.api.Node;
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     public static final String RPC_REPLY = "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"m-1\">\n" +
61             " <ok/>\n" +
62             "</rpc-reply>";
63
64     private SchemaContext schema;
65     private RpcDefinition testRpc;
66
67     @Before
68     public void setUp() throws Exception {
69         final ByteSource byteSource = new ByteSource() {
70             @Override
71             public InputStream openStream() throws IOException {
72                 return XmlDocumentUtilsTest.this.getClass().getResourceAsStream("rpc-test.yang");
73             }
74         };
75         schema = new YangParserImpl().parseSources(Lists.newArrayList(byteSource));
76         final Module rpcTestModule = schema.getModules().iterator().next();
77         testRpc = rpcTestModule.getRpcs().iterator().next();
78     }
79
80     @Test
81     public void testRpcInputTransform() throws Exception {
82
83         final Document inputDocument = readXmlToDocument(XML_CONTENT);
84         final Element input = inputDocument.getDocumentElement();
85
86         final CompositeNode node = inputXmlToCompositeNode(input);
87         final SimpleNode<?> refParsed = node.getSimpleNodesByName("ref").iterator().next();
88         assertEquals(YangInstanceIdentifier.class, refParsed.getValue().getClass());
89         final Document serializedDocument = inputCompositeNodeToXml(node);
90
91         XMLUnit.compareXML(inputDocument, serializedDocument);
92     }
93
94     @Test
95     public void testRpcReplyToDom() throws Exception {
96         final Document reply = readXmlToDocument(RPC_REPLY);
97         final CompositeNode domNodes = XmlDocumentUtils.rpcReplyToDomNodes(reply, QName.create("urn:opendaylight:controller:rpc:test", "2014-07-28", "test"), schema);
98         assertEquals(1, domNodes.getValue().size());
99         final Node<?> outputNode = domNodes.getValue().get(0);
100         assertTrue(outputNode instanceof CompositeNode);
101         assertEquals(1, ((CompositeNode) outputNode).getValue().size());
102         final Node<?> okNode = ((CompositeNode) outputNode).getValue().get(0);
103         assertEquals("ok", okNode.getNodeType().getLocalName());
104     }
105
106     public static Document readXmlToDocument(final String xmlContent) throws SAXException, IOException {
107         return readXmlToDocument(new ByteArrayInputStream(xmlContent.getBytes(Charsets.UTF_8)));
108     }
109
110     public static Document readXmlToDocument(final InputStream xmlContent) throws SAXException, IOException {
111         final DocumentBuilder dBuilder;
112         try {
113             dBuilder = BUILDERFACTORY.newDocumentBuilder();
114         } catch (final ParserConfigurationException e) {
115             throw new IllegalStateException("Failed to parse XML document", e);
116         }
117         final Document doc = dBuilder.parse(xmlContent);
118
119         doc.getDocumentElement().normalize();
120         return doc;
121     }
122
123     public Document inputCompositeNodeToXml(final CompositeNode cNode)
124             throws UnsupportedDataTypeException {
125         return XmlDocumentUtils.toDocument(cNode, testRpc.getInput(), XmlDocumentUtils.defaultValueCodecProvider());
126     }
127
128     public CompositeNode inputXmlToCompositeNode(final Element e) {
129         return (CompositeNode) XmlDocumentUtils.toDomNode(e, Optional.<DataSchemaNode>of(testRpc.getInput()),
130                 Optional.of(XmlDocumentUtils.defaultValueCodecProvider()), Optional.of(schema));
131     }
132 }