BUG 8927: Netconf response payload fails to render in JSON
[yangtools.git] / yang / yang-data-codec-gson / src / test / java / org / opendaylight / yangtools / yang / data / codec / gson / TestUtils.java
index c5b6d80c2e775f635e6ab39f9245c5c8cbb339ed..a8c512301ff2ee66f72bf1a0f1107f7c8841a97c 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
@@ -16,45 +16,35 @@ import com.google.gson.JsonParser;
 import com.google.gson.JsonPrimitive;
 import java.io.BufferedReader;
 import java.io.File;
-import java.io.FileNotFoundException;
 import java.io.FileReader;
 import java.io.IOException;
-import java.net.URI;
+import java.io.InputStream;
+import java.io.StringWriter;
+import java.io.Writer;
 import java.net.URISyntaxException;
-import java.util.ArrayList;
-import java.util.List;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamReader;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
+import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
+import org.opendaylight.yangtools.yang.data.codec.xml.XmlParserStream;
+import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
+import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
-import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 
-public class TestUtils {
+public final class TestUtils {
 
     private TestUtils() {
-    }
-
-    static SchemaContext loadModules(final String resourceDirectory) throws IOException, URISyntaxException {
-        YangContextParser parser = new YangParserImpl();
-        URI path = StreamToNormalizedNodeTest.class.getResource(resourceDirectory).toURI();
-        final File testDir = new File(path);
-        final String[] fileList = testDir.list();
-        final List<File> testFiles = new ArrayList<>();
-        if (fileList == null) {
-            throw new FileNotFoundException(resourceDirectory);
-        }
-        for (String fileName : fileList) {
-            if (!new File(testDir, fileName).isDirectory()) {
-                testFiles.add(new File(testDir, fileName));
-            }
-        }
-        return parser.parseFiles(testFiles);
+        throw new UnsupportedOperationException();
     }
 
     static String loadTextFile(final File file) throws IOException {
-        FileReader fileReader = new FileReader(file);
-        BufferedReader bufReader = new BufferedReader(fileReader);
+        final FileReader fileReader = new FileReader(file);
+        final BufferedReader bufReader = new BufferedReader(fileReader);
 
         String line = null;
-        StringBuilder result = new StringBuilder();
+        final StringBuilder result = new StringBuilder();
         while ((line = bufReader.readLine()) != null) {
             result.append(line);
         }
@@ -66,9 +56,31 @@ public class TestUtils {
         return loadTextFile(new File(TestUtils.class.getResource(relativePath).toURI()));
     }
 
-    static JsonObject childObject(final JsonObject jsonObject,final String ... names) {
-        for (String name : names) {
-            JsonObject childJsonObject = jsonObject.getAsJsonObject(name);
+    static void loadXmlToNormalizedNodes(final InputStream xmlInputStream, final NormalizedNodeResult result,
+            final SchemaContext schemaContext) throws Exception {
+        final XMLInputFactory factory = XMLInputFactory.newInstance();
+        final XMLStreamReader reader = factory.createXMLStreamReader(xmlInputStream);
+        final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
+        final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, schemaContext);
+        xmlParser.parse(reader);
+    }
+
+    static String normalizedNodesToJsonString(final NormalizedNode<?, ?> data, final SchemaContext schemaContext,
+            final SchemaPath rootPath) throws IOException {
+        final Writer writer = new StringWriter();
+        final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.createExclusiveWriter(
+                JSONCodecFactory.getShared(schemaContext), rootPath, null,
+                JsonWriterFactory.createJsonWriter(writer, 2));
+        final NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream);
+        nodeWriter.write(data);
+        nodeWriter.close();
+        final String serializationResult = writer.toString();
+        return serializationResult;
+    }
+
+    static JsonObject childObject(final JsonObject jsonObject, final String... names) {
+        for (final String name : names) {
+            final JsonObject childJsonObject = jsonObject.getAsJsonObject(name);
             if (childJsonObject != null) {
                 return childJsonObject;
             }
@@ -76,9 +88,9 @@ public class TestUtils {
         return null;
     }
 
-    static JsonPrimitive childPrimitive(final JsonObject jsonObject,final String ... names) {
-        for (String name : names) {
-            JsonPrimitive childJsonPrimitive = jsonObject.getAsJsonPrimitive(name);
+    static JsonPrimitive childPrimitive(final JsonObject jsonObject, final String... names) {
+        for (final String name : names) {
+            final JsonPrimitive childJsonPrimitive = jsonObject.getAsJsonPrimitive(name);
             if (childJsonPrimitive != null) {
                 return childJsonPrimitive;
             }
@@ -86,9 +98,9 @@ public class TestUtils {
         return null;
     }
 
-    static JsonArray childArray(final JsonObject jsonObject,final String ... names) {
-        for (String name : names) {
-            JsonArray childJsonArray = jsonObject.getAsJsonArray(name);
+    static JsonArray childArray(final JsonObject jsonObject, final String... names) {
+        for (final String name : names) {
+            final JsonArray childJsonArray = jsonObject.getAsJsonArray(name);
             if (childJsonArray != null) {
                 return childJsonArray;
             }
@@ -96,14 +108,13 @@ public class TestUtils {
         return null;
     }
 
-    static JsonObject resolveCont1(String jsonOutput) {
-        JsonParser parser = new JsonParser();
-        JsonElement rootElement = parser.parse(jsonOutput);
+    static JsonObject resolveCont1(final String jsonOutput) {
+        final JsonParser parser = new JsonParser();
+        final JsonElement rootElement = parser.parse(jsonOutput);
         assertTrue(rootElement.isJsonObject());
-        JsonObject rootObject = rootElement.getAsJsonObject();
-        JsonObject cont1 = childObject(rootObject, "complexjson:cont1", "cont1");
+        final JsonObject rootObject = rootElement.getAsJsonObject();
+        final JsonObject cont1 = childObject(rootObject, "complexjson:cont1", "cont1");
         return cont1;
     }
 
-
 }