6ecc3a509c4bf007dded4920e30abecf5a753a52
[cps.git] /
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2023-2024 Nordix Foundation
4  *  Modifications Copyright (C) 2023-2025 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.cps
23
24 import org.onap.cps.api.CpsDataService
25 import org.onap.cps.integration.base.FunctionalSpecBase
26 import org.onap.cps.api.parameters.FetchDescendantsOption
27 import org.onap.cps.api.exceptions.AlreadyDefinedException
28 import org.onap.cps.api.exceptions.AnchorNotFoundException
29 import org.onap.cps.api.exceptions.CpsPathException
30 import org.onap.cps.api.exceptions.DataNodeNotFoundException
31 import org.onap.cps.api.exceptions.DataNodeNotFoundExceptionBatch
32 import org.onap.cps.api.exceptions.DataValidationException
33 import org.onap.cps.api.exceptions.DataspaceNotFoundException
34 import org.onap.cps.utils.ContentType
35
36 import static org.onap.cps.api.parameters.FetchDescendantsOption.DIRECT_CHILDREN_ONLY
37 import static org.onap.cps.api.parameters.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
38 import static org.onap.cps.api.parameters.FetchDescendantsOption.OMIT_DESCENDANTS
39
40 class DataServiceIntegrationSpec extends FunctionalSpecBase {
41
42     CpsDataService objectUnderTest
43     def originalCountBookstoreChildNodes
44     def originalCountXmlBookstoreChildNodes
45     def originalCountBookstoreTopLevelListNodes
46
47     def setup() {
48         objectUnderTest = cpsDataService
49         originalCountBookstoreChildNodes = countDataNodesInBookstore()
50         originalCountBookstoreTopLevelListNodes = countTopLevelListDataNodesInBookstore()
51         originalCountXmlBookstoreChildNodes = countXmlDataNodesInBookstore()
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             cpsAnchorService.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, ContentType.JSON)
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, ContentType.JSON)
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, ContentType.JSON)
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, ContentType.JSON)
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 list (element) XML data nodes.'() {
281         given: 'a new (categories) data nodes in XML'
282             def xml = '<categories><code>new1</code><name>SciFii</name></categories>'
283         and: 'saving the new list element'
284             objectUnderTest.saveListElements(FUNCTIONAL_TEST_DATASPACE_4, BOOKSTORE_ANCHOR_6, '/bookstore', xml, now, ContentType.XML)
285         when: 'deleting the new list element'
286             objectUnderTest.deleteListOrListElement(FUNCTIONAL_TEST_DATASPACE_4, BOOKSTORE_ANCHOR_6, '/bookstore/categories[@code="new1"]', now)
287         then: 'the original number of data nodes is restored'
288             assert originalCountXmlBookstoreChildNodes == countXmlDataNodesInBookstore()
289     }
290
291     def 'Add and Delete a batch of list element data nodes.'() {
292         given: 'two new (categories) data nodes in a single batch'
293             def json = '{"categories": [ {"code":"new1"}, {"code":"new2"} ] }'
294         when: 'the batches of new list element(s) are saved'
295             objectUnderTest.saveListElements(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , '/bookstore', json, now, ContentType.JSON)
296         then: 'they can be retrieved by their xpaths'
297             assert objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="new1"]', DIRECT_CHILDREN_ONLY).size() == 1
298             assert objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="new2"]', DIRECT_CHILDREN_ONLY).size() == 1
299         and: 'there are now two extra data nodes'
300             assert originalCountBookstoreChildNodes + 2 == countDataNodesInBookstore()
301         when: 'the new elements are deleted'
302             objectUnderTest.deleteDataNode(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="new1"]', now)
303             objectUnderTest.deleteDataNode(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="new2"]', now)
304         then: 'the original number of data nodes is restored'
305             assert originalCountBookstoreChildNodes == countDataNodesInBookstore()
306     }
307
308     def 'Add and Delete a batch of list element data nodes with partial success.'() {
309         given: 'one existing and one new (categories) data nodes in a single batch'
310             def json = '{"categories": [ {"code":"new1"}, {"code":"1"} ] }'
311         when: 'the batches of new list element(s) are saved'
312             objectUnderTest.saveListElements(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , '/bookstore', json, now, ContentType.JSON)
313         then: 'an already defined (batch) exception is thrown for the existing path'
314             def exceptionThrown = thrown(AlreadyDefinedException)
315             assert exceptionThrown.alreadyDefinedObjectNames ==  ['/bookstore/categories[@code=\'1\']' ] as Set
316         and: 'there is now one extra data node'
317             assert originalCountBookstoreChildNodes + 1 == countDataNodesInBookstore()
318         cleanup:
319             restoreBookstoreDataAnchor(1)
320     }
321
322     def 'Attempt to add empty lists.'() {
323         when: 'the batches of new list element(s) are saved'
324             objectUnderTest.replaceListContent(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , '/bookstore', [ ] as String, now, ContentType.JSON)
325         then: 'an data exception is thrown'
326             thrown(DataValidationException)
327     }
328
329     def 'Add child error scenario: #scenario.'() {
330         when: 'attempt to add a child data node with #scenario'
331             objectUnderTest.saveData(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, parentXpath, json, now)
332         then: 'a #expectedException is thrown'
333             thrown(expectedException)
334         where: 'the following data is used'
335             scenario                 | parentXpath                              | json                                || expectedException
336             'parent does not exist'  | '/bookstore/categories[@code="unknown"]' | '{"books": [ {"title":"new"} ] } '  || DataNodeNotFoundException
337             'already existing child' | '/bookstore'                             | '{"categories": [ {"code":"1"} ] }' || AlreadyDefinedException
338     }
339
340     def 'Add multiple child data nodes with partial success.'() {
341         given: 'one existing and one new list element'
342             def json = '{"categories": [ {"code":"1"}, {"code":"new"} ] }'
343         when: 'attempt to add the elements'
344             objectUnderTest.saveData(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore', json, now)
345         then: 'an already defined (batch) exception is thrown for the existing path'
346             def thrown  = thrown(AlreadyDefinedException)
347             assert thrown.alreadyDefinedObjectNames == [ "/bookstore/categories[@code='1']" ] as Set
348         and: 'the new data node has been added i.e. can be retrieved'
349             assert objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="new"]', DIRECT_CHILDREN_ONLY).size() == 1
350     }
351
352     def 'Replace list content #scenario.'() {
353         given: 'the bookstore categories 1 and 2 exist and have at least 1 child each '
354             assert countDataNodesInTree(objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="1"]', DIRECT_CHILDREN_ONLY)) > 1
355             assert countDataNodesInTree(objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="2"]', DIRECT_CHILDREN_ONLY)) > 1
356         when: 'the categories list is replaced with just category "1" and without child nodes (books)'
357             def json = '{"categories": [ {"code":"' +categoryCode + '"' + childJson + '} ] }'
358             objectUnderTest.replaceListContent(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore', json, now, ContentType.JSON)
359         then: 'the new replaced category can be retrieved but has no children anymore'
360             assert expectedNumberOfDataNodes == countDataNodesInTree(objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="' +categoryCode + '"]', DIRECT_CHILDREN_ONLY))
361         when: 'attempt to retrieve a category (code) not in the new list'
362             objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="2"]', DIRECT_CHILDREN_ONLY)
363         then: 'a datanode not found exception occurs'
364             thrown(DataNodeNotFoundException)
365         cleanup:
366             restoreBookstoreDataAnchor(1)
367         where: 'the following data is used'
368             scenario                        | categoryCode | childJson                                 || expectedNumberOfDataNodes
369             'existing code, no children'    | '1'          | ''                                        || 1
370             'existing code, new child'      | '1'          | ', "books" : [ { "title": "New Book" } ]' || 2
371             'existing code, existing child' | '1'          | ', "books" : [ { "title": "Matilda" } ]'  || 2
372             'new code, new child'           | 'new'        | ', "books" : [ { "title": "New Book" } ]' || 2
373     }
374
375     def 'Replace XML list content #scenario.'() {
376         given: 'the XML bookstore categories 1 exists and has at least 1 child'
377             assert countDataNodesInTree(objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_4, BOOKSTORE_ANCHOR_6, '/bookstore/categories[@code=1]', DIRECT_CHILDREN_ONLY)) > 1
378         when: 'the XML categories list is replaced with just category "1" and a new child'
379             def xmlData = '''<bookstore xmlns="org:onap:cps:sample">
380                               <categories>
381                                   <code>1</code>
382                                   <books>
383                                   <title>New Book</title>
384                                   </books>
385                               </categories>
386                            </bookstore>'''
387             objectUnderTest.replaceListContent(FUNCTIONAL_TEST_DATASPACE_4, BOOKSTORE_ANCHOR_6, '/bookstore', xmlData, now, ContentType.XML)
388         then: 'the replaced XML category has one child'
389             assert 2 == countDataNodesInTree(objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_4, BOOKSTORE_ANCHOR_6, '/bookstore/categories[@code=1]', DIRECT_CHILDREN_ONLY))
390         when: 'attempting to retrieve a non-existent category'
391             objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_4, BOOKSTORE_ANCHOR_6, '/bookstore/categories[@code="2"]', DIRECT_CHILDREN_ONLY)
392         then: 'a datanode not found exception occurs'
393             thrown(DataNodeNotFoundException)
394         cleanup:
395             restoreBookstoreDataAnchor(1)
396     }
397
398     def 'Update data node leaves for node that has no leaves (yet).'() {
399         given: 'new (webinfo) datanode without leaves'
400             def json = '{"webinfo": {} }'
401             objectUnderTest.saveData(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , '/bookstore', json, now)
402         when: 'update is performed to add a leaf'
403             def updatedJson = '{"webinfo": {"domain-name":"new leaf data"}}'
404             objectUnderTest.updateNodeLeaves(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, "/bookstore", updatedJson, now, ContentType.JSON)
405         then: 'the updated data nodes are retrieved'
406             def result = cpsDataService.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, "/bookstore/webinfo", INCLUDE_ALL_DESCENDANTS)
407         and: 'the leaf value is updated as expected'
408             assert result.leaves['domain-name'] == ['new leaf data']
409         cleanup:
410             restoreBookstoreDataAnchor(1)
411     }
412
413     def 'Update multiple data leaves error scenario: #scenario.'() {
414         when: 'attempt to update data node for #scenario'
415             objectUnderTest.updateNodeLeaves(dataspaceName, anchorName, xpath, 'irrelevant json data', now, ContentType.JSON)
416         then: 'a #expectedException is thrown'
417             thrown(expectedException)
418         where: 'the following data is used'
419             scenario                 | dataspaceName               | anchorName                 | xpath           || expectedException
420             'invalid dataspace name' | 'Invalid Dataspace'         | 'not-relevant'             | '/not relevant' || DataValidationException
421             'invalid anchor name'    | FUNCTIONAL_TEST_DATASPACE_1 | 'INVALID ANCHOR'           | '/not relevant' || DataValidationException
422             'non-existing dataspace' | 'non-existing-dataspace'    | 'not-relevant'             | '/not relevant' || DataspaceNotFoundException
423             'non-existing anchor'    | FUNCTIONAL_TEST_DATASPACE_1 | 'non-existing-anchor'      | '/not relevant' || AnchorNotFoundException
424             'non-existing-xpath'     | FUNCTIONAL_TEST_DATASPACE_1 | BOOKSTORE_ANCHOR_1         | '/non-existing' || DataValidationException
425     }
426
427     def 'Update data nodes and descendants.'() {
428         given: 'some web info for the bookstore'
429             def json = '{"webinfo": {"domain-name":"ourbookstore.com" ,"contact-email":"info@ourbookstore.com" }}'
430             objectUnderTest.saveData(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , '/bookstore', json, now)
431         when: 'the webinfo (container) is updated'
432             json = '{"webinfo": {"domain-name":"newdomain.com" ,"contact-email":"info@newdomain.com" }}'
433             objectUnderTest.updateDataNodeAndDescendants(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore', json, now, ContentType.JSON)
434         then: 'webinfo has been updated with teh new details'
435             def result = objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/webinfo', DIRECT_CHILDREN_ONLY)
436             result.leaves.'domain-name'[0] == 'newdomain.com'
437             result.leaves.'contact-email'[0] == 'info@newdomain.com'
438         cleanup:
439             restoreBookstoreDataAnchor(1)
440     }
441
442     def 'Update bookstore top-level container data node.'() {
443         when: 'the bookstore top-level container is updated'
444             def json = '{ "bookstore": { "bookstore-name": "new bookstore" }}'
445             objectUnderTest.updateDataNodeAndDescendants(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/', json, now, ContentType.JSON)
446         then: 'bookstore name has been updated'
447             def result = objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore', DIRECT_CHILDREN_ONLY)
448             result.leaves.'bookstore-name'[0] == 'new bookstore'
449         cleanup:
450             restoreBookstoreDataAnchor(1)
451     }
452
453     def 'Update multiple data node leaves.'() {
454         given: 'Updated json for bookstore data'
455             def jsonData =  "{'book-store:books':{'lang':'English/French','price':100,'title':'Matilda'}}"
456         when: 'update is performed for leaves'
457             objectUnderTest.updateNodeLeaves(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_2, "/bookstore/categories[@code='1']", jsonData, now, ContentType.JSON)
458         then: 'the updated data nodes are retrieved'
459             def result = cpsDataService.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_2, "/bookstore/categories[@code=1]/books[@title='Matilda']", INCLUDE_ALL_DESCENDANTS)
460         and: 'the leaf values are updated as expected'
461             assert result[0].leaves['lang'] == 'English/French'
462             assert result[0].leaves['price'] == 100
463         cleanup:
464             restoreBookstoreDataAnchor(2)
465     }
466
467     def 'Update bookstore top-level XML container data node.'() {
468         given: 'Updated xml for bookstore data'
469             def xml = '<categories><code>1</code><name>Gothic</name></categories>'
470         when: 'updating the data node'
471             objectUnderTest.updateDataNodeAndDescendants(FUNCTIONAL_TEST_DATASPACE_4, BOOKSTORE_ANCHOR_6, '/bookstore', xml, now, ContentType.XML)
472         then: 'the updated data nodes are retrieved'
473             def result = objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_4, BOOKSTORE_ANCHOR_6, '/bookstore/categories[@code=1]', DIRECT_CHILDREN_ONLY)
474         and: 'the leaf values are updated as expected'
475             assert result.leaves.'name'[0] == 'Gothic'
476         cleanup:
477             restoreBookstoreXmlDataAnchor(1)
478     }
479
480     def 'Update multiple XML data node leaves.'() {
481         given: 'XML for bookstore data with updated lang and price leaves'
482             def xmlData = '''<books>
483                       <title>2001: A Space Odyssey</title>
484                       <lang>english</lang>
485                       <authors>Iain M. Banks</authors>
486                       <editions>1994</editions>
487                       <price>995</price>
488                       </books> '''
489         when: 'updating node leaves'
490             objectUnderTest.updateNodeLeaves(FUNCTIONAL_TEST_DATASPACE_4, BOOKSTORE_ANCHOR_6, '/bookstore/categories[@code=1]', xmlData, now, ContentType.XML)
491         then: 'the updated data nodes are retrieved'
492             def result = cpsDataService.getDataNodes(FUNCTIONAL_TEST_DATASPACE_4, BOOKSTORE_ANCHOR_6, '/bookstore/categories[@code=1]/books[@title="2001: A Space Odyssey"]', INCLUDE_ALL_DESCENDANTS)
493         and: 'the leaf values are updated as expected'
494             assert result[0].leaves['lang'] == 'english'
495             assert result[0].leaves['price'] == 995
496     }
497
498     def 'Order of leaf-list elements is preserved when "ordered-by user" is set in the YANG model.'() {
499         given: 'Updated json for bookstore data'
500             def jsonData =  "{'book-store:books':{'title':'Matilda', 'authors': ['beta', 'alpha', 'gamma', 'delta']}}"
501         when: 'update is performed for leaves'
502             objectUnderTest.updateNodeLeaves(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_2, "/bookstore/categories[@code='1']", jsonData, now, ContentType.JSON)
503         and: 'the updated data nodes are retrieved'
504             def result = cpsDataService.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_2, "/bookstore/categories[@code=1]/books[@title='Matilda']", INCLUDE_ALL_DESCENDANTS)
505         then: 'the leaf-list values have expected order'
506             assert result[0].leaves['authors'] == ['beta', 'alpha', 'gamma', 'delta']
507         cleanup:
508             restoreBookstoreDataAnchor(2)
509     }
510
511     def 'Leaf-list elements are sorted when "ordered-by user" is not set in the YANG model.'() {
512         given: 'Updated json for bookstore data'
513             def jsonData =  "{'book-store:books':{'title':'Matilda', 'editions': [2011, 1988, 2001, 2022, 2025]}}"
514         when: 'update is performed for leaves'
515             objectUnderTest.updateNodeLeaves(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_2, "/bookstore/categories[@code='1']", jsonData, now, ContentType.JSON)
516         and: 'the updated data nodes are retrieved'
517             def result = cpsDataService.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_2, "/bookstore/categories[@code=1]/books[@title='Matilda']", INCLUDE_ALL_DESCENDANTS)
518         then: 'the leaf-list values have natural order'
519             assert result[0].leaves['editions'] == [1988, 2001, 2011, 2022, 2025]
520         cleanup:
521             restoreBookstoreDataAnchor(2)
522     }
523
524     def countDataNodesInBookstore() {
525         return countDataNodesInTree(objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore', INCLUDE_ALL_DESCENDANTS))
526     }
527
528     def countTopLevelListDataNodesInBookstore() {
529         return countDataNodesInTree(objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/', INCLUDE_ALL_DESCENDANTS))
530     }
531
532     def countXmlDataNodesInBookstore() {
533         return countDataNodesInTree(objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_4, BOOKSTORE_ANCHOR_6, '/bookstore', INCLUDE_ALL_DESCENDANTS))
534     }
535 }