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