2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation
4 * Modifications Copyright (C) 2021 Pantheon.tech
5 * Modifications Copyright (C) 2021 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.google.common.collect.ImmutableSet
25 import com.google.gson.Gson
26 import com.google.gson.GsonBuilder
27 import org.onap.cps.spi.CpsDataPersistenceService
28 import org.onap.cps.spi.entities.FragmentEntity
29 import org.onap.cps.spi.exceptions.AlreadyDefinedException
30 import org.onap.cps.spi.exceptions.AnchorNotFoundException
31 import org.onap.cps.spi.exceptions.DataNodeNotFoundException
32 import org.onap.cps.spi.exceptions.DataspaceNotFoundException
33 import org.onap.cps.spi.model.DataNode
34 import org.onap.cps.spi.model.DataNodeBuilder
35 import org.spockframework.util.CollectionUtil
36 import org.springframework.beans.factory.annotation.Autowired
37 import org.springframework.test.context.jdbc.Sql
39 import javax.validation.ConstraintViolationException
40 import java.util.stream.Collectors
42 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
43 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
45 class CpsDataPersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase {
48 CpsDataPersistenceService objectUnderTest
50 static final Gson GSON = new GsonBuilder().create()
52 static final String SET_DATA = '/data/fragment.sql'
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 list-node fragment with multiple elements including an element with a child datanode.'() {
161 given: 'two new data nodes for an existing list'
162 def listNodeXpaths = ['/parent-201/child-204[@key="B"]', '/parent-201/child-204[@key="C"]']
163 def listNodeCollection = buildDataNodeCollection(listNodeXpaths)
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 listNodeCollection.iterator().next().childDataNodes = [childDataNode]
167 when: 'the data nodes (list elements) are added to existing parent node'
168 objectUnderTest.addListDataNodes(DATASPACE_NAME, ANCHOR_NAME3, '/parent-201', listNodeCollection)
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(listNodeXpaths)
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-node fragment error scenario: #scenario.'() {
183 given: 'list node data fragment as a collection of data nodes'
184 def listNodeCollection = buildDataNodeCollection(listNodeXpaths)
185 when: 'list-node elements added to existing parent node'
186 objectUnderTest.addListDataNodes(DATASPACE_NAME, ANCHOR_NAME3, parentNodeXpath, listNodeCollection)
187 then: 'a #expectedException is thrown'
188 thrown(expectedException)
189 where: 'following parameters were used'
190 scenario | parentNodeXpath | listNodeXpaths || 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-node content of #scenario.'() {
389 given: 'list node data fragment as a collection of data nodes'
390 def listNodeCollection = buildDataNodeCollection(listNodeXpaths)
391 when: 'list-node elements replaced within the existing parent node'
392 objectUnderTest.replaceListDataNodes(DATASPACE_NAME, ANCHOR_NAME3, parentXpath, listNodeCollection)
393 then: 'child list elements are updated as expected, non-list element remains as is'
394 def parentFragment = fragmentRepository.getById(listNodeFragmentID)
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 | listNodeXpaths |parentXpath |listNodeFragmentID || expectedChildXpaths
400 'existing list node 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 node 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 'existing list node 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 'existing list-node 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 'existing grandchild list-node' | ['/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 'existing list node with two list nodes' | ['/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 'existing list node with compounded list node' | ['/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 'existing list node with list node 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-node that has children with #scenario'() {
412 given: 'list node data fragment with child data node fragments'
413 def grandChildDataNodes = buildDataNodeCollection(grandChildXpaths)
414 def listNode = new DataNodeBuilder().withXpath(childXpath).withChildDataNodes(grandChildDataNodes).build()
415 when: 'list-node elements replaced within the existing parent node'
416 objectUnderTest.replaceListDataNodes(DATASPACE_NAME, ANCHOR_NAME3, parentXpath, [ listNode ])
417 then: 'child list elements are updated as expected with non-list elements remaining as is'
418 def parentFragment = fragmentRepository.getById(listNodeFragmentId)
419 def allChildXpaths = parentFragment.getChildFragments().collect { it.getXpath() }
420 assert allChildXpaths.size() == expectedChildXpaths.size()
421 assert allChildXpaths.containsAll(expectedChildXpaths)
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 | expectedChildXpaths | listNodeFragmentId
433 'existing grandchild of list node' | '/parent-203' | '/parent-203/child-204[@key="X"]' | ['/parent-203/child-204/grandchild[@key="2"]'] | ['/parent-203/child-203', '/parent-203/child-204[@key="X"]'] | LIST_DATA_NODE_PARENT203_FRAGMENT_ID
434 'existing grandchild of list node with two new nodes' | '/parent-203' | '/parent-203/child-204[@key="X"]' | ['/parent-203/child-204/grandchild[@key="2"]' , '/parent-203/child-204/grandchild[@key="3"]'] | ['/parent-203/child-203', '/parent-203/child-204[@key="X"]'] | LIST_DATA_NODE_PARENT203_FRAGMENT_ID
435 'existing grandchild with compound list node' | '/parent-205' | '/parent-205/child-205[@key="X"]' | ['/parent-205/child-205/grand-child-206[@key="Y" and @key2="Z"]'] | ['/parent-205/child-205', '/parent-205/child-205[@key="X"]'] | LIST_DATA_NODE_PARENT205_FRAGMENT_ID
436 'two existing list node with a new node' | '/parent-205' | '/parent-205/child-205[@key="X"]' | ['/parent-205/child-205/grandchild[@key="A"]'] | ['/parent-205/child-205', '/parent-205/child-205[@key="X"]'] | LIST_DATA_NODE_PARENT205_FRAGMENT_ID
439 @Sql([CLEAR_DATA, SET_DATA])
440 def 'Replace list-node content of #scenario with grandchildren.'() {
441 given: 'list node data fragment as a collection of data nodes'
442 def listNodeCollection = buildDataNodeCollection(listNodeXpaths)
443 when: 'list-node elements replaced within the existing parent node'
444 objectUnderTest.replaceListDataNodes(DATASPACE_NAME, ANCHOR_NAME3, parentXpath, listNodeCollection)
445 then: 'child list elements are updated as expected with non-list elements remaining as is'
446 def parentFragment = fragmentRepository.getById(listNodeFragmentID)
447 def allChildXpaths = parentFragment.getChildFragments().collect { it.getXpath() }
448 assert allChildXpaths.size() == expectedChildXpaths.size()
449 assert allChildXpaths.containsAll(expectedChildXpaths)
450 and: 'grandchild list elements are updated as expected'
451 def allGrandChildXpaths = parentFragment.getChildFragments().collect {
452 it.getChildFragments().collect {
454 allGrandChildXpaths.removeIf(list -> list.isEmpty())
455 assert allGrandChildXpaths.size() == expectedGrandChildXpaths.size()
456 assert allGrandChildXpaths.containsAll(expectedGrandChildXpaths)
457 where: 'following parameters were used'
458 scenario | listNodeXpaths | parentXpath | listNodeFragmentID || expectedChildXpaths | expectedGrandChildXpaths
459 'existing list node 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"]'] | []
460 'non existing list node 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"]'] | []
464 @Sql([CLEAR_DATA, SET_DATA])
465 def 'Replace list-node fragment error scenario: #scenario.'() {
466 given: 'list node data fragment as a collection of data nodes'
467 def listNodeCollection = buildDataNodeCollection(listNodeXpaths)
468 when: 'list-node elements were replaced under existing parent node'
469 objectUnderTest.replaceListDataNodes(DATASPACE_NAME, ANCHOR_NAME3, parentNodeXpath, listNodeCollection)
470 then: 'a #expectedException is thrown'
471 thrown(expectedException)
472 where: 'following parameters were used'
473 scenario | parentNodeXpath | listNodeXpaths || expectedException
474 'parent node does not exist' | '/unknown' | ['irrelevant'] || DataNodeNotFoundException
477 @Sql([CLEAR_DATA, SET_DATA])
478 def 'Delete list-node content of #scenario.'() {
479 given: 'list node data fragments are present in database'
480 when: 'list-node elements deleted within the existing parent node'
481 objectUnderTest.deleteListDataNodes(DATASPACE_NAME, ANCHOR_NAME3, listNodeXpaths)
482 then: 'child list elements are removed as expected, non-list element remains as is'
483 def parentFragment = fragmentRepository.getById(listNodeFragmentID)
484 def allChildXpaths = parentFragment.getChildFragments().collect { it.getXpath() }
485 assert allChildXpaths.size() == expectedChildXpaths.size()
486 assert allChildXpaths.containsAll(expectedChildXpaths)
487 where: 'following parameters were used'
488 scenario | listNodeXpaths | listNodeFragmentID || expectedChildXpaths
489 'existing list-node with key' | '/parent-203/child-204[@key="A"]' | LIST_DATA_NODE_PARENT203_FRAGMENT_ID || ['/parent-203/child-203', '/parent-203/child-204[@key="X"]']
490 'existing list-node with key' | '/parent-203/child-204[@key="X"]' | LIST_DATA_NODE_PARENT203_FRAGMENT_ID || ['/parent-203/child-203', '/parent-203/child-204[@key="A"]']
491 'existing grand-child list node with keys' | '/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"]']
492 'existing list-node with combined keys' | '/parent-202/child-205[@key="A" and @key2="B"]' | LIST_DATA_NODE_PARENT202_FRAGMENT_ID || ['/parent-202/child-206[@key="A"]']
493 'existing node with list node variants to delete' | '/parent-203/child-204' | LIST_DATA_NODE_PARENT203_FRAGMENT_ID || ['/parent-203/child-203']
494 'existing grandchild list-node' | '/parent-200/child-202/grand-child-202[@key="D"]' | LIST_DATA_NODE_CHILD202_FRAGMENT_ID || []
497 @Sql([CLEAR_DATA, SET_DATA])
498 def 'Delete list-node fragment error scenario: #scenario.'() {
499 given: 'list node data fragments are present in database'
500 when: 'list-node elements are deleted under existing parent node'
501 objectUnderTest.deleteListDataNodes(DATASPACE_NAME, ANCHOR_NAME3, listNodeXpaths)
502 then: 'a #expectedException is thrown'
503 thrown(expectedException)
504 where: 'following parameters were used'
505 scenario | listNodeXpaths || expectedException
506 'list parent node does not exist' | '/unknown/unknown' || DataNodeNotFoundException
507 'list child nodes do not exist' | '/parent-200/unknown' || DataNodeNotFoundException
508 'list child nodes with key does not exist' | '/parent-200/unknown[@key="C"]' || DataNodeNotFoundException
509 'list grandchild nodes parent does not exist' | '/parent-200/unknown/unknown' || DataNodeNotFoundException
510 'non-existing parent with existing list-node' | '/unknown/child-204' || DataNodeNotFoundException
511 'non-existing parent with existing list-node & key' | '/unknown/child-204[@key="A"]' || DataNodeNotFoundException
512 'valid with non existing key' | '/parent-200/child-202/grand-child-202[@key="A"]' || DataNodeNotFoundException
513 'child list node without key' | '/parent-200/child-204/grand-child-204' || DataNodeNotFoundException
514 'valid list node with invalid key' | '/parent-203/child-204[@key="C"]' || DataNodeNotFoundException
518 static Collection<DataNode> buildDataNodeCollection(xpaths) {
519 return xpaths.collect { new DataNodeBuilder().withXpath(it).build() }
522 static DataNode buildDataNode(xpath, leaves, childDataNodes) {
523 return new DataNodeBuilder().withXpath(xpath).withLeaves(leaves).withChildDataNodes(childDataNodes).build()
526 static Map<String, Object> getLeavesMap(FragmentEntity fragmentEntity) {
527 return GSON.fromJson(fragmentEntity.getAttributes(), Map<String, Object>.class)
530 def static assertLeavesMaps(actualLeavesMap, expectedLeavesMap) {
531 expectedLeavesMap.forEach((key, value) -> {
532 def actualValue = actualLeavesMap[key]
533 if (value instanceof Collection<?> && actualValue instanceof Collection<?>) {
534 assert value.size() == actualValue.size()
535 assert value.containsAll(actualValue)
537 assert value == actualValue
543 def static treeToFlatMapByXpath(Map<String, DataNode> flatMap, DataNode dataNodeTree) {
544 flatMap.put(dataNodeTree.getXpath(), dataNodeTree)
545 dataNodeTree.getChildDataNodes()
546 .forEach(childDataNode -> treeToFlatMapByXpath(flatMap, childDataNode))