diff options
Diffstat (limited to 'cps-service/src/test/groovy/org/onap/cps/utils/GsonSpec.groovy')
-rw-r--r-- | cps-service/src/test/groovy/org/onap/cps/utils/GsonSpec.groovy | 40 |
1 files changed, 40 insertions, 0 deletions
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 index 000000000..c100ea31d --- /dev/null +++ b/cps-service/src/test/groovy/org/onap/cps/utils/GsonSpec.groovy @@ -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; + } + } + + +} |