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