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