Addressed an adaptability issue
[cps.git] / cps-service / src / test / groovy / org / onap / cps / utils / JsonParserStreamSpec.groovy
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2022 Nordix Foundation
4  *  Modifications Copyright (C) 2022 TechMahindra Ltd.
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.utils
23
24 import com.google.gson.stream.JsonReader
25 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder
26 import org.opendaylight.yangtools.yang.common.QName
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier
28 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode
30 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier
31 import org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream
32 import org.opendaylight.yangtools.yang.data.impl.schema.Builders
33 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter
34 import org.opendaylight.yangtools.yang.data.api.schema.builder.DataContainerNodeBuilder
35 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
36 import spock.lang.Specification
37 import org.onap.cps.TestUtils
38
39 class JsonParserStreamSpec extends Specification{
40
41     def 'Multiple data tree parsing with ODL JsonStreamParser'(){
42         given: 'json data with two objects and JSON reader'
43             def jsonData = TestUtils.getResourceFileContent('multiple-object-data.json')
44             def jsonReader = new JsonReader(new StringReader(jsonData))
45             def yangResourcesMap = TestUtils.getYangResourcesAsMap('multipleDataTree.yang')
46         and: 'schema context'
47             def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourcesMap).getSchemaContext()
48         and: 'variable to store the result of parsing'
49             DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode> builder =
50                     Builders.containerBuilder().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(schemaContext.getQName()))
51             def normalizedNodeStreamWriter = ImmutableNormalizedNodeStreamWriter.from(builder)
52             def jsonCodecFactory = JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02
53                     .getShared((EffectiveModelContext) schemaContext)
54         and: 'JSON parser stream'
55             def jsonParserStream = JsonParserStream.create(normalizedNodeStreamWriter, jsonCodecFactory)
56         when: 'parsing is invoked with the given JSON reader'
57             jsonParserStream.parse(jsonReader)
58             def result = builder.build()
59         then: 'result is the correct size'
60             result.size() == 2
61         then: 'data container child is a type of normalized node'
62             def dataContainerChild = result.body().getAt(index)
63             dataContainerChild instanceof NormalizedNode == true
64         then: 'qualified name created is as expected'
65             dataContainerChild.identifier.nodeType == QName.create('org:onap:ccsdk:multiDataTree', '2020-09-15', nodeName)
66         where:
67             index   | nodeName
68             0       | 'first-container'
69             1       | 'last-container'
70
71     }
72 }