2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2023-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.integration.functional
24 import org.onap.cps.api.CpsDataService
25 import org.onap.cps.integration.base.FunctionalSpecBase
26 import org.onap.cps.spi.FetchDescendantsOption
27 import org.onap.cps.spi.exceptions.AlreadyDefinedException
28 import org.onap.cps.spi.exceptions.AnchorNotFoundException
29 import org.onap.cps.spi.exceptions.CpsAdminException
30 import org.onap.cps.spi.exceptions.CpsPathException
31 import org.onap.cps.spi.exceptions.DataNodeNotFoundException
32 import org.onap.cps.spi.exceptions.DataNodeNotFoundExceptionBatch
33 import org.onap.cps.spi.exceptions.DataValidationException
34 import org.onap.cps.spi.exceptions.DataspaceNotFoundException
35 import org.onap.cps.spi.model.DeltaReport
37 import static org.onap.cps.spi.FetchDescendantsOption.DIRECT_CHILDREN_ONLY
38 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
39 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
41 class CpsDataServiceIntegrationSpec extends FunctionalSpecBase {
43 CpsDataService objectUnderTest
44 def originalCountBookstoreChildNodes
45 def originalCountBookstoreTopLevelListNodes
48 objectUnderTest = cpsDataService
49 originalCountBookstoreChildNodes = countDataNodesInBookstore()
50 originalCountBookstoreTopLevelListNodes = countTopLevelListDataNodesInBookstore()
53 def 'Read bookstore top-level container(s) using #fetchDescendantsOption.'() {
54 when: 'get data nodes for bookstore container'
55 def result = objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore', fetchDescendantsOption)
56 then: 'the tree consist ouf of #expectNumberOfDataNodes data nodes'
57 assert countDataNodesInTree(result) == expectNumberOfDataNodes
58 and: 'the top level data node has the expected attribute and value'
59 assert result.leaves['bookstore-name'] == ['Easons-1']
60 and: 'they are from the correct dataspace'
61 assert result.dataspace == [FUNCTIONAL_TEST_DATASPACE_1]
62 and: 'they are from the correct anchor'
63 assert result.anchorName == [BOOKSTORE_ANCHOR_1]
64 where: 'the following option is used'
65 fetchDescendantsOption || expectNumberOfDataNodes
67 DIRECT_CHILDREN_ONLY || 7
68 INCLUDE_ALL_DESCENDANTS || 28
69 new FetchDescendantsOption(2) || 28
72 def 'Read bookstore top-level container(s) using "root" path variations.'() {
73 when: 'get data nodes for bookstore container'
74 def result = objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, root, OMIT_DESCENDANTS)
75 then: 'the tree consist correct number of data nodes'
76 assert countDataNodesInTree(result) == 2
77 and: 'the top level data node has the expected number of leaves'
78 assert result.leaves.size() == 2
79 where: 'the following variations of "root" are used'
83 def 'Read data nodes with error: #cpsPath'() {
84 when: 'attempt to get data nodes using invalid path'
85 objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, DIRECT_CHILDREN_ONLY)
86 then: 'a #expectedException is thrown'
87 thrown(expectedException)
89 cpsPath || expectedException
90 'invalid path' || CpsPathException
91 '/non-existing-path' || DataNodeNotFoundException
94 def 'Read (multiple) data nodes (batch) with #cpsPath'() {
95 when: 'attempt to get data nodes using invalid path'
96 objectUnderTest.getDataNodesForMultipleXpaths(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, [ cpsPath ], DIRECT_CHILDREN_ONLY)
97 then: 'no exception is thrown'
100 cpsPath << [ 'invalid path', '/non-existing-path' ]
103 def 'Get data nodes error scenario #scenario'() {
104 when: 'attempt to retrieve data nodes'
105 objectUnderTest.getDataNodes(dataspaceName, anchorName, xpath, OMIT_DESCENDANTS)
106 then: 'expected exception is thrown'
107 thrown(expectedException)
108 where: 'following data is used'
109 scenario | dataspaceName | anchorName | xpath || expectedException
110 'non existent dataspace' | 'non-existent' | 'not-relevant' | '/not-relevant' || DataspaceNotFoundException
111 'non existent anchor' | FUNCTIONAL_TEST_DATASPACE_1 | 'non-existent' | '/not-relevant' || AnchorNotFoundException
112 'non-existent xpath' | FUNCTIONAL_TEST_DATASPACE_1 | BOOKSTORE_ANCHOR_1| '/non-existing' || DataNodeNotFoundException
113 'invalid-dataspace' | 'Invalid dataspace' | 'not-relevant' | '/not-relevant' || DataValidationException
114 'invalid-dataspace' | FUNCTIONAL_TEST_DATASPACE_1 | 'Invalid Anchor' | '/not-relevant' || DataValidationException
117 def 'Delete root data node.'() {
118 when: 'the "root" is deleted'
119 objectUnderTest.deleteDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, [ '/' ], now)
120 and: 'attempt to get the top level data node'
121 objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore', DIRECT_CHILDREN_ONLY)
122 then: 'an datanode not found exception is thrown'
123 thrown(DataNodeNotFoundException)
125 restoreBookstoreDataAnchor(1)
128 def 'Get whole list data' () {
129 def xpathForWholeList = "/bookstore/categories"
130 when: 'get data nodes for bookstore container'
131 def dataNodes = objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, xpathForWholeList, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS)
132 then: 'the tree consist ouf of #expectNumberOfDataNodes data nodes'
133 assert dataNodes.size() == 5
134 and: 'each datanode contains the list node xpath partially in its xpath'
135 dataNodes.each {dataNode ->
136 assert dataNode.xpath.contains(xpathForWholeList)
140 def 'Read (multiple) data nodes with #scenario' () {
141 when: 'attempt to get data nodes using multiple valid xpaths'
142 def dataNodes = objectUnderTest.getDataNodesForMultipleXpaths(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, xpath, OMIT_DESCENDANTS)
143 then: 'expected numer of data nodes are returned'
144 dataNodes.size() == expectedNumberOfDataNodes
145 where: 'the following data was used'
146 scenario | xpath | expectedNumberOfDataNodes
147 'container-node xpath' | ['/bookstore'] | 1
148 'list-item' | ['/bookstore/categories[@code=1]'] | 1
149 'parent-list xpath' | ['/bookstore/categories'] | 5
150 'child-list xpath' | ['/bookstore/categories[@code=1]/books'] | 2
151 'both parent and child list xpath' | ['/bookstore/categories', '/bookstore/categories[@code=1]/books'] | 7
154 def 'Add and Delete a (container) data node using #scenario.'() {
155 when: 'the new datanode is saved'
156 objectUnderTest.saveData(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , parentXpath, json, now)
157 then: 'it can be retrieved by its normalized xpath'
158 def result = objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, normalizedXpathToNode, DIRECT_CHILDREN_ONLY)
159 assert result.size() == 1
160 assert result[0].xpath == normalizedXpathToNode
161 and: 'there is now one extra datanode'
162 assert originalCountBookstoreChildNodes + 1 == countDataNodesInBookstore()
163 when: 'the new datanode is deleted'
164 objectUnderTest.deleteDataNode(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, normalizedXpathToNode, now)
165 then: 'the original number of data nodes is restored'
166 assert originalCountBookstoreChildNodes == countDataNodesInBookstore()
168 scenario | parentXpath | json || normalizedXpathToNode
169 'normalized parent xpath' | '/bookstore' | '{"webinfo": {"domain-name":"ourbookstore.com", "contact-email":"info@ourbookstore.com" }}' || "/bookstore/webinfo"
170 'non-normalized parent xpath' | '/bookstore/categories[ @code="1"]' | '{"books": {"title":"new" }}' || "/bookstore/categories[@code='1']/books[@title='new']"
173 def 'Attempt to create a top level data node using root.'() {
174 given: 'a new anchor'
175 cpsAnchorService.createAnchor(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_SCHEMA_SET, 'newAnchor1');
176 when: 'attempt to save new top level datanode'
177 def json = '{"bookstore": {"bookstore-name": "New Store"} }'
178 objectUnderTest.saveData(FUNCTIONAL_TEST_DATASPACE_1, 'newAnchor1' , '/', json, now)
179 then: 'since there is no data a data node not found exception is thrown'
180 thrown(DataNodeNotFoundException)
183 def 'Attempt to save top level data node that already exist'() {
184 when: 'attempt to save already existing top level node'
185 def json = '{"bookstore": {} }'
186 objectUnderTest.saveData(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, json, now)
187 then: 'an exception that (one cps paths is) already defined is thrown '
188 def exceptionThrown = thrown(AlreadyDefinedException)
189 exceptionThrown.alreadyDefinedObjectNames == ['/bookstore' ] as Set
191 restoreBookstoreDataAnchor(1)
194 def 'Delete a single datanode with invalid path.'() {
195 when: 'attempt to delete a single datanode with invalid path'
196 objectUnderTest.deleteDataNode(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/invalid path', now)
197 then: 'a cps path parser exception is thrown'
198 thrown(CpsPathException)
201 def 'Delete multiple data nodes with invalid path.'() {
202 when: 'attempt to delete datanode collection with invalid path'
203 objectUnderTest.deleteDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, ['/invalid path'], now)
204 then: 'the error is silently ignored'
208 def 'Delete single data node with non-existing path.'() {
209 when: 'attempt to delete a single datanode non-existing invalid path'
210 objectUnderTest.deleteDataNode(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/does/not/exist', now)
211 then: 'a datanode not found exception is thrown'
212 thrown(DataNodeNotFoundException)
215 def 'Delete multiple data nodes with non-existing path(s).'() {
216 when: 'attempt to delete a single datanode non-existing invalid path'
217 objectUnderTest.deleteDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, ['/does/not/exist'], now)
218 then: 'a datanode not found (batch) exception is thrown'
219 thrown(DataNodeNotFoundExceptionBatch)
222 def 'Add and Delete top-level list (element) data nodes with root node.'() {
223 given: 'a new (multiple-data-tree:invoice) datanodes'
224 def json = '{"bookstore-address":[{"bookstore-name":"Easons","address":"Bangalore,India","postal-code":"560043"}]}'
225 when: 'the new list elements are saved'
226 objectUnderTest.saveListElements(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , '/', json, now)
227 then: 'they can be retrieved by their xpaths'
228 objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , '/bookstore-address[@bookstore-name="Easons"]', INCLUDE_ALL_DESCENDANTS)
229 and: 'there is one extra datanode'
230 assert originalCountBookstoreTopLevelListNodes + 1 == countTopLevelListDataNodesInBookstore()
231 when: 'the new elements are deleted'
232 objectUnderTest.deleteDataNode(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , '/bookstore-address[@bookstore-name="Easons"]', now)
233 then: 'the original number of datanodes is restored'
234 assert originalCountBookstoreTopLevelListNodes == countTopLevelListDataNodesInBookstore()
237 def 'Add and Delete list (element) data nodes.'() {
238 given: 'two new (categories) data nodes'
239 def json = '{"categories": [ {"code":"new1"}, {"code":"new2" } ] }'
240 when: 'the new list elements are saved'
241 objectUnderTest.saveListElements(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , '/bookstore', json, now)
242 then: 'they can be retrieved by their xpaths'
243 objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="new1"]', DIRECT_CHILDREN_ONLY).size() == 1
244 objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="new2"]', DIRECT_CHILDREN_ONLY).size() == 1
245 and: 'there are now two extra data nodes'
246 assert originalCountBookstoreChildNodes + 2 == countDataNodesInBookstore()
247 when: 'the new elements are deleted'
248 objectUnderTest.deleteDataNode(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="new1"]', now)
249 objectUnderTest.deleteDataNode(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="new2"]', now)
250 then: 'the original number of data nodes is restored'
251 assert originalCountBookstoreChildNodes == countDataNodesInBookstore()
254 def 'Add list (element) data nodes that already exist.'() {
255 given: 'two (categories) data nodes, one new and one existing'
256 def json = '{"categories": [ {"code":"1"}, {"code":"new1"} ] }'
257 when: 'attempt to save the list element'
258 objectUnderTest.saveListElements(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , '/bookstore', json, now)
259 then: 'an exception that (one cps paths is) already defined is thrown '
260 def exceptionThrown = thrown(AlreadyDefinedException)
261 exceptionThrown.alreadyDefinedObjectNames == ['/bookstore/categories[@code=\'1\']' ] as Set
262 and: 'there is now one extra data nodes'
263 assert originalCountBookstoreChildNodes + 1 == countDataNodesInBookstore()
265 restoreBookstoreDataAnchor(1)
268 def 'Add and Delete list (element) data nodes using lists specific method.'() {
269 given: 'a new (categories) data nodes'
270 def json = '{"categories": [ {"code":"new1"} ] }'
271 and: 'the new list element is saved'
272 objectUnderTest.saveListElements(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , '/bookstore', json, now)
273 when: 'the new element is deleted'
274 objectUnderTest.deleteListOrListElement(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="new1"]', now)
275 then: 'the original number of data nodes is restored'
276 assert originalCountBookstoreChildNodes == countDataNodesInBookstore()
279 def 'Add and Delete a batch of list element data nodes.'() {
280 given: 'two new (categories) data nodes in a single batch'
281 def json = '{"categories": [ {"code":"new1"}, {"code":"new2"} ] }'
282 when: 'the batches of new list element(s) are saved'
283 objectUnderTest.saveListElements(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , '/bookstore', json, now)
284 then: 'they can be retrieved by their xpaths'
285 assert objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="new1"]', DIRECT_CHILDREN_ONLY).size() == 1
286 assert objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="new2"]', DIRECT_CHILDREN_ONLY).size() == 1
287 and: 'there are now two extra data nodes'
288 assert originalCountBookstoreChildNodes + 2 == countDataNodesInBookstore()
289 when: 'the new elements are deleted'
290 objectUnderTest.deleteDataNode(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="new1"]', now)
291 objectUnderTest.deleteDataNode(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="new2"]', now)
292 then: 'the original number of data nodes is restored'
293 assert originalCountBookstoreChildNodes == countDataNodesInBookstore()
296 def 'Add and Delete a batch of list element data nodes with partial success.'() {
297 given: 'one existing and one new (categories) data nodes in a single batch'
298 def json = '{"categories": [ {"code":"new1"}, {"code":"1"} ] }'
299 when: 'the batches of new list element(s) are saved'
300 objectUnderTest.saveListElements(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , '/bookstore', json, now)
301 then: 'an already defined (batch) exception is thrown for the existing path'
302 def exceptionThrown = thrown(AlreadyDefinedException)
303 assert exceptionThrown.alreadyDefinedObjectNames == ['/bookstore/categories[@code=\'1\']' ] as Set
304 and: 'there is now one extra data node'
305 assert originalCountBookstoreChildNodes + 1 == countDataNodesInBookstore()
307 restoreBookstoreDataAnchor(1)
310 def 'Attempt to add empty lists.'() {
311 when: 'the batches of new list element(s) are saved'
312 objectUnderTest.replaceListContent(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , '/bookstore', [ ], now)
313 then: 'an admin exception is thrown'
314 thrown(CpsAdminException)
317 def 'Add child error scenario: #scenario.'() {
318 when: 'attempt to add a child data node with #scenario'
319 objectUnderTest.saveData(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, parentXpath, json, now)
320 then: 'a #expectedException is thrown'
321 thrown(expectedException)
322 where: 'the following data is used'
323 scenario | parentXpath | json || expectedException
324 'parent does not exist' | '/bookstore/categories[@code="unknown"]' | '{"books": [ {"title":"new"} ] } ' || DataNodeNotFoundException
325 'already existing child' | '/bookstore' | '{"categories": [ {"code":"1"} ] }' || AlreadyDefinedException
328 def 'Add multiple child data nodes with partial success.'() {
329 given: 'one existing and one new list element'
330 def json = '{"categories": [ {"code":"1"}, {"code":"new"} ] }'
331 when: 'attempt to add the elements'
332 objectUnderTest.saveData(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore', json, now)
333 then: 'an already defined (batch) exception is thrown for the existing path'
334 def thrown = thrown(AlreadyDefinedException)
335 assert thrown.alreadyDefinedObjectNames == [ "/bookstore/categories[@code='1']" ] as Set
336 and: 'the new data node has been added i.e. can be retrieved'
337 assert objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="new"]', DIRECT_CHILDREN_ONLY).size() == 1
340 def 'Replace list content #scenario.'() {
341 given: 'the bookstore categories 1 and 2 exist and have at least 1 child each '
342 assert countDataNodesInTree(objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="1"]', DIRECT_CHILDREN_ONLY)) > 1
343 assert countDataNodesInTree(objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="2"]', DIRECT_CHILDREN_ONLY)) > 1
344 when: 'the categories list is replaced with just category "1" and without child nodes (books)'
345 def json = '{"categories": [ {"code":"' +categoryCode + '"' + childJson + '} ] }'
346 objectUnderTest.replaceListContent(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore', json, now)
347 then: 'the new replaced category can be retrieved but has no children anymore'
348 assert expectedNumberOfDataNodes == countDataNodesInTree(objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="' +categoryCode + '"]', DIRECT_CHILDREN_ONLY))
349 when: 'attempt to retrieve a category (code) not in the new list'
350 objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="2"]', DIRECT_CHILDREN_ONLY)
351 then: 'a datanode not found exception occurs'
352 thrown(DataNodeNotFoundException)
354 restoreBookstoreDataAnchor(1)
355 where: 'the following data is used'
356 scenario | categoryCode | childJson || expectedNumberOfDataNodes
357 'existing code, no children' | '1' | '' || 1
358 'existing code, new child' | '1' | ', "books" : [ { "title": "New Book" } ]' || 2
359 'existing code, existing child' | '1' | ', "books" : [ { "title": "Matilda" } ]' || 2
360 'new code, new child' | 'new' | ', "books" : [ { "title": "New Book" } ]' || 2
363 def 'Update data node leaves for node that has no leaves (yet).'() {
364 given: 'new (webinfo) datanode without leaves'
365 def json = '{"webinfo": {} }'
366 objectUnderTest.saveData(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , '/bookstore', json, now)
367 when: 'update is performed to add a leaf'
368 def updatedJson = '{"webinfo": {"domain-name":"new leaf data"}}'
369 objectUnderTest.updateNodeLeaves(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, "/bookstore", updatedJson, now)
370 then: 'the updated data nodes are retrieved'
371 def result = cpsDataService.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, "/bookstore/webinfo", INCLUDE_ALL_DESCENDANTS)
372 and: 'the leaf value is updated as expected'
373 assert result.leaves['domain-name'] == ['new leaf data']
375 restoreBookstoreDataAnchor(1)
378 def 'Update multiple data leaves error scenario: #scenario.'() {
379 when: 'attempt to update data node for #scenario'
380 objectUnderTest.updateNodeLeaves(dataspaceName, anchorName, xpath, 'irrelevant json data', now)
381 then: 'a #expectedException is thrown'
382 thrown(expectedException)
383 where: 'the following data is used'
384 scenario | dataspaceName | anchorName | xpath || expectedException
385 'invalid dataspace name' | 'Invalid Dataspace' | 'not-relevant' | '/not relevant' || DataValidationException
386 'invalid anchor name' | FUNCTIONAL_TEST_DATASPACE_1 | 'INVALID ANCHOR' | '/not relevant' || DataValidationException
387 'non-existing dataspace' | 'non-existing-dataspace' | 'not-relevant' | '/not relevant' || DataspaceNotFoundException
388 'non-existing anchor' | FUNCTIONAL_TEST_DATASPACE_1 | 'non-existing-anchor' | '/not relevant' || AnchorNotFoundException
389 'non-existing-xpath' | FUNCTIONAL_TEST_DATASPACE_1 | BOOKSTORE_ANCHOR_1 | '/non-existing' || DataValidationException
392 def 'Update data nodes and descendants.'() {
393 given: 'some web info for the bookstore'
394 def json = '{"webinfo": {"domain-name":"ourbookstore.com" ,"contact-email":"info@ourbookstore.com" }}'
395 objectUnderTest.saveData(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , '/bookstore', json, now)
396 when: 'the webinfo (container) is updated'
397 json = '{"webinfo": {"domain-name":"newdomain.com" ,"contact-email":"info@newdomain.com" }}'
398 objectUnderTest.updateDataNodeAndDescendants(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore', json, now)
399 then: 'webinfo has been updated with teh new details'
400 def result = objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/webinfo', DIRECT_CHILDREN_ONLY)
401 result.leaves.'domain-name'[0] == 'newdomain.com'
402 result.leaves.'contact-email'[0] == 'info@newdomain.com'
404 restoreBookstoreDataAnchor(1)
407 def 'Update bookstore top-level container data node.'() {
408 when: 'the bookstore top-level container is updated'
409 def json = '{ "bookstore": { "bookstore-name": "new bookstore" }}'
410 objectUnderTest.updateDataNodeAndDescendants(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/', json, now)
411 then: 'bookstore name has been updated'
412 def result = objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore', DIRECT_CHILDREN_ONLY)
413 result.leaves.'bookstore-name'[0] == 'new bookstore'
415 restoreBookstoreDataAnchor(1)
418 def 'Update multiple data node leaves.'() {
419 given: 'Updated json for bookstore data'
420 def jsonData = "{'book-store:books':{'lang':'English/French','price':100,'title':'Matilda'}}"
421 when: 'update is performed for leaves'
422 objectUnderTest.updateNodeLeaves(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_2, "/bookstore/categories[@code='1']", jsonData, now)
423 then: 'the updated data nodes are retrieved'
424 def result = cpsDataService.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_2, "/bookstore/categories[@code=1]/books[@title='Matilda']", INCLUDE_ALL_DESCENDANTS)
425 and: 'the leaf values are updated as expected'
426 assert result.leaves['lang'] == ['English/French']
427 assert result.leaves['price'] == [100]
429 restoreBookstoreDataAnchor(2)
432 def 'Get delta between 2 anchors for when #scenario'() {
433 when: 'attempt to get delta report between anchors'
434 def result = objectUnderTest.getDeltaByDataspaceAndAnchors(FUNCTIONAL_TEST_DATASPACE_3, BOOKSTORE_ANCHOR_3, BOOKSTORE_ANCHOR_5, xpath, fetchDescendantOption)
435 then: 'delta report contains expected number of changes'
437 and: 'delta report contains expected action'
438 assert result.get(index).getAction() == expectedActions
439 and: 'delta report contains expected xpath'
440 assert result.get(index).getXpath() == expectedXpath
441 where: 'following data was used'
442 scenario | index | xpath || expectedActions || expectedXpath | fetchDescendantOption
443 'a node is removed' | 0 | '/' || 'remove' || "/bookstore-address[@bookstore-name='Easons-1']" | OMIT_DESCENDANTS
444 'a node is added' | 1 | '/' || 'add' || "/bookstore-address[@bookstore-name='Crossword Bookstores']" | OMIT_DESCENDANTS
447 def 'Get delta between 2 anchors where child nodes are added/removed but parent node remains unchanged'() {
448 def parentNodeXpath = "/bookstore"
449 when: 'attempt to get delta report between anchors'
450 def result = objectUnderTest.getDeltaByDataspaceAndAnchors(FUNCTIONAL_TEST_DATASPACE_3, BOOKSTORE_ANCHOR_3, BOOKSTORE_ANCHOR_5, parentNodeXpath, INCLUDE_ALL_DESCENDANTS)
451 then: 'delta report contains expected number of changes'
453 and: 'the delta report does not contain parent node xpath'
454 def xpaths = getDeltaReportEntities(result).get('xpaths')
455 assert !(xpaths.contains(parentNodeXpath))
458 def 'Get delta between 2 anchors returns empty response when #scenario'() {
459 when: 'attempt to get delta report between anchors'
460 def result = objectUnderTest.getDeltaByDataspaceAndAnchors(FUNCTIONAL_TEST_DATASPACE_3, sourceAnchor, targetAnchor, xpath, INCLUDE_ALL_DESCENDANTS)
461 then: 'delta report is empty'
462 assert result.isEmpty()
463 where: 'following data was used'
464 scenario | sourceAnchor | targetAnchor | xpath
465 'anchors with identical data are queried' | BOOKSTORE_ANCHOR_3 | BOOKSTORE_ANCHOR_4 | '/'
466 'same anchor name is passed as parameter' | BOOKSTORE_ANCHOR_3 | BOOKSTORE_ANCHOR_3 | '/'
467 'non existing xpath' | BOOKSTORE_ANCHOR_3 | BOOKSTORE_ANCHOR_5 | '/non-existing-xpath'
470 def 'Get delta between anchors error scenario: #scenario'() {
471 when: 'attempt to get delta between anchors'
472 objectUnderTest.getDeltaByDataspaceAndAnchors(dataspaceName, sourceAnchor, targetAnchor, '/some-xpath', INCLUDE_ALL_DESCENDANTS)
473 then: 'expected exception is thrown'
474 thrown(expectedException)
475 where: 'following data was used'
476 scenario | dataspaceName | sourceAnchor | targetAnchor || expectedException
477 'invalid dataspace name' | 'Invalid dataspace' | 'not-relevant' | 'not-relevant' || DataValidationException
478 'invalid anchor 1 name' | FUNCTIONAL_TEST_DATASPACE_3 | 'invalid anchor' | 'not-relevant' || DataValidationException
479 'invalid anchor 2 name' | FUNCTIONAL_TEST_DATASPACE_3 | BOOKSTORE_ANCHOR_3 | 'invalid anchor' || DataValidationException
480 'non-existing dataspace' | 'non-existing' | 'not-relevant1' | 'not-relevant2' || DataspaceNotFoundException
481 'non-existing dataspace with same anchor name' | 'non-existing' | 'not-relevant' | 'not-relevant' || DataspaceNotFoundException
482 'non-existing anchor 1' | FUNCTIONAL_TEST_DATASPACE_3 | 'non-existing-anchor' | 'not-relevant' || AnchorNotFoundException
483 'non-existing anchor 2' | FUNCTIONAL_TEST_DATASPACE_3 | BOOKSTORE_ANCHOR_3 | 'non-existing-anchor' || AnchorNotFoundException
486 def 'Get delta between anchors for remove action, where source data node #scenario'() {
487 when: 'attempt to get delta between leaves of data nodes present in 2 anchors'
488 def result = objectUnderTest.getDeltaByDataspaceAndAnchors(FUNCTIONAL_TEST_DATASPACE_3, BOOKSTORE_ANCHOR_5, BOOKSTORE_ANCHOR_3, parentNodeXpath, INCLUDE_ALL_DESCENDANTS)
489 then: 'expected action is present in delta report'
490 assert result.get(0).getAction() == 'remove'
491 where: 'following data was used'
492 scenario | parentNodeXpath
493 'has leaves and child nodes' | "/bookstore/categories[@code='6']"
494 'has leaves only' | "/bookstore/categories[@code='5']/books[@title='Book 11']"
495 'has child data node only' | "/bookstore/support-info/contact-emails"
496 'is empty' | "/bookstore/container-without-leaves"
499 def 'Get delta between anchors for add action, where target data node #scenario'() {
500 when: 'attempt to get delta between leaves of data nodes present in 2 anchors'
501 def result = objectUnderTest.getDeltaByDataspaceAndAnchors(FUNCTIONAL_TEST_DATASPACE_3, BOOKSTORE_ANCHOR_3, BOOKSTORE_ANCHOR_5, parentNodeXpath, INCLUDE_ALL_DESCENDANTS)
502 then: 'the expected action is present in delta report'
503 result.get(0).getAction() == 'add'
504 and: 'the expected xapth is present in delta report'
505 result.get(0).getXpath() == parentNodeXpath
506 where: 'following data was used'
507 scenario | parentNodeXpath
508 'has leaves and child nodes' | "/bookstore/categories[@code='6']"
509 'has leaves only' | "/bookstore/categories[@code='5']/books[@title='Book 11']"
510 'has child data node only' | "/bookstore/support-info/contact-emails"
511 'is empty' | "/bookstore/container-without-leaves"
514 def getDeltaReportEntities(List<DeltaReport> deltaReport) {
517 def sourcePayload = []
518 def targetPayload = []
520 delta -> xpaths.add(delta.getXpath())
521 action.add(delta.getAction())
522 sourcePayload.add(delta.getSourceData())
523 targetPayload.add(delta.getTargetData())
525 return ['xpaths':xpaths, 'action':action, 'sourcePayload':sourcePayload, 'targetPayload':targetPayload]
528 def countDataNodesInBookstore() {
529 return countDataNodesInTree(objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore', INCLUDE_ALL_DESCENDANTS))
532 def countTopLevelListDataNodesInBookstore() {
533 return countDataNodesInTree(objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/', INCLUDE_ALL_DESCENDANTS))