Merge "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             if (new File(testDir, fileName).isDirectory() == false) {
49                 testFiles.add(new File(testDir, fileName));
50             }
51         }
52         return parser.parseYangModels(testFiles);
53     }
54
55     public static SchemaContext loadSchemaContext(Set<Module> modules) {
56         return parser.resolveSchemaContext(modules);
57     }
58
59     public static SchemaContext loadSchemaContext(String resourceDirectory) throws FileNotFoundException {
60         return parser.resolveSchemaContext(loadModules(resourceDirectory));
61     }
62
63     public static Module findModule(Set<Module> modules, String moduleName) {
64         Module result = null;
65         for (Module module : modules) {
66             if (module.getName().equals(moduleName)) {
67                 result = module;
68                 break;
69             }
70         }
71         return result;
72     }
73
74     public static CompositeNode loadCompositeNode(InputStream xmlInputStream) throws FileNotFoundException {
75         if (xmlInputStream == null) {
76             throw new IllegalArgumentException();
77         }
78         Node<?> dataTree;
79         try {
80             dataTree = XmlTreeBuilder.buildDataTree(xmlInputStream);
81         } catch (XMLStreamException e) {
82             logger.error("Error during building data tree from XML", e);
83             return null;
84         }
85         if (dataTree == null) {
86             logger.error("data tree is null");
87             return null;
88         }
89         if (dataTree instanceof SimpleNode) {
90             logger.error("RPC XML was resolved as SimpleNode");
91             return null;
92         }
93         return (CompositeNode) dataTree;
94     }
95
96     public static String getDocumentInPrintableForm(Document doc) {
97         try {
98             ByteArrayOutputStream out = new ByteArrayOutputStream();
99             TransformerFactory tf = TransformerFactory.newInstance();
100             Transformer transformer = tf.newTransformer();
101             transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
102             transformer.setOutputProperty(OutputKeys.METHOD, "xml");
103             transformer.setOutputProperty(OutputKeys.INDENT, "yes");
104             transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
105             transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
106
107             transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8")));
108             byte[] charData = out.toByteArray();
109             return new String(charData, "UTF-8");
110         } catch (IOException | TransformerException e) {
111             String msg = "Error during transformation of Document into String";
112             logger.error(msg, e);
113             return msg;
114         }
115
116     }
117
118 }