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