1 package org.opendaylight.controller.sal.restconf.impl.test;
3 import static org.junit.Assert.assertFalse;
4 import static org.junit.Assert.assertNotNull;
7 import java.util.ArrayList;
11 import javax.ws.rs.WebApplicationException;
12 import javax.xml.stream.XMLStreamException;
13 import javax.xml.transform.OutputKeys;
14 import javax.xml.transform.Transformer;
15 import javax.xml.transform.TransformerException;
16 import javax.xml.transform.TransformerFactory;
17 import javax.xml.transform.dom.DOMSource;
18 import javax.xml.transform.stream.StreamResult;
20 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
21 import org.opendaylight.controller.sal.restconf.impl.StructuredData;
22 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
23 import org.opendaylight.yangtools.yang.data.api.Node;
24 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
25 import org.opendaylight.yangtools.yang.data.impl.XmlTreeBuilder;
26 import org.opendaylight.yangtools.yang.model.api.*;
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;
33 final class TestUtils {
35 private static final Logger logger = LoggerFactory.getLogger(TestUtils.class);
37 private final static YangModelParser parser = new YangParserImpl();
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);
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));
52 return parser.parseYangModels(testFiles);
55 public static SchemaContext loadSchemaContext(Set<Module> modules) {
56 return parser.resolveSchemaContext(modules);
59 public static SchemaContext loadSchemaContext(String resourceDirectory) throws FileNotFoundException {
60 return parser.resolveSchemaContext(loadModules(resourceDirectory));
63 public static Module findModule(Set<Module> modules, String moduleName) {
65 for (Module module : modules) {
66 if (module.getName().equals(moduleName)) {
74 public static CompositeNode loadCompositeNode(InputStream xmlInputStream) throws FileNotFoundException {
75 if (xmlInputStream == null) {
76 throw new IllegalArgumentException();
80 dataTree = XmlTreeBuilder.buildDataTree(xmlInputStream);
81 } catch (XMLStreamException e) {
82 logger.error("Error during building data tree from XML", e);
85 if (dataTree == null) {
86 logger.error("data tree is null");
89 if (dataTree instanceof SimpleNode) {
90 logger.error("RPC XML was resolved as SimpleNode");
93 return (CompositeNode) dataTree;
96 public static String getDocumentInPrintableForm(Document doc) {
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");
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);
118 static String convertXmlDataAndYangToJson(String xmlDataPath, String yangPath) {
119 String jsonResult = null;
120 Set<Module> modules = null;
123 modules = TestUtils.loadModules(YangAndXmlToJsonConversionJsonReaderTest.class.getResource(yangPath).getPath());
124 } catch (FileNotFoundException e) {
127 assertNotNull("modules can't be null.", modules);
129 InputStream xmlStream = YangAndXmlToJsonConversionJsonReaderTest.class.getResourceAsStream(xmlDataPath);
130 CompositeNode compositeNode = null;
132 compositeNode = TestUtils.loadCompositeNode(xmlStream);
133 } catch (FileNotFoundException e) {
136 assertNotNull("Composite node can't be null", compositeNode);
138 StructuredDataToJsonProvider structuredDataToJsonProvider = StructuredDataToJsonProvider.INSTANCE;
139 for (Module module : modules) {
140 for (DataSchemaNode dataSchemaNode : module.getChildNodes()) {
141 StructuredData structuredData = new StructuredData(compositeNode, dataSchemaNode);
142 ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
144 structuredDataToJsonProvider.writeTo(structuredData, null, null, null, null, null, byteArrayOS);
145 } catch (WebApplicationException | IOException e) {
149 "Returning JSON string can't be empty for node " + dataSchemaNode.getQName().getLocalName(),
150 byteArrayOS.toString().isEmpty());
151 jsonResult = byteArrayOS.toString();
153 outputToFile(byteArrayOS);
154 } catch (IOException e) {
155 System.out.println("Output file wasn't cloased sucessfuly.");
162 static void outputToFile(ByteArrayOutputStream outputStream) throws IOException {
163 FileOutputStream fileOS = null;
165 String path = YangAndXmlToJsonConversionJsonReaderTest.class.getResource("/yang-to-json-conversion/xml").getPath();
166 File outFile = new File(path + "/data.json");
167 fileOS = new FileOutputStream(outFile);
169 fileOS.write(outputStream.toByteArray());
170 } catch (IOException e) {
174 } catch (FileNotFoundException e1) {
175 e1.printStackTrace();
179 static String readJsonFromFile(String path,boolean removeWhiteChars) {
180 FileReader fileReader = getFileReader(path);
182 StringBuilder strBuilder = new StringBuilder();
183 char[] buffer = new char[1000];
188 loadedCharNum = fileReader.read(buffer);
189 } catch (IOException e) {
192 if (loadedCharNum == -1) {
195 strBuilder.append(buffer, 0, loadedCharNum);
199 } catch (IOException e) {
200 System.out.println("The file wasn't closed");
202 String rawStr = strBuilder.toString();
203 if (removeWhiteChars) {
204 rawStr = rawStr.replace("\n", "");
205 rawStr = rawStr.replace("\r", "");
206 rawStr = rawStr.replace("\t", "");
207 rawStr = removeSpaces(rawStr);
213 private static FileReader getFileReader(String path) {
214 String fullPath = YangAndXmlToJsonConversionJsonReaderTest.class.getResource(path).getPath();
215 assertNotNull("Path to file can't be null.", fullPath);
216 File file = new File(fullPath);
217 assertNotNull("File can't be null", file);
218 FileReader fileReader = null;
220 fileReader = new FileReader(file);
221 } catch (FileNotFoundException e) {
224 assertNotNull("File reader can't be null.", fileReader);
228 private static String removeSpaces(String rawStr) {
229 StringBuilder strBuilder = new StringBuilder();
232 while (i < rawStr.length()) {
233 if (rawStr.substring(i, i + 1).equals("\"")) {
237 if (!rawStr.substring(i, i + 1).equals(" ") || (quoteCount % 2 == 1)) {
238 strBuilder.append(rawStr.charAt(i));
243 return strBuilder.toString();