2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022-2025 OpenInfra Foundation Europe. All rights reserved.
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
10 * 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.ncmp.impl.inventory
24 import static org.onap.cps.ncmp.impl.inventory.NcmpPersistence.NCMP_DATASPACE_NAME
25 import static org.onap.cps.ncmp.impl.inventory.NcmpPersistence.NCMP_DMI_REGISTRY_ANCHOR
26 import static org.onap.cps.ncmp.impl.inventory.NcmpPersistence.NCMP_DMI_REGISTRY_PARENT
27 import static org.onap.cps.api.parameters.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
28 import static org.onap.cps.api.parameters.FetchDescendantsOption.OMIT_DESCENDANTS
30 import com.hazelcast.config.Config
31 import com.hazelcast.core.Hazelcast
32 import com.hazelcast.instance.impl.HazelcastInstanceFactory
33 import org.onap.cps.api.CpsDataService
34 import org.onap.cps.api.CpsQueryService
35 import org.onap.cps.utils.CpsValidator
36 import org.onap.cps.ncmp.api.inventory.DataStoreSyncState
37 import org.onap.cps.ncmp.api.inventory.models.TrustLevel
38 import org.onap.cps.ncmp.api.inventory.models.CmHandleState
39 import org.onap.cps.api.model.DataNode
40 import spock.lang.Specification
42 class CmHandleQueryServiceImplSpec extends Specification {
44 def mockCpsQueryService = Mock(CpsQueryService)
45 def mockCpsDataService = Mock(CpsDataService)
46 def trustLevelPerDmiPlugin = HazelcastInstanceFactory.getOrCreateHazelcastInstance(new Config('hazelcastInstanceName')).getMap('trustLevelPerDmiPlugin')
47 def trustLevelPerCmHandleId = HazelcastInstanceFactory.getOrCreateHazelcastInstance(new Config('hazelcastInstanceName')).getMap('trustLevelPerCmHandleId')
48 def mockCpsValidator = Mock(CpsValidator)
50 def objectUnderTest = new CmHandleQueryServiceImpl(mockCpsDataService, mockCpsQueryService, trustLevelPerDmiPlugin, trustLevelPerCmHandleId, mockCpsValidator)
51 def static sampleDataNodes = [new DataNode(xpath: "/dmi-registry/cm-handles[@id='ch-1']"),
52 new DataNode(xpath: "/dmi-registry/cm-handles[@id='ch-2']")]
54 def cpsPathWithPrivateField = '//additional-properties[@name=\'Contact3\' and @value=\'newemailforstore3@bookstore.com\']/ancestor::cm-handles/@id'
56 def static pnfDemo = createDataNode('PNFDemo')
57 def static pnfDemo2 = createDataNode('PNFDemo2')
58 def static pnfDemo3 = createDataNode('PNFDemo3')
59 def static pnfDemo4 = createDataNode('PNFDemo4')
60 def static pnfDemo5 = createDataNode('PNFDemo5')
63 trustLevelPerCmHandleId.put("PNFDemo", TrustLevel.COMPLETE)
64 trustLevelPerCmHandleId.put("PNFDemo2", TrustLevel.NONE)
65 trustLevelPerCmHandleId.put("PNFDemo4", TrustLevel.NONE)
69 Hazelcast.getHazelcastInstanceByName('hazelcastInstanceName').shutdown()
72 def 'Query CmHandles with public properties query pair.'() {
73 given: 'the DataNodes queried for a given cpsPath are returned from the persistence service.'
75 when: 'a query on cmhandle public properties is performed with a public property pair'
76 def result = objectUnderTest.queryCmHandlePublicProperties(publicPropertyPairs, outputAlternateId)
77 then: 'the correct cm handle data objects are returned'
78 result.containsAll(expectedCmHandleReferences)
79 result.size() == expectedCmHandleReferences.size()
80 where: 'the following data is used'
81 scenario | publicPropertyPairs | outputAlternateId || expectedCmHandleReferences
82 'single property matches' | [Contact: 'newemailforstore@bookstore.com'] | false || ['PNFDemo', 'PNFDemo2', 'PNFDemo4']
83 'public property does not match' | [wont_match: 'wont_match'] | false || []
84 '2 properties, only one match' | [Contact: 'newemailforstore@bookstore.com', Contact2: 'newemailforstore2@bookstore.com'] | true || ['alt-PNFDemo4']
85 '2 properties, no matches' | [Contact: 'newemailforstore@bookstore.com', Contact2: ''] | false || []
88 def 'Query cm handles on trust level'() {
89 given: 'query properties for #trustLevel'
90 def trustLevelPropertyQueryPairs = ['trustLevel' : trustLevel.toString()]
91 and: 'the dmi cache has been initialised and "knows" about my-dmi-plugin-identifier'
92 trustLevelPerDmiPlugin.put('my-dmi-plugin-identifier', trustLevel)
93 and: 'the DataNodes queried for a given cpsPath are returned from the persistence service'
95 when: 'the query is run'
96 def result = objectUnderTest.queryCmHandlesByTrustLevel(trustLevelPropertyQueryPairs, outputAlternateId)
97 then: 'the result contain trusted cmHandle reference'
98 assert result as Set == expectedCmHandleReference as Set
99 where: 'the following data is used'
100 senario | outputAlternateId | expectedCmHandleReference | trustLevel || resultSize
101 'output cmHandleId for trustLevel Complete' | false | ['PNFDemo'] | TrustLevel.COMPLETE || 1
102 'output alternateId for trustLevel Complete'| true | ['alt-PNFDemo'] | TrustLevel.COMPLETE || 1
103 'output alternateIds for trustLevel None' | true | ['alt-PNFDemo2', 'alt-PNFDemo', 'alt-PNFDemo4']| TrustLevel.NONE || 3
106 def 'Query CmHandles using empty public properties query pair.'() {
107 when: 'a query on CmHandle public properties is executed using an empty map'
108 def result = objectUnderTest.queryCmHandlePublicProperties([:], false)
109 then: 'no cm handles are returned'
113 def 'Query CmHandles using empty private properties query pair.'() {
114 when: 'a query on CmHandle private properties is executed using an empty map'
115 def result = objectUnderTest.queryCmHandleAdditionalProperties([:], false)
116 then: 'no cm handles are returned'
120 def 'Query CmHandles by a private field\'s value.'() {
121 given: 'a data node exists with a certain additional-property'
122 mockCpsQueryService.queryDataLeaf(_, _, cpsPathWithPrivateField, _) >> [pnfDemo5.getLeaves().get('id')]
123 when: 'a query on CmHandle private properties is executed using a map'
124 def result = objectUnderTest.queryCmHandleAdditionalProperties(['Contact3': 'newemailforstore3@bookstore.com'], false)
125 then: 'one cm handle is returned'
129 def 'Get Ids of CmHandles by state.'() {
130 given: 'a cm handle state to query'
131 def cmHandleState = CmHandleState.ADVISED
132 and: 'the persistence service returns a list of data nodes'
133 mockCpsQueryService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
134 "//state[@cm-handle-state='ADVISED']", OMIT_DESCENDANTS, 0) >> sampleDataNodes
135 when: 'cm handles are fetched by state'
136 def result = objectUnderTest.queryCmHandleIdsByState(cmHandleState)
137 then: 'the returned result matches the result from the persistence service'
138 assert result.toSet() == ['ch-1', 'ch-2'].toSet()
141 def 'Check the state of a cmHandle when #scenario.'() {
142 given: 'a cm handle state to compare'
143 def cmHandleState = state
144 and: 'the persistence service returns a list of data nodes'
145 mockCpsDataService.getDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
146 NCMP_DMI_REGISTRY_PARENT + '/cm-handles[@id=\'some-cm-handle\']/state',
147 OMIT_DESCENDANTS) >> [new DataNode(leaves: ['cm-handle-state': 'READY'])]
148 when: 'cm handles are compared by state'
149 def result = objectUnderTest.cmHandleHasState('some-cm-handle', cmHandleState)
150 then: 'the returned result matches the expected result from the persistence service'
151 result == expectedResult
153 scenario | state || expectedResult
154 'the provided state matches' | CmHandleState.READY || true
155 'the provided state does not match'| CmHandleState.DELETED || false
158 def 'Get Cm Handles state by Cm-Handle Id'() {
159 given: 'a cm handle state to query'
160 def cmHandleState = CmHandleState.READY
161 and: 'cps data service returns a list of data nodes'
162 mockCpsDataService.getDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
163 NCMP_DMI_REGISTRY_PARENT + '/cm-handles[@id=\'some-cm-handle\']/state',
164 OMIT_DESCENDANTS) >> [new DataNode(leaves: ['cm-handle-state': 'READY'])]
165 when: 'cm handles are fetched by state and id'
166 def result = objectUnderTest.getCmHandleState('some-cm-handle')
167 then: 'the returned result is a list of data nodes returned by cps data service'
168 assert result == new DataNode(leaves: ['cm-handle-state': 'READY'])
171 def 'Retrieve Cm Handles By Operational Sync State : UNSYNCHRONIZED'() {
172 given: 'cps data service returns a list of data nodes'
173 mockCpsQueryService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
174 '//state/datastores/operational[@sync-state="'+'UNSYNCHRONIZED'+'"]/ancestor::cm-handles', OMIT_DESCENDANTS, 0) >> sampleDataNodes
175 when: 'cm handles are fetched by the UNSYNCHRONIZED operational sync state'
176 def result = objectUnderTest.queryCmHandlesByOperationalSyncState(DataStoreSyncState.UNSYNCHRONIZED)
177 then: 'the returned result is a list of data nodes returned by cps data service'
178 assert result == sampleDataNodes
181 def 'Retrieve cm handle by cps path '() {
182 given: 'a cm handle state to query based on the cps path'
183 def cmHandleDataNode = new DataNode(xpath: "/dmi-registry/cm-handles[@id='ch-1']", leaves: ['id': 'ch-1'])
184 def cpsPath = "//state[@cm-handle-state='LOCKED']"
185 and: 'cps data service returns a valid data node for cm handle ancestor'
186 mockCpsQueryService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
187 cpsPath + '/ancestor::cm-handles', INCLUDE_ALL_DESCENDANTS, 0)
188 >> Arrays.asList(cmHandleDataNode)
189 when: 'get cm handles by cps path is invoked'
190 def result = objectUnderTest.queryCmHandleAncestorsByCpsPath(cpsPath, INCLUDE_ALL_DESCENDANTS)
191 then: 'the returned result is a list of data nodes returned by cps data service'
192 assert result.contains(cmHandleDataNode)
195 def 'Retrieve cm handle by cps path querying cm handle directly'() {
196 given: 'a cm handle to query based on the cps path'
197 def cmHandleDataNode = new DataNode(xpath: "/dmi-registry/cm-handles[@id='ch-2']", leaves: ['id': 'ch-2'])
198 def cpsPath = "//cm-handles[@alternate-id='1']"
199 and: 'cps data service returns a valid data node'
200 mockCpsQueryService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
201 cpsPath, INCLUDE_ALL_DESCENDANTS, 0)
202 >> Arrays.asList(cmHandleDataNode)
203 when: 'get cm handles by cps path is invoked'
204 def result = objectUnderTest.queryCmHandleAncestorsByCpsPath(cpsPath, INCLUDE_ALL_DESCENDANTS)
205 then: 'the returned result is a list of data nodes returned by cps data service'
206 assert result.contains(cmHandleDataNode)
209 def 'Get all cm handles by dmi plugin identifier and alternate id output option where #scenario'() {
210 given: 'the DataNodes queried for a given cpsPath are returned from the persistence service.'
212 when: 'cm Handles are fetched for a given dmi plugin identifier and alternate id output option'
213 def result = objectUnderTest.getCmHandleReferencesByDmiPluginIdentifier('my-dmi-plugin-identifier', outputAlternateId)
214 then: 'result is the correct size'
215 assert result.size() == 3
216 and: 'result contains the correct cm handles'
217 assert result.containsAll(expectedResult)
219 scenario | outputAlternateId || expectedResult
220 'output is for alternate ids' | true || ['alt-PNFDemo', 'alt-PNFDemo2', 'alt-PNFDemo4']
221 'output is for cm handle ids' | false || ['PNFDemo', 'PNFDemo2', 'PNFDemo4']
224 def 'Get all cm handle references when #scenario'() {
225 given: 'the query service returns all references'
227 when: 'the all cm hande references is retrieved with #scenario'
228 def result = objectUnderTest.getAllCmHandleReferences(outputAlternateId)
229 then: 'result contains all the correct cm handle references'
230 result.containsAll(expectedResult)
232 scenario | outputAlternateId || expectedResult
233 'output is alternate ids' | true || ['alt-PNFDemo', 'alt-PNFDemo2', 'alt-PNFDemo3', 'alt-PNFDemo4', 'alt-PNFDemo5']
234 'output is cm handle ids' | false || ['PNFDemo', 'PNFDemo2', 'PNFDemo3', 'PNFDemo4', 'PNFDemo5']
237 def 'Get all cm handle references by cps path'() {
238 when: 'the all cm handle references is retrieved via cps path'
239 objectUnderTest.getCmHandleReferencesByCpsPath(sampleCpsPath, outputAlternateId)
240 then: 'query service to query data leaf is called once with the correct cps path as parameter'
241 1 * mockCpsQueryService.queryDataLeaf(_, _, expectedCpsPathForQuery,_)
243 scenario | sampleCpsPath | outputAlternateId || expectedCpsPathForQuery
244 'cps path suffixes with cm-handles and outputs alternateId' | '/some/path/ending/in/cm-handles' | true || '/some/path/ending/in/cm-handles/@alternate-id'
245 'cps path suffixes without cm-handles and outputs alternateId'| '/some/path/NotEnding/incmhandles'| true || '/some/path/NotEnding/incmhandles/ancestor::cm-handles/@alternate-id'
246 'cps path suffixes with cm-handles and outputs cmHandleId' | '/some/path/ending/in/cm-handles' | false || '/some/path/ending/in/cm-handles/@id'
247 'cps path suffixes without cm-handles and outputs cmhandleId' | '/some/path/NotEnding/incmhandles'| false || '/some/path/NotEnding/incmhandles/ancestor::cm-handles/@id'
251 void mockResponses() {
253 mockCpsQueryService.queryDataLeaf(_, _, '//public-properties[@name=\'Contact\' and @value=\'newemailforstore@bookstore.com\']/ancestor::cm-handles/@id', _) >> [pnfDemo.getLeaves().get('id'), pnfDemo2.getLeaves().get('id'), pnfDemo4.getLeaves().get('id')]
254 mockCpsQueryService.queryDataLeaf(_, _, '//public-properties[@name=\'wont_match\' and @value=\'wont_match\']/ancestor::cm-handles/@id', _) >> []
255 mockCpsQueryService.queryDataLeaf(_, _, '//public-properties[@name=\'Contact2\' and @value=\'newemailforstore2@bookstore.com\']/ancestor::cm-handles/@alternate-id', _) >> [pnfDemo4.getLeaves().get('alternate-id')]
256 mockCpsQueryService.queryDataLeaf(_, _, '//public-properties[@name=\'Contact\' and @value=\'newemailforstore@bookstore.com\']/ancestor::cm-handles/@alternate-id', _) >> [pnfDemo.getLeaves().get('alternate-id'), pnfDemo2.getLeaves().get('alternate-id'), pnfDemo4.getLeaves().get('alternate-id')]
257 mockCpsQueryService.queryDataLeaf(_, _, '//public-properties[@name=\'Contact2\' and @value=\'\']/ancestor::cm-handles/@id', _) >> []
258 mockCpsQueryService.queryDataNodes(_, _, '//state[@cm-handle-state=\"READY\"]/ancestor::cm-handles', _) >> [pnfDemo, pnfDemo3]
259 mockCpsQueryService.queryDataNodes(_, _, '//state[@cm-handle-state=\"LOCKED\"]/ancestor::cm-handles', _) >> [pnfDemo2, pnfDemo4]
260 mockCpsQueryService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, '/dmi-registry/cm-handles[@dmi-service-name=\'my-dmi-plugin-identifier\']', OMIT_DESCENDANTS) >> [pnfDemo, pnfDemo2]
261 mockCpsQueryService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, '/dmi-registry/cm-handles[@dmi-data-service-name=\'my-dmi-plugin-identifier\']', OMIT_DESCENDANTS) >> [pnfDemo, pnfDemo4]
262 mockCpsQueryService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, '/dmi-registry/cm-handles[@dmi-model-service-name=\'my-dmi-plugin-identifier\']', OMIT_DESCENDANTS) >> [pnfDemo2, pnfDemo4]
265 mockCpsQueryService.queryDataLeaf(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, '/dmi-registry/cm-handles[@dmi-service-name=\'my-dmi-plugin-identifier\']/@id', _) >> [pnfDemo.getLeaves().get('id'), pnfDemo2.getLeaves().get('id')]
266 mockCpsQueryService.queryDataLeaf(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, '/dmi-registry/cm-handles[@dmi-data-service-name=\'my-dmi-plugin-identifier\']/@id', _) >> [pnfDemo.getLeaves().get('id'), pnfDemo4.getLeaves().get('id')]
267 mockCpsQueryService.queryDataLeaf(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, '/dmi-registry/cm-handles[@dmi-model-service-name=\'my-dmi-plugin-identifier\']/@id', _) >> [pnfDemo2.getLeaves().get('id'), pnfDemo4.getLeaves().get('id')]
269 mockCpsQueryService.queryDataLeaf(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, '/dmi-registry/cm-handles[@dmi-service-name=\'my-dmi-plugin-identifier\']/@alternate-id', _) >> [pnfDemo.getLeaves().get('alternate-id'), pnfDemo2.getLeaves().get('alternate-id')]
270 mockCpsQueryService.queryDataLeaf(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, '/dmi-registry/cm-handles[@dmi-data-service-name=\'my-dmi-plugin-identifier\']/@alternate-id', _) >> [pnfDemo.getLeaves().get('alternate-id'), pnfDemo4.getLeaves().get('alternate-id')]
271 mockCpsQueryService.queryDataLeaf(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, '/dmi-registry/cm-handles[@dmi-model-service-name=\'my-dmi-plugin-identifier\']/@alternate-id', _) >> [pnfDemo2.getLeaves().get('alternate-id'), pnfDemo4.getLeaves().get('alternate-id')]
272 mockCpsQueryService.queryDataLeaf(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, '/dmi-registry/cm-handles[@id=\'PNFDemo\']/@alternate-id', _) >> [pnfDemo.getLeaves().get('alternate-id')]
273 mockCpsQueryService.queryDataLeaf(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, '/dmi-registry/cm-handles[@id=\'PNFDemo2\' or @id=\'PNFDemo4\' or @id=\'PNFDemo\']/@alternate-id', _) >> [pnfDemo2.getLeaves().get('alternate-id'), pnfDemo.getLeaves().get('alternate-id'), pnfDemo4.getLeaves().get('alternate-id')]
275 mockCpsQueryService.queryDataLeaf(_, _, '/dmi-registry/cm-handles/@alternate-id', _) >> getAllCmHandleReferences(true)
276 mockCpsQueryService.queryDataLeaf(_, _, '/dmi-registry/cm-handles/@id', _) >> getAllCmHandleReferences(false)
281 def static createDataNode(dataNodeId) {
282 return new DataNode(xpath: '/dmi-registry/cm-handles[@id=\'' + dataNodeId + '\']', leaves: ['id':dataNodeId, 'alternate-id':'alt-' + dataNodeId])
285 def static getAllCmHandleReferences(outputAlternateId) {
286 def sampleNodes = [pnfDemo, pnfDemo2, pnfDemo3, pnfDemo4, pnfDemo5]
287 return sampleNodes.collect { dataNode ->
288 if (outputAlternateId) {
289 return dataNode.getLeaves().get('alternate-id')
291 return dataNode.getLeaves().get('id')