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