Ganymed patch fix
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / TestUtils.java
1 package org.opendaylight.controller.sal.restconf.impl.test;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.File;
5 import java.io.FileNotFoundException;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.io.OutputStreamWriter;
9 import java.util.ArrayList;
10 import java.util.List;
11 import java.util.Set;
12
13 import javax.xml.stream.XMLStreamException;
14 import javax.xml.transform.OutputKeys;
15 import javax.xml.transform.Transformer;
16 import javax.xml.transform.TransformerException;
17 import javax.xml.transform.TransformerFactory;
18 import javax.xml.transform.dom.DOMSource;
19 import javax.xml.transform.stream.StreamResult;
20
21 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
22 import org.opendaylight.yangtools.yang.data.api.Node;
23 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
24 import org.opendaylight.yangtools.yang.data.impl.XmlTreeBuilder;
25 import org.opendaylight.yangtools.yang.model.api.Module;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
28 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.w3c.dom.Document;
32
33 final class TestUtils {
34
35     private static final Logger logger = LoggerFactory.getLogger(TestUtils.class);
36
37     private final static YangModelParser parser = new YangParserImpl();
38
39     public static Set<Module> loadModules(String resourceDirectory) throws FileNotFoundException {
40         final File testDir = new File(resourceDirectory);
41         final String[] fileList = testDir.list();
42         final List<File> testFiles = new ArrayList<File>();
43         if (fileList == null) {
44             throw new FileNotFoundException(resourceDirectory);
45         }
46         for (int i = 0; i < fileList.length; i++) {
47             String fileName = fileList[i];
48             testFiles.add(new File(testDir, fileName));
49         }
50         return parser.parseYangModels(testFiles);
51     }
52
53     public static SchemaContext loadSchemaContext(Set<Module> modules) {
54         return parser.resolveSchemaContext(modules);
55     }
56
57     public static SchemaContext loadSchemaContext(String resourceDirectory) throws FileNotFoundException {
58         return parser.resolveSchemaContext(loadModules(resourceDirectory));
59     }
60
61     public static Module findModule(Set<Module> modules, String moduleName) {
62         Module result = null;
63         for (Module module : modules) {
64             if (module.getName().equals(moduleName)) {
65                 result = module;
66                 break;
67             }
68         }
69         return result;
70     }
71
72     public static CompositeNode loadCompositeNode(InputStream xmlInputStream) throws FileNotFoundException {
73         if (xmlInputStream == null) {
74             throw new IllegalArgumentException();
75         }
76         Node<?> dataTree;
77         try {
78             dataTree = XmlTreeBuilder.buildDataTree(xmlInputStream);
79         } catch (XMLStreamException e) {
80             logger.error("Error during building data tree from XML", e);
81             return null;
82         }
83         if (dataTree == null) {
84             logger.error("data tree is null");
85             return null;
86         }
87         if (dataTree instanceof SimpleNode) {
88             logger.error("RPC XML was resolved as SimpleNode");
89             return null;
90         }
91         return (CompositeNode) dataTree;
92     }
93
94     public static String getDocumentInPrintableForm(Document doc) {
95         try {
96             ByteArrayOutputStream out = new ByteArrayOutputStream();
97             TransformerFactory tf = TransformerFactory.newInstance();
98             Transformer transformer = tf.newTransformer();
99             transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
100             transformer.setOutputProperty(OutputKeys.METHOD, "xml");
101             transformer.setOutputProperty(OutputKeys.INDENT, "yes");
102             transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
103             transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
104
105             transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8")));
106             byte[] charData = out.toByteArray();
107             return new String(charData, "UTF-8");
108         } catch (IOException | TransformerException e) {
109             String msg = "Error during transformation of Document into String";
110             logger.error(msg, e);
111             return msg;
112         }
113
114     }
115
116 }