2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2022 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.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
38 import javax.validation.ConstraintViolationException
39 import java.util.stream.Collectors
41 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
42 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
44 class CpsDataPersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase {
47 CpsDataPersistenceService objectUnderTest
49 static final JsonObjectMapper jsonObjectMapper = new JsonObjectMapper(new ObjectMapper())
51 static final String SET_DATA = '/data/fragment.sql'
52 static final long ID_DATA_NODE_WITH_DESCENDANTS = 4001
53 static final String XPATH_DATA_NODE_WITH_DESCENDANTS = '/parent-1'
54 static final String XPATH_DATA_NODE_WITH_LEAVES = '/parent-100'
55 static final long UPDATE_DATA_NODE_FRAGMENT_ID = 4202L
56 static final long UPDATE_DATA_NODE_SUB_FRAGMENT_ID = 4203L
57 static final long LIST_DATA_NODE_PARENT201_FRAGMENT_ID = 4206L
58 static final long LIST_DATA_NODE_PARENT203_FRAGMENT_ID = 4214L
59 static final long LIST_DATA_NODE_PARENT204_FRAGMENT_ID = 4219L
60 static final long LIST_DATA_NODE_PARENT205_FRAGMENT_ID = 4221L
61 static final long LIST_DATA_NODE_CHILD202_FRAGMENT_ID = 4204L
62 static final long LIST_DATA_NODE_PARENT202_FRAGMENT_ID = 4211L
64 static final DataNode newDataNode = new DataNodeBuilder().build()
65 static DataNode existingDataNode
66 static DataNode existingChildDataNode
68 def expectedLeavesByXpathMap = [
69 '/parent-100' : ['parent-leaf': 'parent-leaf value'],
70 '/parent-100/child-001' : ['first-child-leaf': 'first-child-leaf value'],
71 '/parent-100/child-002' : ['second-child-leaf': 'second-child-leaf value'],
72 '/parent-100/child-002/grand-child': ['grand-child-leaf': 'grand-child-leaf value']
76 existingDataNode = createDataNodeTree(XPATH_DATA_NODE_WITH_DESCENDANTS)
77 existingChildDataNode = createDataNodeTree('/parent-1/child-1')
80 @Sql([CLEAR_DATA, SET_DATA])
81 def 'StoreDataNode with descendants.'() {
82 when: 'a fragment with descendants is stored'
83 def parentXpath = "/parent-new"
84 def childXpath = "/parent-new/child-new"
85 def grandChildXpath = "/parent-new/child-new/grandchild-new"
86 objectUnderTest.storeDataNode(DATASPACE_NAME, ANCHOR_NAME1,
87 createDataNodeTree(parentXpath, childXpath, grandChildXpath))
88 then: 'it can be retrieved by its xpath'
89 def parentFragment = getFragmentByXpath(DATASPACE_NAME, ANCHOR_NAME1, parentXpath)
90 and: 'it contains the children'
91 parentFragment.childFragments.size() == 1
92 def childFragment = parentFragment.childFragments[0]
93 childFragment.xpath == childXpath
94 and: "and its children's children"
95 childFragment.childFragments.size() == 1
96 def grandchildFragment = childFragment.childFragments[0]
97 grandchildFragment.xpath == grandChildXpath
100 @Sql([CLEAR_DATA, SET_DATA])
101 def 'Store data node for multiple anchors using the same schema.'() {
102 def xpath = "/parent-new"
103 given: 'a fragment is stored for an anchor'
104 objectUnderTest.storeDataNode(DATASPACE_NAME, ANCHOR_NAME1, createDataNodeTree(xpath))
105 when: 'another fragment is stored for an other anchor, using the same schema set'
106 objectUnderTest.storeDataNode(DATASPACE_NAME, ANCHOR_NAME3, createDataNodeTree(xpath))
107 then: 'both fragments can be retrieved by their xpath'
108 def fragment1 = getFragmentByXpath(DATASPACE_NAME, ANCHOR_NAME1, xpath)
109 fragment1.anchor.name == ANCHOR_NAME1
110 fragment1.xpath == xpath
111 def fragment2 = getFragmentByXpath(DATASPACE_NAME, ANCHOR_NAME3, xpath)
112 fragment2.anchor.name == ANCHOR_NAME3
113 fragment2.xpath == xpath
116 @Sql([CLEAR_DATA, SET_DATA])
117 def 'Store datanode error scenario: #scenario.'() {
118 when: 'attempt to store a data node with #scenario'
119 objectUnderTest.storeDataNode(dataspaceName, anchorName, dataNode)
120 then: 'a #expectedException is thrown'
121 thrown(expectedException)
122 where: 'the following data is used'
123 scenario | dataspaceName | anchorName | dataNode || expectedException
124 'dataspace does not exist' | 'unknown' | 'not-relevant' | newDataNode || DataspaceNotFoundException
125 'schema set does not exist' | DATASPACE_NAME | 'unknown' | newDataNode || AnchorNotFoundException
126 'anchor already exists' | DATASPACE_NAME | ANCHOR_NAME1 | newDataNode || ConstraintViolationException
127 'datanode already exists' | DATASPACE_NAME | ANCHOR_NAME1 | existingDataNode || AlreadyDefinedException
130 @Sql([CLEAR_DATA, SET_DATA])
131 def 'Add a child to a Fragment that already has a child.'() {
132 given: ' a new child node'
133 def newChild = createDataNodeTree('xpath for new child')
134 when: 'the child is added to an existing parent with 1 child'
135 objectUnderTest.addChildDataNode(DATASPACE_NAME, ANCHOR_NAME1, XPATH_DATA_NODE_WITH_DESCENDANTS, newChild)
136 then: 'the parent is now has to 2 children'
137 def expectedExistingChildPath = '/parent-1/child-1'
138 def parentFragment = fragmentRepository.findById(ID_DATA_NODE_WITH_DESCENDANTS).orElseThrow()
139 parentFragment.getChildFragments().size() == 2
140 and: 'it still has the old child'
141 parentFragment.getChildFragments().find({ it.xpath == expectedExistingChildPath })
142 and: 'it has the new child'
143 parentFragment.getChildFragments().find({ it.xpath == newChild.xpath })
146 @Sql([CLEAR_DATA, SET_DATA])
147 def 'Add child error scenario: #scenario.'() {
148 when: 'attempt to add a child data node with #scenario'
149 objectUnderTest.addChildDataNode(DATASPACE_NAME, ANCHOR_NAME1, parentXpath, dataNode)
150 then: 'a #expectedException is thrown'
151 thrown(expectedException)
152 where: 'the following data is used'
153 scenario | parentXpath | dataNode || expectedException
154 'parent does not exist' | 'unknown' | newDataNode || DataNodeNotFoundException
155 'already existing child' | XPATH_DATA_NODE_WITH_DESCENDANTS | existingChildDataNode || AlreadyDefinedException
158 @Sql([CLEAR_DATA, SET_DATA])
159 def 'Add multiple list elements including an element with a child datanode.'() {
160 given: 'two new data nodes for an existing list'
161 def listElementXpaths = ['/parent-201/child-204[@key="B"]', '/parent-201/child-204[@key="C"]']
162 def listElements = toDataNodes(listElementXpaths)
163 and: 'a child node for one of the new data nodes'
164 def childDataNode = buildDataNode('/parent-201/child-204[@key="C"]/grand-child-204[@key2="Z"]', [leave:'value'], [])
165 listElements[0].childDataNodes = [childDataNode]
166 when: 'the data nodes (list elements) are added to existing parent node'
167 objectUnderTest.addListElements(DATASPACE_NAME, ANCHOR_NAME3, '/parent-201', listElements)
168 then: 'new entries successfully persisted, parent node now contains 5 children (2 new + 3 existing before)'
169 def parentFragment = fragmentRepository.getById(LIST_DATA_NODE_PARENT201_FRAGMENT_ID)
170 def allChildXpaths = parentFragment.getChildFragments().collect { it.getXpath() }
171 assert allChildXpaths.size() == 5
172 assert allChildXpaths.containsAll(listElementXpaths)
173 and: 'the child node of the new list entry is also present'
174 def dataspaceEntity = dataspaceRepository.getByName(DATASPACE_NAME)
175 def anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, ANCHOR_NAME3)
176 def listElementChild = fragmentRepository.findByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity, childDataNode.xpath)
177 assert listElementChild.isPresent()
180 @Sql([CLEAR_DATA, SET_DATA])
181 def 'Add list element error scenario: #scenario.'() {
182 given: 'list element as a collection of data nodes'
183 def listElementCollection = toDataNodes(listElementXpaths)
184 when: 'attempt to add list elements to parent node'
185 objectUnderTest.addListElements(DATASPACE_NAME, ANCHOR_NAME3, parentNodeXpath, listElementCollection)
186 then: 'a #expectedException is thrown'
187 thrown(expectedException)
188 where: 'following parameters were used'
189 scenario | parentNodeXpath | listElementXpaths || expectedException
190 'parent node does not exist' | '/unknown' | ['irrelevant'] || DataNodeNotFoundException
191 'already existing fragment' | '/parent-201' | ['/parent-201/child-204[@key="A"]'] || AlreadyDefinedException
195 static def createDataNodeTree(String... xpaths) {
196 def dataNodeBuilder = new DataNodeBuilder().withXpath(xpaths[0])
197 if (xpaths.length > 1) {
198 def xPathsDescendant = Arrays.copyOfRange(xpaths, 1, xpaths.length)
199 def childDataNode = createDataNodeTree(xPathsDescendant)
200 dataNodeBuilder.withChildDataNodes(ImmutableSet.of(childDataNode))
202 dataNodeBuilder.build()
205 def getFragmentByXpath(dataspaceName, anchorName, xpath) {
206 def dataspace = dataspaceRepository.getByName(dataspaceName)
207 def anchor = anchorRepository.getByDataspaceAndName(dataspace, anchorName)
208 return fragmentRepository.findByDataspaceAndAnchorAndXpath(dataspace, anchor, xpath).orElseThrow()
211 @Sql([CLEAR_DATA, SET_DATA])
212 def 'Get data node by xpath without descendants.'() {
213 when: 'data node is requested'
214 def result = objectUnderTest.getDataNode(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES,
215 inputXPath, OMIT_DESCENDANTS)
216 then: 'data node is returned with no descendants'
217 assert result.getXpath() == XPATH_DATA_NODE_WITH_LEAVES
218 and: 'expected leaves'
219 assert result.getChildDataNodes().size() == 0
220 assertLeavesMaps(result.getLeaves(), expectedLeavesByXpathMap[XPATH_DATA_NODE_WITH_LEAVES])
221 where: 'the following data is used'
222 scenario | inputXPath
223 'some xpath' | '/parent-100'
228 @Sql([CLEAR_DATA, SET_DATA])
229 def 'Get data node by xpath with all descendants.'() {
230 when: 'data node is requested with all descendants'
231 def result = objectUnderTest.getDataNode(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES,
232 inputXPath, INCLUDE_ALL_DESCENDANTS)
233 def mappedResult = treeToFlatMapByXpath(new HashMap<>(), result)
234 then: 'data node is returned with all the descendants populated'
235 assert mappedResult.size() == 4
236 assert result.getChildDataNodes().size() == 2
237 assert mappedResult.get('/parent-100/child-001').getChildDataNodes().size() == 0
238 assert mappedResult.get('/parent-100/child-002').getChildDataNodes().size() == 1
239 and: 'extracted leaves maps are matching expected'
240 mappedResult.forEach(
241 (xPath, dataNode) -> assertLeavesMaps(dataNode.getLeaves(), expectedLeavesByXpathMap[xPath]))
242 where: 'the following data is used'
243 scenario | inputXPath
244 'some xpath' | '/parent-100'
249 @Sql([CLEAR_DATA, SET_DATA])
250 def 'Get data node error scenario: #scenario.'() {
251 when: 'attempt to get data node with #scenario'
252 objectUnderTest.getDataNode(dataspaceName, anchorName, xpath, OMIT_DESCENDANTS)
253 then: 'a #expectedException is thrown'
254 thrown(expectedException)
255 where: 'the following data is used'
256 scenario | dataspaceName | anchorName | xpath || expectedException
257 'non-existing dataspace' | 'NO DATASPACE' | 'not relevant' | 'not relevant' || DataspaceNotFoundException
258 'non-existing anchor' | DATASPACE_NAME | 'NO ANCHOR' | 'not relevant' || AnchorNotFoundException
259 'non-existing xpath' | DATASPACE_NAME | ANCHOR_FOR_DATA_NODES_WITH_LEAVES | 'NO XPATH' || DataNodeNotFoundException
262 @Sql([CLEAR_DATA, SET_DATA])
263 def 'Update data node leaves.'() {
264 when: 'update is performed for leaves'
265 objectUnderTest.updateDataLeaves(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES,
266 "/parent-200/child-201", ['leaf-value': 'new'])
267 then: 'leaves are updated for selected data node'
268 def updatedFragment = fragmentRepository.getById(UPDATE_DATA_NODE_FRAGMENT_ID)
269 def updatedLeaves = getLeavesMap(updatedFragment)
270 assert updatedLeaves.size() == 1
271 assert updatedLeaves.'leaf-value' == 'new'
272 and: 'existing child entry remains as is'
273 def childFragment = updatedFragment.getChildFragments().iterator().next()
274 def childLeaves = getLeavesMap(childFragment)
275 assert childFragment.getId() == UPDATE_DATA_NODE_SUB_FRAGMENT_ID
276 assert childLeaves.'leaf-value' == 'original'
279 @Sql([CLEAR_DATA, SET_DATA])
280 def 'Update data leaves error scenario: #scenario.'() {
281 when: 'attempt to update data node for #scenario'
282 objectUnderTest.updateDataLeaves(dataspaceName, anchorName, xpath, ['leaf-name': 'leaf-value'])
283 then: 'a #expectedException is thrown'
284 thrown(expectedException)
285 where: 'the following data is used'
286 scenario | dataspaceName | anchorName | xpath || expectedException
287 'non-existing dataspace' | 'NO DATASPACE' | 'not relevant' | 'not relevant' || DataspaceNotFoundException
288 'non-existing anchor' | DATASPACE_NAME | 'NO ANCHOR' | 'not relevant' || AnchorNotFoundException
289 'non-existing xpath' | DATASPACE_NAME | ANCHOR_FOR_DATA_NODES_WITH_LEAVES | 'NON-EXISTING XPATH' || DataNodeNotFoundException
292 @Sql([CLEAR_DATA, SET_DATA])
293 def 'Replace data node tree with descendants removal.'() {
294 given: 'data node object with leaves updated, no children'
295 def submittedDataNode = buildDataNode("/parent-200/child-201", ['leaf-value': 'new'], [])
296 when: 'replace data node tree is performed'
297 objectUnderTest.replaceDataNodeTree(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, submittedDataNode)
298 then: 'leaves have been updated for selected data node'
299 def updatedFragment = fragmentRepository.getById(UPDATE_DATA_NODE_FRAGMENT_ID)
300 def updatedLeaves = getLeavesMap(updatedFragment)
301 assert updatedLeaves.size() == 1
302 assert updatedLeaves.'leaf-value' == 'new'
303 and: 'updated entry has no children'
304 updatedFragment.getChildFragments().isEmpty()
305 and: 'previously attached child entry is removed from database'
306 fragmentRepository.findById(UPDATE_DATA_NODE_SUB_FRAGMENT_ID).isEmpty()
309 @Sql([CLEAR_DATA, SET_DATA])
310 def 'Replace data node tree with descendants.'() {
311 given: 'data node object with leaves updated, having child with old content'
312 def submittedDataNode = buildDataNode("/parent-200/child-201", ['leaf-value': 'new'], [
313 buildDataNode("/parent-200/child-201/grand-child", ['leaf-value': 'original'], [])
315 when: 'update is performed including descendants'
316 objectUnderTest.replaceDataNodeTree(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, submittedDataNode)
317 then: 'leaves have been updated for selected data node'
318 def updatedFragment = fragmentRepository.getById(UPDATE_DATA_NODE_FRAGMENT_ID)
319 def updatedLeaves = getLeavesMap(updatedFragment)
320 assert updatedLeaves.size() == 1
321 assert updatedLeaves.'leaf-value' == 'new'
322 and: 'existing child entry is not updated as content is same'
323 def childFragment = updatedFragment.getChildFragments().iterator().next()
324 childFragment.getXpath() == '/parent-200/child-201/grand-child'
325 def childLeaves = getLeavesMap(childFragment)
326 assert childLeaves.'leaf-value' == 'original'
329 @Sql([CLEAR_DATA, SET_DATA])
330 def 'Replace data node tree with same descendants but changed leaf value.'() {
331 given: 'data node object with leaves updated, having child with old content'
332 def submittedDataNode = buildDataNode("/parent-200/child-201", ['leaf-value': 'new'], [
333 buildDataNode("/parent-200/child-201/grand-child", ['leaf-value': 'new'], [])
335 when: 'update is performed including descendants'
336 objectUnderTest.replaceDataNodeTree(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, submittedDataNode)
337 then: 'leaves have been updated for selected data node'
338 def updatedFragment = fragmentRepository.getById(UPDATE_DATA_NODE_FRAGMENT_ID)
339 def updatedLeaves = getLeavesMap(updatedFragment)
340 assert updatedLeaves.size() == 1
341 assert updatedLeaves.'leaf-value' == 'new'
342 and: 'existing child entry is updated with the new content'
343 def childFragment = updatedFragment.getChildFragments().iterator().next()
344 childFragment.getXpath() == '/parent-200/child-201/grand-child'
345 def childLeaves = getLeavesMap(childFragment)
346 assert childLeaves.'leaf-value' == 'new'
349 @Sql([CLEAR_DATA, SET_DATA])
350 def 'Replace data node tree with different descendants xpath'() {
351 given: 'data node object with leaves updated, having child with old content'
352 def submittedDataNode = buildDataNode("/parent-200/child-201", ['leaf-value': 'new'], [
353 buildDataNode("/parent-200/child-201/grand-child-new", ['leaf-value': 'new'], [])
355 when: 'update is performed including descendants'
356 objectUnderTest.replaceDataNodeTree(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, submittedDataNode)
357 then: 'leaves have been updated for selected data node'
358 def updatedFragment = fragmentRepository.getById(UPDATE_DATA_NODE_FRAGMENT_ID)
359 def updatedLeaves = getLeavesMap(updatedFragment)
360 assert updatedLeaves.size() == 1
361 assert updatedLeaves.'leaf-value' == 'new'
362 and: 'previously attached child entry is removed from database'
363 fragmentRepository.findById(UPDATE_DATA_NODE_SUB_FRAGMENT_ID).isEmpty()
364 and: 'new child entry is persisted'
365 def childFragment = updatedFragment.getChildFragments().iterator().next()
366 childFragment.getXpath() == '/parent-200/child-201/grand-child-new'
367 def childLeaves = getLeavesMap(childFragment)
368 assert childLeaves.'leaf-value' == 'new'
371 @Sql([CLEAR_DATA, SET_DATA])
372 def 'Replace data node tree error scenario: #scenario.'() {
373 given: 'data node object'
374 def submittedDataNode = buildDataNode(xpath, ['leaf-name': 'leaf-value'], [])
375 when: 'attempt to update data node for #scenario'
376 objectUnderTest.replaceDataNodeTree(dataspaceName, anchorName, submittedDataNode)
377 then: 'a #expectedException is thrown'
378 thrown(expectedException)
379 where: 'the following data is used'
380 scenario | dataspaceName | anchorName | xpath || expectedException
381 'non-existing dataspace' | 'NO DATASPACE' | 'not relevant' | 'not relevant' || DataspaceNotFoundException
382 'non-existing anchor' | DATASPACE_NAME | 'NO ANCHOR' | 'not relevant' || AnchorNotFoundException
383 'non-existing xpath' | DATASPACE_NAME | ANCHOR_FOR_DATA_NODES_WITH_LEAVES | 'NON-EXISTING XPATH' || DataNodeNotFoundException
386 @Sql([CLEAR_DATA, SET_DATA])
387 def 'Replace list content of #scenario.'() {
388 given: 'list element as a collection of data nodes'
389 def listElementCollection = toDataNodes(listElementXpaths)
390 when: 'list elements are replaced within the existing parent node'
391 objectUnderTest.replaceListContent(DATASPACE_NAME, ANCHOR_NAME3, parentXpath, listElementCollection)
392 then: 'list elements are updated as expected, non-list element remains as is'
393 def parentFragment = fragmentRepository.getById(listElementFragmentID)
394 def allChildXpaths = parentFragment.getChildFragments().collect { it.getXpath() }
395 assert allChildXpaths.size() == expectedChildXpaths.size()
396 assert allChildXpaths.containsAll(expectedChildXpaths)
397 where: 'following parameters were used'
398 scenario | listElementXpaths | parentXpath | listElementFragmentID || expectedChildXpaths
399 '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"]']
400 '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"]']
401 '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"]']
402 '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"]']
403 '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"]']
404 '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"]']
405 '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"]']
406 '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"]']
409 @Sql([CLEAR_DATA, SET_DATA])
410 def 'Replace list content that has #scenario'() {
411 given: 'list element with child list element as a collection of data nodes'
412 def grandChildDataNodes = toDataNodes(grandChildXpaths)
413 def listElementCollection = new DataNodeBuilder().withXpath(childXpath).withChildDataNodes(grandChildDataNodes).build()
414 when: 'list elements replaced within the existing parent node'
415 objectUnderTest.replaceListContent(DATASPACE_NAME, ANCHOR_NAME3, parentXpath, [listElementCollection ])
416 then: 'list elements are updated as expected with non-list elements remaining as is'
417 def parentFragment = fragmentRepository.getById(listElementFragmentId)
418 def allChildXpaths = parentFragment.getChildFragments().collect { it.getXpath() }
419 assert allChildXpaths.size() == expectedRemainingChildXpaths.size()
420 assert allChildXpaths.containsAll(expectedRemainingChildXpaths)
421 and: 'grandchild list elements are updated as expected'
422 def allGrandChildXpaths = parentFragment.getChildFragments().collect(){
423 it.getChildFragments().collect(){
425 allGrandChildXpaths.removeIf(list -> list.isEmpty())
426 def grandChildXpathsToList = allGrandChildXpaths.stream().flatMap(List::stream).collect(Collectors.toList())
427 def expectedGrandChildXpaths = grandChildXpaths
428 assert grandChildXpathsToList.size() == expectedGrandChildXpaths.size()
429 assert grandChildXpathsToList.containsAll(expectedGrandChildXpaths)
430 where: 'the following parameters are used'
431 scenario | parentXpath | childXpath | grandChildXpaths | listElementFragmentId || expectedRemainingChildXpaths
432 '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"]']
433 '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"]']
434 '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"]']
437 @Sql([CLEAR_DATA, SET_DATA])
438 def 'Replace list content of #scenario with grandchildren.'() {
439 given: 'list element as a collection of data nodes'
440 def listElementCollection = toDataNodes(listElementXpaths)
441 when: 'list elements are replaced within the existing parent node'
442 objectUnderTest.replaceListContent(DATASPACE_NAME, ANCHOR_NAME3, parentXpath, listElementCollection)
443 then: 'child list elements are updated as expected with non-list elements remaining as is'
444 def parentFragment = fragmentRepository.getById(listElementFragmentID)
445 def allChildXpaths = parentFragment.getChildFragments().collect { it.getXpath() }
446 assert allChildXpaths.size() == expectedChildXpaths.size()
447 assert allChildXpaths.containsAll(expectedChildXpaths)
448 and: 'grandchild list elements are updated as expected'
449 def allGrandChildXpaths = parentFragment.getChildFragments().collect {
450 it.getChildFragments().collect {
452 allGrandChildXpaths.removeIf(list -> list.isEmpty())
453 assert allGrandChildXpaths.size() == expectedGrandChildXpaths.size()
454 assert allGrandChildXpaths.containsAll(expectedGrandChildXpaths)
455 where: 'following parameters were used'
456 scenario | listElementXpaths | parentXpath | listElementFragmentID || expectedChildXpaths | expectedGrandChildXpaths
457 '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"]'] | []
458 '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"]'] | []
462 @Sql([CLEAR_DATA, SET_DATA])
463 def 'Replace content error scenario: #scenario.'() {
464 given: 'list element as a collection of data nodes'
465 def listElementCollection = toDataNodes(listElementXpaths)
466 when: 'list elements were replaced under existing parent node'
467 objectUnderTest.replaceListContent(DATASPACE_NAME, ANCHOR_NAME3, parentNodeXpath, listElementCollection)
468 then: 'a #expectedException is thrown'
469 thrown(expectedException)
470 where: 'following parameters were used'
471 scenario | parentNodeXpath | listElementXpaths || expectedException
472 'parent node does not exist' | '/unknown' | ['irrelevant'] || DataNodeNotFoundException
475 @Sql([CLEAR_DATA, SET_DATA])
476 def 'Delete list scenario: #scenario.'() {
477 when: 'deleting list is executed for: #scenario.'
478 objectUnderTest.deleteListDataNode(DATASPACE_NAME, ANCHOR_NAME3, targetXpaths)
479 then: 'only the expected children remain'
480 def parentFragment = fragmentRepository.getById(parentFragmentId)
481 def remainingChildXpaths = parentFragment.getChildFragments().collect { it.getXpath() }
482 assert remainingChildXpaths.size() == expectedRemainingChildXpaths.size()
483 assert remainingChildXpaths.containsAll(expectedRemainingChildXpaths)
484 where: 'following parameters were used'
485 scenario | targetXpaths | parentFragmentId || expectedRemainingChildXpaths
486 '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"]']
487 '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"]']
488 'whole list' | '/parent-203/child-204' | LIST_DATA_NODE_PARENT203_FRAGMENT_ID || ['/parent-203/child-203']
489 '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"]']
492 @Sql([CLEAR_DATA, SET_DATA])
493 def 'Delete list error scenario: #scenario.'() {
494 when: 'attempting to delete scenario: #scenario.'
495 objectUnderTest.deleteListDataNode(DATASPACE_NAME, ANCHOR_NAME3, targetXpaths)
496 then: 'a DataNodeNotFoundException is thrown'
497 thrown(DataNodeNotFoundException)
498 where: 'following parameters were used'
499 scenario | targetXpaths
500 'whole list, parent node does not exist' | '/unknown/some-child'
501 'list element, parent node does not exist' | '/unknown/child-204[@key="A"]'
502 'whole list does not exist' | '/parent-200/unknown'
503 'list element, list does not exist' | '/parent-200/unknown[@key="C"]'
504 'list element, element does not exist' | '/parent-203/child-204[@key="C"]'
505 'valid datanode but not a list' | '/parent-200/child-202'
508 @Sql([CLEAR_DATA, SET_DATA])
509 def 'Confirm deletion of #scenario.'() {
510 given: 'a valid data node'
513 when: 'data nodes are deleted'
514 objectUnderTest.deleteDataNode(DATASPACE_NAME, ANCHOR_NAME3, xpathForDeletion)
515 then: 'verify data nodes are removed'
517 dataNode = objectUnderTest.getDataNode(DATASPACE_NAME, ANCHOR_NAME3, getDataNodesXpaths, INCLUDE_ALL_DESCENDANTS)
518 dataNodeXpath = dataNode.getXpath()
519 assert dataNodeXpath == expectedXpaths
520 } catch (DataNodeNotFoundException) {
521 assert dataNodeXpath == expectedXpaths
523 where: 'following parameters were used'
524 scenario | xpathForDeletion | getDataNodesXpaths || expectedXpaths
525 'child of target' | '/parent-206/child-206' | '/parent-206/child-206' || null
526 'child data node, parent still exists' | '/parent-206/child-206' | '/parent-206' || '/parent-206'
527 'list element' | '/parent-206/child-206/grand-child-206[@key="A"]' | '/parent-206/child-206/grand-child-206[@key="A"]' || null
528 '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"]'
531 @Sql([CLEAR_DATA, SET_DATA])
532 def 'Delete data node with #scenario.'() {
533 when: 'data node is deleted'
534 objectUnderTest.deleteDataNode(DATASPACE_NAME, ANCHOR_NAME3, datanodeXpath)
535 then: 'a #expectedException is thrown'
536 thrown(DataNodeNotFoundException)
537 where: 'the following parameters were used'
538 scenario | datanodeXpath
539 'valid data node, non existent child node' | '/parent-203/child-non-existent'
540 'invalid list element' | '/parent-206/child-206/grand-child-206@key="A"]'
543 static Collection<DataNode> toDataNodes(xpaths) {
544 return xpaths.collect { new DataNodeBuilder().withXpath(it).build() }
547 static DataNode buildDataNode(xpath, leaves, childDataNodes) {
548 return new DataNodeBuilder().withXpath(xpath).withLeaves(leaves).withChildDataNodes(childDataNodes).build()
551 static Map<String, Object> getLeavesMap(FragmentEntity fragmentEntity) {
552 return jsonObjectMapper.convertJsonString(fragmentEntity.getAttributes(), Map<String, Object>.class)
555 def static assertLeavesMaps(actualLeavesMap, expectedLeavesMap) {
556 expectedLeavesMap.forEach((key, value) -> {
557 def actualValue = actualLeavesMap[key]
558 if (value instanceof Collection<?> && actualValue instanceof Collection<?>) {
559 assert value.size() == actualValue.size()
560 assert value.containsAll(actualValue)
562 assert value == actualValue
568 def static treeToFlatMapByXpath(Map<String, DataNode> flatMap, DataNode dataNodeTree) {
569 flatMap.put(dataNodeTree.getXpath(), dataNodeTree)
570 dataNodeTree.getChildDataNodes()
571 .forEach(childDataNode -> treeToFlatMapByXpath(flatMap, childDataNode))