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