2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 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
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.api.impl
23 import org.onap.cps.cpspath.parser.PathParsingException
24 import org.onap.cps.ncmp.api.inventory.CmHandleQueries
25 import org.onap.cps.ncmp.api.inventory.CmHandleQueriesImpl
26 import org.onap.cps.ncmp.api.inventory.InventoryPersistence
27 import org.onap.cps.ncmp.api.models.CmHandleQueryServiceParameters
28 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle
29 import org.onap.cps.spi.FetchDescendantsOption
30 import org.onap.cps.spi.exceptions.DataInUseException
31 import org.onap.cps.spi.exceptions.DataValidationException
32 import org.onap.cps.spi.model.ConditionProperties
33 import org.onap.cps.spi.model.DataNode
34 import spock.lang.Specification
35 import java.util.stream.Collectors
37 class NetworkCmProxyCmHandlerQueryServiceSpec extends Specification {
39 def cmHandleQueries = Mock(CmHandleQueries)
40 def partiallyMockedCmHandleQueries = Spy(CmHandleQueriesImpl)
41 def mockInventoryPersistence = Mock(InventoryPersistence)
43 def static someCmHandleDataNode = new DataNode(xpath: '/dmi-registry/cm-handles[@id=\'some-cmhandle-id\']', leaves: ['id':'some-cmhandle-id'])
44 def dmiRegistry = new DataNode(xpath: '/dmi-registry', childDataNodes: createDataNodeList(['PNFDemo1', 'PNFDemo2', 'PNFDemo3', 'PNFDemo4']))
46 static def queryResultCmHandleMap = createCmHandleMap(['H1', 'H2'])
48 def objectUnderTest = new NetworkCmProxyCmHandlerQueryServiceImpl(cmHandleQueries, mockInventoryPersistence)
49 def objectUnderTestSpy = new NetworkCmProxyCmHandlerQueryServiceImpl(partiallyMockedCmHandleQueries, mockInventoryPersistence)
51 def 'Retrieve cm handles with cpsPath when combined with no Module Query.'() {
52 given: 'a cmHandleWithCpsPath condition property'
53 def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
54 def conditionProperties = createConditionProperties('cmHandleWithCpsPath', [['cpsPath' : '/some/cps/path']])
55 cmHandleQueryParameters.setCmHandleQueryParameters([conditionProperties])
56 and: 'cmHandleQueries returns a non null query result'
57 cmHandleQueries.queryCmHandleDataNodesByCpsPath('/some/cps/path', FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> [new DataNode(leaves: ['id':'some-cmhandle-id'])]
58 and: 'CmHandleQueries returns cmHandles with the relevant query result'
59 cmHandleQueries.combineCmHandleQueries(*_) >> ['PNFDemo1': new NcmpServiceCmHandle(cmHandleId: 'PNFDemo1'), 'PNFDemo3': new NcmpServiceCmHandle(cmHandleId: 'PNFDemo3')]
60 when: 'the query is executed for both cm handle ids and details'
61 def returnedCmHandlesJustIds = objectUnderTest.queryCmHandleIds(cmHandleQueryParameters)
62 def returnedCmHandlesWithData = objectUnderTest.queryCmHandles(cmHandleQueryParameters)
63 then: 'the correct expected cm handles ids are returned'
64 returnedCmHandlesJustIds == ['PNFDemo1', 'PNFDemo3'] as Set
65 and: 'the correct ncmp service cm handles are returned'
66 returnedCmHandlesWithData.stream().map(CmHandle -> CmHandle.cmHandleId).collect(Collectors.toSet()) == ['PNFDemo1', 'PNFDemo3'] as Set
69 def 'Retrieve cm handles with cpsPath where #scenario.'() {
70 given: 'a cmHandleWithCpsPath condition property'
71 def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
72 def conditionProperties = createConditionProperties('cmHandleWithCpsPath', [['cpsPath' : '/some/cps/path']])
73 cmHandleQueryParameters.setCmHandleQueryParameters([conditionProperties])
74 and: 'cmHandleQueries throws a path parsing exception'
75 cmHandleQueries.queryCmHandleDataNodesByCpsPath('/some/cps/path', FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> { throw thrownException }
76 when: 'the query is executed for both cm handle ids and details'
77 objectUnderTest.queryCmHandleIds(cmHandleQueryParameters)
78 objectUnderTest.queryCmHandles(cmHandleQueryParameters)
79 then: 'a data validation exception is thrown'
80 thrown(expectedException)
81 where: 'the following data is used'
82 scenario | thrownException || expectedException
83 'a PathParsingException is thrown' | new PathParsingException('some message', 'some details') || DataValidationException
84 'any other Exception is thrown' | new DataInUseException('some message', 'some details') || DataInUseException
87 def 'Query cm handles with public properties when combined with empty modules query result.'() {
88 given: 'a public properties condition property'
89 def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
90 def conditionProperties = createConditionProperties('hasAllProperties', [['some-property-key': 'some-property-value']])
91 cmHandleQueryParameters.setCmHandleQueryParameters([conditionProperties])
92 and: 'CmHandleQueries returns cmHandles with the relevant query result'
93 cmHandleQueries.combineCmHandleQueries(*_) >> ['PNFDemo1': new NcmpServiceCmHandle(cmHandleId: 'PNFDemo1'), 'PNFDemo3': new NcmpServiceCmHandle(cmHandleId: 'PNFDemo3')]
94 when: 'the query is executed for both cm handle ids and details'
95 def returnedCmHandlesJustIds = objectUnderTest.queryCmHandleIds(cmHandleQueryParameters)
96 def returnedCmHandlesWithData = objectUnderTest.queryCmHandles(cmHandleQueryParameters)
97 then: 'the correct expected cm handles ids are returned'
98 returnedCmHandlesJustIds == ['PNFDemo1', 'PNFDemo3'] as Set
99 and: 'the correct cm handle data objects are returned'
100 returnedCmHandlesWithData.stream().map(dataNode -> dataNode.cmHandleId).collect(Collectors.toSet()) == ['PNFDemo1', 'PNFDemo3'] as Set
103 def 'Retrieve cm handles with module names when #scenario from query.'() {
104 given: 'a modules condition property'
105 def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
106 def conditionProperties = createConditionProperties('hasAllModules', [['moduleName': 'some-module-name']])
107 cmHandleQueryParameters.setCmHandleQueryParameters([conditionProperties])
108 and: 'null is returned from the state and public property queries'
109 cmHandleQueries.combineCmHandleQueries(*_) >> null
110 and: '#scenario from the modules query'
111 mockInventoryPersistence.getCmHandleIdsWithGivenModules(*_) >> cmHandleIdsFromService
112 and: 'the same cmHandles are returned from the persistence service layer'
113 cmHandleIdsFromService.size() * mockInventoryPersistence.getDataNode(*_) >> returnedCmHandles
114 when: 'the query is executed for both cm handle ids and details'
115 def returnedCmHandlesJustIds = objectUnderTest.queryCmHandleIds(cmHandleQueryParameters)
116 def returnedCmHandlesWithData = objectUnderTest.queryCmHandles(cmHandleQueryParameters)
117 then: 'the correct expected cm handles ids are returned'
118 returnedCmHandlesJustIds == cmHandleIdsFromService as Set
119 and: 'the correct cm handle data objects are returned'
120 returnedCmHandlesWithData.stream().map(dataNode -> dataNode.cmHandleId).collect(Collectors.toSet()) == cmHandleIdsFromService as Set
121 where: 'the following data is used'
122 scenario | cmHandleIdsFromService | returnedCmHandles
123 'One anchor returned' | ['some-cmhandle-id'] | someCmHandleDataNode
124 'No anchors are returned' | [] | null
127 def 'Retrieve cm handles with combined queries when #scenario.'() {
128 given: 'all condition properties used'
129 def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
130 def conditionPubProps = createConditionProperties('hasAllProperties', [['some-property-key': 'some-property-value']])
131 def conditionModules = createConditionProperties('hasAllModules', [['moduleName': 'some-module-name']])
132 def conditionState = createConditionProperties('cmHandleWithCpsPath', [['cpsPath' : '/some/cps/path']])
133 cmHandleQueryParameters.setCmHandleQueryParameters([conditionPubProps, conditionModules, conditionState])
134 and: 'cmHandles are returned from the state and public property combined queries'
135 cmHandleQueries.combineCmHandleQueries(*_) >> combinedQueryMap
136 and: 'cmHandles are returned from the module names query'
137 mockInventoryPersistence.getCmHandleIdsWithGivenModules(['some-module-name']) >> anchorsForModuleQuery
138 and: 'cmHandleQueries returns a datanode result'
139 2 * cmHandleQueries.queryCmHandleDataNodesByCpsPath(*_) >> [someCmHandleDataNode]
140 when: 'the query is executed for both cm handle ids and details'
141 def returnedCmHandlesJustIds = objectUnderTest.queryCmHandleIds(cmHandleQueryParameters)
142 def returnedCmHandlesWithData = objectUnderTest.queryCmHandles(cmHandleQueryParameters)
143 then: 'the correct expected cm handles ids are returned'
144 returnedCmHandlesJustIds == expectedCmHandleIds as Set
145 and: 'the correct cm handle data objects are returned'
146 returnedCmHandlesWithData.stream().map(dataNode -> dataNode.cmHandleId).collect(Collectors.toSet()) == expectedCmHandleIds as Set
147 where: 'the following data is used'
148 scenario | combinedQueryMap | anchorsForModuleQuery || expectedCmHandleIds
149 'combined and modules queries intersect' | ['PNFDemo1': new NcmpServiceCmHandle(cmHandleId: 'PNFDemo1')] | ['PNFDemo1', 'PNFDemo2'] || ['PNFDemo1']
150 'only module query results exist' | [:] | ['PNFDemo1', 'PNFDemo2'] || []
151 'only combined query results exist' | ['PNFDemo1': new NcmpServiceCmHandle(cmHandleId: 'PNFDemo1'), 'PNFDemo2': new NcmpServiceCmHandle(cmHandleId: 'PNFDemo2')] | [] || []
152 'neither queries return results' | [:] | [] || []
153 'none intersect' | ['PNFDemo1': new NcmpServiceCmHandle(cmHandleId: 'PNFDemo1')] | ['PNFDemo2'] || []
156 def 'Retrieve cm handles when the query is empty.'() {
157 given: 'We use an empty query'
158 def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
159 and: 'the inventory persistence returns the dmi registry datanode with just ids'
160 mockInventoryPersistence.getDataNode("/dmi-registry", FetchDescendantsOption.FETCH_DIRECT_CHILDREN_ONLY) >> dmiRegistry
161 and: 'the inventory persistence returns the dmi registry datanode with data'
162 mockInventoryPersistence.getDataNode("/dmi-registry") >> dmiRegistry
163 when: 'the query is executed for both cm handle ids and details'
164 def returnedCmHandlesJustIds = objectUnderTest.queryCmHandleIds(cmHandleQueryParameters)
165 def returnedCmHandlesWithData = objectUnderTest.queryCmHandles(cmHandleQueryParameters)
166 then: 'the correct expected cm handles are returned'
167 returnedCmHandlesJustIds == ['PNFDemo1', 'PNFDemo2', 'PNFDemo3', 'PNFDemo4'] as Set
168 returnedCmHandlesWithData.stream().map(d -> d.cmHandleId).collect(Collectors.toSet()) == ['PNFDemo1', 'PNFDemo2', 'PNFDemo3', 'PNFDemo4'] as Set
172 def 'Retrieve all CMHandleIds for empty query parameters' () {
173 given: 'We query without any parameters'
174 def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
175 and: 'the inventoryPersistence returns all four CmHandleIds'
176 mockInventoryPersistence.getDataNode(*_) >> dmiRegistry
177 when: 'the query executed'
178 def resultSet = objectUnderTest.queryCmHandleIdsForInventory(cmHandleQueryParameters)
179 then: 'the size of the result list equals the size of all cmHandleIds.'
180 resultSet.size() == 4
183 def 'Retrieve CMHandleIds when #scenario.' () {
184 given: 'a query object created with #condition'
185 def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
186 def conditionProperties = createConditionProperties(conditionName, [['some-key': 'some-value']])
187 cmHandleQueryParameters.setCmHandleQueryParameters([conditionProperties])
188 and: 'the inventoryPersistence returns different CmHandleIds'
189 partiallyMockedCmHandleQueries.queryCmHandlePublicProperties(*_) >> cmHandlesWithMatchingPublicProperties
190 partiallyMockedCmHandleQueries.queryCmHandleAdditionalProperties(*_) >> cmHandlesWithMatchingPrivateProperties
191 when: 'the query executed'
192 def result = objectUnderTestSpy.queryCmHandleIdsForInventory(cmHandleQueryParameters)
193 then: 'the expected number of results are returned.'
194 assert result.size() == expectedCmHandleIdsSize
195 where: 'the following data is used'
196 scenario | conditionName | cmHandlesWithMatchingPublicProperties | cmHandlesWithMatchingPrivateProperties || expectedCmHandleIdsSize
197 'all properties, only public matching' | 'hasAllProperties' | queryResultCmHandleMap | null || 2
198 'all properties, no matching cm handles' | 'hasAllProperties' | [:] | [:] || 0
199 'additional properties, some matching cm handles' | 'hasAllAdditionalProperties' | [:] | queryResultCmHandleMap || 2
200 'additional properties, no matching cm handles' | 'hasAllAdditionalProperties' | null | [:] || 0
203 def 'Retrieve CMHandleIds by different DMI properties with #scenario.' () {
204 given: 'a query object created with dmi plugin as condition'
205 def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
206 def conditionProperties = createConditionProperties('cmHandleWithDmiPlugin', [['some-key': 'some-value']])
207 cmHandleQueryParameters.setCmHandleQueryParameters([conditionProperties])
208 and: 'the inventoryPersistence returns different CmHandleIds'
209 partiallyMockedCmHandleQueries.getCmHandlesByDmiPluginIdentifier(*_) >> cmHandleQueryResult
210 when: 'the query executed'
211 def result = objectUnderTestSpy.queryCmHandleIdsForInventory(cmHandleQueryParameters)
212 then: 'the expected number of results are returned.'
213 assert result.size() == expectedCmHandleIdsSize
214 where: 'the following data is used'
215 scenario | cmHandleQueryResult || expectedCmHandleIdsSize
216 'some matches' | queryResultCmHandleMap.values() || 2
217 'no matches' | [] || 0
220 static def createCmHandleMap(cmHandleIds) {
221 def cmHandleMap = [:]
222 cmHandleIds.each{ cmHandleMap[it] = new NcmpServiceCmHandle(cmHandleId : it) }
226 def createConditionProperties(String conditionName, List<Map<String, String>> conditionParameters) {
227 return new ConditionProperties(conditionName : conditionName, conditionParameters : conditionParameters)
230 def static createDataNodeList(dataNodeIds) {
232 dataNodeIds.each{ dataNodes << new DataNode(xpath: "/dmi-registry/cm-handles[@id='${it}']", leaves: ['id':it]) }