Merge "Normalize parent xpath when building datanodes in CpsDataService"
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / utils / DataNodeHelperSpec.groovy
1 /*
2  * ============LICENSE_START========================================================
3  * Copyright (c) 2023 Nordix Foundation.
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.ncmp.api.impl.utils
22
23 import org.onap.cps.ncmp.api.impl.subscriptions.SubscriptionStatus
24 import org.onap.cps.spi.model.DataNodeBuilder
25
26 class DataNodeHelperSpec extends DataNodeBaseSpec {
27
28     def 'Get data node leaves as expected from a nested data node.'() {
29         given: 'a nested data node'
30             def dataNode = new DataNodeBuilder().withDataspace('NCMP-Admin')
31                 .withAnchor('AVC-Subscriptions').withXpath('/subscription-registry/subscription')
32                 .withLeaves([clientID:'SCO-9989752', isTagged:false, subscriptionName:'cm-subscription-001'])
33                 .withChildDataNodes([dataNode4]).build()
34         when: 'the nested data node is flatten and retrieves the leaves '
35             def result = DataNodeHelper.getDataNodeLeaves([dataNode])
36         then: 'the result list size is 5'
37             result.size() == 5
38         and: 'all the leaves result list are equal to given leaves of data nodes'
39             result[0] == [clientID:'SCO-9989752', isTagged:false, subscriptionName:'cm-subscription-001']
40             result[1] == [datastore:'passthrough-running']
41             result[2] == [status:'PENDING', cmHandleId:'CMHandle3']
42             result[3] == [status:'ACCEPTED', cmHandleId:'CMHandle2']
43             result[4] == [status:'REJECTED', cmHandleId:'CMHandle1']
44     }
45
46     def 'Get cm handle id to status as expected from a nested data node.'() {
47         given: 'a nested data node'
48             def dataNode = new DataNodeBuilder().withDataspace('NCMP-Admin')
49                 .withAnchor('AVC-Subscriptions').withXpath('/subscription-registry/subscription')
50                 .withLeaves([clientID:'SCO-9989752', isTagged:false, subscriptionName:'cm-subscription-001'])
51                 .withChildDataNodes([dataNode4]).build()
52         and: 'the nested data node is flatten and retrieves the leaves '
53             def leaves = DataNodeHelper.getDataNodeLeaves([dataNode])
54         when:'cm handle id to status is retrieved'
55             def result = DataNodeHelper.getCmHandleIdToStatus(leaves)
56         then: 'the result list size is 3'
57             result.size() == 3
58         and: 'the result contains expected values'
59             result[0] as List == ['PENDING', 'CMHandle3']
60             result[1] as List == ['ACCEPTED', 'CMHandle2']
61             result[2] as List == ['REJECTED', 'CMHandle1']
62     }
63
64     def 'Get cm handle id to status map as expected from list of collection' () {
65         given: 'a list of collection'
66             def cmHandleCollection = [['PENDING', 'CMHandle3'], ['ACCEPTED', 'CMHandle2'], ['REJECTED', 'CMHandle1']]
67         when: 'the map is formed up with a method call'
68             def result = DataNodeHelper.getCmHandleIdToStatusMap(cmHandleCollection)
69         then: 'the map values are as expected'
70             result.keySet() == ['CMHandle3', 'CMHandle2', 'CMHandle1'] as Set
71             result.values() as List == [SubscriptionStatus.PENDING, SubscriptionStatus.ACCEPTED, SubscriptionStatus.REJECTED]
72     }
73
74
75     def 'Get cm handle id to status map as expected from a nested data node.'() {
76         given: 'a nested data node'
77             def dataNode = new DataNodeBuilder().withDataspace('NCMP-Admin')
78                 .withAnchor('AVC-Subscriptions').withXpath('/subscription-registry/subscription')
79                 .withLeaves([clientID:'SCO-9989752', isTagged:false, subscriptionName:'cm-subscription-001'])
80                 .withChildDataNodes([dataNode4]).build()
81         when:'cm handle id to status is being extracted'
82             def result = DataNodeHelper.getCmHandleIdToStatusMapFromDataNodes([dataNode]);
83         then: 'the keys are retrieved as expected'
84             result.keySet() == ['CMHandle3','CMHandle2','CMHandle1'] as Set
85     }
86 }