2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022-2025 OpenInfra Foundation Europe. All rights reserved.
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
9 * 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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.ncmp.impl.inventory
23 import org.onap.cps.api.exceptions.DataInUseException
24 import org.onap.cps.api.exceptions.DataValidationException
25 import org.onap.cps.api.model.ConditionProperties
26 import org.onap.cps.api.model.DataNode
27 import org.onap.cps.cpspath.parser.PathParsingException
28 import org.onap.cps.ncmp.api.inventory.models.CmHandleQueryServiceParameters
29 import org.onap.cps.ncmp.api.inventory.models.NcmpServiceCmHandle
30 import org.onap.cps.ncmp.impl.inventory.models.YangModelCmHandle
31 import org.onap.cps.ncmp.impl.inventory.trustlevel.TrustLevelManager
32 import spock.lang.Specification
34 import static org.onap.cps.ncmp.impl.inventory.NcmpPersistence.NCMP_DMI_REGISTRY_PARENT
36 class ParameterizedCmHandleQueryServiceSpec extends Specification {
38 def cmHandleQueries = Mock(CmHandleQueryService)
39 def partiallyMockedCmHandleQueries = Spy(CmHandleQueryService)
40 def mockInventoryPersistence = Mock(InventoryPersistence)
41 def mockTrustLevelManager = Mock(TrustLevelManager)
43 def dmiRegistry = new DataNode(xpath: NCMP_DMI_REGISTRY_PARENT, childDataNodes: createDataNodeList(['PNFDemo1', 'PNFDemo2', 'PNFDemo3', 'PNFDemo4']))
45 def objectUnderTest = new ParameterizedCmHandleQueryServiceImpl(cmHandleQueries, mockInventoryPersistence, mockTrustLevelManager)
46 def objectUnderTestWithPartiallyMockedQueries = new ParameterizedCmHandleQueryServiceImpl(partiallyMockedCmHandleQueries, mockInventoryPersistence, mockTrustLevelManager)
48 def 'Query cm handle ids with cpsPath.'() {
49 given: 'a cmHandleWithCpsPath condition property'
50 def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
51 def conditionProperties = createConditionProperties('cmHandleWithCpsPath', [['cpsPath' : '/some/cps/path']])
52 cmHandleQueryParameters.setCmHandleQueryParameters([conditionProperties])
53 and: 'the query get the cm handle references'
54 cmHandleQueries.getCmHandleReferencesByCpsPath('/some/cps/path', outputAlternateId) >> cmHandleReferences.asCollection()
55 when: 'the query is executed for cm handle ids'
56 def result = objectUnderTest.queryCmHandleReferenceIds(cmHandleQueryParameters, outputAlternateId)
57 then: 'the correct expected cm handles ids are returned'
58 assert result == expectedCmhandleReference
59 where: 'the following data is used'
60 senario | outputAlternateId | cmHandleReferences || expectedCmhandleReference
61 'output CmHandle Ids' | false | ['some-cmhandle-id'] as Set || ['some-cmhandle-id'] as Set
62 'output Alternate Ids' | true | ['some-alternate-id'] as Set || ['some-alternate-id'] as Set
65 def 'Query cm handle where cps path itself is ancestor axis.'() {
66 given: 'a cmHandleWithCpsPath condition property'
67 def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
68 def conditionProperties = createConditionProperties('cmHandleWithCpsPath', [['cpsPath' : '/some/cps/path']])
69 cmHandleQueryParameters.setCmHandleQueryParameters([conditionProperties])
70 and: 'the query get the cm handle references'
71 cmHandleQueries.getCmHandleReferencesByCpsPath('/some/cps/path', outputAlternateId) >> cmHandleReferences.asCollection()
72 when: 'the query is executed for cm handle ids'
73 def result = objectUnderTest.queryCmHandleIdsForInventory(cmHandleQueryParameters, outputAlternateId)
74 then: 'the correct expected cm handles ids are returned'
75 assert result == expectedCmhandleReference
76 where: 'the following data is used'
77 senario | outputAlternateId | cmHandleReferences || expectedCmhandleReference
78 'outputAlternate is false' | false | ['some-cmhandle-id'] as Set || ['some-cmhandle-id'] as Set
79 'outputAlternate is true' | true | ['some-alternate-id'] as Set|| ['some-alternate-id'] as Set
82 def 'Cm handle ids query with error: #scenario.'() {
83 given: 'a cmHandleWithCpsPath condition property'
84 def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
85 def conditionProperties = createConditionProperties('cmHandleWithCpsPath', [['cpsPath' : '/some/cps/path']])
86 cmHandleQueryParameters.setCmHandleQueryParameters([conditionProperties])
87 and: 'cmHandleQueries throws a path parsing exception'
88 cmHandleQueries.getCmHandleReferencesByCpsPath('/some/cps/path', _) >> { throw thrownException }
89 when: 'the query is executed for cm handle ids'
90 objectUnderTest.queryCmHandleReferenceIds(cmHandleQueryParameters, false)
91 then: 'a data validation exception is thrown'
92 thrown(expectedException)
93 where: 'the following data is used'
94 scenario | thrownException || expectedException
95 'PathParsingException' | new PathParsingException('some message', 'some details') || DataValidationException
96 'any other Exception' | new DataInUseException('some message', 'some details') || DataInUseException
99 def 'Cm handle ids cpsPath query for private properties (not allowed).'() {
100 given: 'a CpsPath condition property for private properties'
101 def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
102 def conditionProperties = createConditionProperties('cmHandleWithCpsPath', [['cpsPath' : '/additional-properties']])
103 cmHandleQueryParameters.setCmHandleQueryParameters([conditionProperties])
104 when: 'the query is executed for cm handle ids'
105 def result = objectUnderTest.queryCmHandleReferenceIds(cmHandleQueryParameters, false)
106 then: 'empty result is returned'
107 assert result.isEmpty()
110 def 'Query cm handle ids with module names when #scenario from query.'() {
111 given: 'a modules condition property'
112 def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
113 def conditionProperties = createConditionProperties('hasAllModules', [['moduleName': 'some-module-name']])
114 cmHandleQueryParameters.setCmHandleQueryParameters([conditionProperties])
115 when: 'the query is executed for cm handle ids'
116 def result = objectUnderTest.queryCmHandleReferenceIds(cmHandleQueryParameters, false)
117 then: 'the inventory service is called with the correct module names'
118 1 * mockInventoryPersistence.getCmHandleReferencesWithGivenModules(['some-module-name'], false) >> cmHandleIdsFromService
119 and: 'the correct expected cm handles ids are returned'
120 assert result.size() == cmHandleIdsFromService.size()
121 assert result.containsAll(cmHandleIdsFromService)
122 where: 'the following data is used'
123 scenario | cmHandleIdsFromService
124 'One anchor returned' | ['some-cmhandle-id']
125 'No anchors are returned' | []
128 def 'Query cm handles with some trust level query parameters'() {
129 given: 'a trust level condition property'
130 def trustLevelQueryParameters = new CmHandleQueryServiceParameters()
131 def trustLevelConditionProperties = createConditionProperties('cmHandleWithTrustLevel', [['trustLevel': 'COMPLETE'] as Map])
132 trustLevelQueryParameters.setCmHandleQueryParameters([trustLevelConditionProperties])
133 when: 'the query is being executed'
134 objectUnderTest.queryCmHandleReferenceIds(trustLevelQueryParameters, false)
135 then: 'the query is being delegated to the cm handle query service with correct parameter'
136 1 * cmHandleQueries.queryCmHandlesByTrustLevel(['trustLevel': 'COMPLETE'] as Map, false)
139 def 'Query cm handle details with module names when #scenario from query.'() {
140 given: 'a modules condition property'
141 def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
142 def conditionProperties = createConditionProperties('hasAllModules', [['moduleName': 'some-module-name']])
143 cmHandleQueryParameters.setCmHandleQueryParameters([conditionProperties])
144 when: 'the query is executed for cm handle ids'
145 def result = objectUnderTest.queryCmHandles(cmHandleQueryParameters).collectList().block()
146 then: 'the inventory service is called with the correct module names'
147 1 * mockInventoryPersistence.getCmHandleReferencesWithGivenModules(['some-module-name'], false) >> ['ch1']
148 and: 'the inventory service is called with teh correct if and returns a yang model cm handle'
149 1 * mockInventoryPersistence.getYangModelCmHandles(['ch1']) >>
150 [new YangModelCmHandle(id: 'abc', dmiProperties: [new YangModelCmHandle.Property('name','value')], publicProperties: [])]
151 and: 'the expected cm handle(s) are returned as NCMP Service cm handles'
152 assert result[0] instanceof NcmpServiceCmHandle
153 assert result.size() == 1
154 assert result[0].dmiProperties == [name:'value']
157 def 'Query cm handle references when the query is empty.'() {
158 given: 'We use an empty query'
159 def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
160 and: 'the inventory persistence returns the dmi registry datanode with just cm handle references'
161 cmHandleQueries.getAllCmHandleReferences(outputAlternateId) >> getCmHandleReferencesForDmiRegistry(outputAlternateId)
162 when: 'the query is executed for both cm handle ids'
163 def result = objectUnderTest.queryCmHandleReferenceIds(cmHandleQueryParameters, outputAlternateId)
164 then: 'the correct expected cm handles are returned'
165 assert result.containsAll(expectedCmhandleReferences)
166 where: 'the following data is used'
167 senario | outputAlternateId || expectedCmhandleReferences
168 'outputAlternate is false' | false || ['PNFDemo1', 'PNFDemo2', 'PNFDemo3', 'PNFDemo4']
169 'outputAlternate is true' | true || ['alt-PNFDemo1', 'alt-PNFDemo2', 'alt-PNFDemo3', 'alt-PNFDemo4']
172 def 'Query cm handle details when the query is empty.'() {
173 given: 'We use an empty query'
174 def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
175 and: 'the inventory persistence returns the cm handle ids of all cm handles'
176 cmHandleQueries.getAllCmHandleReferences(false) >> getCmHandleReferencesForDmiRegistry(false)
177 and: 'the inventory persistence returns the cm handle details when requested'
178 mockInventoryPersistence.getYangModelCmHandles(_) >> dmiRegistry.childDataNodes.collect { new YangModelCmHandle(id: it.leaves.get("id").toString(), dmiProperties: [], publicProperties: []) }
179 when: 'the query is executed for both cm handle details'
180 def result = objectUnderTest.queryCmHandles(cmHandleQueryParameters).collectList().block()
181 then: 'the correct cm handles are returned'
182 assert result.size() == 4
183 assert result.cmHandleId.containsAll('PNFDemo1', 'PNFDemo2', 'PNFDemo3', 'PNFDemo4')
186 def 'Query CMHandleId with #scenario.' () {
187 given: 'a query object created with #condition'
188 def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
189 def conditionProperties = createConditionProperties(conditionName, [['some-key': 'some-value']])
190 cmHandleQueryParameters.setCmHandleQueryParameters([conditionProperties])
191 and: 'the inventoryPersistence returns different CmHandleIds'
192 partiallyMockedCmHandleQueries.queryCmHandlePublicProperties(*_) >> cmHandlesWithMatchingPublicProperties
193 partiallyMockedCmHandleQueries.queryCmHandleAdditionalProperties(*_) >> cmHandlesWithMatchingPrivateProperties
194 when: 'the query executed'
195 def result = objectUnderTestWithPartiallyMockedQueries.queryCmHandleIdsForInventory(cmHandleQueryParameters, false)
196 then: 'the expected number of results are returned.'
197 assert result.size() == expectedCmHandleIdsSize
198 where: 'the following data is used'
199 scenario | conditionName | cmHandlesWithMatchingPublicProperties | cmHandlesWithMatchingPrivateProperties || expectedCmHandleIdsSize
200 'all properties, only public matching' | 'hasAllProperties' | ['h1', 'h2'] | null || 2
201 'all properties, no matching cm handles' | 'hasAllProperties' | [] | [] || 0
202 'additional properties, some matching cm handles' | 'hasAllAdditionalProperties' | [] | ['h1', 'h2'] || 2
203 'additional properties, no matching cm handles' | 'hasAllAdditionalProperties' | null | [] || 0
206 def 'Retrieve alternate ids by different DMI properties.' () {
207 given: 'a query object created with dmi plugin as condition'
208 def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
209 def conditionProperties = createConditionProperties('cmHandleWithDmiPlugin', [['some-key': 'some-value']])
210 cmHandleQueryParameters.setCmHandleQueryParameters([conditionProperties])
211 and: 'the inventoryPersistence returns different CmHandleIds'
212 partiallyMockedCmHandleQueries.getCmHandleReferencesByDmiPluginIdentifier(_,_) >> []
213 when: 'the query executed'
214 def result = objectUnderTestWithPartiallyMockedQueries.queryCmHandleIdsForInventory(cmHandleQueryParameters, true)
215 then: 'the expected number of results are returned.'
216 assert result.size() == 0
219 def 'Retrieve cm handle ids by different DMI properties.' () {
220 given: 'a query object created with dmi plugin as condition'
221 def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
222 def conditionProperties = createConditionProperties('cmHandleWithDmiPlugin', [['some-key': 'some-value']])
223 cmHandleQueryParameters.setCmHandleQueryParameters([conditionProperties])
224 and: 'the inventoryPersistence returns different CmHandleIds'
225 partiallyMockedCmHandleQueries.getCmHandleReferencesByDmiPluginIdentifier(_, _) >> ['h1','h2']
226 when: 'the query executed'
227 def result = objectUnderTestWithPartiallyMockedQueries.queryCmHandleIdsForInventory(cmHandleQueryParameters, false)
228 then: 'the expected number of results are returned.'
229 assert result.size() == 2
233 def 'Combine two query results where #scenario.'() {
234 when: 'two query results in the form of a map of NcmpServiceCmHandles are combined into a single query result'
235 def result = objectUnderTest.combineCmHandleQueryResults(firstQuery, secondQuery)
236 then: 'the returned result is the same as the expected result'
237 result == expectedResult
239 scenario | firstQuery | secondQuery || expectedResult
240 'two queries with unique and non unique entries exist' | ['PNFDemo', 'PNFDemo2'] | ['PNFDemo', 'PNFDemo3'] || ['PNFDemo']
241 'the first query contains entries and second query is empty' | ['PNFDemo', 'PNFDemo2'] | [] || []
242 'the second query contains entries and first query is empty' | [] | ['PNFDemo', 'PNFDemo3'] || []
243 'the first query contains entries and second query is null' | ['PNFDemo', 'PNFDemo2'] | null || ['PNFDemo', 'PNFDemo2']
244 'the second query contains entries and first query is null' | null | ['PNFDemo', 'PNFDemo3'] || ['PNFDemo', 'PNFDemo3']
245 'both queries are empty' | [] | [] || []
246 'both queries are null' | null | null || null
249 def 'Query CM handle details by DMI service name.'() {
250 given: 'query parameters with the cmHandleWithDmiPlugin condition'
251 def queryParams = new CmHandleQueryServiceParameters(
252 cmHandleQueryParameters: [
253 createConditionProperties('cmHandleWithDmiPlugin', [['some-key': 'some-value']])
256 and: 'the query service returns a matching cm handle id'
257 def expectedCmHandleId = 'cm-handle from query service'
258 partiallyMockedCmHandleQueries.getCmHandleReferencesByDmiPluginIdentifier(_, false) >> [expectedCmHandleId]
259 and: 'the inventory persistence returns the matching cm handle object'
260 mockInventoryPersistence.getYangModelCmHandles([expectedCmHandleId]) >> [
261 new YangModelCmHandle(
262 id: expectedCmHandleId,
263 dmiProperties: [new YangModelCmHandle.Property('name', 'value')],
267 when: 'the query is executed'
268 def result = objectUnderTestWithPartiallyMockedQueries.queryInventoryForCmHandles(queryParams).collectList().block()
269 then: 'the result contains the correct cm handle id'
270 assert result.size() == 1
271 assert result[0].cmHandleId == 'cm-handle from query service'
274 def createConditionProperties(String conditionName, List<Map<String, String>> conditionParameters) {
275 return new ConditionProperties(conditionName : conditionName, conditionParameters : conditionParameters)
278 def static createDataNodeList(dataNodeIds) {
280 dataNodeIds.each{ dataNodes << new DataNode(xpath: "/dmi-registry/cm-handles[@id='${it}']", leaves: ['id':it, 'alternate-id':'alt-' + it]) }
284 def getCmHandleReferencesForDmiRegistry(outputAlternateId) {
285 def cmHandles = dmiRegistry.childDataNodes ?: []
286 def cmHandleReferences = []
287 def attributeName = outputAlternateId ? 'alternate-id' : 'id'
288 cmHandles.each { cmHandle ->
289 cmHandleReferences.add(cmHandle.leaves.get(attributeName))
291 return cmHandleReferences