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