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