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