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