017ede7de26f75512671cbf8bdb6f66ad95ec0a7
[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 import org.onap.cps.spi.model.DeltaReport
36
37 import java.time.OffsetDateTime
38
39 import static org.onap.cps.spi.FetchDescendantsOption.DIRECT_CHILDREN_ONLY
40 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
41 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
42
43 class CpsDataServiceIntegrationSpec extends FunctionalSpecBase {
44
45     CpsDataService objectUnderTest
46     def originalCountBookstoreChildNodes
47     def originalCountBookstoreTopLevelListNodes
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-1']
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 correct number of data nodes'
78             assert countDataNodesInTree(result) == 2
79         and: 'the top level data node has the expected number of leaves'
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 'Get data nodes error scenario #scenario'() {
106         when: 'attempt to retrieve data nodes'
107             objectUnderTest.getDataNodes(dataspaceName, anchorName, xpath, OMIT_DESCENDANTS)
108         then: 'expected exception is thrown'
109             thrown(expectedException)
110         where: 'following data is used'
111             scenario                 | dataspaceName                | anchorName        | xpath           || expectedException
112             'non existent dataspace' | 'non-existent'               | 'not-relevant'    | '/not-relevant' || DataspaceNotFoundException
113             'non existent anchor'    | FUNCTIONAL_TEST_DATASPACE_1  | 'non-existent'    | '/not-relevant' || AnchorNotFoundException
114             'non-existent xpath'     | FUNCTIONAL_TEST_DATASPACE_1  | BOOKSTORE_ANCHOR_1| '/non-existing' || DataNodeNotFoundException
115             'invalid-dataspace'      | 'Invalid dataspace'          | 'not-relevant'    | '/not-relevant' || DataValidationException
116             'invalid-dataspace'      | FUNCTIONAL_TEST_DATASPACE_1  | 'Invalid Anchor'  | '/not-relevant' || DataValidationException
117     }
118
119     def 'Delete root data node.'() {
120         when: 'the "root" is deleted'
121             objectUnderTest.deleteDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, [ '/' ], now)
122         and: 'attempt to get the top level data node'
123             objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore', DIRECT_CHILDREN_ONLY)
124         then: 'an datanode not found exception is thrown'
125             thrown(DataNodeNotFoundException)
126         cleanup:
127             restoreBookstoreDataAnchor(1)
128     }
129
130     def 'Get whole list data' () {
131             def xpathForWholeList = "/bookstore/categories"
132         when: 'get data nodes for bookstore container'
133             def dataNodes = objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, xpathForWholeList, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS)
134         then: 'the tree consist ouf of #expectNumberOfDataNodes data nodes'
135             assert dataNodes.size() == 5
136         and: 'each datanode contains the list node xpath partially in its xpath'
137             dataNodes.each {dataNode ->
138                 assert dataNode.xpath.contains(xpathForWholeList)
139             }
140     }
141
142     def 'Read (multiple) data nodes with #scenario' () {
143         when: 'attempt to get data nodes using multiple valid xpaths'
144             def dataNodes = objectUnderTest.getDataNodesForMultipleXpaths(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, xpath, OMIT_DESCENDANTS)
145         then: 'expected numer of data nodes are returned'
146             dataNodes.size() == expectedNumberOfDataNodes
147         where: 'the following data was used'
148                     scenario                    |                       xpath                                       |   expectedNumberOfDataNodes
149             'container-node xpath'              | ['/bookstore']                                                    |               1
150             'list-item'                         | ['/bookstore/categories[@code=1]']                                |               1
151             'parent-list xpath'                 | ['/bookstore/categories']                                         |               5
152             'child-list xpath'                  | ['/bookstore/categories[@code=1]/books']                          |               2
153             'both parent and child list xpath'  | ['/bookstore/categories', '/bookstore/categories[@code=1]/books'] |               7
154     }
155
156     def 'Add and Delete a (container) data node using #scenario.'() {
157             when: 'the new datanode is saved'
158                 objectUnderTest.saveData(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , parentXpath, json, now)
159             then: 'it can be retrieved by its normalized xpath'
160                 def result = objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, normalizedXpathToNode, DIRECT_CHILDREN_ONLY)
161                 assert result.size() == 1
162                 assert result[0].xpath == normalizedXpathToNode
163             and: 'there is now one extra datanode'
164                 assert originalCountBookstoreChildNodes + 1 == countDataNodesInBookstore()
165             when: 'the new datanode is deleted'
166                 objectUnderTest.deleteDataNode(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, normalizedXpathToNode, now)
167             then: 'the original number of data nodes is restored'
168                 assert originalCountBookstoreChildNodes == countDataNodesInBookstore()
169             where:
170                 scenario                      | parentXpath                         | json                                                                                        || normalizedXpathToNode
171                 'normalized parent xpath'     | '/bookstore'                        | '{"webinfo": {"domain-name":"ourbookstore.com", "contact-email":"info@ourbookstore.com" }}' || "/bookstore/webinfo"
172                 'non-normalized parent xpath' | '/bookstore/categories[ @code="1"]' | '{"books": {"title":"new" }}'                                                               || "/bookstore/categories[@code='1']/books[@title='new']"
173     }
174
175     def 'Attempt to create a top level data node using root.'() {
176         given: 'a new anchor'
177             cpsAdminService.createAnchor(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_SCHEMA_SET, 'newAnchor1');
178         when: 'attempt to save new top level datanode'
179             def json = '{"bookstore": {"bookstore-name": "New Store"} }'
180             objectUnderTest.saveData(FUNCTIONAL_TEST_DATASPACE_1, 'newAnchor1' , '/', json, now)
181         then: 'since there is no data a data node not found exception is thrown'
182             thrown(DataNodeNotFoundException)
183     }
184
185     def 'Attempt to save top level data node that already exist'() {
186         when: 'attempt to save already existing top level node'
187             def json = '{"bookstore": {} }'
188             objectUnderTest.saveData(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, json, now)
189         then: 'an exception that (one cps paths is)  already defined is thrown '
190             def exceptionThrown = thrown(AlreadyDefinedException)
191             exceptionThrown.alreadyDefinedObjectNames == ['/bookstore' ] as Set
192         cleanup:
193             restoreBookstoreDataAnchor(1)
194     }
195
196     def 'Delete a single datanode with invalid path.'() {
197         when: 'attempt to delete a single datanode with invalid path'
198             objectUnderTest.deleteDataNode(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/invalid path', now)
199         then: 'a cps path parser exception is thrown'
200             thrown(CpsPathException)
201     }
202
203     def 'Delete multiple data nodes with invalid path.'() {
204         when: 'attempt to delete datanode collection with invalid path'
205             objectUnderTest.deleteDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, ['/invalid path'], now)
206         then: 'the error is silently ignored'
207             noExceptionThrown()
208     }
209
210     def 'Delete single data node with non-existing path.'() {
211         when: 'attempt to delete a single datanode non-existing invalid path'
212             objectUnderTest.deleteDataNode(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/does/not/exist', now)
213         then: 'a datanode not found exception is thrown'
214             thrown(DataNodeNotFoundException)
215     }
216
217     def 'Delete multiple data nodes with non-existing path(s).'() {
218         when: 'attempt to delete a single datanode non-existing invalid path'
219             objectUnderTest.deleteDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, ['/does/not/exist'], now)
220         then: 'a  datanode not found (batch) exception is thrown'
221             thrown(DataNodeNotFoundExceptionBatch)
222     }
223
224     def 'Add and Delete top-level list (element) data nodes with root node.'() {
225         given: 'a new (multiple-data-tree:invoice) datanodes'
226             def json = '{"bookstore-address":[{"bookstore-name":"Easons","address":"Bangalore,India","postal-code":"560043"}]}'
227         when: 'the new list elements are saved'
228             objectUnderTest.saveListElements(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , '/', json, now)
229         then: 'they can be retrieved by their xpaths'
230             objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , '/bookstore-address[@bookstore-name="Easons"]', INCLUDE_ALL_DESCENDANTS)
231         and: 'there is one extra datanode'
232             assert originalCountBookstoreTopLevelListNodes + 1 == countTopLevelListDataNodesInBookstore()
233         when: 'the new elements are deleted'
234             objectUnderTest.deleteDataNode(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , '/bookstore-address[@bookstore-name="Easons"]', now)
235         then: 'the original number of datanodes is restored'
236             assert originalCountBookstoreTopLevelListNodes == countTopLevelListDataNodesInBookstore()
237     }
238
239     def 'Add and Delete list (element) data nodes.'() {
240         given: 'two new (categories) data nodes'
241             def json = '{"categories": [ {"code":"new1"}, {"code":"new2" } ] }'
242         when: 'the new list elements are saved'
243             objectUnderTest.saveListElements(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , '/bookstore', json, now)
244         then: 'they can be retrieved by their xpaths'
245             objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="new1"]', DIRECT_CHILDREN_ONLY).size() == 1
246             objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="new2"]', DIRECT_CHILDREN_ONLY).size() == 1
247         and: 'there are now two extra data nodes'
248             assert originalCountBookstoreChildNodes + 2 == countDataNodesInBookstore()
249         when: 'the new elements are deleted'
250             objectUnderTest.deleteDataNode(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="new1"]', now)
251             objectUnderTest.deleteDataNode(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="new2"]', now)
252         then: 'the original number of data nodes is restored'
253             assert originalCountBookstoreChildNodes == countDataNodesInBookstore()
254     }
255
256     def 'Add list (element) data nodes that already exist.'() {
257         given: 'two (categories) data nodes, one new and one existing'
258             def json = '{"categories": [ {"code":"1"}, {"code":"new1"} ] }'
259         when: 'attempt to save the list element'
260             objectUnderTest.saveListElements(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , '/bookstore', json, now)
261         then: 'an exception that (one cps paths is)  already defined is thrown '
262             def exceptionThrown = thrown(AlreadyDefinedException)
263             exceptionThrown.alreadyDefinedObjectNames == ['/bookstore/categories[@code=\'1\']' ] as Set
264         and: 'there is now one extra data nodes'
265             assert originalCountBookstoreChildNodes + 1 == countDataNodesInBookstore()
266         cleanup:
267             restoreBookstoreDataAnchor(1)
268     }
269
270     def 'Add and Delete list (element) data nodes using lists specific method.'() {
271         given: 'a new (categories) data nodes'
272             def json = '{"categories": [ {"code":"new1"} ] }'
273         and: 'the new list element is saved'
274             objectUnderTest.saveListElements(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , '/bookstore', json, now)
275         when: 'the new element is deleted'
276             objectUnderTest.deleteListOrListElement(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="new1"]', now)
277         then: 'the original number of data nodes is restored'
278             assert originalCountBookstoreChildNodes == countDataNodesInBookstore()
279     }
280
281     def 'Add and Delete a batch of lists (element) data nodes.'() {
282         given: 'two new (categories) data nodes in two separate batches'
283             def json1 = '{"categories": [ {"code":"new1"} ] }'
284             def json2 = '{"categories": [ {"code":"new2"} ] } '
285         when: 'the batches of new list element(s) are saved'
286             objectUnderTest.saveListElementsBatch(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , '/bookstore', [json1, json2], now)
287         then: 'they can be retrieved by their xpaths'
288             assert objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="new1"]', DIRECT_CHILDREN_ONLY).size() == 1
289             assert objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="new2"]', DIRECT_CHILDREN_ONLY).size() == 1
290         and: 'there are now two extra data nodes'
291             assert originalCountBookstoreChildNodes + 2 == countDataNodesInBookstore()
292         when: 'the new elements are deleted'
293             objectUnderTest.deleteDataNode(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="new1"]', now)
294             objectUnderTest.deleteDataNode(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="new2"]', now)
295         then: 'the original number of data nodes is restored'
296             assert originalCountBookstoreChildNodes == countDataNodesInBookstore()
297     }
298
299     def 'Add and Delete a batch of lists (element) data nodes with partial success.'() {
300         given: 'two new (categories) data nodes in two separate batches'
301             def jsonNewElement = '{"categories": [ {"code":"new1"} ] }'
302             def jsonExistingElement = '{"categories": [ {"code":"1"} ] } '
303         when: 'the batches of new list element(s) are saved'
304             objectUnderTest.saveListElementsBatch(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , '/bookstore', [jsonNewElement, jsonExistingElement], now)
305         then: 'an already defined (batch) exception is thrown for the existing path'
306             def exceptionThrown = thrown(AlreadyDefinedException)
307             assert exceptionThrown.alreadyDefinedObjectNames ==  ['/bookstore/categories[@code=\'1\']' ] as Set
308         and: 'there is now one extra data node'
309             assert originalCountBookstoreChildNodes + 1 == countDataNodesInBookstore()
310         cleanup:
311             restoreBookstoreDataAnchor(1)
312     }
313
314     def 'Attempt to add empty lists.'() {
315         when: 'the batches of new list element(s) are saved'
316             objectUnderTest.replaceListContent(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1 , '/bookstore', [ ], now)
317         then: 'an admin exception is thrown'
318             thrown(CpsAdminException)
319     }
320
321     def 'Add child error scenario: #scenario.'() {
322         when: 'attempt to add a child data node with #scenario'
323             objectUnderTest.saveData(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, parentXpath, json, now)
324         then: 'a #expectedException is thrown'
325             thrown(expectedException)
326         where: 'the following data is used'
327             scenario                 | parentXpath                              | json                                || expectedException
328             'parent does not exist'  | '/bookstore/categories[@code="unknown"]' | '{"books": [ {"title":"new"} ] } '  || DataNodeNotFoundException
329             'already existing child' | '/bookstore'                             | '{"categories": [ {"code":"1"} ] }' || AlreadyDefinedException
330     }
331
332     def 'Add multiple child data nodes with partial success.'() {
333         given: 'one existing and one new list element'
334             def json = '{"categories": [ {"code":"1"}, {"code":"new"} ] }'
335         when: 'attempt to add the elements'
336             objectUnderTest.saveData(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore', json, now)
337         then: 'an already defined (batch) exception is thrown for the existing path'
338             def thrown  = thrown(AlreadyDefinedException)
339             assert thrown.alreadyDefinedObjectNames == [ "/bookstore/categories[@code='1']" ] as Set
340         and: 'the new data node has been added i.e. can be retrieved'
341             assert objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="new"]', DIRECT_CHILDREN_ONLY).size() == 1
342     }
343
344     def 'Replace list content #scenario.'() {
345         given: 'the bookstore categories 1 and 2 exist and have at least 1 child each '
346             assert countDataNodesInTree(objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="1"]', DIRECT_CHILDREN_ONLY)) > 1
347             assert countDataNodesInTree(objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="2"]', DIRECT_CHILDREN_ONLY)) > 1
348         when: 'the categories list is replaced with just category "1" and without child nodes (books)'
349             def json = '{"categories": [ {"code":"' +categoryCode + '"' + childJson + '} ] }'
350             objectUnderTest.replaceListContent(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore', json, now)
351         then: 'the new replaced category can be retrieved but has no children anymore'
352             assert expectedNumberOfDataNodes == countDataNodesInTree(objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="' +categoryCode + '"]', DIRECT_CHILDREN_ONLY))
353         when: 'attempt to retrieve a category (code) not in the new list'
354             objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="2"]', DIRECT_CHILDREN_ONLY)
355         then: 'a datanode not found exception occurs'
356             thrown(DataNodeNotFoundException)
357         cleanup:
358             restoreBookstoreDataAnchor(1)
359         where: 'the following data is used'
360             scenario                        | categoryCode | childJson                                 || expectedNumberOfDataNodes
361             'existing code, no children'    | '1'          | ''                                        || 1
362             'existing code, new child'      | '1'          | ', "books" : [ { "title": "New Book" } ]' || 2
363             'existing code, existing child' | '1'          | ', "books" : [ { "title": "Matilda" } ]'  || 2
364             'new code, new child'           | 'new'        | ', "books" : [ { "title": "New Book" } ]' || 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 'Update bookstore top-level container data node.'() {
412         when: 'the bookstore top-level container is updated'
413             def json = '{ "bookstore": { "bookstore-name": "new bookstore" }}'
414             objectUnderTest.updateDataNodeAndDescendants(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/', json, now)
415         then: 'bookstore name has been updated'
416             def result = objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore', DIRECT_CHILDREN_ONLY)
417             result.leaves.'bookstore-name'[0] == 'new bookstore'
418         cleanup:
419             restoreBookstoreDataAnchor(1)
420     }
421
422     def 'Update multiple data node leaves.'() {
423         given: 'Updated json for bookstore data'
424             def jsonData =  "{'book-store:books':{'lang':'English/French','price':100,'title':'Matilda'}}"
425         when: 'update is performed for leaves'
426             objectUnderTest.updateNodeLeaves(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_2, "/bookstore/categories[@code='1']", jsonData, now)
427         then: 'the updated data nodes are retrieved'
428             def result = cpsDataService.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_2, "/bookstore/categories[@code=1]/books[@title='Matilda']", INCLUDE_ALL_DESCENDANTS)
429         and: 'the leaf values are updated as expected'
430             assert result.leaves['lang'] == ['English/French']
431             assert result.leaves['price'] == [100]
432         cleanup:
433             restoreBookstoreDataAnchor(2)
434     }
435
436     def 'Get delta between 2 anchors for when #scenario'() {
437         when: 'attempt to get delta report between anchors'
438             def result = objectUnderTest.getDeltaByDataspaceAndAnchors(FUNCTIONAL_TEST_DATASPACE_3, BOOKSTORE_ANCHOR_3, BOOKSTORE_ANCHOR_5, xpath, fetchDescendantOption)
439         then: 'delta report contains expected number of changes'
440             result.size() == 2
441         and: 'delta report contains expected action'
442             assert result.get(index).getAction() == expectedActions
443         and: 'delta report contains expected xpath'
444             assert result.get(index).getXpath() == expectedXpath
445         where: 'following data was used'
446             scenario            | index | xpath || expectedActions || expectedXpath                                                | fetchDescendantOption
447             'a node is removed' |   0   | '/'   ||    'remove'     || "/bookstore-address[@bookstore-name='Easons-1']"             | OMIT_DESCENDANTS
448             'a node is added'   |   1   | '/'   ||     'add'       || "/bookstore-address[@bookstore-name='Crossword Bookstores']" | OMIT_DESCENDANTS
449     }
450
451     def 'Get delta between 2 anchors where child nodes are added/removed but parent node remains unchanged'() {
452         def parentNodeXpath = "/bookstore"
453         when: 'attempt to get delta report between anchors'
454             def result = objectUnderTest.getDeltaByDataspaceAndAnchors(FUNCTIONAL_TEST_DATASPACE_3, BOOKSTORE_ANCHOR_3, BOOKSTORE_ANCHOR_5, parentNodeXpath, INCLUDE_ALL_DESCENDANTS)
455         then: 'delta report contains expected number of changes'
456             result.size() == 11
457         and: 'the delta report does not contain parent node xpath'
458             def xpaths = getDeltaReportEntities(result).get('xpaths')
459             assert !(xpaths.contains(parentNodeXpath))
460     }
461
462     def 'Get delta between 2 anchors returns empty response when #scenario'() {
463         when: 'attempt to get delta report between anchors'
464             def result = objectUnderTest.getDeltaByDataspaceAndAnchors(FUNCTIONAL_TEST_DATASPACE_3, sourceAnchor, targetAnchor, xpath, INCLUDE_ALL_DESCENDANTS)
465         then: 'delta report is empty'
466             assert result.isEmpty()
467         where: 'following data was used'
468             scenario                              | sourceAnchor       | targetAnchor       | xpath
469         'anchors with identical data are queried' | BOOKSTORE_ANCHOR_3 | BOOKSTORE_ANCHOR_4 | '/'
470         'same anchor name is passed as parameter' | BOOKSTORE_ANCHOR_3 | BOOKSTORE_ANCHOR_3 | '/'
471         'non existing xpath'                      | BOOKSTORE_ANCHOR_3 | BOOKSTORE_ANCHOR_5 | '/non-existing-xpath'
472     }
473
474     def 'Get delta between anchors error scenario: #scenario'() {
475         when: 'attempt to get delta between anchors'
476             objectUnderTest.getDeltaByDataspaceAndAnchors(dataspaceName, sourceAnchor, targetAnchor, '/some-xpath', INCLUDE_ALL_DESCENDANTS)
477         then: 'expected exception is thrown'
478             thrown(expectedException)
479         where: 'following data was used'
480                     scenario                               | dataspaceName               | sourceAnchor          | targetAnchor          || expectedException
481             'invalid dataspace name'                       | 'Invalid dataspace'         | 'not-relevant'        | 'not-relevant'        || DataValidationException
482             'invalid anchor 1 name'                        | FUNCTIONAL_TEST_DATASPACE_3 | 'invalid anchor'      | 'not-relevant'        || DataValidationException
483             'invalid anchor 2 name'                        | FUNCTIONAL_TEST_DATASPACE_3 | BOOKSTORE_ANCHOR_3    | 'invalid anchor'      || DataValidationException
484             'non-existing dataspace'                       | 'non-existing'              | 'not-relevant1'       | 'not-relevant2'       || DataspaceNotFoundException
485             'non-existing dataspace with same anchor name' | 'non-existing'              | 'not-relevant'        | 'not-relevant'        || DataspaceNotFoundException
486             'non-existing anchor 1'                        | FUNCTIONAL_TEST_DATASPACE_3 | 'non-existing-anchor' | 'not-relevant'        || AnchorNotFoundException
487             'non-existing anchor 2'                        | FUNCTIONAL_TEST_DATASPACE_3 | BOOKSTORE_ANCHOR_3    | 'non-existing-anchor' || AnchorNotFoundException
488     }
489
490     def 'Get delta between anchors for remove action, where source data node #scenario'() {
491         when: 'attempt to get delta between leaves of data nodes present in 2 anchors'
492             def result = objectUnderTest.getDeltaByDataspaceAndAnchors(FUNCTIONAL_TEST_DATASPACE_3, BOOKSTORE_ANCHOR_5, BOOKSTORE_ANCHOR_3, parentNodeXpath, INCLUDE_ALL_DESCENDANTS)
493         then: 'expected action is present in delta report'
494             assert result.get(0).getAction() == 'remove'
495         where: 'following data was used'
496             scenario                     | parentNodeXpath
497             'has leaves and child nodes' | "/bookstore/categories[@code='6']"
498             'has leaves only'            | "/bookstore/categories[@code='5']/books[@title='Book 11']"
499             'has child data node only'   | "/bookstore/support-info/contact-emails"
500             'is empty'                   | "/bookstore/container-without-leaves"
501     }
502
503     def 'Get delta between anchors for add action, where target data node #scenario'() {
504         when: 'attempt to get delta between leaves of data nodes present in 2 anchors'
505             def result = objectUnderTest.getDeltaByDataspaceAndAnchors(FUNCTIONAL_TEST_DATASPACE_3, BOOKSTORE_ANCHOR_3, BOOKSTORE_ANCHOR_5, parentNodeXpath, INCLUDE_ALL_DESCENDANTS)
506         then: 'the expected action is present in delta report'
507             result.get(0).getAction() == 'add'
508         and: 'the expected xapth is present in delta report'
509             result.get(0).getXpath() == parentNodeXpath
510         where: 'following data was used'
511             scenario                     | parentNodeXpath
512             'has leaves and child nodes' | "/bookstore/categories[@code='6']"
513             'has leaves only'            | "/bookstore/categories[@code='5']/books[@title='Book 11']"
514             'has child data node only'   | "/bookstore/support-info/contact-emails"
515             'is empty'                   | "/bookstore/container-without-leaves"
516     }
517
518     def getDeltaReportEntities(List<DeltaReport> deltaReport) {
519         def xpaths = []
520         def action = []
521         def sourcePayload = []
522         def targetPayload = []
523         deltaReport.each {
524             delta -> xpaths.add(delta.getXpath())
525                 action.add(delta.getAction())
526                 sourcePayload.add(delta.getSourceData())
527                 targetPayload.add(delta.getTargetData())
528         }
529         return ['xpaths':xpaths, 'action':action, 'sourcePayload':sourcePayload, 'targetPayload':targetPayload]
530     }
531
532     def countDataNodesInBookstore() {
533         return countDataNodesInTree(objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore', INCLUDE_ALL_DESCENDANTS))
534     }
535
536     def countTopLevelListDataNodesInBookstore() {
537         return countDataNodesInTree(objectUnderTest.getDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/', INCLUDE_ALL_DESCENDANTS))
538     }
539 }