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