013bace04d058e125bce7e7eafd26b097cfc2322
[cps.git] /
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2022-2023 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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.impl.inventory
22
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.spi.FetchDescendantsOption
28 import org.onap.cps.spi.exceptions.DataInUseException
29 import org.onap.cps.spi.exceptions.DataValidationException
30 import org.onap.cps.spi.model.ConditionProperties
31 import org.onap.cps.spi.model.DataNode
32 import spock.lang.Specification
33
34 import static org.onap.cps.ncmp.impl.inventory.NcmpPersistence.NCMP_DMI_REGISTRY_PARENT
35
36 class ParameterizedCmHandleQueryServiceSpec extends Specification {
37
38     def cmHandleQueries = Mock(CmHandleQueryService)
39     def partiallyMockedCmHandleQueries = Spy(CmHandleQueryService)
40     def mockInventoryPersistence = Mock(InventoryPersistence)
41
42     def dmiRegistry = new DataNode(xpath: NCMP_DMI_REGISTRY_PARENT, childDataNodes: createDataNodeList(['PNFDemo1', 'PNFDemo2', 'PNFDemo3', 'PNFDemo4']))
43
44     def objectUnderTest = new ParameterizedCmHandleQueryServiceImpl(cmHandleQueries, mockInventoryPersistence)
45     def objectUnderTestWithPartiallyMockedQueries = new ParameterizedCmHandleQueryServiceImpl(partiallyMockedCmHandleQueries, mockInventoryPersistence)
46
47     def 'Query cm handle ids with cpsPath.'() {
48         given: 'a cmHandleWithCpsPath condition property'
49             def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
50             def conditionProperties = createConditionProperties('cmHandleWithCpsPath', [['cpsPath' : '/some/cps/path']])
51             cmHandleQueryParameters.setCmHandleQueryParameters([conditionProperties])
52         and: 'the query get the cm handle datanodes excluding all descendants returns a datanode'
53             cmHandleQueries.queryCmHandleAncestorsByCpsPath('/some/cps/path', FetchDescendantsOption.OMIT_DESCENDANTS) >> [new DataNode(leaves: ['id':'some-cmhandle-id'])]
54         when: 'the query is executed for cm handle ids'
55             def result = objectUnderTest.queryCmHandleIds(cmHandleQueryParameters)
56         then: 'the correct expected cm handles ids are returned'
57             assert result == ['some-cmhandle-id'] as Set
58     }
59
60     def 'Query cm handle where  cps path itself is ancestor axis.'() {
61         given: 'a cmHandleWithCpsPath condition property'
62             def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
63             def conditionProperties = createConditionProperties('cmHandleWithCpsPath', [['cpsPath' : '/some/cps/path']])
64             cmHandleQueryParameters.setCmHandleQueryParameters([conditionProperties])
65         and: 'the query get the cm handle data nodes excluding all descendants returns a datanode'
66             cmHandleQueries.queryCmHandleAncestorsByCpsPath('/some/cps/path', FetchDescendantsOption.OMIT_DESCENDANTS) >> [new DataNode(leaves: ['id':'some-cmhandle-id'])]
67         when: 'the query is executed for cm handle ids'
68             def result = objectUnderTest.queryCmHandleIdsForInventory(cmHandleQueryParameters)
69         then: 'the correct expected cm handles ids are returned'
70             assert result == ['some-cmhandle-id'] as Set
71     }
72
73     def 'Cm handle ids query with error: #scenario.'() {
74         given: 'a cmHandleWithCpsPath condition property'
75             def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
76             def conditionProperties = createConditionProperties('cmHandleWithCpsPath', [['cpsPath' : '/some/cps/path']])
77             cmHandleQueryParameters.setCmHandleQueryParameters([conditionProperties])
78         and: 'cmHandleQueries throws a path parsing exception'
79             cmHandleQueries.queryCmHandleAncestorsByCpsPath('/some/cps/path', FetchDescendantsOption.OMIT_DESCENDANTS) >> { throw thrownException }
80         when: 'the query is executed for cm handle ids'
81             objectUnderTest.queryCmHandleIds(cmHandleQueryParameters)
82         then: 'a data validation exception is thrown'
83             thrown(expectedException)
84         where: 'the following data is used'
85             scenario               | thrownException                                          || expectedException
86             'PathParsingException' | new PathParsingException('some message', 'some details') || DataValidationException
87             'any other Exception'  | new DataInUseException('some message', 'some details')   || DataInUseException
88     }
89
90     def 'Cm handle ids cpsPath query for private properties (not allowed).'() {
91         given: 'a CpsPath condition property for private properties'
92             def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
93             def conditionProperties = createConditionProperties('cmHandleWithCpsPath', [['cpsPath' : '/additional-properties']])
94             cmHandleQueryParameters.setCmHandleQueryParameters([conditionProperties])
95         when: 'the query is executed for cm handle ids'
96             def result = objectUnderTest.queryCmHandleIds(cmHandleQueryParameters)
97         then: 'empty result is returned'
98             assert result.isEmpty()
99     }
100
101     def 'Query cm handle ids with module names when #scenario from query.'() {
102         given: 'a modules condition property'
103             def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
104             def conditionProperties = createConditionProperties('hasAllModules', [['moduleName': 'some-module-name']])
105             cmHandleQueryParameters.setCmHandleQueryParameters([conditionProperties])
106         when: 'the query is executed for cm handle ids'
107             def result = objectUnderTest.queryCmHandleIds(cmHandleQueryParameters)
108         then: 'the inventory service is called with the correct module names'
109             1 * mockInventoryPersistence.getCmHandleIdsWithGivenModules(['some-module-name']) >> cmHandleIdsFromService
110         and: 'the correct expected cm handles ids are returned'
111             assert result.size() == cmHandleIdsFromService.size()
112             assert result.containsAll(cmHandleIdsFromService)
113         where: 'the following data is used'
114             scenario                  | cmHandleIdsFromService
115             'One anchor returned'     | ['some-cmhandle-id']
116             'No anchors are returned' | []
117     }
118
119     def 'Query cm handles with some trust level query parameters'() {
120         given: 'a trust level condition property'
121             def trustLevelQueryParameters = new CmHandleQueryServiceParameters()
122             def trustLevelConditionProperties = createConditionProperties('cmHandleWithTrustLevel', [['trustLevel': 'COMPLETE'] as Map])
123             trustLevelQueryParameters.setCmHandleQueryParameters([trustLevelConditionProperties])
124         when: 'the query is being executed'
125             objectUnderTest.queryCmHandleIds(trustLevelQueryParameters)
126         then: 'the query is being delegated to the cm handle query service with correct parameter'
127             1 * cmHandleQueries.queryCmHandlesByTrustLevel(['trustLevel': 'COMPLETE'] as Map)
128     }
129
130     def 'Query cm handle details with module names when #scenario from query.'() {
131         given: 'a modules condition property'
132             def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
133             def conditionProperties = createConditionProperties('hasAllModules', [['moduleName': 'some-module-name']])
134             cmHandleQueryParameters.setCmHandleQueryParameters([conditionProperties])
135         when: 'the query is executed for cm handle ids'
136             def result = objectUnderTest.queryCmHandles(cmHandleQueryParameters)
137         then: 'the inventory service is called with the correct module names'
138             1 * mockInventoryPersistence.getCmHandleIdsWithGivenModules(['some-module-name']) >> ['ch1']
139         and: 'the inventory service is called with teh correct if and returns a yang model cm handle'
140             1 * mockInventoryPersistence.getYangModelCmHandles(['ch1']) >>
141                 [new YangModelCmHandle(id: 'abc', dmiProperties: [new YangModelCmHandle.Property('name','value')], publicProperties: [])]
142         and: 'the expected cm handle(s) are returned as NCMP Service cm handles'
143             assert result[0] instanceof NcmpServiceCmHandle
144             assert result.size() == 1
145             assert result[0].dmiProperties == [name:'value']
146     }
147
148     def 'Query cm handle ids when the query is empty.'() {
149         given: 'We use an empty query'
150             def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
151         and: 'the inventory persistence returns the dmi registry datanode with just ids'
152             mockInventoryPersistence.getDataNode(NCMP_DMI_REGISTRY_PARENT, FetchDescendantsOption.DIRECT_CHILDREN_ONLY) >> [dmiRegistry]
153         when: 'the query is executed for both cm handle ids'
154             def result = objectUnderTest.queryCmHandleIds(cmHandleQueryParameters)
155         then: 'the correct expected cm handles are returned'
156             assert result.containsAll('PNFDemo1', 'PNFDemo2', 'PNFDemo3', 'PNFDemo4')
157     }
158
159     def 'Query cm handle details when the query is empty.'() {
160         given: 'We use an empty query'
161             def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
162         and: 'the inventory persistence returns the dmi registry datanode with just ids'
163             mockInventoryPersistence.getDataNode(NCMP_DMI_REGISTRY_PARENT) >> [dmiRegistry]
164         when: 'the query is executed for both cm handle details'
165             def result = objectUnderTest.queryCmHandles(cmHandleQueryParameters)
166         then: 'the correct cm handles are returned'
167             assert result.size() == 4
168             assert result.cmHandleId.containsAll('PNFDemo1', 'PNFDemo2', 'PNFDemo3', 'PNFDemo4')
169     }
170
171     def 'Query CMHandleId with #scenario.' () {
172         given: 'a query object created with #condition'
173             def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
174             def conditionProperties = createConditionProperties(conditionName, [['some-key': 'some-value']])
175             cmHandleQueryParameters.setCmHandleQueryParameters([conditionProperties])
176         and: 'the inventoryPersistence returns different CmHandleIds'
177             partiallyMockedCmHandleQueries.queryCmHandlePublicProperties(*_) >> cmHandlesWithMatchingPublicProperties
178             partiallyMockedCmHandleQueries.queryCmHandleAdditionalProperties(*_) >> cmHandlesWithMatchingPrivateProperties
179         when: 'the query executed'
180             def result = objectUnderTestWithPartiallyMockedQueries.queryCmHandleIdsForInventory(cmHandleQueryParameters)
181         then: 'the expected number of results are returned.'
182             assert result.size() == expectedCmHandleIdsSize
183         where: 'the following data is used'
184             scenario                                          | conditionName                | cmHandlesWithMatchingPublicProperties | cmHandlesWithMatchingPrivateProperties || expectedCmHandleIdsSize
185             'all properties, only public matching'            | 'hasAllProperties'           | ['h1', 'h2']                          | null                                   || 2
186             'all properties, no matching cm handles'          | 'hasAllProperties'           | []                                    | []                                     || 0
187             'additional properties, some matching cm handles' | 'hasAllAdditionalProperties' | []                                    | ['h1', 'h2']                           || 2
188             'additional properties, no matching cm handles'   | 'hasAllAdditionalProperties' | null                                  | []                                     || 0
189     }
190
191     def 'Retrieve CMHandleIds by different DMI properties with #scenario.' () {
192         given: 'a query object created with dmi plugin as condition'
193             def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
194             def conditionProperties = createConditionProperties('cmHandleWithDmiPlugin', [['some-key': 'some-value']])
195             cmHandleQueryParameters.setCmHandleQueryParameters([conditionProperties])
196         and: 'the inventoryPersistence returns different CmHandleIds'
197             partiallyMockedCmHandleQueries.getCmHandleIdsByDmiPluginIdentifier(*_) >> cmHandleQueryResult
198         when: 'the query executed'
199             def result = objectUnderTestWithPartiallyMockedQueries.queryCmHandleIdsForInventory(cmHandleQueryParameters)
200         then: 'the expected number of results are returned.'
201             assert result.size() == expectedCmHandleIdsSize
202         where: 'the following data is used'
203             scenario       | cmHandleQueryResult || expectedCmHandleIdsSize
204             'some matches' | ['h1','h2']         || 2
205             'no matches'   | []                  || 0
206     }
207
208     def 'Combine two query results where #scenario.'() {
209         when: 'two query results in the form of a map of NcmpServiceCmHandles are combined into a single query result'
210             def result = objectUnderTest.combineCmHandleQueryResults(firstQuery, secondQuery)
211         then: 'the returned result is the same as the expected result'
212             result == expectedResult
213         where:
214             scenario                                                     | firstQuery              | secondQuery             || expectedResult
215             'two queries with unique and non unique entries exist'       | ['PNFDemo', 'PNFDemo2'] | ['PNFDemo', 'PNFDemo3'] || ['PNFDemo']
216             'the first query contains entries and second query is empty' | ['PNFDemo', 'PNFDemo2'] | []                      || []
217             'the second query contains entries and first query is empty' | []                      | ['PNFDemo', 'PNFDemo3'] || []
218             'the first query contains entries and second query is null'  | ['PNFDemo', 'PNFDemo2'] | null                    || ['PNFDemo', 'PNFDemo2']
219             'the second query contains entries and first query is null'  | null                    | ['PNFDemo', 'PNFDemo3'] || ['PNFDemo', 'PNFDemo3']
220             'both queries are empty'                                     | []                      | []                      || []
221             'both queries are null'                                      | null                    | null                    || null
222     }
223
224     def createConditionProperties(String conditionName, List<Map<String, String>> conditionParameters) {
225         return new ConditionProperties(conditionName : conditionName, conditionParameters : conditionParameters)
226     }
227
228     def static createDataNodeList(dataNodeIds) {
229         def dataNodes =[]
230         dataNodeIds.each{ dataNodes << new DataNode(xpath: "/dmi-registry/cm-handles[@id='${it}']", leaves: ['id':it]) }
231         return dataNodes
232     }
233 }