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