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