Bulk-add copyright headers to java files
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / implementation / src / test / java / org / opendaylight / controller / sal / connector / remoterpc / SerilizationTest.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 package org.opendaylight.controller.sal.connector.remoterpc;
9
10 import org.junit.Test;
11 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
12 import org.opendaylight.yangtools.yang.data.api.Node;
13 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
14 import org.opendaylight.yangtools.yang.data.impl.NodeUtils;
15 import org.opendaylight.yangtools.yang.data.impl.XmlTreeBuilder;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18 import org.w3c.dom.Document;
19
20 import javax.xml.stream.XMLStreamException;
21 import javax.xml.transform.OutputKeys;
22 import javax.xml.transform.Transformer;
23 import javax.xml.transform.TransformerException;
24 import javax.xml.transform.TransformerFactory;
25 import javax.xml.transform.dom.DOMSource;
26 import javax.xml.transform.stream.StreamResult;
27 import java.io.FileNotFoundException;
28 import java.io.InputStream;
29 import java.io.StringWriter;
30
31 public class SerilizationTest {
32
33   private static final Logger _logger = LoggerFactory.getLogger(SerilizationTest.class);
34
35   public void fromXml() {
36   }
37
38   @Test
39   public void toXml() throws FileNotFoundException {
40
41     InputStream xmlStream = SerilizationTest.class.getResourceAsStream("/FourSimpleChildren.xml");
42     StringWriter writer = new StringWriter();
43
44     CompositeNode data = loadCompositeNode(xmlStream);
45     Document domTree = NodeUtils.buildShadowDomTree(data);
46     try {
47       TransformerFactory tf = TransformerFactory.newInstance();
48       Transformer transformer = tf.newTransformer();
49       transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
50       //transformer.setOutputProperty(OutputKeys.METHOD, "xml");
51       //transformer.setOutputProperty(OutputKeys.INDENT, "yes");
52       //transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
53       //transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
54       transformer.transform(new DOMSource(domTree), new StreamResult(writer));
55     } catch (TransformerException e) {
56       _logger.error("Error during translation of Document to OutputStream", e);
57     }
58
59     _logger.info("Parsed xml [{}]", writer.toString());
60   }
61
62   //Note to self:  Stolen from TestUtils
63   ///Users/alefan/odl/controller4/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/TestUtils.java
64   // Figure out how to include TestUtils through pom ...was getting errors
65   private CompositeNode loadCompositeNode(InputStream xmlInputStream) throws FileNotFoundException {
66     if (xmlInputStream == null) {
67       throw new IllegalArgumentException();
68     }
69     Node<?> dataTree;
70     try {
71       dataTree = XmlTreeBuilder.buildDataTree(xmlInputStream);
72     } catch (XMLStreamException e) {
73       _logger.error("Error during building data tree from XML", e);
74       return null;
75     }
76     if (dataTree == null) {
77       _logger.error("data tree is null");
78       return null;
79     }
80     if (dataTree instanceof SimpleNode) {
81       _logger.error("RPC XML was resolved as SimpleNode");
82       return null;
83     }
84     return (CompositeNode) dataTree;
85   }
86 }