Improve batch delete schemasets performance
[cps.git] / cps-ri / src / test / groovy / org / onap / cps / spi / impl / CpsDataPersistenceServiceIntegrationSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2023 Nordix Foundation
4  *  Modifications Copyright (C) 2021 Pantheon.tech
5  *  Modifications Copyright (C) 2021-2022 Bell Canada.
6  *  Modifications Copyright (C) 2022 TechMahindra Ltd.
7  *  ================================================================================
8  *  Licensed under the Apache License, Version 2.0 (the "License");
9  *  you may not use this file except in compliance with the License.
10  *  You may obtain a copy of the License at
11  *
12  *        http://www.apache.org/licenses/LICENSE-2.0
13  *
14  *  Unless required by applicable law or agreed to in writing, software
15  *  distributed under the License is distributed on an "AS IS" BASIS,
16  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  *  See the License for the specific language governing permissions and
18  *  limitations under the License.
19  *
20  *  SPDX-License-Identifier: Apache-2.0
21  *  ============LICENSE_END=========================================================
22  */
23
24 package org.onap.cps.spi.impl
25
26 import com.fasterxml.jackson.databind.ObjectMapper
27 import com.google.common.collect.ImmutableSet
28 import org.onap.cps.cpspath.parser.PathParsingException
29 import org.onap.cps.spi.CpsDataPersistenceService
30 import org.onap.cps.spi.entities.FragmentEntity
31 import org.onap.cps.spi.exceptions.AlreadyDefinedExceptionBatch
32 import org.onap.cps.spi.exceptions.AnchorNotFoundException
33 import org.onap.cps.spi.exceptions.CpsAdminException
34 import org.onap.cps.spi.exceptions.CpsPathException
35 import org.onap.cps.spi.exceptions.DataNodeNotFoundException
36 import org.onap.cps.spi.exceptions.DataspaceNotFoundException
37 import org.onap.cps.spi.model.DataNode
38 import org.onap.cps.spi.model.DataNodeBuilder
39 import org.onap.cps.utils.JsonObjectMapper
40 import org.springframework.beans.factory.annotation.Autowired
41 import org.springframework.test.context.jdbc.Sql
42
43 import javax.validation.ConstraintViolationException
44
45 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
46 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
47
48 class CpsDataPersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase {
49
50     @Autowired
51     CpsDataPersistenceService objectUnderTest
52
53     static JsonObjectMapper jsonObjectMapper = new JsonObjectMapper(new ObjectMapper())
54     static DataNodeBuilder dataNodeBuilder = new DataNodeBuilder()
55
56     static final String SET_DATA = '/data/fragment.sql'
57     static long ID_DATA_NODE_WITH_DESCENDANTS = 4001
58     static String XPATH_DATA_NODE_WITH_DESCENDANTS = '/parent-1'
59     static String XPATH_DATA_NODE_WITH_LEAVES = '/parent-207'
60     static long DATA_NODE_202_FRAGMENT_ID = 4202L
61     static long CHILD_OF_DATA_NODE_202_FRAGMENT_ID = 4203L
62     static long LIST_DATA_NODE_PARENT201_FRAGMENT_ID = 4206L
63     static long LIST_DATA_NODE_PARENT203_FRAGMENT_ID = 4214L
64     static long LIST_DATA_NODE_PARENT202_FRAGMENT_ID = 4211L
65     static long PARENT_3_FRAGMENT_ID = 4003L
66
67     static Collection<DataNode> newDataNodes = [new DataNodeBuilder().build()]
68     static Collection<DataNode> existingDataNodes = [createDataNodeTree(XPATH_DATA_NODE_WITH_DESCENDANTS)]
69     static Collection<DataNode> existingChildDataNodes = [createDataNodeTree('/parent-1/child-1')]
70
71     def static deleteTestParentXPath = '/parent-200'
72     def static deleteTestChildXpath = "${deleteTestParentXPath}/child-with-slash[@key='a/b']"
73     def static deleteTestGrandChildXPath = "${deleteTestChildXpath}/grandChild"
74
75     def expectedLeavesByXpathMap = [
76             '/parent-207'                      : ['parent-leaf': 'parent-leaf value'],
77             '/parent-207/child-001'            : ['first-child-leaf': 'first-child-leaf value'],
78             '/parent-207/child-002'            : ['second-child-leaf': 'second-child-leaf value'],
79             '/parent-207/child-002/grand-child': ['grand-child-leaf': 'grand-child-leaf value']
80     ]
81
82     @Sql([CLEAR_DATA, SET_DATA])
83     def 'Get existing datanode with descendants.'() {
84         when: 'the node is retrieved by its xpath'
85             def dataNode = objectUnderTest.getDataNode(DATASPACE_NAME, ANCHOR_NAME1, '/parent-1', INCLUDE_ALL_DESCENDANTS)
86         then: 'the path and prefix are populated correctly'
87             assert dataNode.xpath == '/parent-1'
88         and: 'dataNode has no prefix (to be addressed by CPS-1301'
89             assert dataNode.moduleNamePrefix == null
90         and: 'the child node has the correct path'
91             assert dataNode.childDataNodes[0].xpath == '/parent-1/child-1'
92     }
93
94     @Sql([CLEAR_DATA, SET_DATA])
95     def 'Storing and Retrieving a new DataNodes with descendants.'() {
96         when: 'a fragment with descendants is stored'
97             def parentXpath = '/parent-new'
98             def childXpath = '/parent-new/child-new'
99             def grandChildXpath = '/parent-new/child-new/grandchild-new'
100             def dataNodes = [createDataNodeTree(parentXpath, childXpath, grandChildXpath)]
101             objectUnderTest.storeDataNodes(DATASPACE_NAME, ANCHOR_NAME1, dataNodes)
102         then: 'it can be retrieved by its xpath'
103             def dataNode = objectUnderTest.getDataNode(DATASPACE_NAME, ANCHOR_NAME1, parentXpath, INCLUDE_ALL_DESCENDANTS)
104             assert dataNode.xpath == parentXpath
105         and: 'it has the correct child'
106             assert dataNode.childDataNodes.size() == 1
107             def childDataNode = dataNode.childDataNodes[0]
108             assert childDataNode.xpath == childXpath
109         and: 'and its grandchild'
110             assert childDataNode.childDataNodes.size() == 1
111             def grandChildDataNode = childDataNode.childDataNodes[0]
112             assert grandChildDataNode.xpath == grandChildXpath
113     }
114
115     @Sql([CLEAR_DATA, SET_DATA])
116     def 'Store data node for multiple anchors using the same schema.'() {
117         def xpath = '/parent-new'
118         given: 'a fragment is stored for an anchor'
119             objectUnderTest.storeDataNodes(DATASPACE_NAME, ANCHOR_NAME1, [createDataNodeTree(xpath)])
120         when: 'another fragment is stored for an other anchor, using the same schema set'
121             objectUnderTest.storeDataNodes(DATASPACE_NAME, ANCHOR_NAME3, [createDataNodeTree(xpath)])
122         then: 'both fragments can be retrieved by their xpath'
123             def fragment1 = getFragmentByXpath(DATASPACE_NAME, ANCHOR_NAME1, xpath)
124             fragment1.anchor.name == ANCHOR_NAME1
125             fragment1.xpath == xpath
126             def fragment2 = getFragmentByXpath(DATASPACE_NAME, ANCHOR_NAME3, xpath)
127             fragment2.anchor.name == ANCHOR_NAME3
128             fragment2.xpath == xpath
129     }
130
131     @Sql([CLEAR_DATA, SET_DATA])
132     def 'Store datanodes error scenario: #scenario.'() {
133         when: 'attempt to store a data node with #scenario'
134             objectUnderTest.storeDataNodes(dataspaceName, anchorName, dataNodes)
135         then: 'a #expectedException is thrown'
136             thrown(expectedException)
137         where: 'the following data is used'
138             scenario                    | dataspaceName  | anchorName     | dataNodes          || expectedException
139             'dataspace does not exist'  | 'unknown'      | 'not-relevant' | newDataNodes       || DataspaceNotFoundException
140             'schema set does not exist' | DATASPACE_NAME | 'unknown'      | newDataNodes       || AnchorNotFoundException
141             'anchor already exists'     | DATASPACE_NAME | ANCHOR_NAME1   | newDataNodes       || ConstraintViolationException
142             'datanode already exists'   | DATASPACE_NAME | ANCHOR_NAME1   | existingDataNodes  || AlreadyDefinedExceptionBatch
143     }
144
145     @Sql([CLEAR_DATA, SET_DATA])
146     def 'Add children to a Fragment that already has a child.'() {
147         given: 'collection of new child data nodes'
148             def newChild1 = createDataNodeTree('/parent-1/child-2')
149             def newChild2 = createDataNodeTree('/parent-1/child-3')
150             def newChildrenCollection = [newChild1, newChild2]
151         when: 'the child is added to an existing parent with 1 child'
152             objectUnderTest.addChildDataNodes(DATASPACE_NAME, ANCHOR_NAME1, XPATH_DATA_NODE_WITH_DESCENDANTS, newChildrenCollection)
153         then: 'the parent is now has to 3 children'
154             def expectedExistingChildPath = '/parent-1/child-1'
155             def parentFragment = fragmentRepository.findById(ID_DATA_NODE_WITH_DESCENDANTS).orElseThrow()
156             parentFragment.childFragments.size() == 3
157         and: 'it still has the old child'
158             parentFragment.childFragments.find({ it.xpath == expectedExistingChildPath })
159         and: 'it has the new children'
160             parentFragment.childFragments.find({ it.xpath == newChildrenCollection[0].xpath })
161             parentFragment.childFragments.find({ it.xpath == newChildrenCollection[1].xpath })
162     }
163
164     @Sql([CLEAR_DATA, SET_DATA])
165     def 'Add child error scenario: #scenario.'() {
166         when: 'attempt to add a child data node with #scenario'
167             objectUnderTest.addChildDataNodes(DATASPACE_NAME, ANCHOR_NAME1, parentXpath, dataNodes)
168         then: 'a #expectedException is thrown'
169             thrown(expectedException)
170         where: 'the following data is used'
171             scenario                 | parentXpath                      | dataNodes               || expectedException
172             'parent does not exist'  | '/unknown'                       | newDataNodes            || DataNodeNotFoundException
173             'already existing child' | XPATH_DATA_NODE_WITH_DESCENDANTS | existingChildDataNodes  || AlreadyDefinedExceptionBatch
174     }
175
176     @Sql([CLEAR_DATA, SET_DATA])
177     def 'Add collection of multiple new list elements including an element with a child datanode.'() {
178         given: 'two new child list elements for an existing parent'
179             def listElementXpaths = ['/parent-201/child-204[@key="NEW1"]', '/parent-201/child-204[@key="NEW2"]']
180             def listElements = toDataNodes(listElementXpaths)
181         and: 'a (grand)child data node for one of the new list elements'
182             def grandChild = buildDataNode('/parent-201/child-204[@key="NEW1"]/grand-child-204[@key2="NEW1-CHILD"]', [leave:'value'], [])
183             listElements[0].childDataNodes = [grandChild]
184         when: 'the new data node (list elements) are added to an existing parent node'
185             objectUnderTest.addMultipleLists(DATASPACE_NAME, ANCHOR_NAME3, '/parent-201', [listElements])
186         then: 'new entries are successfully persisted, parent node now contains 5 children (2 new + 3 existing before)'
187             def parentFragment = fragmentRepository.getById(LIST_DATA_NODE_PARENT201_FRAGMENT_ID)
188             def allChildXpaths = parentFragment.childFragments.collect { it.xpath }
189             assert allChildXpaths.size() == 5
190             assert allChildXpaths.containsAll(listElementXpaths)
191         and: 'the (grand)child node of the new list entry is also present'
192             def dataspaceEntity = dataspaceRepository.getByName(DATASPACE_NAME)
193             def anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, ANCHOR_NAME3)
194             def grandChildFragmentEntity = fragmentRepository.findByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity, grandChild.xpath)
195             assert grandChildFragmentEntity.isPresent()
196     }
197
198     @Sql([CLEAR_DATA, SET_DATA])
199     def 'Add multiple list with a mix of existing and new elements'() {
200         given: 'two new child list elements for an existing parent'
201             def existingDataNode = dataNodeBuilder.withXpath('/parent-207/child-001').withLeaves(['id': '001']).build()
202             def newDataNode1 = dataNodeBuilder.withXpath('/parent-207/child-new1').withLeaves(['id': 'new1']).build()
203             def newDataNode2 = dataNodeBuilder.withXpath('/parent-200/child-new2').withLeaves(['id': 'new2']).build()
204             def dataNodeList1 = [existingDataNode, newDataNode1]
205             def dataNodeList2 = [newDataNode2]
206         when: 'duplicate data node is requested to be added'
207             objectUnderTest.addMultipleLists(DATASPACE_NAME, ANCHOR_HAVING_SINGLE_TOP_LEVEL_FRAGMENT, '/', [dataNodeList1, dataNodeList2])
208         then: 'already defined batch exception is thrown'
209             def thrown = thrown(AlreadyDefinedExceptionBatch)
210         and: 'it only contains the xpath(s) of the duplicated elements'
211             assert thrown.alreadyDefinedXpaths.size() == 1
212             assert thrown.alreadyDefinedXpaths.contains('/parent-207/child-001')
213         and: 'it does NOT contains the xpaths of the new element that were not combined with existing elements'
214             assert !thrown.alreadyDefinedXpaths.contains('/parent-207/child-new1')
215             assert !thrown.alreadyDefinedXpaths.contains('/parent-207/child-new1')
216         and: 'the new entity is inserted correctly'
217             def dataspaceEntity = dataspaceRepository.getByName(DATASPACE_NAME)
218             def anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, ANCHOR_HAVING_SINGLE_TOP_LEVEL_FRAGMENT)
219             fragmentRepository.findByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity, '/parent-200/child-new2').isPresent()
220     }
221
222     @Sql([CLEAR_DATA, SET_DATA])
223     def 'Add list element error scenario: #scenario.'() {
224         given: 'list element as a collection of data nodes'
225             def listElements = toDataNodes(listElementXpaths)
226         when: 'attempt to add list elements to parent node'
227             objectUnderTest.addListElements(DATASPACE_NAME, ANCHOR_NAME3, parentNodeXpath, listElements)
228         then: 'a #expectedException is thrown'
229             thrown(expectedException)
230         where: 'following parameters were used'
231             scenario                        | parentNodeXpath | listElementXpaths                   || expectedException
232             'parent node does not exist'    | '/unknown'      | ['irrelevant']                      || DataNodeNotFoundException
233             'data fragment already exists'  | '/parent-201'   | ["/parent-201/child-204[@key='A']"] || AlreadyDefinedExceptionBatch
234     }
235
236     @Sql([CLEAR_DATA, SET_DATA])
237     def 'Get data node by xpath without descendants.'() {
238         when: 'data node is requested'
239             def result = objectUnderTest.getDataNode(DATASPACE_NAME, ANCHOR_HAVING_SINGLE_TOP_LEVEL_FRAGMENT,
240                     inputXPath, OMIT_DESCENDANTS)
241         then: 'data node is returned with no descendants'
242             assert result.xpath == XPATH_DATA_NODE_WITH_LEAVES
243         and: 'expected leaves'
244             assert result.childDataNodes.size() == 0
245             assertLeavesMaps(result.leaves, expectedLeavesByXpathMap[XPATH_DATA_NODE_WITH_LEAVES])
246         where: 'the following data is used'
247             scenario      | inputXPath
248             'some xpath'  | '/parent-207'
249             'root xpath'  | '/'
250             'empty xpath' | ''
251     }
252
253     @Sql([CLEAR_DATA, SET_DATA])
254     def 'Cps Path query with syntax error throws a CPS Path Exception.'() {
255         when: 'trying to execute a query with a syntax (parsing) error'
256             objectUnderTest.getDataNode(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, 'invalid-cps-path/child' , OMIT_DESCENDANTS)
257         then: 'exception is thrown'
258             def exceptionThrown = thrown(CpsPathException)
259             assert exceptionThrown.getDetails().contains('failed to parse at line 1 due to extraneous input \'invalid-cps-path\' expecting \'/\'')
260     }
261
262     @Sql([CLEAR_DATA, SET_DATA])
263     def 'Get data node by xpath with all descendants.'() {
264         when: 'data node is requested with all descendants'
265             def result = objectUnderTest.getDataNode(DATASPACE_NAME, ANCHOR_HAVING_SINGLE_TOP_LEVEL_FRAGMENT,
266                     inputXPath, INCLUDE_ALL_DESCENDANTS)
267             def mappedResult = treeToFlatMapByXpath(new HashMap<>(), result)
268         then: 'data node is returned with all the descendants populated'
269             assert mappedResult.size() == 4
270             assert result.childDataNodes.size() == 2
271             assert mappedResult.get('/parent-207/child-001').childDataNodes.size() == 0
272             assert mappedResult.get('/parent-207/child-002').childDataNodes.size() == 1
273         and: 'extracted leaves maps are matching expected'
274             mappedResult.forEach(
275                     (xPath, dataNode) -> assertLeavesMaps(dataNode.leaves, expectedLeavesByXpathMap[xPath]))
276         where: 'the following data is used'
277             scenario      | inputXPath
278             'some xpath'  | '/parent-207'
279             'root xpath'  | '/'
280             'empty xpath' | ''
281     }
282
283     @Sql([CLEAR_DATA, SET_DATA])
284     def 'Get data node error scenario: #scenario.'() {
285         when: 'attempt to get data node with #scenario'
286             objectUnderTest.getDataNode(dataspaceName, anchorName, xpath, OMIT_DESCENDANTS)
287         then: 'a #expectedException is thrown'
288             thrown(expectedException)
289         where: 'the following data is used'
290             scenario                 | dataspaceName  | anchorName                        | xpath           || expectedException
291             'non-existing dataspace' | 'NO DATASPACE' | 'not relevant'                    | '/not relevant' || DataspaceNotFoundException
292             'non-existing anchor'    | DATASPACE_NAME | 'NO ANCHOR'                       | '/not relevant' || AnchorNotFoundException
293             'non-existing xpath'     | DATASPACE_NAME | ANCHOR_FOR_DATA_NODES_WITH_LEAVES | '/NO-XPATH'     || DataNodeNotFoundException
294             'invalid xpath'          | DATASPACE_NAME | ANCHOR_FOR_DATA_NODES_WITH_LEAVES | 'INVALID XPATH' || CpsPathException
295     }
296
297     @Sql([CLEAR_DATA, SET_DATA])
298     def 'Get multiple data nodes by xpath.'() {
299         when: 'fetch #scenario.'
300             def results = objectUnderTest.getDataNodes(DATASPACE_NAME, ANCHOR_NAME3, inputXpaths, OMIT_DESCENDANTS)
301         then: 'the expected number of data nodes are returned'
302             assert results.size() == expectedResultSize
303         where: 'following parameters were used'
304             scenario                               | inputXpaths                                     || expectedResultSize
305             '0 nodes'                              | []                                              || 0
306             '1 node'                               | ["/parent-200"]                                 || 1
307             '2 unique nodes'                       | ["/parent-200", "/parent-201"]                  || 2
308             '3 unique nodes'                       | ["/parent-200", "/parent-201", "/parent-202"]   || 3
309             '1 unique node with duplicate xpath'   | ["/parent-200", "/parent-200"]                  || 1
310             '2 unique nodes with duplicate xpath'  | ["/parent-200", "/parent-202", "/parent-200"]   || 2
311             'list element with key (single quote)' | ["/parent-201/child-204[@key='A']"]             || 1
312             'list element with key (double quote)' | ['/parent-201/child-204[@key="A"]']             || 1
313             'non-existing xpath'                   | ["/NO-XPATH"]                                   || 0
314             'existing and non-existing xpaths'     | ["/parent-200", "/NO-XPATH", "/parent-201"]     || 2
315             'invalid xpath'                        | ["INVALID XPATH"]                               || 0
316             'valid and invalid xpaths'             | ["/parent-200", "INVALID XPATH", "/parent-201"] || 2
317             'root xpath'                           | ["/"]                                           || 7
318             'empty (root) xpath'                   | [""]                                            || 7
319             'root and top-level xpaths'            | ["/", "/parent-200", "/parent-201"]             || 7
320             'root and child xpaths'                | ["/", "/parent-200/child-201"]                  || 8
321     }
322
323     @Sql([CLEAR_DATA, SET_DATA])
324     def 'Get multiple data nodes error scenario: #scenario.'() {
325         when: 'attempt to get data nodes with #scenario'
326             objectUnderTest.getDataNodes(dataspaceName, anchorName, ['/not-relevant'], OMIT_DESCENDANTS)
327         then: 'a #expectedException is thrown'
328             thrown(expectedException)
329         where: 'the following data is used'
330             scenario                 | dataspaceName  | anchorName     || expectedException
331             'non-existing dataspace' | 'NO DATASPACE' | 'not relevant' || DataspaceNotFoundException
332             'non-existing anchor'    | DATASPACE_NAME | 'NO ANCHOR'    || AnchorNotFoundException
333     }
334
335     @Sql([CLEAR_DATA, SET_DATA])
336     def 'Update data node leaves.'() {
337         when: 'update is performed for leaves'
338             objectUnderTest.updateDataLeaves(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES,
339                     '/parent-200/child-201', ['leaf-value': 'new'])
340         then: 'leaves are updated for selected data node'
341             def updatedFragment = fragmentRepository.getById(DATA_NODE_202_FRAGMENT_ID)
342             def updatedLeaves = getLeavesMap(updatedFragment)
343             assert updatedLeaves.size() == 1
344             assert updatedLeaves.'leaf-value' == 'new'
345         and: 'existing child entry remains as is'
346             def childFragment = updatedFragment.childFragments.iterator().next()
347             def childLeaves = getLeavesMap(childFragment)
348             assert childFragment.id == CHILD_OF_DATA_NODE_202_FRAGMENT_ID
349             assert childLeaves.'leaf-value' == 'original'
350     }
351
352     @Sql([CLEAR_DATA, SET_DATA])
353     def 'Update data leaves error scenario: #scenario.'() {
354         when: 'attempt to update data node for #scenario'
355             objectUnderTest.updateDataLeaves(dataspaceName, anchorName, xpath, ['leaf-name': 'leaf-value'])
356         then: 'a #expectedException is thrown'
357             thrown(expectedException)
358         where: 'the following data is used'
359             scenario                 | dataspaceName  | anchorName                        | xpath                 || expectedException
360             'non-existing dataspace' | 'NO DATASPACE' | 'not relevant'                    | '/not relevant'       || DataspaceNotFoundException
361             'non-existing anchor'    | DATASPACE_NAME | 'NO ANCHOR'                       | '/not relevant'       || AnchorNotFoundException
362             'non-existing xpath'     | DATASPACE_NAME | ANCHOR_FOR_DATA_NODES_WITH_LEAVES | '/NON-EXISTING-XPATH' || DataNodeNotFoundException
363     }
364
365     @Sql([CLEAR_DATA, SET_DATA])
366     def 'Update data node and descendants by removing descendants.'() {
367         given: 'data node object with leaves updated, no children'
368             def submittedDataNode = buildDataNode('/parent-200/child-201', ['leaf-value': 'new'], [])
369         when: 'update data nodes and descendants is performed'
370             objectUnderTest.updateDataNodeAndDescendants(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, submittedDataNode)
371         then: 'leaves have been updated for selected data node'
372             def updatedFragment = fragmentRepository.getById(DATA_NODE_202_FRAGMENT_ID)
373             def updatedLeaves = getLeavesMap(updatedFragment)
374             assert updatedLeaves.size() == 1
375             assert updatedLeaves.'leaf-value' == 'new'
376         and: 'updated entry has no children'
377             updatedFragment.childFragments.isEmpty()
378         and: 'previously attached child entry is removed from database'
379             fragmentRepository.findById(CHILD_OF_DATA_NODE_202_FRAGMENT_ID).isEmpty()
380     }
381
382     @Sql([CLEAR_DATA, SET_DATA])
383     def 'Update data node and descendants with new descendants'() {
384         given: 'data node object with leaves updated, having child with old content'
385             def submittedDataNode = buildDataNode('/parent-200/child-201', ['leaf-value': 'new'], [
386                   buildDataNode('/parent-200/child-201/grand-child', ['leaf-value': 'original'], [])
387             ])
388         when: 'update is performed including descendants'
389             objectUnderTest.updateDataNodeAndDescendants(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, submittedDataNode)
390         then: 'leaves have been updated for selected data node'
391             def updatedFragment = fragmentRepository.getById(DATA_NODE_202_FRAGMENT_ID)
392             def updatedLeaves = getLeavesMap(updatedFragment)
393             assert updatedLeaves.size() == 1
394             assert updatedLeaves.'leaf-value' == 'new'
395         and: 'existing child entry is not updated as content is same'
396             def childFragment = updatedFragment.childFragments.iterator().next()
397             childFragment.xpath == '/parent-200/child-201/grand-child'
398             def childLeaves = getLeavesMap(childFragment)
399             assert childLeaves.'leaf-value' == 'original'
400     }
401
402     @Sql([CLEAR_DATA, SET_DATA])
403     def 'Update data node and descendants with same descendants but changed leaf value.'() {
404         given: 'data node object with leaves updated, having child with old content'
405             def submittedDataNode = buildDataNode('/parent-200/child-201', ['leaf-value': 'new'], [
406                     buildDataNode('/parent-200/child-201/grand-child', ['leaf-value': 'new'], [])
407             ])
408         when: 'update is performed including descendants'
409             objectUnderTest.updateDataNodeAndDescendants(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, submittedDataNode)
410         then: 'leaves have been updated for selected data node'
411             def updatedFragment = fragmentRepository.getById(DATA_NODE_202_FRAGMENT_ID)
412             def updatedLeaves = getLeavesMap(updatedFragment)
413             assert updatedLeaves.size() == 1
414             assert updatedLeaves.'leaf-value' == 'new'
415         and: 'existing child entry is updated with the new content'
416             def childFragment = updatedFragment.childFragments.iterator().next()
417             childFragment.xpath == '/parent-200/child-201/grand-child'
418             def childLeaves = getLeavesMap(childFragment)
419             assert childLeaves.'leaf-value' == 'new'
420     }
421
422     @Sql([CLEAR_DATA, SET_DATA])
423     def 'Update data node and descendants with different descendants xpath'() {
424         given: 'data node object with leaves updated, having child with old content'
425             def submittedDataNode = buildDataNode('/parent-200/child-201', ['leaf-value': 'new'], [
426                     buildDataNode('/parent-200/child-201/grand-child-new', ['leaf-value': 'new'], [])
427             ])
428         when: 'update is performed including descendants'
429             objectUnderTest.updateDataNodeAndDescendants(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, submittedDataNode)
430         then: 'leaves have been updated for selected data node'
431             def updatedFragment = fragmentRepository.getById(DATA_NODE_202_FRAGMENT_ID)
432             def updatedLeaves = getLeavesMap(updatedFragment)
433             assert updatedLeaves.size() == 1
434             assert updatedLeaves.'leaf-value' == 'new'
435         and: 'previously attached child entry is removed from database'
436             fragmentRepository.findById(CHILD_OF_DATA_NODE_202_FRAGMENT_ID).isEmpty()
437         and: 'new child entry is persisted'
438             def childFragment = updatedFragment.childFragments.iterator().next()
439             childFragment.xpath == '/parent-200/child-201/grand-child-new'
440             def childLeaves = getLeavesMap(childFragment)
441             assert childLeaves.'leaf-value' == 'new'
442     }
443
444     @Sql([CLEAR_DATA, SET_DATA])
445     def 'Update data node and descendants error scenario: #scenario.'() {
446         given: 'data node object'
447             def submittedDataNode = buildDataNode(xpath, ['leaf-name': 'leaf-value'], [])
448         when: 'attempt to update data node for #scenario'
449             objectUnderTest.updateDataNodeAndDescendants(dataspaceName, anchorName, submittedDataNode)
450         then: 'a #expectedException is thrown'
451             thrown(expectedException)
452         where: 'the following data is used'
453             scenario                 | dataspaceName  | anchorName                        | xpath                 || expectedException
454             'non-existing dataspace' | 'NO DATASPACE' | 'not relevant'                    | '/not relevant'       || DataspaceNotFoundException
455             'non-existing anchor'    | DATASPACE_NAME | 'NO ANCHOR'                       | '/not relevant'       || AnchorNotFoundException
456             'non-existing xpath'     | DATASPACE_NAME | ANCHOR_FOR_DATA_NODES_WITH_LEAVES | '/NON-EXISTING-XPATH' || DataNodeNotFoundException
457             'invalid xpath'          | DATASPACE_NAME | ANCHOR_FOR_DATA_NODES_WITH_LEAVES | 'INVALID XPATH'       || CpsPathException
458     }
459
460     @Sql([CLEAR_DATA, SET_DATA])
461     def 'Update existing list with #scenario.'() {
462         given: 'a parent having a list of data nodes containing: #originalKeys (ech list element has a child too)'
463             def parentXpath = '/parent-3'
464             if (originalKeys.size() > 0) {
465                 def originalListEntriesAsDataNodes = createChildListAllHavingAttributeValue(parentXpath, 'original value', originalKeys, true)
466                 objectUnderTest.addListElements(DATASPACE_NAME, ANCHOR_NAME1, parentXpath, originalListEntriesAsDataNodes)
467             }
468         and: 'each original list element has one child'
469             def originalParentFragment = fragmentRepository.getById(PARENT_3_FRAGMENT_ID)
470             originalParentFragment.childFragments.each {assert it.childFragments.size() == 1 }
471         when: 'it is updated with #scenario'
472             def replacementListEntriesAsDataNodes = createChildListAllHavingAttributeValue(parentXpath, 'new value', replacementKeys, false)
473             objectUnderTest.replaceListContent(DATASPACE_NAME, ANCHOR_NAME1, parentXpath, replacementListEntriesAsDataNodes)
474         then: 'the result list ONLY contains the expected replacement elements'
475             def parentFragment = fragmentRepository.getById(PARENT_3_FRAGMENT_ID)
476             def allChildXpaths = parentFragment.childFragments.collect { it.xpath }
477             def expectedListEntriesAfterUpdateAsXpaths = keysToXpaths(parentXpath, replacementKeys)
478             assert allChildXpaths.size() == replacementKeys.size()
479             assert allChildXpaths.containsAll(expectedListEntriesAfterUpdateAsXpaths)
480         and: 'all the list elements have the new values'
481             assert parentFragment.childFragments.stream().allMatch(childFragment -> childFragment.attributes.contains('new value'))
482         and: 'there are no more grandchildren as none of the replacement list entries had a child'
483             parentFragment.childFragments.each {assert it.childFragments.size() == 0 }
484         where: 'the following replacement lists are applied'
485             scenario                                            | originalKeys | replacementKeys
486             'one existing entry only'                           | []           | ['NEW']
487             'multiple new entries'                              | []           | ['NEW1', 'NEW2']
488             'one new entry only (existing entries are deleted)' | ['A', 'B']   | ['NEW1', 'NEW2']
489             'one existing on new entry'                         | ['A', 'B']   | ['A', 'NEW']
490             'one existing entry only'                           | ['A', 'B']   | ['A']
491     }
492
493     @Sql([CLEAR_DATA, SET_DATA])
494     def 'Replacing existing list element with attributes and (grand)child.'() {
495         given: 'a parent with list elements A and B with attribute and grandchild tagged as "org"'
496             def parentXpath = '/parent-3'
497             def originalListEntriesAsDataNodes = createChildListAllHavingAttributeValue(parentXpath, 'org', ['A','B'], true)
498             objectUnderTest.addListElements(DATASPACE_NAME, ANCHOR_NAME1, parentXpath, originalListEntriesAsDataNodes)
499         when: 'A is replaced with an entry with attribute and grandchild tagged tagged as "new" (B is not in replacement list)'
500             def replacementListEntriesAsDataNodes = createChildListAllHavingAttributeValue(parentXpath, 'new', ['A'], true)
501             objectUnderTest.replaceListContent(DATASPACE_NAME, ANCHOR_NAME1, parentXpath, replacementListEntriesAsDataNodes)
502         then: 'The updated fragment has a child-list with ONLY element "A"'
503             def parentFragment = fragmentRepository.getById(PARENT_3_FRAGMENT_ID)
504             parentFragment.childFragments.size() == 1
505             def childListElementA = parentFragment.childFragments[0]
506             childListElementA.xpath == "/parent-3/child-list[@key='A']"
507         and: 'element "A" has an attribute with the "new" (tag) value'
508             childListElementA.attributes == '{"attr1": "new"}'
509         and: 'element "A" has a only one (grand)child'
510             childListElementA.childFragments.size() == 1
511         and: 'the grandchild is the new grandchild (tag)'
512             def grandChild = childListElementA.childFragments[0]
513             grandChild.xpath == "/parent-3/child-list[@key='A']/new-grand-child"
514         and: 'the grandchild has an attribute with the "new" (tag) value'
515             grandChild.attributes == '{"attr1": "new"}'
516     }
517
518     @Sql([CLEAR_DATA, SET_DATA])
519     def 'Replace list element for a parent (parent-1) with existing one (non-list) child'() {
520         when: 'a list element is added under the parent'
521             def replacementListEntriesAsDataNodes = createChildListAllHavingAttributeValue(XPATH_DATA_NODE_WITH_DESCENDANTS, 'new', ['A','B'], false)
522             objectUnderTest.replaceListContent(DATASPACE_NAME, ANCHOR_NAME1, XPATH_DATA_NODE_WITH_DESCENDANTS, replacementListEntriesAsDataNodes)
523         then: 'the parent will have 3 children after the replacement'
524             def parentFragment = fragmentRepository.getById(ID_DATA_NODE_WITH_DESCENDANTS)
525             parentFragment.childFragments.size() == 3
526             def xpaths = parentFragment.childFragments.collect {it.xpath}
527         and: 'one of the children is the original child fragment'
528             xpaths.contains('/parent-1/child-1')
529         and: 'it has the two new list elements'
530             xpaths.containsAll("/parent-1/child-list[@key='A']", "/parent-1/child-list[@key='B']")
531     }
532
533     @Sql([CLEAR_DATA, SET_DATA])
534     def 'Replace list content using unknown parent'() {
535         given: 'list element as a collection of data nodes'
536             def listElementCollection = toDataNodes(['irrelevant'])
537         when: 'attempt to replace list elements under unknown parent node'
538             objectUnderTest.replaceListContent(DATASPACE_NAME, ANCHOR_NAME3, '/unknown', listElementCollection)
539         then: 'a datanode not found exception is thrown'
540             thrown(DataNodeNotFoundException)
541     }
542
543     @Sql([CLEAR_DATA, SET_DATA])
544     def 'Replace list content with empty collection is not supported'() {
545         when: 'attempt to replace list elements with empty collection'
546             objectUnderTest.replaceListContent(DATASPACE_NAME, ANCHOR_NAME3, '/parent-203', [])
547         then: 'a CPS admin exception is thrown'
548             def thrown = thrown(CpsAdminException)
549             assert thrown.message == 'Invalid list replacement'
550     }
551
552     @Sql([CLEAR_DATA, SET_DATA])
553     def 'Delete list scenario: #scenario.'() {
554         when: 'deleting list is executed for: #scenario.'
555             objectUnderTest.deleteListDataNode(DATASPACE_NAME, ANCHOR_NAME3, targetXpaths)
556         and: 'remaining children are fetched'
557             def parentFragment = fragmentRepository.getById(parentFragmentId)
558             def remainingChildXpaths = parentFragment.childFragments.collect { it.xpath }
559         then: 'only the expected children remain'
560             assert remainingChildXpaths.size() == expectedRemainingChildXpaths.size()
561             assert remainingChildXpaths.containsAll(expectedRemainingChildXpaths)
562         where: 'following parameters were used'
563             scenario                          | targetXpaths                                                 | parentFragmentId                     || expectedRemainingChildXpaths
564             'list element with key'           | '/parent-203/child-204[@key="A"]'                            | LIST_DATA_NODE_PARENT203_FRAGMENT_ID || ["/parent-203/child-203", "/parent-203/child-204[@key='B']"]
565             'list element with combined keys' | '/parent-202/child-205[@key="A" and @key2="B"]'              | LIST_DATA_NODE_PARENT202_FRAGMENT_ID || ["/parent-202/child-206[@key='A']"]
566             'whole list'                      | '/parent-203/child-204'                                      | LIST_DATA_NODE_PARENT203_FRAGMENT_ID || ['/parent-203/child-203']
567             'list element under list element' | '/parent-203/child-204[@key="B"]/grand-child-204[@key2="Y"]' | LIST_DATA_NODE_PARENT203_FRAGMENT_ID || ["/parent-203/child-203", "/parent-203/child-204[@key='A']", "/parent-203/child-204[@key='B']"]
568     }
569
570     @Sql([CLEAR_DATA, SET_DATA])
571     def 'Delete multiple data nodes using scenario: #scenario.'() {
572         when: 'deleting nodes is executed for: #scenario.'
573             objectUnderTest.deleteDataNodes(DATASPACE_NAME, ANCHOR_NAME3, targetXpaths)
574         and: 'remaining children are fetched'
575             def parentFragment = fragmentRepository.getById(LIST_DATA_NODE_PARENT203_FRAGMENT_ID)
576             def remainingChildXpaths = parentFragment.childFragments.collect { it.xpath }
577         then: 'only the expected children remain'
578             assert remainingChildXpaths.size() == expectedRemainingChildXpaths.size()
579             assert remainingChildXpaths.containsAll(expectedRemainingChildXpaths)
580         where: 'following parameters were used'
581             scenario                          | targetXpaths                                                           || expectedRemainingChildXpaths
582             'delete nothing'                  | []                                                                     || ["/parent-203/child-203", "/parent-203/child-204[@key='A']", "/parent-203/child-204[@key='B']"]
583             'datanode'                        | ['/parent-203/child-203']                                              || ["/parent-203/child-204[@key='A']", "/parent-203/child-204[@key='B']"]
584             '1 list element'                  | ['/parent-203/child-204[@key="A"]']                                    || ["/parent-203/child-203", "/parent-203/child-204[@key='B']"]
585             '2 list elements'                 | ['/parent-203/child-204[@key="A"]', '/parent-203/child-204[@key="B"]'] || ["/parent-203/child-203"]
586             'whole list'                      | ['/parent-203/child-204']                                              || ['/parent-203/child-203']
587             'list and element in same list'   | ['/parent-203/child-204', '/parent-203/child-204[@key="A"]']           || ['/parent-203/child-203']
588             'list element under list element' | ['/parent-203/child-204[@key="B"]/grand-child-204[@key2="Y"]']         || ["/parent-203/child-203", "/parent-203/child-204[@key='A']", "/parent-203/child-204[@key='B']"]
589             'valid but non-existing xpath'    | ['/non-existing', '/parent-203/child-204']                             || ['/parent-203/child-203']
590             'invalid xpath'                   | ['INVALID XPATH', '/parent-203/child-204']                             || ['/parent-203/child-203']
591     }
592
593     @Sql([CLEAR_DATA, SET_DATA])
594     def 'Delete data nodes with "/"-token in list key value: #scenario. (CPS-1409)'() {
595         given: 'a data nodes with list-element child with "/" in index value (and grandchild)'
596             def grandChild = new DataNodeBuilder().withXpath(deleteTestGrandChildXPath).build()
597             def child = new DataNodeBuilder().withXpath(deleteTestChildXpath).withChildDataNodes([grandChild]).build()
598             objectUnderTest.addChildDataNode(DATASPACE_NAME, ANCHOR_NAME3, deleteTestParentXPath, child)
599         and: 'number of children before delete is stored'
600             def numberOfChildrenBeforeDelete = objectUnderTest.getDataNode(DATASPACE_NAME, ANCHOR_NAME3, pathToParentOfDeletedNode, INCLUDE_ALL_DESCENDANTS).childDataNodes.size()
601         when: 'target node is deleted'
602             objectUnderTest.deleteDataNode(DATASPACE_NAME, ANCHOR_NAME3, deleteTarget)
603         then: 'one child has been deleted'
604             def numberOfChildrenAfterDelete = objectUnderTest.getDataNode(DATASPACE_NAME, ANCHOR_NAME3, pathToParentOfDeletedNode, INCLUDE_ALL_DESCENDANTS).childDataNodes.size()
605             assert numberOfChildrenAfterDelete == numberOfChildrenBeforeDelete - 1
606         where:
607             scenario                | deleteTarget              | pathToParentOfDeletedNode
608             'list element with /'   | deleteTestChildXpath      | deleteTestParentXPath
609             'child of list element' | deleteTestGrandChildXPath | deleteTestChildXpath
610     }
611
612     @Sql([CLEAR_DATA, SET_DATA])
613     def 'Delete list error scenario: #scenario.'() {
614         when: 'attempting to delete scenario: #scenario.'
615             objectUnderTest.deleteListDataNode(DATASPACE_NAME, ANCHOR_NAME3, targetXpaths)
616         then: 'a DataNodeNotFoundException is thrown'
617             thrown(DataNodeNotFoundException)
618         where: 'following parameters were used'
619             scenario                                   | targetXpaths
620             'whole list, parent node does not exist'   | '/unknown/some-child'
621             'list element, parent node does not exist' | '/unknown/child-204[@key="A"]'
622             'whole list does not exist'                | '/parent-200/unknown'
623             'list element, list does not exist'        | '/parent-200/unknown[@key="C"]'
624             'list element, element does not exist'     | '/parent-203/child-204[@key="C"]'
625             'valid datanode but not a list'            | '/parent-200/child-202'
626     }
627
628     @Sql([CLEAR_DATA, SET_DATA])
629     def 'Delete data node by xpath #scenario.'() {
630         given: 'a valid data node'
631             def dataNode
632         and: 'data nodes are deleted'
633             objectUnderTest.deleteDataNode(DATASPACE_NAME, ANCHOR_NAME3, xpathForDeletion)
634         when: 'verify data nodes are removed'
635             objectUnderTest.getDataNode(DATASPACE_NAME, ANCHOR_NAME3, xpathForDeletion, INCLUDE_ALL_DESCENDANTS)
636         then:
637             thrown(DataNodeNotFoundException)
638         and: 'some related object is not deleted'
639             if (xpathSurvivor!=null) {
640                 dataNode = objectUnderTest.getDataNode(DATASPACE_NAME, ANCHOR_NAME3, xpathSurvivor, INCLUDE_ALL_DESCENDANTS)
641                 assert dataNode.xpath == xpathSurvivor
642             }
643         where: 'following parameters were used'
644             scenario                               | xpathForDeletion                                  || xpathSurvivor
645             'child data node, parent still exists' | '/parent-206/child-206'                           || '/parent-206'
646             'list element, sibling still exists'   | '/parent-206/child-206/grand-child-206[@key="A"]' || "/parent-206/child-206/grand-child-206[@key='X']"
647             'container node'                       | '/parent-206'                                     || null
648             'container list node'                  | '/parent-206[@key="A"]'                           || "/parent-206[@key='B']"
649             'root node with xpath /'               | '/'                                               || null
650             'root node with xpath passed as blank' | ''                                                || null
651     }
652
653     @Sql([CLEAR_DATA, SET_DATA])
654     def 'Delete data node error scenario: #scenario.'() {
655         when: 'data node is deleted'
656             objectUnderTest.deleteDataNode(DATASPACE_NAME, ANCHOR_NAME3, datanodeXpath)
657         then: 'a #expectedException is thrown'
658             thrown(expectedException)
659         where: 'the following parameters were used'
660             scenario                                        | datanodeXpath                                    | expectedException
661             'valid data node, non existent child node'      | '/parent-203/child-non-existent'                 | DataNodeNotFoundException
662             'invalid list element'                          | '/parent-206/child-206/grand-child-206@key="A"]' | PathParsingException
663     }
664
665     @Sql([CLEAR_DATA, SET_DATA])
666     def 'Delete data node for an anchor.'() {
667         given: 'a data-node exists for an anchor'
668             assert fragmentsExistInDB(1001, 3003)
669         when: 'data nodes are deleted '
670             objectUnderTest.deleteDataNodes(DATASPACE_NAME, ANCHOR_NAME3)
671         then: 'all data-nodes are deleted successfully'
672             assert !fragmentsExistInDB(1001, 3003)
673     }
674
675     @Sql([CLEAR_DATA, SET_DATA])
676     def 'Delete data node for multiple anchors.'() {
677         given: 'a data-node exists for an anchor'
678             assert fragmentsExistInDB(1001, 3001)
679             assert fragmentsExistInDB(1001, 3003)
680         when: 'data nodes are deleted '
681             objectUnderTest.deleteDataNodes(DATASPACE_NAME, ['ANCHOR-001', 'ANCHOR-003'])
682         then: 'all data-nodes are deleted successfully'
683             assert !fragmentsExistInDB(1001, 3001)
684             assert !fragmentsExistInDB(1001, 3003)
685     }
686
687     def fragmentsExistInDB(dataSpaceId, anchorId) {
688         !fragmentRepository.findRootsByDataspaceAndAnchor(dataSpaceId, anchorId).isEmpty()
689     }
690
691     static Collection<DataNode> toDataNodes(xpaths) {
692         return xpaths.collect { new DataNodeBuilder().withXpath(it).build() }
693     }
694
695
696     static DataNode buildDataNode(xpath, leaves, childDataNodes) {
697         return dataNodeBuilder.withXpath(xpath).withLeaves(leaves).withChildDataNodes(childDataNodes).build()
698     }
699
700     static Map<String, Object> getLeavesMap(FragmentEntity fragmentEntity) {
701         return jsonObjectMapper.convertJsonString(fragmentEntity.attributes, Map<String, Object>.class)
702     }
703
704     def static assertLeavesMaps(actualLeavesMap, expectedLeavesMap) {
705         expectedLeavesMap.forEach((key, value) -> {
706             def actualValue = actualLeavesMap[key]
707             if (value instanceof Collection<?> && actualValue instanceof Collection<?>) {
708                 assert value.size() == actualValue.size()
709                 assert value.containsAll(actualValue)
710             } else {
711                 assert value == actualValue
712             }
713         })
714         return true
715     }
716
717     def static treeToFlatMapByXpath(Map<String, DataNode> flatMap, DataNode dataNodeTree) {
718         flatMap.put(dataNodeTree.xpath, dataNodeTree)
719         dataNodeTree.childDataNodes
720                 .forEach(childDataNode -> treeToFlatMapByXpath(flatMap, childDataNode))
721         return flatMap
722     }
723
724     def keysToXpaths(parent, Collection keys) {
725         return keys.collect { "${parent}/child-list[@key='${it}']".toString() }
726     }
727
728     def static createDataNodeTree(String... xpaths) {
729         def dataNodeBuilder = new DataNodeBuilder().withXpath(xpaths[0])
730         if (xpaths.length > 1) {
731             def xPathsDescendant = Arrays.copyOfRange(xpaths, 1, xpaths.length)
732             def childDataNode = createDataNodeTree(xPathsDescendant)
733             dataNodeBuilder.withChildDataNodes(ImmutableSet.of(childDataNode))
734         }
735         dataNodeBuilder.build()
736     }
737
738     def getFragmentByXpath(dataspaceName, anchorName, xpath) {
739         def dataspace = dataspaceRepository.getByName(dataspaceName)
740         def anchor = anchorRepository.getByDataspaceAndName(dataspace, anchorName)
741         return fragmentRepository.findByDataspaceAndAnchorAndXpath(dataspace, anchor, xpath).orElseThrow()
742     }
743
744
745     def createChildListAllHavingAttributeValue(parentXpath, tag, Collection keys, boolean addGrandChild) {
746         def listElementAsDataNodes = keysToXpaths(parentXpath, keys).collect {
747                 new DataNodeBuilder()
748                     .withXpath(it)
749                     .withLeaves([attr1: tag])
750                     .build()
751         }
752         if (addGrandChild) {
753             listElementAsDataNodes.each {it.childDataNodes = [createGrandChild(it.xpath, tag)]}
754         }
755         return listElementAsDataNodes
756     }
757
758     def createGrandChild(parentXPath, tag) {
759         new DataNodeBuilder()
760             .withXpath("${parentXPath}/${tag}-grand-child")
761             .withLeaves([attr1: tag])
762             .build()
763     }
764
765 }