2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2022 Nordix Foundation
4 * Modifications Copyright (C) 2021 Pantheon.tech
5 * Modifications Copyright (C) 2021-2022 Bell Canada.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
22 package org.onap.cps.spi.impl
24 import com.fasterxml.jackson.databind.ObjectMapper
25 import com.google.common.collect.ImmutableSet
26 import org.onap.cps.spi.CpsDataPersistenceService
27 import org.onap.cps.spi.entities.FragmentEntity
28 import org.onap.cps.spi.exceptions.AlreadyDefinedException
29 import org.onap.cps.spi.exceptions.AnchorNotFoundException
30 import org.onap.cps.spi.exceptions.DataNodeNotFoundException
31 import org.onap.cps.spi.exceptions.DataspaceNotFoundException
32 import org.onap.cps.spi.model.DataNode
33 import org.onap.cps.spi.model.DataNodeBuilder
34 import org.onap.cps.utils.JsonObjectMapper
35 import org.springframework.beans.factory.annotation.Autowired
36 import org.springframework.test.context.jdbc.Sql
37 import javax.validation.ConstraintViolationException
38 import java.util.stream.Collectors
40 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
41 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
43 class CpsDataPersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase {
46 CpsDataPersistenceService objectUnderTest
48 static final JsonObjectMapper jsonObjectMapper = new JsonObjectMapper(new ObjectMapper())
50 static final String SET_DATA = '/data/fragment.sql'
51 static final int DATASPACE_1001_ID = 1001L
52 static final int ANCHOR_3003_ID = 3003L
53 static final long ID_DATA_NODE_WITH_DESCENDANTS = 4001
54 static final String XPATH_DATA_NODE_WITH_DESCENDANTS = '/parent-1'
55 static final String XPATH_DATA_NODE_WITH_LEAVES = '/parent-100'
56 static final long UPDATE_DATA_NODE_FRAGMENT_ID = 4202L
57 static final long UPDATE_DATA_NODE_SUB_FRAGMENT_ID = 4203L
58 static final long LIST_DATA_NODE_PARENT201_FRAGMENT_ID = 4206L
59 static final long LIST_DATA_NODE_PARENT203_FRAGMENT_ID = 4214L
60 static final long LIST_DATA_NODE_PARENT204_FRAGMENT_ID = 4219L
61 static final long LIST_DATA_NODE_PARENT205_FRAGMENT_ID = 4221L
62 static final long LIST_DATA_NODE_CHILD202_FRAGMENT_ID = 4204L
63 static final long LIST_DATA_NODE_PARENT202_FRAGMENT_ID = 4211L
65 static final DataNode newDataNode = new DataNodeBuilder().build()
66 static DataNode existingDataNode
67 static DataNode existingChildDataNode
69 def expectedLeavesByXpathMap = [
70 '/parent-100' : ['parent-leaf': 'parent-leaf value'],
71 '/parent-100/child-001' : ['first-child-leaf': 'first-child-leaf value'],
72 '/parent-100/child-002' : ['second-child-leaf': 'second-child-leaf value'],
73 '/parent-100/child-002/grand-child': ['grand-child-leaf': 'grand-child-leaf value']
77 existingDataNode = createDataNodeTree(XPATH_DATA_NODE_WITH_DESCENDANTS)
78 existingChildDataNode = createDataNodeTree('/parent-1/child-1')
81 @Sql([CLEAR_DATA, SET_DATA])
82 def 'StoreDataNode with descendants.'() {
83 when: 'a fragment with descendants is stored'
84 def parentXpath = "/parent-new"
85 def childXpath = "/parent-new/child-new"
86 def grandChildXpath = "/parent-new/child-new/grandchild-new"
87 objectUnderTest.storeDataNode(DATASPACE_NAME, ANCHOR_NAME1,
88 createDataNodeTree(parentXpath, childXpath, grandChildXpath))
89 then: 'it can be retrieved by its xpath'
90 def parentFragment = getFragmentByXpath(DATASPACE_NAME, ANCHOR_NAME1, parentXpath)
91 and: 'it contains the children'
92 parentFragment.childFragments.size() == 1
93 def childFragment = parentFragment.childFragments[0]
94 childFragment.xpath == childXpath
95 and: "and its children's children"
96 childFragment.childFragments.size() == 1
97 def grandchildFragment = childFragment.childFragments[0]
98 grandchildFragment.xpath == grandChildXpath
101 @Sql([CLEAR_DATA, SET_DATA])
102 def 'Store data node for multiple anchors using the same schema.'() {
103 def xpath = "/parent-new"
104 given: 'a fragment is stored for an anchor'
105 objectUnderTest.storeDataNode(DATASPACE_NAME, ANCHOR_NAME1, createDataNodeTree(xpath))
106 when: 'another fragment is stored for an other anchor, using the same schema set'
107 objectUnderTest.storeDataNode(DATASPACE_NAME, ANCHOR_NAME3, createDataNodeTree(xpath))
108 then: 'both fragments can be retrieved by their xpath'
109 def fragment1 = getFragmentByXpath(DATASPACE_NAME, ANCHOR_NAME1, xpath)
110 fragment1.anchor.name == ANCHOR_NAME1
111 fragment1.xpath == xpath
112 def fragment2 = getFragmentByXpath(DATASPACE_NAME, ANCHOR_NAME3, xpath)
113 fragment2.anchor.name == ANCHOR_NAME3
114 fragment2.xpath == xpath
117 @Sql([CLEAR_DATA, SET_DATA])
118 def 'Store datanode error scenario: #scenario.'() {
119 when: 'attempt to store a data node with #scenario'
120 objectUnderTest.storeDataNode(dataspaceName, anchorName, dataNode)
121 then: 'a #expectedException is thrown'
122 thrown(expectedException)
123 where: 'the following data is used'
124 scenario | dataspaceName | anchorName | dataNode || expectedException
125 'dataspace does not exist' | 'unknown' | 'not-relevant' | newDataNode || DataspaceNotFoundException
126 'schema set does not exist' | DATASPACE_NAME | 'unknown' | newDataNode || AnchorNotFoundException
127 'anchor already exists' | DATASPACE_NAME | ANCHOR_NAME1 | newDataNode || ConstraintViolationException
128 'datanode already exists' | DATASPACE_NAME | ANCHOR_NAME1 | existingDataNode || AlreadyDefinedException
131 @Sql([CLEAR_DATA, SET_DATA])
132 def 'Add a child to a Fragment that already has a child.'() {
133 given: ' a new child node'
134 def newChild = createDataNodeTree('xpath for new child')
135 when: 'the child is added to an existing parent with 1 child'
136 objectUnderTest.addChildDataNode(DATASPACE_NAME, ANCHOR_NAME1, XPATH_DATA_NODE_WITH_DESCENDANTS, newChild)
137 then: 'the parent is now has to 2 children'
138 def expectedExistingChildPath = '/parent-1/child-1'
139 def parentFragment = fragmentRepository.findById(ID_DATA_NODE_WITH_DESCENDANTS).orElseThrow()
140 parentFragment.getChildFragments().size() == 2
141 and: 'it still has the old child'
142 parentFragment.getChildFragments().find({ it.xpath == expectedExistingChildPath })
143 and: 'it has the new child'
144 parentFragment.getChildFragments().find({ it.xpath == newChild.xpath })
147 @Sql([CLEAR_DATA, SET_DATA])
148 def 'Add child error scenario: #scenario.'() {
149 when: 'attempt to add a child data node with #scenario'
150 objectUnderTest.addChildDataNode(DATASPACE_NAME, ANCHOR_NAME1, parentXpath, dataNode)
151 then: 'a #expectedException is thrown'
152 thrown(expectedException)
153 where: 'the following data is used'
154 scenario | parentXpath | dataNode || expectedException
155 'parent does not exist' | 'unknown' | newDataNode || DataNodeNotFoundException
156 'already existing child' | XPATH_DATA_NODE_WITH_DESCENDANTS | existingChildDataNode || AlreadyDefinedException
159 @Sql([CLEAR_DATA, SET_DATA])
160 def 'Add multiple list elements including an element with a child datanode.'() {
161 given: 'two new data nodes for an existing list'
162 def listElementXpaths = ['/parent-201/child-204[@key="B"]', '/parent-201/child-204[@key="C"]']
163 def listElements = toDataNodes(listElementXpaths)
164 and: 'a child node for one of the new data nodes'
165 def childDataNode = buildDataNode('/parent-201/child-204[@key="C"]/grand-child-204[@key2="Z"]', [leave:'value'], [])
166 listElements[0].childDataNodes = [childDataNode]
167 when: 'the data nodes (list elements) are added to existing parent node'
168 objectUnderTest.addListElements(DATASPACE_NAME, ANCHOR_NAME3, '/parent-201', listElements)
169 then: 'new entries successfully persisted, parent node now contains 5 children (2 new + 3 existing before)'
170 def parentFragment = fragmentRepository.getById(LIST_DATA_NODE_PARENT201_FRAGMENT_ID)
171 def allChildXpaths = parentFragment.getChildFragments().collect { it.getXpath() }
172 assert allChildXpaths.size() == 5
173 assert allChildXpaths.containsAll(listElementXpaths)
174 and: 'the child node of the new list entry is also present'
175 def dataspaceEntity = dataspaceRepository.getByName(DATASPACE_NAME)
176 def anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, ANCHOR_NAME3)
177 def listElementChild = fragmentRepository.findByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity, childDataNode.xpath)
178 assert listElementChild.isPresent()
181 @Sql([CLEAR_DATA, SET_DATA])
182 def 'Add list element error scenario: #scenario.'() {
183 given: 'list element as a collection of data nodes'
184 def listElementCollection = toDataNodes(listElementXpaths)
185 when: 'attempt to add list elements to parent node'
186 objectUnderTest.addListElements(DATASPACE_NAME, ANCHOR_NAME3, parentNodeXpath, listElementCollection)
187 then: 'a #expectedException is thrown'
188 thrown(expectedException)
189 where: 'following parameters were used'
190 scenario | parentNodeXpath | listElementXpaths || expectedException
191 'parent node does not exist' | '/unknown' | ['irrelevant'] || DataNodeNotFoundException
192 'already existing fragment' | '/parent-201' | ['/parent-201/child-204[@key="A"]'] || AlreadyDefinedException
196 static def createDataNodeTree(String... xpaths) {
197 def dataNodeBuilder = new DataNodeBuilder().withXpath(xpaths[0])
198 if (xpaths.length > 1) {
199 def xPathsDescendant = Arrays.copyOfRange(xpaths, 1, xpaths.length)
200 def childDataNode = createDataNodeTree(xPathsDescendant)
201 dataNodeBuilder.withChildDataNodes(ImmutableSet.of(childDataNode))
203 dataNodeBuilder.build()
206 def getFragmentByXpath(dataspaceName, anchorName, xpath) {
207 def dataspace = dataspaceRepository.getByName(dataspaceName)
208 def anchor = anchorRepository.getByDataspaceAndName(dataspace, anchorName)
209 return fragmentRepository.findByDataspaceAndAnchorAndXpath(dataspace, anchor, xpath).orElseThrow()
212 @Sql([CLEAR_DATA, SET_DATA])
213 def 'Get data node by xpath without descendants.'() {
214 when: 'data node is requested'
215 def result = objectUnderTest.getDataNode(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES,
216 inputXPath, OMIT_DESCENDANTS)
217 then: 'data node is returned with no descendants'
218 assert result.getXpath() == XPATH_DATA_NODE_WITH_LEAVES
219 and: 'expected leaves'
220 assert result.getChildDataNodes().size() == 0
221 assertLeavesMaps(result.getLeaves(), expectedLeavesByXpathMap[XPATH_DATA_NODE_WITH_LEAVES])
222 where: 'the following data is used'
223 scenario | inputXPath
224 'some xpath' | '/parent-100'
229 @Sql([CLEAR_DATA, SET_DATA])
230 def 'Get data node by xpath with all descendants.'() {
231 when: 'data node is requested with all descendants'
232 def result = objectUnderTest.getDataNode(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES,
233 inputXPath, INCLUDE_ALL_DESCENDANTS)
234 def mappedResult = treeToFlatMapByXpath(new HashMap<>(), result)
235 then: 'data node is returned with all the descendants populated'
236 assert mappedResult.size() == 4
237 assert result.getChildDataNodes().size() == 2
238 assert mappedResult.get('/parent-100/child-001').getChildDataNodes().size() == 0
239 assert mappedResult.get('/parent-100/child-002').getChildDataNodes().size() == 1
240 and: 'extracted leaves maps are matching expected'
241 mappedResult.forEach(
242 (xPath, dataNode) -> assertLeavesMaps(dataNode.getLeaves(), expectedLeavesByXpathMap[xPath]))
243 where: 'the following data is used'
244 scenario | inputXPath
245 'some xpath' | '/parent-100'
250 @Sql([CLEAR_DATA, SET_DATA])
251 def 'Get data node error scenario: #scenario.'() {
252 when: 'attempt to get data node with #scenario'
253 objectUnderTest.getDataNode(dataspaceName, anchorName, xpath, OMIT_DESCENDANTS)
254 then: 'a #expectedException is thrown'
255 thrown(expectedException)
256 where: 'the following data is used'
257 scenario | dataspaceName | anchorName | xpath || expectedException
258 'non-existing dataspace' | 'NO DATASPACE' | 'not relevant' | 'not relevant' || DataspaceNotFoundException
259 'non-existing anchor' | DATASPACE_NAME | 'NO ANCHOR' | 'not relevant' || AnchorNotFoundException
260 'non-existing xpath' | DATASPACE_NAME | ANCHOR_FOR_DATA_NODES_WITH_LEAVES | 'NO XPATH' || DataNodeNotFoundException
263 @Sql([CLEAR_DATA, SET_DATA])
264 def 'Update data node leaves.'() {
265 when: 'update is performed for leaves'
266 objectUnderTest.updateDataLeaves(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES,
267 "/parent-200/child-201", ['leaf-value': 'new'])
268 then: 'leaves are updated for selected data node'
269 def updatedFragment = fragmentRepository.getById(UPDATE_DATA_NODE_FRAGMENT_ID)
270 def updatedLeaves = getLeavesMap(updatedFragment)
271 assert updatedLeaves.size() == 1
272 assert updatedLeaves.'leaf-value' == 'new'
273 and: 'existing child entry remains as is'
274 def childFragment = updatedFragment.getChildFragments().iterator().next()
275 def childLeaves = getLeavesMap(childFragment)
276 assert childFragment.getId() == UPDATE_DATA_NODE_SUB_FRAGMENT_ID
277 assert childLeaves.'leaf-value' == 'original'
280 @Sql([CLEAR_DATA, SET_DATA])
281 def 'Update data leaves error scenario: #scenario.'() {
282 when: 'attempt to update data node for #scenario'
283 objectUnderTest.updateDataLeaves(dataspaceName, anchorName, xpath, ['leaf-name': 'leaf-value'])
284 then: 'a #expectedException is thrown'
285 thrown(expectedException)
286 where: 'the following data is used'
287 scenario | dataspaceName | anchorName | xpath || expectedException
288 'non-existing dataspace' | 'NO DATASPACE' | 'not relevant' | 'not relevant' || DataspaceNotFoundException
289 'non-existing anchor' | DATASPACE_NAME | 'NO ANCHOR' | 'not relevant' || AnchorNotFoundException
290 'non-existing xpath' | DATASPACE_NAME | ANCHOR_FOR_DATA_NODES_WITH_LEAVES | 'NON-EXISTING XPATH' || DataNodeNotFoundException
293 @Sql([CLEAR_DATA, SET_DATA])
294 def 'Replace data node tree with descendants removal.'() {
295 given: 'data node object with leaves updated, no children'
296 def submittedDataNode = buildDataNode("/parent-200/child-201", ['leaf-value': 'new'], [])
297 when: 'replace data node tree is performed'
298 objectUnderTest.replaceDataNodeTree(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, submittedDataNode)
299 then: 'leaves have been updated for selected data node'
300 def updatedFragment = fragmentRepository.getById(UPDATE_DATA_NODE_FRAGMENT_ID)
301 def updatedLeaves = getLeavesMap(updatedFragment)
302 assert updatedLeaves.size() == 1
303 assert updatedLeaves.'leaf-value' == 'new'
304 and: 'updated entry has no children'
305 updatedFragment.getChildFragments().isEmpty()
306 and: 'previously attached child entry is removed from database'
307 fragmentRepository.findById(UPDATE_DATA_NODE_SUB_FRAGMENT_ID).isEmpty()
310 @Sql([CLEAR_DATA, SET_DATA])
311 def 'Replace data node tree with descendants.'() {
312 given: 'data node object with leaves updated, having child with old content'
313 def submittedDataNode = buildDataNode("/parent-200/child-201", ['leaf-value': 'new'], [
314 buildDataNode("/parent-200/child-201/grand-child", ['leaf-value': 'original'], [])
316 when: 'update is performed including descendants'
317 objectUnderTest.replaceDataNodeTree(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, submittedDataNode)
318 then: 'leaves have been updated for selected data node'
319 def updatedFragment = fragmentRepository.getById(UPDATE_DATA_NODE_FRAGMENT_ID)
320 def updatedLeaves = getLeavesMap(updatedFragment)
321 assert updatedLeaves.size() == 1
322 assert updatedLeaves.'leaf-value' == 'new'
323 and: 'existing child entry is not updated as content is same'
324 def childFragment = updatedFragment.getChildFragments().iterator().next()
325 childFragment.getXpath() == '/parent-200/child-201/grand-child'
326 def childLeaves = getLeavesMap(childFragment)
327 assert childLeaves.'leaf-value' == 'original'
330 @Sql([CLEAR_DATA, SET_DATA])
331 def 'Replace data node tree with same descendants but changed leaf value.'() {
332 given: 'data node object with leaves updated, having child with old content'
333 def submittedDataNode = buildDataNode("/parent-200/child-201", ['leaf-value': 'new'], [
334 buildDataNode("/parent-200/child-201/grand-child", ['leaf-value': 'new'], [])
336 when: 'update is performed including descendants'
337 objectUnderTest.replaceDataNodeTree(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, submittedDataNode)
338 then: 'leaves have been updated for selected data node'
339 def updatedFragment = fragmentRepository.getById(UPDATE_DATA_NODE_FRAGMENT_ID)
340 def updatedLeaves = getLeavesMap(updatedFragment)
341 assert updatedLeaves.size() == 1
342 assert updatedLeaves.'leaf-value' == 'new'
343 and: 'existing child entry is updated with the new content'
344 def childFragment = updatedFragment.getChildFragments().iterator().next()
345 childFragment.getXpath() == '/parent-200/child-201/grand-child'
346 def childLeaves = getLeavesMap(childFragment)
347 assert childLeaves.'leaf-value' == 'new'
350 @Sql([CLEAR_DATA, SET_DATA])
351 def 'Replace data node tree with different descendants xpath'() {
352 given: 'data node object with leaves updated, having child with old content'
353 def submittedDataNode = buildDataNode("/parent-200/child-201", ['leaf-value': 'new'], [
354 buildDataNode("/parent-200/child-201/grand-child-new", ['leaf-value': 'new'], [])
356 when: 'update is performed including descendants'
357 objectUnderTest.replaceDataNodeTree(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, submittedDataNode)
358 then: 'leaves have been updated for selected data node'
359 def updatedFragment = fragmentRepository.getById(UPDATE_DATA_NODE_FRAGMENT_ID)
360 def updatedLeaves = getLeavesMap(updatedFragment)
361 assert updatedLeaves.size() == 1
362 assert updatedLeaves.'leaf-value' == 'new'
363 and: 'previously attached child entry is removed from database'
364 fragmentRepository.findById(UPDATE_DATA_NODE_SUB_FRAGMENT_ID).isEmpty()
365 and: 'new child entry is persisted'
366 def childFragment = updatedFragment.getChildFragments().iterator().next()
367 childFragment.getXpath() == '/parent-200/child-201/grand-child-new'
368 def childLeaves = getLeavesMap(childFragment)
369 assert childLeaves.'leaf-value' == 'new'
372 @Sql([CLEAR_DATA, SET_DATA])
373 def 'Replace data node tree error scenario: #scenario.'() {
374 given: 'data node object'
375 def submittedDataNode = buildDataNode(xpath, ['leaf-name': 'leaf-value'], [])
376 when: 'attempt to update data node for #scenario'
377 objectUnderTest.replaceDataNodeTree(dataspaceName, anchorName, submittedDataNode)
378 then: 'a #expectedException is thrown'
379 thrown(expectedException)
380 where: 'the following data is used'
381 scenario | dataspaceName | anchorName | xpath || expectedException
382 'non-existing dataspace' | 'NO DATASPACE' | 'not relevant' | 'not relevant' || DataspaceNotFoundException
383 'non-existing anchor' | DATASPACE_NAME | 'NO ANCHOR' | 'not relevant' || AnchorNotFoundException
384 'non-existing xpath' | DATASPACE_NAME | ANCHOR_FOR_DATA_NODES_WITH_LEAVES | 'NON-EXISTING XPATH' || DataNodeNotFoundException
387 @Sql([CLEAR_DATA, SET_DATA])
388 def 'Replace list content of #scenario.'() {
389 given: 'list element as a collection of data nodes'
390 def listElementCollection = toDataNodes(listElementXpaths)
391 when: 'list elements are replaced within the existing parent node'
392 objectUnderTest.replaceListContent(DATASPACE_NAME, ANCHOR_NAME3, parentXpath, listElementCollection)
393 then: 'list elements are updated as expected, non-list element remains as is'
394 def parentFragment = fragmentRepository.getById(listElementFragmentID)
395 def allChildXpaths = parentFragment.getChildFragments().collect { it.getXpath() }
396 assert allChildXpaths.size() == expectedChildXpaths.size()
397 assert allChildXpaths.containsAll(expectedChildXpaths)
398 where: 'following parameters were used'
399 scenario | listElementXpaths | parentXpath | listElementFragmentID || expectedChildXpaths
400 'existing list element with non existing key' | ['/parent-201/child-204[@key="B"]'] | '/parent-201' | LIST_DATA_NODE_PARENT201_FRAGMENT_ID || ['/parent-201/child-203', '/parent-201/child-204[@key="B"]']
401 'non existing list element with non existing key' | ['/parent-201/child-205[@key="1"]'] | '/parent-201' | LIST_DATA_NODE_PARENT201_FRAGMENT_ID || ['/parent-201/child-203', '/parent-201/child-204[@key="A"]', '/parent-201/child-204[@key="X"]', '/parent-201/child-205[@key="1"]']
402 'list element with 1 existing key' | ['/parent-201/child-204[@key="X"]'] | '/parent-201' | LIST_DATA_NODE_PARENT201_FRAGMENT_ID || ['/parent-201/child-203', '/parent-201/child-204[@key="X"]']
403 'list element with combined keys' | ['/parent-202/child-205[@key="A"]'] | '/parent-202' | LIST_DATA_NODE_PARENT202_FRAGMENT_ID || ['/parent-202/child-206[@key="A"]', '/parent-202/child-205[@key="A"]']
404 'grandchild list element' | ['/parent-200/child-202/grand-child-202[@key="E"]'] | '/parent-200/child-202' | LIST_DATA_NODE_CHILD202_FRAGMENT_ID || ['/parent-200/child-202/grand-child-202[@key="E"]']
405 'list element with two list elements' | ['/parent-201/child-204[@key="new X"]', '/parent-201/child-204[@key="Y"]'] | '/parent-201' | LIST_DATA_NODE_PARENT201_FRAGMENT_ID || ['/parent-201/child-203', '/parent-201/child-204[@key="new X"]', '/parent-201/child-204[@key="Y"]']
406 'list element with compounded list element' | ['/parent-202/child-205[@key="A" and @key2="B"]'] | '/parent-202' | LIST_DATA_NODE_PARENT202_FRAGMENT_ID || ['/parent-202/child-206[@key="A"]', '/parent-202/child-205[@key="A" and @key2="B"]']
407 'list element with list element with parent with key value' | ['/parent-204[@key="L"]/child-210[@key="N"]'] | '/parent-204[@key="L"]' | LIST_DATA_NODE_PARENT204_FRAGMENT_ID || ['/parent-204[@key="L"]/child-210[@key="N"]']
410 @Sql([CLEAR_DATA, SET_DATA])
411 def 'Replace list content that has #scenario'() {
412 given: 'list element with child list element as a collection of data nodes'
413 def grandChildDataNodes = toDataNodes(grandChildXpaths)
414 def listElementCollection = new DataNodeBuilder().withXpath(childXpath).withChildDataNodes(grandChildDataNodes).build()
415 when: 'list elements replaced within the existing parent node'
416 objectUnderTest.replaceListContent(DATASPACE_NAME, ANCHOR_NAME3, parentXpath, [listElementCollection ])
417 then: 'list elements are updated as expected with non-list elements remaining as is'
418 def parentFragment = fragmentRepository.getById(listElementFragmentId)
419 def allChildXpaths = parentFragment.getChildFragments().collect { it.getXpath() }
420 assert allChildXpaths.size() == expectedRemainingChildXpaths.size()
421 assert allChildXpaths.containsAll(expectedRemainingChildXpaths)
422 and: 'grandchild list elements are updated as expected'
423 def allGrandChildXpaths = parentFragment.getChildFragments().collect(){
424 it.getChildFragments().collect(){
426 allGrandChildXpaths.removeIf(list -> list.isEmpty())
427 def grandChildXpathsToList = allGrandChildXpaths.stream().flatMap(List::stream).collect(Collectors.toList())
428 def expectedGrandChildXpaths = grandChildXpaths
429 assert grandChildXpathsToList.size() == expectedGrandChildXpaths.size()
430 assert grandChildXpathsToList.containsAll(expectedGrandChildXpaths)
431 where: 'the following parameters are used'
432 scenario | parentXpath | childXpath | grandChildXpaths | listElementFragmentId || expectedRemainingChildXpaths
433 'grandchild of list' | '/parent-203' | '/parent-203/child-204[@key="X"]' | ['/parent-203/child-204/grandchild[@key="2"]'] | LIST_DATA_NODE_PARENT203_FRAGMENT_ID || ['/parent-203/child-203', '/parent-203/child-204[@key="X"]']
434 'grandchild of list with two new element' | '/parent-203' | '/parent-203/child-204[@key="X"]' | ['/parent-203/child-204/grandchild[@key="2"]' , '/parent-203/child-204/grandchild[@key="3"]'] | LIST_DATA_NODE_PARENT203_FRAGMENT_ID || ['/parent-203/child-203', '/parent-203/child-204[@key="X"]']
435 'grandchild with compound list elements' | '/parent-205' | '/parent-205/child-205[@key="X"]' | ['/parent-205/child-205/grand-child-206[@key="Y" and @key2="Z"]'] | LIST_DATA_NODE_PARENT205_FRAGMENT_ID || ['/parent-205/child-205', '/parent-205/child-205[@key="X"]']
438 @Sql([CLEAR_DATA, SET_DATA])
439 def 'Replace list content of #scenario with grandchildren.'() {
440 given: 'list element as a collection of data nodes'
441 def listElementCollection = toDataNodes(listElementXpaths)
442 when: 'list elements are replaced within the existing parent node'
443 objectUnderTest.replaceListContent(DATASPACE_NAME, ANCHOR_NAME3, parentXpath, listElementCollection)
444 then: 'child list elements are updated as expected with non-list elements remaining as is'
445 def parentFragment = fragmentRepository.getById(listElementFragmentID)
446 def allChildXpaths = parentFragment.getChildFragments().collect { it.getXpath() }
447 assert allChildXpaths.size() == expectedChildXpaths.size()
448 assert allChildXpaths.containsAll(expectedChildXpaths)
449 and: 'grandchild list elements are updated as expected'
450 def allGrandChildXpaths = parentFragment.getChildFragments().collect {
451 it.getChildFragments().collect {
453 allGrandChildXpaths.removeIf(list -> list.isEmpty())
454 assert allGrandChildXpaths.size() == expectedGrandChildXpaths.size()
455 assert allGrandChildXpaths.containsAll(expectedGrandChildXpaths)
456 where: 'following parameters were used'
457 scenario | listElementXpaths | parentXpath | listElementFragmentID || expectedChildXpaths | expectedGrandChildXpaths
458 'existing list element with existing keys' | ['/parent-203/child-204[@key="X"]'] | '/parent-203' | LIST_DATA_NODE_PARENT203_FRAGMENT_ID || ['/parent-203/child-203', '/parent-203/child-204[@key="X"]'] | []
459 'non existing list element with existing keys' | ['/parent-203/child-204[@key="V"]'] | '/parent-203' | LIST_DATA_NODE_PARENT203_FRAGMENT_ID || ['/parent-203/child-203', '/parent-203/child-204[@key="V"]'] | []
463 @Sql([CLEAR_DATA, SET_DATA])
464 def 'Replace content error scenario: #scenario.'() {
465 given: 'list element as a collection of data nodes'
466 def listElementCollection = toDataNodes(listElementXpaths)
467 when: 'list elements were replaced under existing parent node'
468 objectUnderTest.replaceListContent(DATASPACE_NAME, ANCHOR_NAME3, parentNodeXpath, listElementCollection)
469 then: 'a #expectedException is thrown'
470 thrown(expectedException)
471 where: 'following parameters were used'
472 scenario | parentNodeXpath | listElementXpaths || expectedException
473 'parent node does not exist' | '/unknown' | ['irrelevant'] || DataNodeNotFoundException
476 @Sql([CLEAR_DATA, SET_DATA])
477 def 'Delete list scenario: #scenario.'() {
478 when: 'deleting list is executed for: #scenario.'
479 objectUnderTest.deleteListDataNode(DATASPACE_NAME, ANCHOR_NAME3, targetXpaths)
480 then: 'only the expected children remain'
481 def parentFragment = fragmentRepository.getById(parentFragmentId)
482 def remainingChildXpaths = parentFragment.getChildFragments().collect { it.getXpath() }
483 assert remainingChildXpaths.size() == expectedRemainingChildXpaths.size()
484 assert remainingChildXpaths.containsAll(expectedRemainingChildXpaths)
485 where: 'following parameters were used'
486 scenario | targetXpaths | parentFragmentId || expectedRemainingChildXpaths
487 '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="X"]']
488 '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"]']
489 'whole list' | '/parent-203/child-204' | LIST_DATA_NODE_PARENT203_FRAGMENT_ID || ['/parent-203/child-203']
490 'list element under list element' | '/parent-203/child-204[@key="X"]/grand-child-204[@key2="Y"]' | LIST_DATA_NODE_PARENT203_FRAGMENT_ID || ['/parent-203/child-203', '/parent-203/child-204[@key="X"]', '/parent-203/child-204[@key="A"]']
493 @Sql([CLEAR_DATA, SET_DATA])
494 def 'Delete list error scenario: #scenario.'() {
495 when: 'attempting to delete scenario: #scenario.'
496 objectUnderTest.deleteListDataNode(DATASPACE_NAME, ANCHOR_NAME3, targetXpaths)
497 then: 'a DataNodeNotFoundException is thrown'
498 thrown(DataNodeNotFoundException)
499 where: 'following parameters were used'
500 scenario | targetXpaths
501 'whole list, parent node does not exist' | '/unknown/some-child'
502 'list element, parent node does not exist' | '/unknown/child-204[@key="A"]'
503 'whole list does not exist' | '/parent-200/unknown'
504 'list element, list does not exist' | '/parent-200/unknown[@key="C"]'
505 'list element, element does not exist' | '/parent-203/child-204[@key="C"]'
506 'valid datanode but not a list' | '/parent-200/child-202'
509 @Sql([CLEAR_DATA, SET_DATA])
510 def 'Confirm deletion of #scenario.'() {
511 given: 'a valid data node'
514 when: 'data nodes are deleted'
515 objectUnderTest.deleteDataNode(DATASPACE_NAME, ANCHOR_NAME3, xpathForDeletion)
516 then: 'verify data nodes are removed'
518 dataNode = objectUnderTest.getDataNode(DATASPACE_NAME, ANCHOR_NAME3, getDataNodesXpaths, INCLUDE_ALL_DESCENDANTS)
519 dataNodeXpath = dataNode.getXpath()
520 assert dataNodeXpath == expectedXpaths
521 } catch (DataNodeNotFoundException) {
522 assert dataNodeXpath == expectedXpaths
524 where: 'following parameters were used'
525 scenario | xpathForDeletion | getDataNodesXpaths || expectedXpaths
526 'child of target' | '/parent-206/child-206' | '/parent-206/child-206' || null
527 'child data node, parent still exists' | '/parent-206/child-206' | '/parent-206' || '/parent-206'
528 'list element' | '/parent-206/child-206/grand-child-206[@key="A"]' | '/parent-206/child-206/grand-child-206[@key="A"]' || null
529 'list element, sibling still exists' | '/parent-206/child-206/grand-child-206[@key="A"]' | '/parent-206/child-206/grand-child-206[@key="X"]' || '/parent-206/child-206/grand-child-206[@key="X"]'
532 @Sql([CLEAR_DATA, SET_DATA])
533 def 'Delete data node with #scenario.'() {
534 when: 'data node is deleted'
535 objectUnderTest.deleteDataNode(DATASPACE_NAME, ANCHOR_NAME3, datanodeXpath)
536 then: 'a #expectedException is thrown'
537 thrown(DataNodeNotFoundException)
538 where: 'the following parameters were used'
539 scenario | datanodeXpath
540 'valid data node, non existent child node' | '/parent-203/child-non-existent'
541 'invalid list element' | '/parent-206/child-206/grand-child-206@key="A"]'
544 @Sql([CLEAR_DATA, SET_DATA])
545 def 'Delete data node for an anchor.'() {
546 given: 'a data-node exists for an anchor'
547 assert fragmentsExistInDB(DATASPACE_1001_ID, ANCHOR_3003_ID)
548 when: 'data nodes are deleted '
549 objectUnderTest.deleteDataNodes(DATASPACE_NAME, ANCHOR_NAME3)
550 then: 'all data-nodes are deleted successfully'
551 assert !fragmentsExistInDB(DATASPACE_1001_ID, ANCHOR_3003_ID)
554 def fragmentsExistInDB(dataSpaceId, anchorId) {
555 !fragmentRepository.findRootsByDataspaceAndAnchor(dataSpaceId, anchorId).isEmpty()
558 static Collection<DataNode> toDataNodes(xpaths) {
559 return xpaths.collect { new DataNodeBuilder().withXpath(it).build() }
562 static DataNode buildDataNode(xpath, leaves, childDataNodes) {
563 return new DataNodeBuilder().withXpath(xpath).withLeaves(leaves).withChildDataNodes(childDataNodes).build()
566 static Map<String, Object> getLeavesMap(FragmentEntity fragmentEntity) {
567 return jsonObjectMapper.convertJsonString(fragmentEntity.getAttributes(), Map<String, Object>.class)
570 def static assertLeavesMaps(actualLeavesMap, expectedLeavesMap) {
571 expectedLeavesMap.forEach((key, value) -> {
572 def actualValue = actualLeavesMap[key]
573 if (value instanceof Collection<?> && actualValue instanceof Collection<?>) {
574 assert value.size() == actualValue.size()
575 assert value.containsAll(actualValue)
577 assert value == actualValue
583 def static treeToFlatMapByXpath(Map<String, DataNode> flatMap, DataNode dataNodeTree) {
584 flatMap.put(dataNodeTree.getXpath(), dataNodeTree)
585 dataNodeTree.getChildDataNodes()
586 .forEach(childDataNode -> treeToFlatMapByXpath(flatMap, childDataNode))