- dataSchemaNode = module.getChildNodes().iterator().next();
- }
- return dataSchemaNode;
- }
-
- public static String writeCompNodeWithSchemaContextToJson(CompositeNode compositeNode, Set<Module> modules,
- DataSchemaNode dataSchemaNode) throws IOException, WebApplicationException {
- String jsonResult;
-
- assertNotNull(dataSchemaNode);
- assertNotNull("Composite node can't be null", compositeNode);
- ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
-
- ControllerContext.getInstance().setSchemas(loadSchemaContext(modules));
-
- StructuredDataToJsonProvider structuredDataToJsonProvider = StructuredDataToJsonProvider.INSTANCE;
- structuredDataToJsonProvider.writeTo(new StructuredData(compositeNode, dataSchemaNode), null, null, null, null,
- null, byteArrayOS);
-
- jsonResult = byteArrayOS.toString();
-
- return jsonResult;
- }
-
- public static CompositeNode loadCompositeNode(String xmlDataPath) {
- InputStream xmlStream = TestUtils.class.getResourceAsStream(xmlDataPath);
- CompositeNode compositeNode = null;
- try {
- XmlReader xmlReader = new XmlReader();
- compositeNode = xmlReader.read(xmlStream);
-
- } catch (UnsupportedFormatException | XMLStreamException e) {
- e.printStackTrace();
- }
- return compositeNode;
- }
-
- static void outputToFile(ByteArrayOutputStream outputStream, String outputDir) throws IOException {
- FileOutputStream fileOS = null;
- try {
- String path = TestUtils.class.getResource(outputDir).getPath();
- File outFile = new File(path + "/data.json");
- fileOS = new FileOutputStream(outFile);
- try {
- fileOS.write(outputStream.toByteArray());
- } catch (IOException e) {
- e.printStackTrace();
- }
- fileOS.close();
- } catch (FileNotFoundException e1) {
- e1.printStackTrace();
- }
- }
-
- static String readJsonFromFile(String path, boolean removeWhiteChars) {
- FileReader fileReader = getFileReader(path);
-
- StringBuilder strBuilder = new StringBuilder();
- char[] buffer = new char[1000];
-
- while (true) {
- int loadedCharNum;
- try {
- loadedCharNum = fileReader.read(buffer);
- } catch (IOException e) {
- break;
- }
- if (loadedCharNum == -1) {
- break;
- }
- strBuilder.append(buffer, 0, loadedCharNum);
- }
- try {
- fileReader.close();
- } catch (IOException e) {
- System.out.println("The file wasn't closed");
- }
- String rawStr = strBuilder.toString();
- if (removeWhiteChars) {
- rawStr = rawStr.replace("\n", "");
- rawStr = rawStr.replace("\r", "");
- rawStr = rawStr.replace("\t", "");
- rawStr = removeSpaces(rawStr);
- }
-
- return rawStr;
- }
-
- private static FileReader getFileReader(String path) {
- String fullPath = TestUtils.class.getResource(path).getPath();
- assertNotNull("Path to file can't be null.", fullPath);
- File file = new File(fullPath);
- assertNotNull("File can't be null", file);
- FileReader fileReader = null;
- try {
- fileReader = new FileReader(file);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- assertNotNull("File reader can't be null.", fileReader);
- return fileReader;
- }
-
- private static String removeSpaces(String rawStr) {
- StringBuilder strBuilder = new StringBuilder();
- int i = 0;
- int quoteCount = 0;
- while (i < rawStr.length()) {
- if (rawStr.substring(i, i + 1).equals("\"")) {
- quoteCount++;
- }
-
- if (!rawStr.substring(i, i + 1).equals(" ") || (quoteCount % 2 == 1)) {
- strBuilder.append(rawStr.charAt(i));
- }
- i++;