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