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