Tests added for multiple data tree analysis
[cps.git] / cps-service / src / test / groovy / org / onap / cps / utils / GsonSpec.groovy
diff --git a/cps-service/src/test/groovy/org/onap/cps/utils/GsonSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/utils/GsonSpec.groovy
new file mode 100644 (file)
index 0000000..c100ea3
--- /dev/null
@@ -0,0 +1,40 @@
+package org.onap.cps.utils
+
+import com.google.gson.stream.JsonReader
+import org.onap.cps.TestUtils
+import spock.lang.Specification
+
+
+class GsonSpec extends Specification{
+
+    def 'Iterate over JSON data with gson JsonReader'(){
+        given: 'json data with two objects and JSON reader'
+            def jsonData = TestUtils.getResourceFileContent('multiple-object-data.json')
+            def objectUnderTest = new JsonReader(new StringReader(jsonData));
+        when: 'data is iterated over with JsonReader methods'
+            iterateWithJsonReader(objectUnderTest)
+        then: 'no exception is thrown'
+            noExceptionThrown()
+    }
+
+    def iterateWithJsonReader(JsonReader jsonReader){
+        switch(jsonReader.peek()) {
+            case "STRING":
+                print(jsonReader.nextString() + " ")
+                break;
+            case "BEGIN_OBJECT":
+                jsonReader.beginObject();
+                while (jsonReader.hasNext()) {
+                    print(jsonReader.nextName() + " ")
+                    iterateWithJsonReader(jsonReader)
+                }
+                jsonReader.endObject();
+                println("")
+                break;
+            default:
+                break;
+        }
+    }
+
+
+}