Align JSON DataNode for Get and Post/Put API in CPS
[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  *  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
10  *
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.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.utils
23
24 import org.onap.cps.spi.model.DataNodeBuilder
25 import spock.lang.Specification
26
27 class DataMapUtilsSpec extends Specification {
28
29     def noChildren = []
30
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)]
37                 ),
38             ])
39
40     static def buildDataNode(xpath,  leaves,  children) {
41         return new DataNodeBuilder().withXpath(xpath).withLeaves(leaves).withChildDataNodes(children).build()
42     }
43
44     def 'Data node structure conversion to map.'() {
45         when: 'data node structure is converted to a map'
46             def result = DataMapUtils.toDataMap(dataNode)
47
48         then: 'root node identifier is null'
49             result.parent == null
50
51         then: 'root node leaves are top level elements'
52             result.parentLeaf == 'parentLeafValue'
53             result.parentLeafList == ['parentLeafListEntry1','parentLeafListEntry2']
54
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'])
58
59         and: 'leaves for child element is populated under its node identifier'
60             result.'child-object'.childLeaf == 'childLeafValue'
61
62         and: 'leaves for grandchild element is populated under its node identifier'
63             result.'child-object'.'grand-child-object'.grandChildLeaf == 'grandChildLeafValue'
64     }
65
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)
69
70         then: 'root node identifier is not null'
71             result.parent != null
72
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']
77
78         and: 'leaves for child element is populated under its node identifier'
79             parentNode.'child-object'.childLeaf == 'childLeafValue'
80
81         and: 'leaves for grandchild element is populated under its node identifier'
82             parentNode.'child-object'.'grand-child-object'.grandChildLeaf == 'grandChildLeafValue'
83     }
84 }