429ab40b90a0fdc9d025c95760a28a90cf247d8f
[cps.git] / cps-service / src / test / groovy / org / onap / cps / utils / DataMapUtilsSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Pantheon.tech
4  *  Modifications Copyright (C) 2020 Nordix Foundation
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  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.utils
22
23 import org.onap.cps.spi.model.DataNodeBuilder
24 import spock.lang.Specification
25
26 class DataMapUtilsSpec extends Specification {
27
28     def noChildren = []
29
30     def dataNode = buildDataNode(
31         "/parent",[parentLeaf:'parentLeafValue', parentLeafList:['parentLeafListEntry1','parentLeafListEntry2']],[
32                 buildDataNode('/parent/child-list[@id=1]',[listElementLeaf:'listElement1leafValue'],noChildren),
33                 buildDataNode('/parent/child-list[@id=2]',[listElementLeaf:'listElement2leafValue'],noChildren),
34                 buildDataNode('/parent/child-object',[childLeaf:'childLeafValue'],
35                         [buildDataNode('/parent/child-object/grand-child-object',[grandChildLeaf:'grandChildLeafValue'],noChildren)]
36                 ),
37             ])
38
39     static def buildDataNode(xpath,  leaves,  children) {
40         return new DataNodeBuilder().withXpath(xpath).withLeaves(leaves).withChildDataNodes(children).build()
41     }
42
43     def 'Data node structure conversion to map.'() {
44         when: 'data node structure is converted to a map'
45             Map result = DataMapUtils.toDataMap(dataNode)
46
47         then: 'root node leaves are top level elements'
48             result.parentLeaf == 'parentLeafValue'
49             result.parentLeafList == ['parentLeafListEntry1','parentLeafListEntry2']
50
51         and: 'leaves of child list element are listed as structures under common identifier'
52             result.'child-list'.collect().containsAll(['listElementLeaf': 'listElement1leafValue'],
53                                                       ['listElementLeaf': 'listElement2leafValue'])
54
55         and: 'leaves for child element is populated under its node identifier'
56             Map childObjectData = result.'child-object'
57             childObjectData.childLeaf == 'childLeafValue'
58
59         and: 'leaves for grandchild element is populated under its node identifier'
60             Map grandChildObjectData = childObjectData.'grand-child-object'
61             grandChildObjectData.grandChildLeaf == 'grandChildLeafValue'
62     }
63
64 }