2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022-2023 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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.ncmp.api.impl
24 import org.onap.cps.cpspath.parser.PathParsingException
25 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
26 import org.onap.cps.ncmp.api.inventory.CmHandleQueries
27 import org.onap.cps.ncmp.api.inventory.CmHandleQueriesImpl
28 import org.onap.cps.ncmp.api.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
38 class NetworkCmProxyCmHandleQueryServiceSpec extends Specification {
40 def cmHandleQueries = Mock(CmHandleQueries)
41 def partiallyMockedCmHandleQueries = Spy(CmHandleQueriesImpl)
42 def mockInventoryPersistence = Mock(InventoryPersistence)
44 def dmiRegistry = new DataNode(xpath: '/dmi-registry', childDataNodes: createDataNodeList(['PNFDemo1', 'PNFDemo2', 'PNFDemo3', 'PNFDemo4']))
46 def objectUnderTest = new NetworkCmProxyCmHandleQueryServiceImpl(cmHandleQueries, mockInventoryPersistence)
47 def objectUnderTestWithPartiallyMockedQueries = new NetworkCmProxyCmHandleQueryServiceImpl(partiallyMockedCmHandleQueries, mockInventoryPersistence)
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.queryCmHandleDataNodesByCpsPath('/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
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.queryCmHandleDataNodesByCpsPath('/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
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()
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' | []
108 def 'Query cm handle details with module names when #scenario from query.'() {
109 given: 'a modules condition property'
110 def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
111 def conditionProperties = createConditionProperties('hasAllModules', [['moduleName': 'some-module-name']])
112 cmHandleQueryParameters.setCmHandleQueryParameters([conditionProperties])
113 when: 'the query is executed for cm handle ids'
114 def result = objectUnderTest.queryCmHandles(cmHandleQueryParameters)
115 then: 'the inventory service is called with the correct module names'
116 1 * mockInventoryPersistence.getCmHandleIdsWithGivenModules(['some-module-name']) >> ['ch1']
117 and: 'the inventory service is called with teh correct if and returns a yang model cm handle'
118 1 * mockInventoryPersistence.getYangModelCmHandles(['ch1']) >>
119 [new YangModelCmHandle(id: 'abc', dmiProperties: [new YangModelCmHandle.Property('name','value')], publicProperties: [])]
120 and: 'the expected cm handle(s) are returned as NCMP Service cm handles'
121 assert result[0] instanceof NcmpServiceCmHandle
122 assert result.size() == 1
123 assert result[0].dmiProperties == [name:'value']
126 def 'Query cm handle ids when the query is empty.'() {
127 given: 'We use an empty query'
128 def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
129 and: 'the inventory persistence returns the dmi registry datanode with just ids'
130 mockInventoryPersistence.getDataNode("/dmi-registry", FetchDescendantsOption.DIRECT_CHILDREN_ONLY) >> [dmiRegistry]
131 when: 'the query is executed for both cm handle ids'
132 def result = objectUnderTest.queryCmHandleIds(cmHandleQueryParameters)
133 then: 'the correct expected cm handles are returned'
134 assert result.containsAll('PNFDemo1', 'PNFDemo2', 'PNFDemo3', 'PNFDemo4')
137 def 'Query cm handle details 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("/dmi-registry") >> [dmiRegistry]
142 when: 'the query is executed for both cm handle details'
143 def result = objectUnderTest.queryCmHandles(cmHandleQueryParameters)
144 then: 'the correct cm handles are returned'
145 assert result.size() == 4
146 assert result.cmHandleId.containsAll('PNFDemo1', 'PNFDemo2', 'PNFDemo3', 'PNFDemo4')
149 def 'Query CMHandleId with #scenario.' () {
150 given: 'a query object created with #condition'
151 def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
152 def conditionProperties = createConditionProperties(conditionName, [['some-key': 'some-value']])
153 cmHandleQueryParameters.setCmHandleQueryParameters([conditionProperties])
154 and: 'the inventoryPersistence returns different CmHandleIds'
155 partiallyMockedCmHandleQueries.queryCmHandlePublicProperties(*_) >> cmHandlesWithMatchingPublicProperties
156 partiallyMockedCmHandleQueries.queryCmHandleAdditionalProperties(*_) >> cmHandlesWithMatchingPrivateProperties
157 when: 'the query executed'
158 def result = objectUnderTestWithPartiallyMockedQueries.queryCmHandleIdsForInventory(cmHandleQueryParameters)
159 then: 'the expected number of results are returned.'
160 assert result.size() == expectedCmHandleIdsSize
161 where: 'the following data is used'
162 scenario | conditionName | cmHandlesWithMatchingPublicProperties | cmHandlesWithMatchingPrivateProperties || expectedCmHandleIdsSize
163 'all properties, only public matching' | 'hasAllProperties' | ['h1', 'h2'] | null || 2
164 'all properties, no matching cm handles' | 'hasAllProperties' | [] | [] || 0
165 'additional properties, some matching cm handles' | 'hasAllAdditionalProperties' | [] | ['h1', 'h2'] || 2
166 'additional properties, no matching cm handles' | 'hasAllAdditionalProperties' | null | [] || 0
169 def 'Retrieve CMHandleIds by different DMI properties with #scenario.' () {
170 given: 'a query object created with dmi plugin as condition'
171 def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
172 def conditionProperties = createConditionProperties('cmHandleWithDmiPlugin', [['some-key': 'some-value']])
173 cmHandleQueryParameters.setCmHandleQueryParameters([conditionProperties])
174 and: 'the inventoryPersistence returns different CmHandleIds'
175 partiallyMockedCmHandleQueries.getCmHandleIdsByDmiPluginIdentifier(*_) >> cmHandleQueryResult
176 when: 'the query executed'
177 def result = objectUnderTestWithPartiallyMockedQueries.queryCmHandleIdsForInventory(cmHandleQueryParameters)
178 then: 'the expected number of results are returned.'
179 assert result.size() == expectedCmHandleIdsSize
180 where: 'the following data is used'
181 scenario | cmHandleQueryResult || expectedCmHandleIdsSize
182 'some matches' | ['h1','h2'] || 2
183 'no matches' | [] || 0
186 def 'Combine two query results where #scenario.'() {
187 when: 'two query results in the form of a map of NcmpServiceCmHandles are combined into a single query result'
188 def result = objectUnderTest.combineCmHandleQueryResults(firstQuery, secondQuery)
189 then: 'the returned result is the same as the expected result'
190 result == expectedResult
192 scenario | firstQuery | secondQuery || expectedResult
193 'two queries with unique and non unique entries exist' | ['PNFDemo', 'PNFDemo2'] | ['PNFDemo', 'PNFDemo3'] || ['PNFDemo']
194 'the first query contains entries and second query is empty' | ['PNFDemo', 'PNFDemo2'] | [] || []
195 'the second query contains entries and first query is empty' | [] | ['PNFDemo', 'PNFDemo3'] || []
196 'the first query contains entries and second query is null' | ['PNFDemo', 'PNFDemo2'] | null || ['PNFDemo', 'PNFDemo2']
197 'the second query contains entries and first query is null' | null | ['PNFDemo', 'PNFDemo3'] || ['PNFDemo', 'PNFDemo3']
198 'both queries are empty' | [] | [] || []
199 'both queries are null' | null | null || null
202 def createConditionProperties(String conditionName, List<Map<String, String>> conditionParameters) {
203 return new ConditionProperties(conditionName : conditionName, conditionParameters : conditionParameters)
206 def static createDataNodeList(dataNodeIds) {
208 dataNodeIds.each{ dataNodes << new DataNode(xpath: "/dmi-registry/cm-handles[@id='${it}']", leaves: ['id':it]) }