Enhanced message handling
[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     InputStream xmlStream = SerilizationTest.class.getResourceAsStream("/AddFlow.xml");
43     StringWriter writer = new StringWriter();
44
45     CompositeNode data = loadCompositeNode(xmlStream);
46     Document domTree = NodeUtils.buildShadowDomTree(data);
47     try {
48       TransformerFactory tf = TransformerFactory.newInstance();
49       Transformer transformer = tf.newTransformer();
50       transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
51       //transformer.setOutputProperty(OutputKeys.METHOD, "xml");
52       //transformer.setOutputProperty(OutputKeys.INDENT, "yes");
53       //transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
54       //transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
55       transformer.transform(new DOMSource(domTree), new StreamResult(writer));
56     } catch (TransformerException e) {
57       _logger.error("Error during translation of Document to OutputStream", e);
58     }
59
60     _logger.info("Parsed xml [{}]", writer.toString());
61   }
62
63   // Figure out how to include TestUtils through pom ...was getting errors
64   private CompositeNode loadCompositeNode(InputStream xmlInputStream) throws FileNotFoundException {
65     if (xmlInputStream == null) {
66       throw new IllegalArgumentException();
67     }
68     Node<?> dataTree;
69     try {
70       dataTree = XmlTreeBuilder.buildDataTree(xmlInputStream);
71     } catch (XMLStreamException e) {
72       _logger.error("Error during building data tree from XML", e);
73       return null;
74     }
75     if (dataTree == null) {
76       _logger.error("data tree is null");
77       return null;
78     }
79     if (dataTree instanceof SimpleNode) {
80       _logger.error("RPC XML was resolved as SimpleNode");
81       return null;
82     }
83     return (CompositeNode) dataTree;
84   }
85 }