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