Tests added for multiple data tree analysis 57/131357/5
authoremaclee <lee.anjella.macabuhay@est.tech>
Mon, 3 Oct 2022 11:03:00 +0000 (12:03 +0100)
committeremaclee <lee.anjella.macabuhay@est.tech>
Tue, 4 Oct 2022 08:00:24 +0000 (09:00 +0100)
**this commit is for analysis purposes only, not for merge
- add unit test for gson JSONreader
- add unit test for ODL JsonParserStream

Issue-ID: CPS-1286
Signed-off-by: emaclee <lee.anjella.macabuhay@est.tech>
Change-Id: Ia4a8c3e36f83b4cd857b4ff20f6a37e418ffd2ec

cps-service/src/test/groovy/org/onap/cps/utils/GsonSpec.groovy [new file with mode: 0644]
cps-service/src/test/groovy/org/onap/cps/utils/JsonParserStreamSpec.groovy [new file with mode: 0644]
cps-service/src/test/resources/multiple-object-data.json [new file with mode: 0644]
cps-service/src/test/resources/multipleDataTree.yang [new file with mode: 0644]

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;
+        }
+    }
+
+
+}
diff --git a/cps-service/src/test/groovy/org/onap/cps/utils/JsonParserStreamSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/utils/JsonParserStreamSpec.groovy
new file mode 100644 (file)
index 0000000..68f9251
--- /dev/null
@@ -0,0 +1,51 @@
+package org.onap.cps.utils
+
+import com.google.gson.stream.JsonReader
+import org.onap.cps.yang.YangTextSchemaSourceSetBuilder
+import org.opendaylight.yangtools.yang.common.QName
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier
+import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode
+import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier
+import org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream
+import org.opendaylight.yangtools.yang.data.impl.schema.Builders
+import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter
+import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+import spock.lang.Specification
+import org.onap.cps.TestUtils
+
+class JsonParserStreamSpec extends Specification{
+
+    def 'Multiple data tree parsing with ODL JsonStreamParser'(){
+        given: 'json data with two objects and JSON reader'
+            def jsonData = TestUtils.getResourceFileContent('multiple-object-data.json')
+            def jsonReader = new JsonReader(new StringReader(jsonData))
+            def yangResourcesMap = TestUtils.getYangResourcesAsMap('multipleDataTree.yang')
+        and: 'schema context'
+            def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourcesMap).getSchemaContext()
+        and: 'variable to store the result of parsing'
+            DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode> builder =
+                    Builders.containerBuilder().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(schemaContext.getQName()));
+            def normalizedNodeStreamWriter = ImmutableNormalizedNodeStreamWriter.from(builder);
+            def jsonCodecFactory = JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02
+                    .getShared((EffectiveModelContext) schemaContext);
+        and: 'JSON parser stream'
+            def jsonParserStream = JsonParserStream.create(normalizedNodeStreamWriter, jsonCodecFactory)
+        when: 'parsing is invoked with the given JSON reader'
+            jsonParserStream.parse(jsonReader)
+            def result = builder.build()
+        then: 'result is the correct size'
+            result.size() == 2
+        then: 'data container child is a type of normalized node'
+            def dataContainerChild = result.getValue()[index]
+            dataContainerChild instanceof NormalizedNode == true
+        then: 'qualified name created is as expected'
+            dataContainerChild.nodeType == QName.create('org:onap:ccsdk:multiDataTree', '2020-09-15', nodeName)
+        where:
+            index   | nodeName
+            0       | 'first-container'
+            1       | 'last-container'
+
+    }
+}
\ No newline at end of file
diff --git a/cps-service/src/test/resources/multiple-object-data.json b/cps-service/src/test/resources/multiple-object-data.json
new file mode 100644 (file)
index 0000000..a3c4676
--- /dev/null
@@ -0,0 +1,8 @@
+{
+  "first-container": {
+    "a-leaf": "a-Value"
+  },
+  "last-container": {
+    "x-leaf": "x-value"
+  }
+}
\ No newline at end of file
diff --git a/cps-service/src/test/resources/multipleDataTree.yang b/cps-service/src/test/resources/multipleDataTree.yang
new file mode 100644 (file)
index 0000000..15fd042
--- /dev/null
@@ -0,0 +1,23 @@
+module multipleDataTree {
+    yang-version 1.1;
+    namespace "org:onap:ccsdk:multiDataTree";
+
+    prefix multiple-data-tree;
+
+    revision "2020-09-15" {
+        description
+        "Sample Model for multiple data tree";
+    }
+
+    container first-container {
+        leaf a-leaf {
+            type string;
+        }
+    }
+
+    container last-container {
+        leaf x-leaf {
+            type string;
+        }
+    }
+}