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