Merge "Fix: Make bookstore data consistent"
[cps.git] / integration-test / src / test / groovy / org / onap / cps / integration / functional / CpsDataServiceIntegrationSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2023 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.integration.functional
23
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
36 import java.time.OffsetDateTime
37
38 import static org.onap.cps.spi.FetchDescendantsOption.DIRECT_CHILDREN_ONLY
39 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
40 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
41
42 class CpsDataServiceIntegrationSpec extends FunctionalSpecBase {
43
44     CpsDataService objectUnderTest
45     def originalCountBookstoreChildNodes
46     def originalCountBookstoreTopLevelListNodes
47     def now = OffsetDateTime.now()
48
49     def setup() {
50         objectUnderTest = cpsDataService
51         originalCountBookstoreChildNodes = countDataNodesInBookstore()
52         originalCountBookstoreTopLevelListNodes = countTopLevelListDataNodesInBookstore()
53     }
54
55     def 'Read bookstore top-level container(s) using #fetchDescendantsOption.'() {
56         when: 'get data nodes for bookstore container'
57             def result = objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore', fetchDescendantsOption)
58         then: 'the tree consist ouf of #expectNumberOfDataNodes data nodes'
59             assert countDataNodesInTree(result) == expectNumberOfDataNodes
60         and: 'the top level data node has the expected attribute and value'
61             assert result.leaves['bookstore-name'] == ['Easons']
62         and: 'they are from the correct dataspace'
63             assert result.dataspace == [FUNCTIONAL_TEST_DATASPACE_1]
64         and: 'they are from the correct anchor'
65             assert result.anchorName == [BOOKSTORE_ANCHOR_1]
66         where: 'the following option is used'
67             fetchDescendantsOption        || expectNumberOfDataNodes
68             OMIT_DESCENDANTS              || 1
69             DIRECT_CHILDREN_ONLY          || 7
70             INCLUDE_ALL_DESCENDANTS       || 28
71             new FetchDescendantsOption(2) || 28
72     }
73
74     def 'Read bookstore top-level container(s) using "root" path variations.'() {
75         when: 'get data nodes for bookstore container'
76             def result = objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, root, OMIT_DESCENDANTS)
77         then: 'the tree consist ouf of one data node'
78             assert countDataNodesInTree(result) == 2
79         and: 'the top level data node has the expected attribute and value'
80             assert result.leaves.size() == 2
81         where: 'the following variations of "root" are used'
82             root << [ '/', '' ]
83     }
84
85     def 'Read data nodes with error: #cpsPath'() {
86         when: 'attempt to get data nodes using invalid path'
87             objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, DIRECT_CHILDREN_ONLY)
88         then: 'a #expectedException is thrown'
89             thrown(expectedException)
90         where:
91             cpsPath              || expectedException
92             'invalid path'       || CpsPathException
93             '/non-existing-path' || DataNodeNotFoundException
94     }
95
96     def 'Read (multiple) data nodes (batch) with #cpsPath'() {
97         when: 'attempt to get data nodes using invalid path'
98             objectUnderTest.getDataNodesForMultipleXpaths(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, [ cpsPath ], DIRECT_CHILDREN_ONLY)
99         then: 'no exception is thrown'
100             noExceptionThrown()
101         where:
102             cpsPath << [ 'invalid path', '/non-existing-path' ]
103     }
104
105     def 'Delete root data node.'() {
106         when: 'the "root" is deleted'
107             objectUnderTest.deleteDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, [ '/' ], now)
108         and: 'attempt to get the top level data node'
109             objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore', DIRECT_CHILDREN_ONLY)
110         then: 'an datanode not found exception is thrown'
111             thrown(DataNodeNotFoundException)
112         cleanup:
113             restoreBookstoreDataAnchor(1)
114     }
115
116     def 'Get whole list data' () {
117             def xpathForWholeList = "/bookstore/categories"
118         when: 'get data nodes for bookstore container'
119             def dataNodes = objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, xpathForWholeList, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS)
120         then: 'the tree consist ouf of #expectNumberOfDataNodes data nodes'
121             assert dataNodes.size() == 5
122         and: 'each datanode contains the list node xpath partially in its xpath'
123             dataNodes.each {dataNode ->
124                 assert dataNode.xpath.contains(xpathForWholeList)
125             }
126     }
127
128     def 'Read (multiple) data nodes with #scenario' () {
129         when: 'attempt to get data nodes using multiple valid xpaths'
130             def dataNodes = objectUnderTest.getDataNodesForMultipleXpaths(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, xpath, OMIT_DESCENDANTS)
131         then: 'expected numer of data nodes are returned'
132             dataNodes.size() == expectedNumberOfDataNodes
133         where: 'the following data was used'
134                     scenario                    |                       xpath                                       |   expectedNumberOfDataNodes
135             'container-node xpath'              | ['/bookstore']                                                    |               1
136             'list-item'                         | ['/bookstore/categories[@code=1]']                                |               1
137             'parent-list xpath'                 | ['/bookstore/categories']                                         |               5
138             'child-list xpath'                  | ['/bookstore/categories[@code=1]/books']                          |               2
139             'both parent and child list xpath'  | ['/bookstore/categories', '/bookstore/categories[@code=1]/books'] |               7
140     }
141
142     def 'Add and Delete a (container) data node using #scenario.'() {
143             when: 'the new datanode is saved'
144                 objectUnderTest.saveData(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , parentXpath, json, now)
145             then: 'it can be retrieved by its normalized xpath'
146                 def result = objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, normalizedXpathToNode, DIRECT_CHILDREN_ONLY)
147                 assert result.size() == 1
148                 assert result[0].xpath == normalizedXpathToNode
149             and: 'there is now one extra datanode'
150                 assert originalCountBookstoreChildNodes + 1 == countDataNodesInBookstore()
151             when: 'the new datanode is deleted'
152                 objectUnderTest.deleteDataNode(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, normalizedXpathToNode, now)
153             then: 'the original number of data nodes is restored'
154                 assert originalCountBookstoreChildNodes == countDataNodesInBookstore()
155             where:
156                 scenario                      | parentXpath                         | json                                                                                        || normalizedXpathToNode
157                 'normalized parent xpath'     | '/bookstore'                        | '{"webinfo": {"domain-name":"ourbookstore.com", "contact-email":"info@ourbookstore.com" }}' || "/bookstore/webinfo"
158                 'non-normalized parent xpath' | '/bookstore/categories[ @code="1"]' | '{"books": {"title":"new" }}'                                                               || "/bookstore/categories[@code='1']/books[@title='new']"
159     }
160
161     def 'Attempt to create a top level data node using root.'() {
162         given: 'a new anchor'
163             cpsAdminService.createAnchor(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_SCHEMA_SET, 'newAnchor1');
164         when: 'attempt to save new top level datanode'
165             def json = '{"bookstore": {"bookstore-name": "New Store"} }'
166             objectUnderTest.saveData(FUNCTIONAL_TEST_DATASPACE_1, 'newAnchor1' , '/', json, now)
167         then: 'since there is no data a data node not found exception is thrown'
168             thrown(DataNodeNotFoundException)
169     }
170
171     def 'Attempt to save top level data node that already exist'() {
172         when: 'attempt to save already existing top level node'
173             def json = '{"bookstore": {} }'
174             objectUnderTest.saveData(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, json, now)
175         then: 'an exception that (one cps paths is)  already defined is thrown '
176             def exceptionThrown = thrown(AlreadyDefinedException)
177             exceptionThrown.alreadyDefinedObjectNames == ['/bookstore' ] as Set
178         cleanup:
179             restoreBookstoreDataAnchor(1)
180     }
181
182     def 'Delete a single datanode with invalid path.'() {
183         when: 'attempt to delete a single datanode with invalid path'
184             objectUnderTest.deleteDataNode(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/invalid path', now)
185         then: 'a cps path parser exception is thrown'
186             thrown(CpsPathException)
187     }
188
189     def 'Delete multiple data nodes with invalid path.'() {
190         when: 'attempt to delete datanode collection with invalid path'
191             objectUnderTest.deleteDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, ['/invalid path'], now)
192         then: 'the error is silently ignored'
193             noExceptionThrown()
194     }
195
196     def 'Delete single data node with non-existing path.'() {
197         when: 'attempt to delete a single datanode non-existing invalid path'
198             objectUnderTest.deleteDataNode(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/does/not/exist', now)
199         then: 'a datanode not found exception is thrown'
200             thrown(DataNodeNotFoundException)
201     }
202
203     def 'Delete multiple data nodes with non-existing path(s).'() {
204         when: 'attempt to delete a single datanode non-existing invalid path'
205             objectUnderTest.deleteDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, ['/does/not/exist'], now)
206         then: 'a  datanode not found (batch) exception is thrown'
207             thrown(DataNodeNotFoundExceptionBatch)
208     }
209
210     def 'Add and Delete top-level list (element) data nodes with root node.'() {
211         given: 'a new (multiple-data-tree:invoice) datanodes'
212             def json = '{"bookstore-address":[{"bookstore-name":"Scholastic","address":"Bangalore,India","postal-code":"560043"}]}'
213         when: 'the new list elements are saved'
214             objectUnderTest.saveListElements(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , '/', json, now)
215         then: 'they can be retrieved by their xpaths'
216             objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , '/bookstore-address[@bookstore-name="Easons"]', INCLUDE_ALL_DESCENDANTS)
217         and: 'there is one extra datanode'
218             assert originalCountBookstoreTopLevelListNodes + 1 == countTopLevelListDataNodesInBookstore()
219         when: 'the new elements are deleted'
220             objectUnderTest.deleteDataNode(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , '/bookstore-address[@bookstore-name="Easons"]', now)
221         then: 'the original number of datanodes is restored'
222             assert originalCountBookstoreTopLevelListNodes == countTopLevelListDataNodesInBookstore()
223     }
224
225     def 'Add and Delete list (element) data nodes.'() {
226         given: 'two new (categories) data nodes'
227             def json = '{"categories": [ {"code":"new1"}, {"code":"new2" } ] }'
228         when: 'the new list elements are saved'
229             objectUnderTest.saveListElements(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , '/bookstore', json, now)
230         then: 'they can be retrieved by their xpaths'
231             objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="new1"]', DIRECT_CHILDREN_ONLY).size() == 1
232             objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="new2"]', DIRECT_CHILDREN_ONLY).size() == 1
233         and: 'there are now two extra data nodes'
234             assert originalCountBookstoreChildNodes + 2 == countDataNodesInBookstore()
235         when: 'the new elements are deleted'
236             objectUnderTest.deleteDataNode(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="new1"]', now)
237             objectUnderTest.deleteDataNode(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="new2"]', now)
238         then: 'the original number of data nodes is restored'
239             assert originalCountBookstoreChildNodes == countDataNodesInBookstore()
240     }
241
242     def 'Add list (element) data nodes that already exist.'() {
243         given: 'two (categories) data nodes, one new and one existing'
244             def json = '{"categories": [ {"code":"1"}, {"code":"new1"} ] }'
245         when: 'attempt to save the list element'
246             objectUnderTest.saveListElements(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , '/bookstore', json, now)
247         then: 'an exception that (one cps paths is)  already defined is thrown '
248             def exceptionThrown = thrown(AlreadyDefinedException)
249             exceptionThrown.alreadyDefinedObjectNames == ['/bookstore/categories[@code=\'1\']' ] as Set
250         and: 'there is now one extra data nodes'
251             assert originalCountBookstoreChildNodes + 1 == countDataNodesInBookstore()
252         cleanup:
253             restoreBookstoreDataAnchor(1)
254     }
255
256     def 'Add and Delete list (element) data nodes using lists specific method.'() {
257         given: 'a new (categories) data nodes'
258             def json = '{"categories": [ {"code":"new1"} ] }'
259         and: 'the new list element is saved'
260             objectUnderTest.saveListElements(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , '/bookstore', json, now)
261         when: 'the new element is deleted'
262             objectUnderTest.deleteListOrListElement(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="new1"]', now)
263         then: 'the original number of data nodes is restored'
264             assert originalCountBookstoreChildNodes == countDataNodesInBookstore()
265     }
266
267     def 'Add and Delete a batch of lists (element) data nodes.'() {
268         given: 'two new (categories) data nodes in two separate batches'
269             def json1 = '{"categories": [ {"code":"new1"} ] }'
270             def json2 = '{"categories": [ {"code":"new2"} ] } '
271         when: 'the batches of new list element(s) are saved'
272             objectUnderTest.saveListElementsBatch(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , '/bookstore', [json1, json2], now)
273         then: 'they can be retrieved by their xpaths'
274             assert objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="new1"]', DIRECT_CHILDREN_ONLY).size() == 1
275             assert objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="new2"]', DIRECT_CHILDREN_ONLY).size() == 1
276         and: 'there are now two extra data nodes'
277             assert originalCountBookstoreChildNodes + 2 == countDataNodesInBookstore()
278         when: 'the new elements are deleted'
279             objectUnderTest.deleteDataNode(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="new1"]', now)
280             objectUnderTest.deleteDataNode(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="new2"]', now)
281         then: 'the original number of data nodes is restored'
282             assert originalCountBookstoreChildNodes == countDataNodesInBookstore()
283     }
284
285     def 'Add and Delete a batch of lists (element) data nodes with partial success.'() {
286         given: 'two new (categories) data nodes in two separate batches'
287             def jsonNewElement = '{"categories": [ {"code":"new1"} ] }'
288             def jsonExistingElement = '{"categories": [ {"code":"1"} ] } '
289         when: 'the batches of new list element(s) are saved'
290             objectUnderTest.saveListElementsBatch(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , '/bookstore', [jsonNewElement, jsonExistingElement], now)
291         then: 'an already defined (batch) exception is thrown for the existing path'
292             def exceptionThrown = thrown(AlreadyDefinedException)
293             assert exceptionThrown.alreadyDefinedObjectNames ==  ['/bookstore/categories[@code=\'1\']' ] as Set
294         and: 'there is now one extra data node'
295             assert originalCountBookstoreChildNodes + 1 == countDataNodesInBookstore()
296         cleanup:
297             restoreBookstoreDataAnchor(1)
298     }
299
300     def 'Attempt to add empty lists.'() {
301         when: 'the batches of new list element(s) are saved'
302             objectUnderTest.replaceListContent(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , '/bookstore', [ ], now)
303         then: 'an admin exception is thrown'
304             thrown(CpsAdminException)
305     }
306
307     def 'Add child error scenario: #scenario.'() {
308         when: 'attempt to add a child data node with #scenario'
309             objectUnderTest.saveData(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, parentXpath, json, now)
310         then: 'a #expectedException is thrown'
311             thrown(expectedException)
312         where: 'the following data is used'
313             scenario                 | parentXpath                              | json                                || expectedException
314             'parent does not exist'  | '/bookstore/categories[@code="unknown"]' | '{"books": [ {"title":"new"} ] } '  || DataNodeNotFoundException
315             'already existing child' | '/bookstore'                             | '{"categories": [ {"code":"1"} ] }' || AlreadyDefinedException
316     }
317
318     def 'Add multiple child data nodes with partial success.'() {
319         given: 'one existing and one new list element'
320             def json = '{"categories": [ {"code":"1"}, {"code":"new"} ] }'
321         when: 'attempt to add the elements'
322             objectUnderTest.saveData(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore', json, now)
323         then: 'an already defined (batch) exception is thrown for the existing path'
324             def thrown  = thrown(AlreadyDefinedException)
325             assert thrown.alreadyDefinedObjectNames == [ "/bookstore/categories[@code='1']" ] as Set
326         and: 'the new data node has been added i.e. can be retrieved'
327             assert objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="new"]', DIRECT_CHILDREN_ONLY).size() == 1
328     }
329
330     def 'Replace list content #scenario.'() {
331         given: 'the bookstore categories 1 and 2 exist and have at least 1 child each '
332             assert countDataNodesInTree(objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="1"]', DIRECT_CHILDREN_ONLY)) > 1
333             assert countDataNodesInTree(objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="2"]', DIRECT_CHILDREN_ONLY)) > 1
334         when: 'the categories list is replaced with just category "1" and without child nodes (books)'
335             def json = '{"categories": [ {"code":"' +categoryCode + '"' + childJson + '} ] }'
336             objectUnderTest.replaceListContent(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore', json, now)
337         then: 'the new replaced category can be retrieved but has no children anymore'
338             assert expectedNumberOfDataNodes == countDataNodesInTree(objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="' +categoryCode + '"]', DIRECT_CHILDREN_ONLY))
339         when: 'attempt to retrieve a category (code) not in the new list'
340             objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="2"]', DIRECT_CHILDREN_ONLY)
341         then: 'a datanode not found exception occurs'
342             thrown(DataNodeNotFoundException)
343         cleanup:
344             restoreBookstoreDataAnchor(1)
345         where: 'the following data is used'
346             scenario                        | categoryCode | childJson                                 || expectedNumberOfDataNodes
347             'existing code, no children'    | '1'          | ''                                        || 1
348             'existing code, new child'      | '1'          | ', "books" : [ { "title": "New Book" } ]' || 2
349             'existing code, existing child' | '1'          | ', "books" : [ { "title": "Matilda" } ]'  || 2
350             'new code, new child'           | 'new'        | ', "books" : [ { "title": "New Book" } ]' || 2
351     }
352
353     def 'Update multiple data node leaves.'() {
354         given: 'Updated json for bookstore data'
355             def jsonData =  "{'book-store:books':{'lang':'English/French','price':100,'title':'Matilda'}}"
356         when: 'update is performed for leaves'
357             objectUnderTest.updateNodeLeaves(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_2, "/bookstore/categories[@code='1']", jsonData, now)
358         then: 'the updated data nodes are retrieved'
359             def result = cpsDataService.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_2, "/bookstore/categories[@code=1]/books[@title='Matilda']", INCLUDE_ALL_DESCENDANTS)
360         and: 'the leaf values are updated as expected'
361             assert result.leaves['lang'] == ['English/French']
362             assert result.leaves['price'] == [100]
363         cleanup:
364             restoreBookstoreDataAnchor(2)
365     }
366
367     def 'Update data node leaves for node that has no leaves (yet).'() {
368         given: 'new (webinfo) datanode without leaves'
369             def json = '{"webinfo": {} }'
370             objectUnderTest.saveData(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , '/bookstore', json, now)
371         when: 'update is performed to add a leaf'
372             def updatedJson = '{"webinfo": {"domain-name":"new leaf data"}}'
373             objectUnderTest.updateNodeLeaves(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, "/bookstore", updatedJson, now)
374         then: 'the updated data nodes are retrieved'
375             def result = cpsDataService.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, "/bookstore/webinfo", INCLUDE_ALL_DESCENDANTS)
376         and: 'the leaf value is updated as expected'
377             assert result.leaves['domain-name'] == ['new leaf data']
378         cleanup:
379             restoreBookstoreDataAnchor(1)
380     }
381
382     def 'Update multiple data leaves error scenario: #scenario.'() {
383         when: 'attempt to update data node for #scenario'
384             objectUnderTest.updateNodeLeaves(dataspaceName, anchorName, xpath, 'irrelevant json data', now)
385         then: 'a #expectedException is thrown'
386             thrown(expectedException)
387         where: 'the following data is used'
388             scenario                 | dataspaceName               | anchorName                 | xpath           || expectedException
389             'invalid dataspace name' | 'Invalid Dataspace'         | 'not-relevant'             | '/not relevant' || DataValidationException
390             'invalid anchor name'    | FUNCTIONAL_TEST_DATASPACE_1 | 'INVALID ANCHOR'           | '/not relevant' || DataValidationException
391             'non-existing dataspace' | 'non-existing-dataspace'    | 'not-relevant'             | '/not relevant' || DataspaceNotFoundException
392             'non-existing anchor'    | FUNCTIONAL_TEST_DATASPACE_1 | 'non-existing-anchor'      | '/not relevant' || AnchorNotFoundException
393             'non-existing-xpath'     | FUNCTIONAL_TEST_DATASPACE_1 | BOOKSTORE_ANCHOR_1         | '/non-existing' || DataValidationException
394     }
395
396     def 'Update data nodes and descendants.'() {
397         given: 'some web info for the bookstore'
398             def json = '{"webinfo": {"domain-name":"ourbookstore.com" ,"contact-email":"info@ourbookstore.com" }}'
399             objectUnderTest.saveData(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , '/bookstore', json, now)
400         when: 'the webinfo (container) is updated'
401             json = '{"webinfo": {"domain-name":"newdomain.com" ,"contact-email":"info@newdomain.com" }}'
402             objectUnderTest.updateDataNodeAndDescendants(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore', json, now)
403         then: 'webinfo has been updated with teh new details'
404             def result = objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/webinfo', DIRECT_CHILDREN_ONLY)
405             result.leaves.'domain-name'[0] == 'newdomain.com'
406             result.leaves.'contact-email'[0] == 'info@newdomain.com'
407         cleanup:
408             restoreBookstoreDataAnchor(1)
409     }
410
411     def countDataNodesInBookstore() {
412         return countDataNodesInTree(objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore', INCLUDE_ALL_DESCENDANTS))
413     }
414
415     def countTopLevelListDataNodesInBookstore() {
416         return countDataNodesInTree(objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/', INCLUDE_ALL_DESCENDANTS))
417     }
418 }