CPS-1401 Implement V2 of GET Data Node API
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / inventory / CmHandleQueriesImplSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2022 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
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.ncmp.api.inventory
23
24 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle
25 import org.onap.cps.spi.CpsDataPersistenceService
26 import org.onap.cps.spi.model.DataNode
27 import spock.lang.Shared
28 import spock.lang.Specification
29
30 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
31 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
32
33 class CmHandleQueriesImplSpec extends Specification {
34     def cpsDataPersistenceService = Mock(CpsDataPersistenceService)
35
36     def objectUnderTest = new CmHandleQueriesImpl(cpsDataPersistenceService)
37
38     @Shared
39     def static sampleDataNodes = [new DataNode()]
40
41     def dataNodeWithPrivateField = '//additional-properties[@name=\"Contact3\" and @value=\"newemailforstore3@bookstore.com\"]/ancestor::cm-handles'
42
43     def static pnfDemo = createDataNode('PNFDemo')
44     def static pnfDemo2 = createDataNode('PNFDemo2')
45     def static pnfDemo3 = createDataNode('PNFDemo3')
46     def static pnfDemo4 = createDataNode('PNFDemo4')
47     def static pnfDemo5 = createDataNode('PNFDemo5')
48
49     def static pnfDemoCmHandle = new NcmpServiceCmHandle(cmHandleId: 'PNFDemo')
50     def static pnfDemo2CmHandle = new NcmpServiceCmHandle(cmHandleId: 'PNFDemo2')
51     def static pnfDemo3CmHandle = new NcmpServiceCmHandle(cmHandleId: 'PNFDemo3')
52
53     def 'Query CmHandles with public properties query pair.'() {
54         given: 'the DataNodes queried for a given cpsPath are returned from the persistence service.'
55             mockResponses()
56         when: 'a query on cmhandle public properties is performed with a public property pair'
57             def returnedCmHandlesWithData = objectUnderTest.queryCmHandlePublicProperties(publicPropertyPairs)
58         then: 'the correct cm handle data objects are returned'
59             returnedCmHandlesWithData.keySet().containsAll(expectedCmHandleIds)
60             returnedCmHandlesWithData.keySet().size() == expectedCmHandleIds.size()
61         where: 'the following data is used'
62             scenario                         | publicPropertyPairs                                                                           || expectedCmHandleIds
63             'single property matches'        | ['Contact' : 'newemailforstore@bookstore.com']                                                || ['PNFDemo', 'PNFDemo2', 'PNFDemo4']
64             'public property does not match' | ['wont_match' : 'wont_match']                                                                 || []
65             '2 properties, only one match'   | ['Contact' : 'newemailforstore@bookstore.com', 'Contact2': 'newemailforstore2@bookstore.com'] || ['PNFDemo4']
66             '2 properties, no matches'       | ['Contact' : 'newemailforstore@bookstore.com', 'Contact2': '']                                || []
67     }
68
69     def 'Query CmHandles using empty public properties query pair.'() {
70         when: 'a query on CmHandle public properties is executed using an empty map'
71             def returnedCmHandlesWithData = objectUnderTest.queryCmHandlePublicProperties([:])
72         then: 'no cm handles are returned'
73             returnedCmHandlesWithData.keySet().size() == 0
74     }
75
76     def 'Query CmHandles using empty private properties query pair.'() {
77         when: 'a query on CmHandle private properties is executed using an empty map'
78             def returnedCmHandlesWithData = objectUnderTest.queryCmHandleAdditionalProperties([:])
79         then: 'no cm handles are returned'
80             returnedCmHandlesWithData.keySet().size() == 0
81     }
82
83     def 'Query CmHandles by a private field\'s value.'() {
84         given: 'a data node exists with a certain additional-property'
85             cpsDataPersistenceService.queryDataNodes(_, _, dataNodeWithPrivateField, _) >> [pnfDemo5]
86         when: 'a query on CmHandle private properties is executed using a map'
87             def returnedCmHandlesWithData = objectUnderTest.queryCmHandleAdditionalProperties(['Contact3': 'newemailforstore3@bookstore.com'])
88         then: 'one cm handle is returned'
89             returnedCmHandlesWithData.keySet().size() == 1
90     }
91
92     def 'Combine two query results where #scenario.'() {
93         when: 'two query results in the form of a map of NcmpServiceCmHandles are combined into a single query result'
94             def result = objectUnderTest.combineCmHandleQueries(firstQuery, secondQuery)
95         then: 'the returned result is the same as the expected result'
96             result == expectedResult
97         where:
98             scenario                                                     | firstQuery                                                 | secondQuery                                                || expectedResult
99             'two queries with unique and non unique entries exist'       | ['PNFDemo': pnfDemoCmHandle, 'PNFDemo2': pnfDemo2CmHandle] | ['PNFDemo': pnfDemoCmHandle, 'PNFDemo3': pnfDemo3CmHandle] || ['PNFDemo': pnfDemoCmHandle]
100             'the first query contains entries and second query is empty' | ['PNFDemo': pnfDemoCmHandle, 'PNFDemo2': pnfDemo2CmHandle] | [:]                                                        || [:]
101             'the second query contains entries and first query is empty' | [:]                                                        | ['PNFDemo': pnfDemoCmHandle, 'PNFDemo3': pnfDemo3CmHandle] || [:]
102             'the first query contains entries and second query is null'  | ['PNFDemo': pnfDemoCmHandle, 'PNFDemo2': pnfDemo2CmHandle] | null                                                       || ['PNFDemo': pnfDemoCmHandle, 'PNFDemo2': pnfDemo2CmHandle]
103             'the second query contains entries and first query is null'  | null                                                       | ['PNFDemo': pnfDemoCmHandle, 'PNFDemo3': pnfDemo3CmHandle] || ['PNFDemo': pnfDemoCmHandle, 'PNFDemo3': pnfDemo3CmHandle]
104             'both queries are empty'                                     | [:]                                                        | [:]                                                        || [:]
105             'both queries are null'                                      | null                                                       | null                                                       || null
106     }
107
108     def 'Get CmHandles by it\'s state.'() {
109         given: 'a cm handle state to query'
110             def cmHandleState = CmHandleState.ADVISED
111         and: 'the persistence service returns a list of data nodes'
112             cpsDataPersistenceService.queryDataNodes('NCMP-Admin', 'ncmp-dmi-registry',
113                 '//state[@cm-handle-state="ADVISED"]/ancestor::cm-handles', INCLUDE_ALL_DESCENDANTS) >> sampleDataNodes
114         when: 'cm handles are fetched by state'
115             def result = objectUnderTest.queryCmHandlesByState(cmHandleState)
116         then: 'the returned result matches the result from the persistence service'
117             assert result == sampleDataNodes
118     }
119
120     def 'Check the state of a cmHandle when #scenario.'() {
121         given: 'a cm handle state to compare'
122             def cmHandleState = state
123         and: 'the persistence service returns a list of data nodes'
124             cpsDataPersistenceService.getDataNodes('NCMP-Admin', 'ncmp-dmi-registry',
125                     '/dmi-registry/cm-handles[@id=\'some-cm-handle\']/state', OMIT_DESCENDANTS) >> [new DataNode(leaves: ['cm-handle-state': 'READY'])]
126         when: 'cm handles are compared by state'
127             def result = objectUnderTest.cmHandleHasState('some-cm-handle', cmHandleState)
128         then: 'the returned result matches the expected result from the persistence service'
129             result == expectedResult
130         where:
131             scenario                           | state                 || expectedResult
132             'the provided state matches'       | CmHandleState.READY   || true
133             'the provided state does not match'| CmHandleState.DELETED || false
134     }
135
136     def 'Get Cm Handles state by Cm-Handle Id'() {
137         given: 'a cm handle state to query'
138             def cmHandleState = CmHandleState.READY
139         and: 'cps data service returns a list of data nodes'
140             cpsDataPersistenceService.getDataNodes('NCMP-Admin', 'ncmp-dmi-registry',
141                 '/dmi-registry/cm-handles[@id=\'some-cm-handle\']/state', OMIT_DESCENDANTS) >> [new DataNode(leaves: ['cm-handle-state': 'READY'])]
142         when: 'cm handles are fetched by state and id'
143             def result = objectUnderTest.getCmHandleState('some-cm-handle')
144         then: 'the returned result is a list of data nodes returned by cps data service'
145             assert result == new DataNode(leaves: ['cm-handle-state': 'READY'])
146     }
147
148     def 'Retrieve Cm Handles By Operational Sync State : UNSYNCHRONIZED'() {
149         given: 'a cm handle state to query'
150             def cmHandleState = CmHandleState.READY
151         and: 'cps data service returns a list of data nodes'
152             cpsDataPersistenceService.queryDataNodes('NCMP-Admin', 'ncmp-dmi-registry',
153                 '//state/datastores/operational[@sync-state="'+'UNSYNCHRONIZED'+'"]/ancestor::cm-handles', OMIT_DESCENDANTS) >> sampleDataNodes
154         when: 'cm handles are fetched by the UNSYNCHRONIZED operational sync state'
155             def result = objectUnderTest.queryCmHandlesByOperationalSyncState(DataStoreSyncState.UNSYNCHRONIZED)
156         then: 'the returned result is a list of data nodes returned by cps data service'
157             assert result == sampleDataNodes
158     }
159
160     def 'Retrieve cm handle by cps path '() {
161         given: 'a cm handle state to query based on the cps path'
162             def cmHandleDataNode = new DataNode(xpath: 'xpath', leaves: ['cm-handle-state': 'LOCKED'])
163             def cpsPath = '//cps-path'
164         and: 'cps data service returns a valid data node'
165             cpsDataPersistenceService.queryDataNodes('NCMP-Admin', 'ncmp-dmi-registry',
166                 cpsPath + '/ancestor::cm-handles', INCLUDE_ALL_DESCENDANTS)
167                 >> Arrays.asList(cmHandleDataNode)
168         when: 'get cm handles by cps path is invoked'
169             def result = objectUnderTest.queryCmHandleDataNodesByCpsPath(cpsPath, INCLUDE_ALL_DESCENDANTS)
170         then: 'the returned result is a list of data nodes returned by cps data service'
171             assert result.contains(cmHandleDataNode)
172     }
173
174     def 'Get all cm handles by dmi plugin identifier'() {
175         given: 'the DataNodes queried for a given cpsPath are returned from the persistence service.'
176             mockResponses()
177         when: 'cm Handles are fetched for a given dmi plugin identifier'
178             def result = objectUnderTest.getCmHandlesByDmiPluginIdentifier('my-dmi-plugin-identifier')
179         then: 'result is the correct size'
180             assert result.size() == 3
181         and: 'result contains the correct cm handles'
182             assert result.cmHandleId.containsAll('PNFDemo', 'PNFDemo2', 'PNFDemo4')
183     }
184
185     void mockResponses() {
186         cpsDataPersistenceService.queryDataNodes(_, _, '//public-properties[@name=\"Contact\" and @value=\"newemailforstore@bookstore.com\"]/ancestor::cm-handles', _) >> [pnfDemo, pnfDemo2, pnfDemo4]
187         cpsDataPersistenceService.queryDataNodes(_, _, '//public-properties[@name=\"wont_match\" and @value=\"wont_match\"]/ancestor::cm-handles', _) >> []
188         cpsDataPersistenceService.queryDataNodes(_, _, '//public-properties[@name=\"Contact2\" and @value=\"newemailforstore2@bookstore.com\"]/ancestor::cm-handles', _) >> [pnfDemo4]
189         cpsDataPersistenceService.queryDataNodes(_, _, '//public-properties[@name=\"Contact2\" and @value=\"\"]/ancestor::cm-handles', _) >> []
190         cpsDataPersistenceService.queryDataNodes(_, _, '//state[@cm-handle-state=\"READY\"]/ancestor::cm-handles', _) >> [pnfDemo, pnfDemo3]
191         cpsDataPersistenceService.queryDataNodes(_, _, '//state[@cm-handle-state=\"LOCKED\"]/ancestor::cm-handles', _) >> [pnfDemo2, pnfDemo4]
192         cpsDataPersistenceService.queryDataNodes('NCMP-Admin','ncmp-dmi-registry','/dmi-registry/cm-handles[@dmi-service-name=\'my-dmi-plugin-identifier\']',OMIT_DESCENDANTS) >> [pnfDemo, pnfDemo2]
193         cpsDataPersistenceService.queryDataNodes('NCMP-Admin','ncmp-dmi-registry','/dmi-registry/cm-handles[@dmi-data-service-name=\'my-dmi-plugin-identifier\']',OMIT_DESCENDANTS) >> [pnfDemo,pnfDemo4]
194         cpsDataPersistenceService.queryDataNodes('NCMP-Admin','ncmp-dmi-registry','/dmi-registry/cm-handles[@dmi-model-service-name=\'my-dmi-plugin-identifier\']',OMIT_DESCENDANTS) >> [pnfDemo2,pnfDemo4]
195     }
196
197     def static createDataNode(dataNodeId) {
198         return new DataNode(xpath: '/dmi-registry/cm-handles[@id=\'' + dataNodeId + '\']', leaves: ['id':dataNodeId])
199     }
200 }