5619c5ac57e87183e465e2575e9d722b58d78603
[cps.git] /
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2022-2025 Nordix Foundation
4  *  Modifications Copyright (C) 2022 Bell Canada
5  *  Modifications Copyright (C) 2024 TechMahindra Ltd.
6  *  ================================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *        http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS,
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  *
19  *  SPDX-License-Identifier: Apache-2.0
20  *  ============LICENSE_END=========================================================
21  */
22
23 package org.onap.cps.ncmp.impl.inventory
24
25 import com.fasterxml.jackson.databind.ObjectMapper
26 import java.time.OffsetDateTime
27 import java.time.ZoneOffset
28 import java.time.format.DateTimeFormatter
29 import org.onap.cps.api.CpsAnchorService
30 import org.onap.cps.api.CpsDataService
31 import org.onap.cps.api.CpsModuleService
32 import org.onap.cps.api.exceptions.DataNodeNotFoundException
33 import org.onap.cps.api.exceptions.DataValidationException
34 import org.onap.cps.api.model.DataNode
35 import org.onap.cps.api.model.ModuleDefinition
36 import org.onap.cps.api.model.ModuleReference
37 import org.onap.cps.utils.CpsValidator
38 import org.onap.cps.ncmp.api.exceptions.CmHandleNotFoundException
39 import org.onap.cps.ncmp.api.inventory.models.CompositeState
40 import org.onap.cps.ncmp.api.inventory.models.CmHandleState
41 import org.onap.cps.ncmp.impl.inventory.models.YangModelCmHandle
42 import org.onap.cps.ncmp.impl.utils.YangDataConverter
43 import org.onap.cps.utils.ContentType
44 import org.onap.cps.utils.JsonObjectMapper
45 import spock.lang.Shared
46 import spock.lang.Specification
47
48 import static org.onap.cps.api.parameters.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
49 import static org.onap.cps.api.parameters.FetchDescendantsOption.OMIT_DESCENDANTS
50 import static org.onap.cps.ncmp.impl.inventory.NcmpPersistence.NCMP_DATASPACE_NAME
51 import static org.onap.cps.ncmp.impl.inventory.NcmpPersistence.NCMP_DMI_REGISTRY_ANCHOR
52 import static org.onap.cps.ncmp.impl.inventory.NcmpPersistence.NCMP_DMI_REGISTRY_PARENT
53 import static org.onap.cps.ncmp.impl.inventory.NcmpPersistence.NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME
54 import static org.onap.cps.ncmp.impl.inventory.NcmpPersistence.NO_TIMESTAMP
55
56 class InventoryPersistenceImplSpec extends Specification {
57
58     def spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper()))
59
60     def mockCpsDataService = Mock(CpsDataService)
61
62     def mockCpsModuleService = Mock(CpsModuleService)
63
64     def mockCpsAnchorService = Mock(CpsAnchorService)
65
66     def mockCpsValidator = Mock(CpsValidator)
67
68     def mockCmHandleQueries = Mock(CmHandleQueryService)
69
70     def mockYangDataConverter = Mock(YangDataConverter)
71
72     def objectUnderTest = new InventoryPersistenceImpl(mockCpsValidator, spiedJsonObjectMapper, mockCpsAnchorService, mockCpsModuleService, mockCpsDataService, mockCmHandleQueries)
73
74     def formattedDateAndTime = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
75             .format(OffsetDateTime.of(2022, 12, 31, 20, 30, 40, 1, ZoneOffset.UTC))
76
77     def cmHandleId = 'some-cm-handle'
78     def alternateId = 'some-alternate-id'
79     def leaves = ["id":cmHandleId, "alternateId":alternateId,"dmi-service-name":"common service name","dmi-data-service-name":"data service name","dmi-model-service-name":"model service name"]
80     def xpath = "/dmi-registry/cm-handles[@id='some-cm-handle']"
81
82     def cmHandleId2 = 'another-cm-handle'
83     def alternateId2 = 'another-alternate-id'
84     def xpath2 = "/dmi-registry/cm-handles[@id='another-cm-handle']"
85
86     def dataNode = new DataNode(xpath: "/dmi-registry/cm-handles[@id='some cm handle']/additional-properties[@name='name1']", leaves: leaves)
87
88     @Shared
89     def childDataNodesForCmHandleWithAllProperties = [new DataNode(xpath: "/dmi-registry/cm-handles[@id='some cm handle']/additional-properties[@name='name1']", leaves: ["name":"name1", "value":"value1"]),
90                                                       new DataNode(xpath: "/dmi-registry/cm-handles[@id='some cm handle']/public-properties[@name='name2']", leaves: ["name":"name2","value":"value2"])]
91
92     @Shared
93     def childDataNodesForCmHandleWithDMIProperties = [new DataNode(xpath: "/dmi-registry/cm-handles[@id='some-cm-handle']/additional-properties[@name='name1']", leaves: ["name":"name1", "value":"value1"])]
94
95     @Shared
96     def childDataNodesForCmHandleWithPublicProperties = [new DataNode(xpath: "/dmi-registry/cm-handles[@id='some-cm-handle']/public-properties[@name='name2']", leaves: ["name":"name2","value":"value2"])]
97
98     @Shared
99     def childDataNodesForCmHandleWithState = [new DataNode(xpath: "/dmi-registry/cm-handles[@id='some-cm-handle']/state", leaves: ['cm-handle-state': 'ADVISED'])]
100
101     def 'Retrieve CmHandle using datanode with #scenario.'() {
102         given: 'the cps data service returns a data node from the DMI registry'
103             def dataNode = new DataNode(childDataNodes:childDataNodes, leaves: leaves)
104             mockCpsDataService.getDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, xpath, INCLUDE_ALL_DESCENDANTS) >> [dataNode]
105         when: 'retrieving the yang modelled cm handle'
106             def result = objectUnderTest.getYangModelCmHandle(cmHandleId)
107         then: 'the result has the correct id and service names'
108             result.id == cmHandleId
109             result.dmiServiceName == 'common service name'
110             result.dmiDataServiceName == 'data service name'
111             result.dmiModelServiceName == 'model service name'
112         and: 'the expected DMI properties'
113             result.dmiProperties == expectedDmiProperties
114             result.publicProperties == expectedPublicProperties
115         and: 'the state details are returned'
116             result.compositeState.cmHandleState == expectedCompositeState
117         and: 'the CM Handle ID is validated'
118             1 * mockCpsValidator.validateNameCharacters(cmHandleId)
119         where: 'the following parameters are used'
120             scenario                    | childDataNodes                                || expectedDmiProperties                               || expectedPublicProperties                              || expectedCompositeState
121             'no properties'             | []                                            || []                                                  || []                                                    || null
122             'DMI and public properties' | childDataNodesForCmHandleWithAllProperties    || [new YangModelCmHandle.Property("name1", "value1")] || [new YangModelCmHandle.Property("name2", "value2")]   || null
123             'just DMI properties'       | childDataNodesForCmHandleWithDMIProperties    || [new YangModelCmHandle.Property("name1", "value1")] || []                                                    || null
124             'just public properties'    | childDataNodesForCmHandleWithPublicProperties || []                                                  || [new YangModelCmHandle.Property("name2", "value2")]   || null
125             'with state details'        | childDataNodesForCmHandleWithState            || []                                                  || []                                                    || CmHandleState.ADVISED
126     }
127
128     def 'Handling missing service names as null.'() {
129         given: 'the cps data service returns a data node from the DMI registry with empty child and leaf attributes'
130             def dataNode = new DataNode(childDataNodes:[], leaves: ['id':cmHandleId])
131             mockCpsDataService.getDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, xpath, INCLUDE_ALL_DESCENDANTS) >> [dataNode]
132         when: 'retrieving the yang modelled cm handle'
133             def result = objectUnderTest.getYangModelCmHandle(cmHandleId)
134         then: 'the service names are returned as null'
135             result.dmiServiceName == null
136             result.dmiDataServiceName == null
137             result.dmiModelServiceName == null
138         and: 'the CM Handle ID is validated'
139             1 * mockCpsValidator.validateNameCharacters(cmHandleId)
140     }
141
142     def 'Retrieve multiple YangModelCmHandles using cm handle ids'() {
143         given: 'the cps data service returns 2 data nodes from the DMI registry'
144             def dataNodes = [new DataNode(xpath: xpath, leaves: ['id': cmHandleId]), new DataNode(xpath: xpath2, leaves: ['id': cmHandleId2])]
145             mockCpsDataService.getDataNodesForMultipleXpaths(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, [xpath, xpath2] , INCLUDE_ALL_DESCENDANTS) >> dataNodes
146         when: 'retrieving the yang modelled cm handles'
147             def results = objectUnderTest.getYangModelCmHandles([cmHandleId, cmHandleId2])
148         then: 'verify both have returned and cm handle Ids are correct'
149             assert results.size() == 2
150             assert results.id.containsAll([cmHandleId, cmHandleId2])
151     }
152
153     def 'YangModelCmHandles are not returned for invalid cm handle ids'() {
154         given: 'invalid cm handle id throws a data validation exception'
155             mockCpsValidator.validateNameCharacters('Invalid Cm Handle Id') >> {throw new DataValidationException('','')}
156         and: 'empty collection is returned as no valid cm handle ids are given'
157             mockCpsDataService.getDataNodesForMultipleXpaths(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, [] , INCLUDE_ALL_DESCENDANTS) >> []
158         when: 'retrieving the yang modelled cm handles'
159             def results = objectUnderTest.getYangModelCmHandles(['Invalid Cm Handle Id'])
160         then: 'no YangModelCmHandle is returned'
161             assert results.size() == 0
162     }
163
164     def "Retrieve multiple YangModelCmHandles using cm handle references"() {
165         given: 'the cps data service returns 2 data nodes from the DMI registry'
166             def dataNodes = [new DataNode(xpath: xpath, leaves: ['id': cmHandleId, 'alternate-id':alternateId]), new DataNode(xpath: xpath2, leaves: ['id': cmHandleId2,'alternate-id':alternateId2])]
167             mockCmHandleQueries.queryNcmpRegistryByCpsPath(_, INCLUDE_ALL_DESCENDANTS, _) >> dataNodes
168         when: 'retrieving the yang modelled cm handle'
169             def results = objectUnderTest.getYangModelCmHandlesFromCmHandleReferences([cmHandleId, cmHandleId2])
170         then: 'verify both have returned and cmhandleIds are correct'
171             assert results.size() == 2
172             assert results.id.containsAll([cmHandleId, cmHandleId2])
173     }
174
175     def 'Get a Cm Handle Composite State'() {
176         given: 'a valid cm handle id'
177             def cmHandleId = 'Some-Cm-Handle'
178             def dataNode = new DataNode(leaves: ['cm-handle-state': 'ADVISED'])
179         and: 'cps data service returns a valid data node'
180             mockCpsDataService.getDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
181                     '/dmi-registry/cm-handles[@id=\'Some-Cm-Handle\']/state', INCLUDE_ALL_DESCENDANTS) >> [dataNode]
182         when: 'get cm handle state is invoked'
183             def result = objectUnderTest.getCmHandleState(cmHandleId)
184         then: 'result has returned the correct cm handle state'
185             result.cmHandleState == CmHandleState.ADVISED
186         and: 'the CM Handle ID is validated'
187             1 * mockCpsValidator.validateNameCharacters(cmHandleId)
188     }
189
190     def 'Update Cm Handle with #scenario State'() {
191         given: 'a cm handle and a composite state'
192             def cmHandleId = 'Some-Cm-Handle'
193             def compositeState = new CompositeState(cmHandleState: cmHandleState, lastUpdateTime: formattedDateAndTime)
194         when: 'update cm handle state is invoked with the #scenario state'
195             objectUnderTest.saveCmHandleState(cmHandleId, compositeState)
196         then: 'update node leaves is invoked with the correct params'
197             1 * mockCpsDataService.updateDataNodeAndDescendants(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, '/dmi-registry/cm-handles[@id=\'Some-Cm-Handle\']', expectedJsonData, _ as OffsetDateTime, ContentType.JSON)
198         where: 'the following states are used'
199             scenario    | cmHandleState          || expectedJsonData
200             'READY'     | CmHandleState.READY    || '{"state":{"cm-handle-state":"READY","last-update-time":"2022-12-31T20:30:40.000+0000"}}'
201             'LOCKED'    | CmHandleState.LOCKED   || '{"state":{"cm-handle-state":"LOCKED","last-update-time":"2022-12-31T20:30:40.000+0000"}}'
202             'DELETING'  | CmHandleState.DELETING || '{"state":{"cm-handle-state":"DELETING","last-update-time":"2022-12-31T20:30:40.000+0000"}}'
203     }
204
205     def 'Update Cm Handles with #scenario States'() {
206         given: 'a map of cm handles composite states'
207             def compositeState1 = new CompositeState(cmHandleState: cmHandleState, lastUpdateTime: formattedDateAndTime)
208             def compositeState2 = new CompositeState(cmHandleState: cmHandleState, lastUpdateTime: formattedDateAndTime)
209         when: 'update cm handle state is invoked with the #scenario state'
210             def cmHandleStateMap = ['Some-Cm-Handle1' : compositeState1, 'Some-Cm-Handle2' : compositeState2]
211             objectUnderTest.saveCmHandleStateBatch(cmHandleStateMap)
212         then: 'update node leaves is invoked with the correct params'
213             1 * mockCpsDataService.updateDataNodesAndDescendants(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, cmHandlesJsonDataMap, _ as OffsetDateTime, ContentType.JSON)
214         where: 'the following states are used'
215             scenario    | cmHandleState          || cmHandlesJsonDataMap
216             'READY'     | CmHandleState.READY    || ['/dmi-registry/cm-handles[@id=\'Some-Cm-Handle1\']':'{"state":{"cm-handle-state":"READY","last-update-time":"2022-12-31T20:30:40.000+0000"}}', '/dmi-registry/cm-handles[@id=\'Some-Cm-Handle2\']':'{"state":{"cm-handle-state":"READY","last-update-time":"2022-12-31T20:30:40.000+0000"}}']
217             'LOCKED'    | CmHandleState.LOCKED   || ['/dmi-registry/cm-handles[@id=\'Some-Cm-Handle1\']':'{"state":{"cm-handle-state":"LOCKED","last-update-time":"2022-12-31T20:30:40.000+0000"}}', '/dmi-registry/cm-handles[@id=\'Some-Cm-Handle2\']':'{"state":{"cm-handle-state":"LOCKED","last-update-time":"2022-12-31T20:30:40.000+0000"}}']
218             'DELETING'  | CmHandleState.DELETING || ['/dmi-registry/cm-handles[@id=\'Some-Cm-Handle1\']':'{"state":{"cm-handle-state":"DELETING","last-update-time":"2022-12-31T20:30:40.000+0000"}}', '/dmi-registry/cm-handles[@id=\'Some-Cm-Handle2\']':'{"state":{"cm-handle-state":"DELETING","last-update-time":"2022-12-31T20:30:40.000+0000"}}']
219     }
220
221     def 'Getting module definitions by module'() {
222         given: 'cps module service returns module definition for module name'
223             def moduleDefinitions = [new ModuleDefinition('moduleName','revision','content')]
224             mockCpsModuleService.getModuleDefinitionsByAnchorAndModule(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME,'some-cmHandle-Id', 'some-module', '2024-01-25') >> moduleDefinitions
225         when: 'get module definitions is invoked with module name'
226             def result = objectUnderTest.getModuleDefinitionsByCmHandleAndModule('some-cmHandle-Id', 'some-module', '2024-01-25')
227         then: 'returned result are the same module definitions as returned from module service'
228             assert result == moduleDefinitions
229         and: 'cm handle id and module name validated'
230             1 * mockCpsValidator.validateNameCharacters('some-cmHandle-Id', 'some-module')
231     }
232
233     def 'Getting module definitions with cm handle id'() {
234         given: 'cps module service returns module definitions for cm handle id'
235             def moduleDefinitions = [new ModuleDefinition('moduleName','revision','content')]
236             mockCpsModuleService.getModuleDefinitionsByAnchorName(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME,'some-cmHandle-Id') >> moduleDefinitions
237         when: 'get module definitions is invoked with cm handle id'
238             def result = objectUnderTest.getModuleDefinitionsByCmHandleId('some-cmHandle-Id')
239         then: 'the returned result are the same module definitions as returned from the module service'
240             assert result == moduleDefinitions
241     }
242
243     def 'Get module references'() {
244         given: 'cps module service returns a collection of module references'
245             def moduleReferences = [new ModuleReference('moduleName','revision','namespace')]
246             mockCpsModuleService.getYangResourcesModuleReferences(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME,'some-cmHandle-Id') >> moduleReferences
247         when: 'get yang resources module references by cmHandle is invoked'
248             def result = objectUnderTest.getYangResourcesModuleReferences('some-cmHandle-Id')
249         then: 'the returned result is a collection of module definitions'
250             assert result == moduleReferences
251         and: 'the CM Handle ID is validated'
252             1 * mockCpsValidator.validateNameCharacters('some-cmHandle-Id')
253     }
254
255     def 'Save Cmhandle'() {
256         given: 'cmHandle represented as Yang Model'
257             def yangModelCmHandle = new YangModelCmHandle(id: 'cmhandle', dmiProperties: [], publicProperties: [])
258         when: 'the method to save cmhandle is called'
259             objectUnderTest.saveCmHandle(yangModelCmHandle)
260         then: 'the data service method to save list elements is called once'
261             1 * mockCpsDataService.saveListElements(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, NCMP_DMI_REGISTRY_PARENT,
262                     _,null, ContentType.JSON) >> {
263                 args -> {
264                     assert args[3].startsWith('{"cm-handles":[{"id":"cmhandle","additional-properties":[],"public-properties":[]}]}')
265                 }
266             }
267     }
268
269     def 'Save Multiple Cmhandles'() {
270         given: 'cm handles represented as Yang Model'
271             def yangModelCmHandle1 = new YangModelCmHandle(id: 'cmhandle1')
272             def yangModelCmHandle2 = new YangModelCmHandle(id: 'cmhandle2')
273         when: 'the cm handles are saved'
274             objectUnderTest.saveCmHandleBatch([yangModelCmHandle1, yangModelCmHandle2])
275         then: 'CPS Data Service persists both cm handles as a batch'
276             1 * mockCpsDataService.saveListElements(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
277                     NCMP_DMI_REGISTRY_PARENT, _,null, ContentType.JSON) >> {
278                 args -> {
279                     def jsonData = (args[3] as String)
280                     jsonData.contains('cmhandle1')
281                     jsonData.contains('cmhandle2')
282                 }
283             }
284     }
285
286     def 'Delete list or list elements'() {
287         when: 'the method to delete list or list elements is called'
288             objectUnderTest.deleteListOrListElement('sample xPath')
289         then: 'the data service method to save list elements is called once'
290             1 * mockCpsDataService.deleteListOrListElement(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,'sample xPath',null)
291     }
292
293     def 'Get data node via xPath'() {
294         when: 'the method to get data nodes is called'
295             objectUnderTest.getDataNode('sample xPath')
296         then: 'the data persistence service method to get data node is invoked once'
297             1 * mockCpsDataService.getDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,'sample xPath', INCLUDE_ALL_DESCENDANTS)
298     }
299
300     def 'Get cmHandle data node'() {
301         given: 'expected xPath to get cmHandle data node'
302             def expectedXPath = '/dmi-registry/cm-handles[@id=\'sample cmHandleId\']'
303         when: 'the method to get data nodes is called'
304             objectUnderTest.getCmHandleDataNodeByCmHandleId('sample cmHandleId', INCLUDE_ALL_DESCENDANTS)
305         then: 'the data persistence service method to get cmHandle data node is invoked once with expected xPath'
306             1 * mockCpsDataService.getDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, expectedXPath, INCLUDE_ALL_DESCENDANTS)
307     }
308
309     def 'Get yang model cm handle by alternate id'() {
310         given: 'expected xPath to get cmHandle data node'
311             def expectedXPath = '/dmi-registry/cm-handles[@alternate-id=\'alternate id\']'
312             def expectedDataNode = new DataNode(xpath: expectedXPath, leaves: [id: 'id', alternateId: 'alternate id'])
313         and: 'query service is invoked with expected xpath'
314             mockCmHandleQueries.queryNcmpRegistryByCpsPath(expectedXPath, OMIT_DESCENDANTS, _) >> [expectedDataNode]
315             mockYangDataConverter.toYangModelCmHandle(expectedDataNode) >> new YangModelCmHandle(id: 'id')
316         expect: 'getting the yang model cm handle'
317             assert objectUnderTest.getYangModelCmHandleByAlternateId('alternate id') == new YangModelCmHandle(id: 'id')
318     }
319
320     def 'Attempt to get non existing yang model cm handle by alternate id'() {
321         given: 'query service is invoked and returns empty collection of data nodes'
322             mockCmHandleQueries.queryNcmpRegistryByCpsPath(*_) >> []
323         when: 'getting the yang model cm handle'
324             objectUnderTest.getYangModelCmHandleByAlternateId('alternate id')
325         then: 'no data found exception thrown'
326             def thrownException = thrown(CmHandleNotFoundException)
327             assert thrownException.getMessage().contains('Cm handle not found')
328             assert thrownException.getDetails().contains('No cm handles found with reference alternate id')
329     }
330
331     def 'Get multiple yang model cm handles by alternate ids #scenario'() {
332         when: 'getting the yang model cm handle with a empty/populated collection of alternate Ids'
333             objectUnderTest.getYangModelCmHandleByAlternateIds(alternateIdCollection)
334         then: 'query service invoked when needed'
335             expectedInvocations * mockCmHandleQueries.queryNcmpRegistryByCpsPath(*_) >> [dataNode]
336         where: 'collections are either empty or populated with alternate ids'
337             scenario               | alternateIdCollection || expectedInvocations
338             'empty collection'     | []                    || 0
339             'populated collection' | ['alt']               || 1
340     }
341
342     def 'Get CM handle ids for CM Handles that has given module names'() {
343         when: 'the method to get cm handles is called'
344             objectUnderTest.getCmHandleReferencesWithGivenModules(['sample-module-name'], false)
345         then: 'the admin persistence service method to query anchors is invoked once with the same parameter'
346             1 * mockCpsAnchorService.queryAnchorNames(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, ['sample-module-name'])
347     }
348
349     def 'Get Alternate Ids for CM Handles that has given module names'() {
350         given: 'A Collection of data nodes'
351             def dataNodes = [new DataNode(xpath: "/dmi-registry/cm-handles[@id='ch-1']", leaves: ['id': 'ch-1', 'alternate-id': 'alt-1'])]
352         when: 'the methods to get dataNodes is called and returns correct values'
353             mockCpsAnchorService.queryAnchorNames(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, ['sample-module-name']) >> ['ch-1']
354             mockCpsDataService.getDataNodesForMultipleXpaths(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, ["/dmi-registry/cm-handles[@id='ch-1']"], INCLUDE_ALL_DESCENDANTS) >> dataNodes
355         and: 'the method returns a result'
356             def result = objectUnderTest.getCmHandleReferencesWithGivenModules(['sample-module-name'], true)
357         then: 'the result contains the correct alternate Id'
358             assert result == ['alt-1'] as HashSet
359     }
360
361     def 'Replace list content'() {
362         when: 'replace list content method is called with xpath and data nodes collection'
363             objectUnderTest.replaceListContent('sample xpath', [new DataNode()])
364         then: 'the cps data service method to replace list content is invoked once with same parameters'
365             1 * mockCpsDataService.replaceListContent(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,'sample xpath', [new DataNode()], NO_TIMESTAMP);
366     }
367
368     def 'Delete data node via xPath'() {
369         when: 'Delete data node method is called with xpath as parameter'
370             objectUnderTest.deleteDataNode('sample dataNode xpath')
371         then: 'the cps data service method to delete data node is invoked once with the same xPath'
372             1 * mockCpsDataService.deleteDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, 'sample dataNode xpath', NO_TIMESTAMP);
373     }
374
375     def 'Delete multiple data nodes via xPath'() {
376         when: 'Delete data nodes method is called with multiple xpaths as parameters'
377             objectUnderTest.deleteDataNodes(['xpath1', 'xpath2'])
378         then: 'the cps data service method to delete data nodes is invoked once with the same xPaths'
379             1 * mockCpsDataService.deleteDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, ['xpath1', 'xpath2'], NO_TIMESTAMP);
380     }
381
382     def 'CM handle exists'() {
383         given: 'data service returns a datanode with correct cm handle id'
384             mockCpsDataService.getDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, xpath, OMIT_DESCENDANTS) >> [dataNode]
385         expect: 'cm handle exists for given cm handle id'
386             assert true == objectUnderTest.isExistingCmHandleId(cmHandleId)
387     }
388
389     def 'CM handle does not exist, empty dataNode collection returned'() {
390         given: 'data service returns an empty datanode'
391             mockCpsDataService.getDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, xpath, OMIT_DESCENDANTS) >> []
392         expect: 'false is returned for non-existent cm handle'
393             assert false == objectUnderTest.isExistingCmHandleId(cmHandleId)
394     }
395
396     def 'CM handle does not exist, exception thrown'() {
397         given: 'data service throws an exception'
398             mockCpsDataService.getDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, "/dmi-registry/cm-handles[@id='non-existent-cm-handle']", OMIT_DESCENDANTS) >> {throw new DataNodeNotFoundException('','')}
399         expect: 'false is returned for non-existent cm handle'
400             assert false == objectUnderTest.isExistingCmHandleId('non-existent-cm-handle')
401     }
402 }