Handling Yang module upgrade error scenarios
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / inventory / CmHandleQueriesImplSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2022-2023 Nordix Foundation
4  *  Modifications Copyright (C) 2023 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.ncmp.api.impl.inventory
23
24 import org.onap.cps.ncmp.api.impl.trustlevel.TrustLevel
25 import org.onap.cps.spi.utils.CpsValidator
26
27 import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NCMP_DATASPACE_NAME
28 import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NCMP_DMI_REGISTRY_ANCHOR
29 import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NCMP_DMI_REGISTRY_PARENT
30 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
31 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
32
33 import com.hazelcast.map.IMap
34 import org.onap.cps.ncmp.api.impl.inventory.CmHandleQueriesImpl
35 import org.onap.cps.ncmp.api.impl.inventory.CmHandleState
36 import org.onap.cps.ncmp.api.impl.inventory.DataStoreSyncState
37 import org.onap.cps.spi.CpsDataPersistenceService
38 import org.onap.cps.spi.model.DataNode
39 import spock.lang.Shared
40 import spock.lang.Specification
41
42 class CmHandleQueriesImplSpec extends Specification {
43     def cpsDataPersistenceService = Mock(CpsDataPersistenceService)
44     def mockCpsValidator = Mock(CpsValidator)
45     def trustLevelPerCmHandle = [ 'my completed cm handle': TrustLevel.COMPLETE, 'my untrusted cm handle': TrustLevel.NONE ]
46
47     def objectUnderTest = new CmHandleQueriesImpl(cpsDataPersistenceService, trustLevelPerCmHandle, mockCpsValidator)
48
49     @Shared
50     def static sampleDataNodes = [new DataNode()]
51
52     def dataNodeWithPrivateField = '//additional-properties[@name=\"Contact3\" and @value=\"newemailforstore3@bookstore.com\"]/ancestor::cm-handles'
53
54     def static pnfDemo = createDataNode('PNFDemo')
55     def static pnfDemo2 = createDataNode('PNFDemo2')
56     def static pnfDemo3 = createDataNode('PNFDemo3')
57     def static pnfDemo4 = createDataNode('PNFDemo4')
58     def static pnfDemo5 = createDataNode('PNFDemo5')
59
60     def 'Query CmHandles with public properties query pair.'() {
61         given: 'the DataNodes queried for a given cpsPath are returned from the persistence service.'
62             mockResponses()
63         when: 'a query on cmhandle public properties is performed with a public property pair'
64             def result = objectUnderTest.queryCmHandlePublicProperties(publicPropertyPairs)
65         then: 'the correct cm handle data objects are returned'
66             result.containsAll(expectedCmHandleIds)
67             result.size() == expectedCmHandleIds.size()
68         where: 'the following data is used'
69             scenario                         | publicPropertyPairs                                                                      || expectedCmHandleIds
70             'single property matches'        | [Contact: 'newemailforstore@bookstore.com']                                              || ['PNFDemo', 'PNFDemo2', 'PNFDemo4']
71             'public property does not match' | [wont_match: 'wont_match']                                                               || []
72             '2 properties, only one match'   | [Contact: 'newemailforstore@bookstore.com', Contact2: 'newemailforstore2@bookstore.com'] || ['PNFDemo4']
73             '2 properties, no matches'       | [Contact: 'newemailforstore@bookstore.com', Contact2: '']                                || []
74     }
75
76     def 'Query cm handles on trust level'() {
77         given: 'query properties for trustlevel COMPLETE'
78             def trustLevelPropertyQueryPairs = ['trustLevel' : TrustLevel.COMPLETE.toString()]
79         when: 'the query is executed'
80             def result = objectUnderTest.queryCmHandlesByTrustLevel(trustLevelPropertyQueryPairs)
81         then: 'the result only contains the completed cm handle'
82             assert result.size() == 1
83             assert result[0] == 'my completed cm handle'
84     }
85
86     def 'Query CmHandles using empty public properties query pair.'() {
87         when: 'a query on CmHandle public properties is executed using an empty map'
88             def result = objectUnderTest.queryCmHandlePublicProperties([:])
89         then: 'no cm handles are returned'
90             result.size() == 0
91     }
92
93     def 'Query CmHandles using empty private properties query pair.'() {
94         when: 'a query on CmHandle private properties is executed using an empty map'
95             def result = objectUnderTest.queryCmHandleAdditionalProperties([:])
96         then: 'no cm handles are returned'
97             result.size() == 0
98     }
99
100     def 'Query CmHandles by a private field\'s value.'() {
101         given: 'a data node exists with a certain additional-property'
102             cpsDataPersistenceService.queryDataNodes(_, _, dataNodeWithPrivateField, _) >> [pnfDemo5]
103         when: 'a query on CmHandle private properties is executed using a map'
104             def result = objectUnderTest.queryCmHandleAdditionalProperties(['Contact3': 'newemailforstore3@bookstore.com'])
105         then: 'one cm handle is returned'
106             result.size() == 1
107     }
108
109     def 'Get CmHandles by it\'s state.'() {
110         given: 'a cm handle state to query'
111             def cmHandleState = CmHandleState.ADVISED
112         and: 'the persistence service returns a list of data nodes'
113             cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
114                 '//state[@cm-handle-state="ADVISED"]/ancestor::cm-handles', INCLUDE_ALL_DESCENDANTS) >> sampleDataNodes
115         when: 'cm handles are fetched by state'
116             def result = objectUnderTest.queryCmHandlesByState(cmHandleState)
117         then: 'the returned result matches the result from the persistence service'
118             assert result == sampleDataNodes
119     }
120
121     def 'Check the state of a cmHandle when #scenario.'() {
122         given: 'a cm handle state to compare'
123             def cmHandleState = state
124         and: 'the persistence service returns a list of data nodes'
125             cpsDataPersistenceService.getDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
126                     NCMP_DMI_REGISTRY_PARENT + '/cm-handles[@id=\'some-cm-handle\']/state',
127                     OMIT_DESCENDANTS) >> [new DataNode(leaves: ['cm-handle-state': 'READY'])]
128         when: 'cm handles are compared by state'
129             def result = objectUnderTest.cmHandleHasState('some-cm-handle', cmHandleState)
130         then: 'the returned result matches the expected result from the persistence service'
131             result == expectedResult
132         where:
133             scenario                           | state                 || expectedResult
134             'the provided state matches'       | CmHandleState.READY   || true
135             'the provided state does not match'| CmHandleState.DELETED || false
136     }
137
138     def 'Get Cm Handles state by Cm-Handle Id'() {
139         given: 'a cm handle state to query'
140             def cmHandleState = CmHandleState.READY
141         and: 'cps data service returns a list of data nodes'
142             cpsDataPersistenceService.getDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
143                     NCMP_DMI_REGISTRY_PARENT + '/cm-handles[@id=\'some-cm-handle\']/state',
144                     OMIT_DESCENDANTS) >> [new DataNode(leaves: ['cm-handle-state': 'READY'])]
145         when: 'cm handles are fetched by state and id'
146             def result = objectUnderTest.getCmHandleState('some-cm-handle')
147         then: 'the returned result is a list of data nodes returned by cps data service'
148             assert result == new DataNode(leaves: ['cm-handle-state': 'READY'])
149     }
150
151     def 'Retrieve Cm Handles By Operational Sync State : UNSYNCHRONIZED'() {
152         given: 'a cm handle state to query'
153             def cmHandleState = CmHandleState.READY
154         and: 'cps data service returns a list of data nodes'
155             cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
156                 '//state/datastores/operational[@sync-state="'+'UNSYNCHRONIZED'+'"]/ancestor::cm-handles', OMIT_DESCENDANTS) >> sampleDataNodes
157         when: 'cm handles are fetched by the UNSYNCHRONIZED operational sync state'
158             def result = objectUnderTest.queryCmHandlesByOperationalSyncState(DataStoreSyncState.UNSYNCHRONIZED)
159         then: 'the returned result is a list of data nodes returned by cps data service'
160             assert result == sampleDataNodes
161     }
162
163     def 'Retrieve cm handle by cps path '() {
164         given: 'a cm handle state to query based on the cps path'
165             def cmHandleDataNode = new DataNode(xpath: 'xpath', leaves: ['cm-handle-state': 'LOCKED'])
166             def cpsPath = '//cps-path'
167         and: 'cps data service returns a valid data node'
168             cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
169                 cpsPath + '/ancestor::cm-handles', INCLUDE_ALL_DESCENDANTS)
170                 >> Arrays.asList(cmHandleDataNode)
171         when: 'get cm handles by cps path is invoked'
172             def result = objectUnderTest.queryCmHandleAncestorsByCpsPath(cpsPath, INCLUDE_ALL_DESCENDANTS)
173         then: 'the returned result is a list of data nodes returned by cps data service'
174             assert result.contains(cmHandleDataNode)
175     }
176
177     def 'Get all cm handles by dmi plugin identifier'() {
178         given: 'the DataNodes queried for a given cpsPath are returned from the persistence service.'
179             mockResponses()
180         when: 'cm Handles are fetched for a given dmi plugin identifier'
181             def result = objectUnderTest.getCmHandleIdsByDmiPluginIdentifier('my-dmi-plugin-identifier')
182         then: 'result is the correct size'
183             assert result.size() == 3
184         and: 'result contains the correct cm handles'
185             assert result.containsAll('PNFDemo', 'PNFDemo2', 'PNFDemo4')
186     }
187
188     void mockResponses() {
189         cpsDataPersistenceService.queryDataNodes(_, _, '//public-properties[@name=\"Contact\" and @value=\"newemailforstore@bookstore.com\"]/ancestor::cm-handles', _) >> [pnfDemo, pnfDemo2, pnfDemo4]
190         cpsDataPersistenceService.queryDataNodes(_, _, '//public-properties[@name=\"wont_match\" and @value=\"wont_match\"]/ancestor::cm-handles', _) >> []
191         cpsDataPersistenceService.queryDataNodes(_, _, '//public-properties[@name=\"Contact2\" and @value=\"newemailforstore2@bookstore.com\"]/ancestor::cm-handles', _) >> [pnfDemo4]
192         cpsDataPersistenceService.queryDataNodes(_, _, '//public-properties[@name=\"Contact2\" and @value=\"\"]/ancestor::cm-handles', _) >> []
193         cpsDataPersistenceService.queryDataNodes(_, _, '//state[@cm-handle-state=\"READY\"]/ancestor::cm-handles', _) >> [pnfDemo, pnfDemo3]
194         cpsDataPersistenceService.queryDataNodes(_, _, '//state[@cm-handle-state=\"LOCKED\"]/ancestor::cm-handles', _) >> [pnfDemo2, pnfDemo4]
195         cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, '/dmi-registry/cm-handles[@dmi-service-name=\'my-dmi-plugin-identifier\']', OMIT_DESCENDANTS) >> [pnfDemo, pnfDemo2]
196         cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, '/dmi-registry/cm-handles[@dmi-data-service-name=\'my-dmi-plugin-identifier\']', OMIT_DESCENDANTS) >> [pnfDemo, pnfDemo4]
197         cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, '/dmi-registry/cm-handles[@dmi-model-service-name=\'my-dmi-plugin-identifier\']', OMIT_DESCENDANTS) >> [pnfDemo2, pnfDemo4]
198     }
199
200     def static createDataNode(dataNodeId) {
201         return new DataNode(xpath: '/dmi-registry/cm-handles[@id=\'' + dataNodeId + '\']', leaves: ['id':dataNodeId])
202     }
203 }