2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Pantheon.tech
4 * Modifications Copyright (C) 2020 Nordix Foundation
5 * Modifications Copyright (C) 2022 Bell Canada.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.utils
24 import org.onap.cps.spi.model.DataNodeBuilder
25 import spock.lang.Specification
27 class DataMapUtilsSpec extends Specification {
31 def dataNode = buildDataNode(
32 "/parent",[parentLeaf:'parentLeafValue', parentLeafList:['parentLeafListEntry1','parentLeafListEntry2']],[
33 buildDataNode('/parent/child-list[@id=1]',[listElementLeaf:'listElement1leafValue'],noChildren),
34 buildDataNode('/parent/child-list[@id=2]',[listElementLeaf:'listElement2leafValue'],noChildren),
35 buildDataNode('/parent/child-object',[childLeaf:'childLeafValue'],
36 [buildDataNode('/parent/child-object/grand-child-object',[grandChildLeaf:'grandChildLeafValue'],noChildren)]
40 static def buildDataNode(xpath, leaves, children) {
41 return new DataNodeBuilder().withXpath(xpath).withLeaves(leaves).withChildDataNodes(children).build()
44 def 'Data node structure conversion to map.'() {
45 when: 'data node structure is converted to a map'
46 def result = DataMapUtils.toDataMap(dataNode)
48 then: 'root node identifier is null'
51 then: 'root node leaves are top level elements'
52 result.parentLeaf == 'parentLeafValue'
53 result.parentLeafList == ['parentLeafListEntry1','parentLeafListEntry2']
55 and: 'leaves of child list element are listed as structures under common identifier'
56 result.'child-list'.collect().containsAll(['listElementLeaf': 'listElement1leafValue'],
57 ['listElementLeaf': 'listElement2leafValue'])
59 and: 'leaves for child element is populated under its node identifier'
60 result.'child-object'.childLeaf == 'childLeafValue'
62 and: 'leaves for grandchild element is populated under its node identifier'
63 result.'child-object'.'grand-child-object'.grandChildLeaf == 'grandChildLeafValue'
66 def 'Data node structure conversion to map with root node identifier.'() {
67 when: 'data node structure is converted to a map with root node identifier'
68 def result = DataMapUtils.toDataMapWithIdentifier(dataNode)
70 then: 'root node identifier is not null'
73 then: 'root node leaves are populated under its node identifier'
74 def parentNode = result.parent
75 parentNode.parentLeaf == 'parentLeafValue'
76 parentNode.parentLeafList == ['parentLeafListEntry1','parentLeafListEntry2']
78 and: 'leaves for child element is populated under its node identifier'
79 parentNode.'child-object'.childLeaf == 'childLeafValue'
81 and: 'leaves for grandchild element is populated under its node identifier'
82 parentNode.'child-object'.'grand-child-object'.grandChildLeaf == 'grandChildLeafValue'