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