2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2022 Nordix Foundation
4 * Modifications Copyright (C) 2021 Pantheon.tech
5 * Modifications Copyright (C) 2021-2022 Bell Canada.
6 * 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=========================================================
23 package org.onap.cps.spi.impl
25 import com.fasterxml.jackson.databind.ObjectMapper
26 import com.google.common.collect.ImmutableSet
27 import org.onap.cps.cpspath.parser.PathParsingException
28 import org.onap.cps.spi.CpsDataPersistenceService
29 import org.onap.cps.spi.entities.FragmentEntity
30 import org.onap.cps.spi.exceptions.AlreadyDefinedExceptionBatch
31 import org.onap.cps.spi.exceptions.AnchorNotFoundException
32 import org.onap.cps.spi.exceptions.CpsAdminException
33 import org.onap.cps.spi.exceptions.CpsPathException
34 import org.onap.cps.spi.exceptions.DataNodeNotFoundException
35 import org.onap.cps.spi.exceptions.DataspaceNotFoundException
36 import org.onap.cps.spi.model.DataNode
37 import org.onap.cps.spi.model.DataNodeBuilder
38 import org.onap.cps.utils.JsonObjectMapper
39 import org.springframework.beans.factory.annotation.Autowired
40 import org.springframework.test.context.jdbc.Sql
42 import javax.validation.ConstraintViolationException
44 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
45 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
47 class CpsDataPersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase {
50 CpsDataPersistenceService objectUnderTest
52 static JsonObjectMapper jsonObjectMapper = new JsonObjectMapper(new ObjectMapper())
53 static DataNodeBuilder dataNodeBuilder = new DataNodeBuilder()
55 static final String SET_DATA = '/data/fragment.sql'
56 static int DATASPACE_1001_ID = 1001L
57 static int ANCHOR_3003_ID = 3003L
58 static long ID_DATA_NODE_WITH_DESCENDANTS = 4001
59 static String XPATH_DATA_NODE_WITH_DESCENDANTS = '/parent-1'
60 static String XPATH_DATA_NODE_WITH_LEAVES = '/parent-207'
61 static long DATA_NODE_202_FRAGMENT_ID = 4202L
62 static long CHILD_OF_DATA_NODE_202_FRAGMENT_ID = 4203L
63 static long LIST_DATA_NODE_PARENT201_FRAGMENT_ID = 4206L
64 static long LIST_DATA_NODE_PARENT203_FRAGMENT_ID = 4214L
65 static long LIST_DATA_NODE_PARENT202_FRAGMENT_ID = 4211L
66 static long PARENT_3_FRAGMENT_ID = 4003L
68 static Collection<DataNode> newDataNodes = [new DataNodeBuilder().build()]
69 static Collection<DataNode> existingDataNodes = [createDataNodeTree(XPATH_DATA_NODE_WITH_DESCENDANTS)]
70 static Collection<DataNode> existingChildDataNodes = [createDataNodeTree('/parent-1/child-1')]
72 def static deleteTestParentXPath = '/parent-200'
73 def static deleteTestChildXpath = "${deleteTestParentXPath}/child-with-slash[@key='a/b']"
74 def static deleteTestGrandChildXPath = "${deleteTestChildXpath}/grandChild"
76 def expectedLeavesByXpathMap = [
77 '/parent-207' : ['parent-leaf': 'parent-leaf value'],
78 '/parent-207/child-001' : ['first-child-leaf': 'first-child-leaf value'],
79 '/parent-207/child-002' : ['second-child-leaf': 'second-child-leaf value'],
80 '/parent-207/child-002/grand-child': ['grand-child-leaf': 'grand-child-leaf value']
83 @Sql([CLEAR_DATA, SET_DATA])
84 def 'Get existing datanode with descendants.'() {
85 when: 'the node is retrieved by its xpath'
86 def dataNode = objectUnderTest.getDataNode(DATASPACE_NAME, ANCHOR_NAME1, '/parent-1', INCLUDE_ALL_DESCENDANTS)
87 then: 'the path and prefix are populated correctly'
88 assert dataNode.xpath == '/parent-1'
89 and: 'dataNode has no prefix (to be addressed by CPS-1301'
90 assert dataNode.moduleNamePrefix == null
91 and: 'the child node has the correct path'
92 assert dataNode.childDataNodes[0].xpath == '/parent-1/child-1'
95 @Sql([CLEAR_DATA, SET_DATA])
96 def 'Storing and Retrieving a new DataNodes with descendants.'() {
97 when: 'a fragment with descendants is stored'
98 def parentXpath = '/parent-new'
99 def childXpath = '/parent-new/child-new'
100 def grandChildXpath = '/parent-new/child-new/grandchild-new'
101 def dataNodes = [createDataNodeTree(parentXpath, childXpath, grandChildXpath)]
102 objectUnderTest.storeDataNodes(DATASPACE_NAME, ANCHOR_NAME1, dataNodes)
103 then: 'it can be retrieved by its xpath'
104 def dataNode = objectUnderTest.getDataNode(DATASPACE_NAME, ANCHOR_NAME1, parentXpath, INCLUDE_ALL_DESCENDANTS)
105 assert dataNode.xpath == parentXpath
106 and: 'it has the correct child'
107 assert dataNode.childDataNodes.size() == 1
108 def childDataNode = dataNode.childDataNodes[0]
109 assert childDataNode.xpath == childXpath
110 and: 'and its grandchild'
111 assert childDataNode.childDataNodes.size() == 1
112 def grandChildDataNode = childDataNode.childDataNodes[0]
113 assert grandChildDataNode.xpath == grandChildXpath
116 @Sql([CLEAR_DATA, SET_DATA])
117 def 'Store data node for multiple anchors using the same schema.'() {
118 def xpath = '/parent-new'
119 given: 'a fragment is stored for an anchor'
120 objectUnderTest.storeDataNodes(DATASPACE_NAME, ANCHOR_NAME1, [createDataNodeTree(xpath)])
121 when: 'another fragment is stored for an other anchor, using the same schema set'
122 objectUnderTest.storeDataNodes(DATASPACE_NAME, ANCHOR_NAME3, [createDataNodeTree(xpath)])
123 then: 'both fragments can be retrieved by their xpath'
124 def fragment1 = getFragmentByXpath(DATASPACE_NAME, ANCHOR_NAME1, xpath)
125 fragment1.anchor.name == ANCHOR_NAME1
126 fragment1.xpath == xpath
127 def fragment2 = getFragmentByXpath(DATASPACE_NAME, ANCHOR_NAME3, xpath)
128 fragment2.anchor.name == ANCHOR_NAME3
129 fragment2.xpath == xpath
132 @Sql([CLEAR_DATA, SET_DATA])
133 def 'Store datanodes error scenario: #scenario.'() {
134 when: 'attempt to store a data node with #scenario'
135 objectUnderTest.storeDataNodes(dataspaceName, anchorName, dataNodes)
136 then: 'a #expectedException is thrown'
137 thrown(expectedException)
138 where: 'the following data is used'
139 scenario | dataspaceName | anchorName | dataNodes || expectedException
140 'dataspace does not exist' | 'unknown' | 'not-relevant' | newDataNodes || DataspaceNotFoundException
141 'schema set does not exist' | DATASPACE_NAME | 'unknown' | newDataNodes || AnchorNotFoundException
142 'anchor already exists' | DATASPACE_NAME | ANCHOR_NAME1 | newDataNodes || ConstraintViolationException
143 'datanode already exists' | DATASPACE_NAME | ANCHOR_NAME1 | existingDataNodes || AlreadyDefinedExceptionBatch
146 @Sql([CLEAR_DATA, SET_DATA])
147 def 'Add children to a Fragment that already has a child.'() {
148 given: 'collection of new child data nodes'
149 def newChild1 = createDataNodeTree('/parent-1/child-2')
150 def newChild2 = createDataNodeTree('/parent-1/child-3')
151 def newChildrenCollection = [newChild1, newChild2]
152 when: 'the child is added to an existing parent with 1 child'
153 objectUnderTest.addChildDataNodes(DATASPACE_NAME, ANCHOR_NAME1, XPATH_DATA_NODE_WITH_DESCENDANTS, newChildrenCollection)
154 then: 'the parent is now has to 3 children'
155 def expectedExistingChildPath = '/parent-1/child-1'
156 def parentFragment = fragmentRepository.findById(ID_DATA_NODE_WITH_DESCENDANTS).orElseThrow()
157 parentFragment.childFragments.size() == 3
158 and: 'it still has the old child'
159 parentFragment.childFragments.find({ it.xpath == expectedExistingChildPath })
160 and: 'it has the new children'
161 parentFragment.childFragments.find({ it.xpath == newChildrenCollection[0].xpath })
162 parentFragment.childFragments.find({ it.xpath == newChildrenCollection[1].xpath })
165 @Sql([CLEAR_DATA, SET_DATA])
166 def 'Add child error scenario: #scenario.'() {
167 when: 'attempt to add a child data node with #scenario'
168 objectUnderTest.addChildDataNodes(DATASPACE_NAME, ANCHOR_NAME1, parentXpath, dataNodes)
169 then: 'a #expectedException is thrown'
170 thrown(expectedException)
171 where: 'the following data is used'
172 scenario | parentXpath | dataNodes || expectedException
173 'parent does not exist' | '/unknown' | newDataNodes || DataNodeNotFoundException
174 'already existing child' | XPATH_DATA_NODE_WITH_DESCENDANTS | existingChildDataNodes || AlreadyDefinedExceptionBatch
177 @Sql([CLEAR_DATA, SET_DATA])
178 def 'Add collection of multiple new list elements including an element with a child datanode.'() {
179 given: 'two new child list elements for an existing parent'
180 def listElementXpaths = ['/parent-201/child-204[@key="NEW1"]', '/parent-201/child-204[@key="NEW2"]']
181 def listElements = toDataNodes(listElementXpaths)
182 and: 'a (grand)child data node for one of the new list elements'
183 def grandChild = buildDataNode('/parent-201/child-204[@key="NEW1"]/grand-child-204[@key2="NEW1-CHILD"]', [leave:'value'], [])
184 listElements[0].childDataNodes = [grandChild]
185 when: 'the new data node (list elements) are added to an existing parent node'
186 objectUnderTest.addMultipleLists(DATASPACE_NAME, ANCHOR_NAME3, '/parent-201', [listElements])
187 then: 'new entries are successfully persisted, parent node now contains 5 children (2 new + 3 existing before)'
188 def parentFragment = fragmentRepository.getById(LIST_DATA_NODE_PARENT201_FRAGMENT_ID)
189 def allChildXpaths = parentFragment.childFragments.collect { it.xpath }
190 assert allChildXpaths.size() == 5
191 assert allChildXpaths.containsAll(listElementXpaths)
192 and: 'the (grand)child node of the new list entry is also present'
193 def dataspaceEntity = dataspaceRepository.getByName(DATASPACE_NAME)
194 def anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, ANCHOR_NAME3)
195 def grandChildFragmentEntity = fragmentRepository.findByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity, grandChild.xpath)
196 assert grandChildFragmentEntity.isPresent()
199 @Sql([CLEAR_DATA, SET_DATA])
200 def 'Add multiple list with a mix of existing and new elements'() {
201 given: 'two new child list elements for an existing parent'
202 def existingDataNode = dataNodeBuilder.withXpath('/parent-207/child-001').withLeaves(['id': '001']).build()
203 def newDataNode1 = dataNodeBuilder.withXpath('/parent-207/child-new1').withLeaves(['id': 'new1']).build()
204 def newDataNode2 = dataNodeBuilder.withXpath('/parent-200/child-new2').withLeaves(['id': 'new2']).build()
205 def dataNodeList1 = [existingDataNode, newDataNode1]
206 def dataNodeList2 = [newDataNode2]
207 when: 'duplicate data node is requested to be added'
208 objectUnderTest.addMultipleLists(DATASPACE_NAME, ANCHOR_HAVING_SINGLE_TOP_LEVEL_FRAGMENT, '/', [dataNodeList1, dataNodeList2])
209 then: 'already defined batch exception is thrown'
210 def thrown = thrown(AlreadyDefinedExceptionBatch)
211 and: 'it only contains the xpath(s) of the duplicated elements'
212 assert thrown.alreadyDefinedXpaths.size() == 1
213 assert thrown.alreadyDefinedXpaths.contains('/parent-207/child-001')
214 and: 'it does NOT contains the xpaths of the new element that were not combined with existing elements'
215 assert !thrown.alreadyDefinedXpaths.contains('/parent-207/child-new1')
216 assert !thrown.alreadyDefinedXpaths.contains('/parent-207/child-new1')
217 and: 'the new entity is inserted correctly'
218 def dataspaceEntity = dataspaceRepository.getByName(DATASPACE_NAME)
219 def anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, ANCHOR_HAVING_SINGLE_TOP_LEVEL_FRAGMENT)
220 fragmentRepository.findByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity, '/parent-200/child-new2').isPresent()
223 @Sql([CLEAR_DATA, SET_DATA])
224 def 'Add list element error scenario: #scenario.'() {
225 given: 'list element as a collection of data nodes'
226 def listElements = toDataNodes(listElementXpaths)
227 when: 'attempt to add list elements to parent node'
228 objectUnderTest.addListElements(DATASPACE_NAME, ANCHOR_NAME3, parentNodeXpath, listElements)
229 then: 'a #expectedException is thrown'
230 thrown(expectedException)
231 where: 'following parameters were used'
232 scenario | parentNodeXpath | listElementXpaths || expectedException
233 'parent node does not exist' | '/unknown' | ['irrelevant'] || DataNodeNotFoundException
234 'data fragment already exists' | '/parent-201' | ["/parent-201/child-204[@key='A']"] || AlreadyDefinedExceptionBatch
237 @Sql([CLEAR_DATA, SET_DATA])
238 def 'Get data node by xpath without descendants.'() {
239 when: 'data node is requested'
240 def result = objectUnderTest.getDataNode(DATASPACE_NAME, ANCHOR_HAVING_SINGLE_TOP_LEVEL_FRAGMENT,
241 inputXPath, OMIT_DESCENDANTS)
242 then: 'data node is returned with no descendants'
243 assert result.xpath == XPATH_DATA_NODE_WITH_LEAVES
244 and: 'expected leaves'
245 assert result.childDataNodes.size() == 0
246 assertLeavesMaps(result.leaves, expectedLeavesByXpathMap[XPATH_DATA_NODE_WITH_LEAVES])
247 where: 'the following data is used'
248 scenario | inputXPath
249 'some xpath' | '/parent-207'
254 @Sql([CLEAR_DATA, SET_DATA])
255 def 'Cps Path query with syntax error throws a CPS Path Exception.'() {
256 when: 'trying to execute a query with a syntax (parsing) error'
257 objectUnderTest.getDataNode(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, 'invalid-cps-path/child' , OMIT_DESCENDANTS)
258 then: 'exception is thrown'
259 def exceptionThrown = thrown(CpsPathException)
260 assert exceptionThrown.getDetails().contains('failed to parse at line 1 due to extraneous input \'invalid-cps-path\' expecting \'/\'')
263 @Sql([CLEAR_DATA, SET_DATA])
264 def 'Get data node by xpath with all descendants.'() {
265 when: 'data node is requested with all descendants'
266 def result = objectUnderTest.getDataNode(DATASPACE_NAME, ANCHOR_HAVING_SINGLE_TOP_LEVEL_FRAGMENT,
267 inputXPath, INCLUDE_ALL_DESCENDANTS)
268 def mappedResult = treeToFlatMapByXpath(new HashMap<>(), result)
269 then: 'data node is returned with all the descendants populated'
270 assert mappedResult.size() == 4
271 assert result.childDataNodes.size() == 2
272 assert mappedResult.get('/parent-207/child-001').childDataNodes.size() == 0
273 assert mappedResult.get('/parent-207/child-002').childDataNodes.size() == 1
274 and: 'extracted leaves maps are matching expected'
275 mappedResult.forEach(
276 (xPath, dataNode) -> assertLeavesMaps(dataNode.leaves, expectedLeavesByXpathMap[xPath]))
277 where: 'the following data is used'
278 scenario | inputXPath
279 'some xpath' | '/parent-207'
284 @Sql([CLEAR_DATA, SET_DATA])
285 def 'Get data node error scenario: #scenario.'() {
286 when: 'attempt to get data node with #scenario'
287 objectUnderTest.getDataNode(dataspaceName, anchorName, xpath, OMIT_DESCENDANTS)
288 then: 'a #expectedException is thrown'
289 thrown(expectedException)
290 where: 'the following data is used'
291 scenario | dataspaceName | anchorName | xpath || expectedException
292 'non-existing dataspace' | 'NO DATASPACE' | 'not relevant' | '/not relevant' || DataspaceNotFoundException
293 'non-existing anchor' | DATASPACE_NAME | 'NO ANCHOR' | '/not relevant' || AnchorNotFoundException
294 'non-existing xpath' | DATASPACE_NAME | ANCHOR_FOR_DATA_NODES_WITH_LEAVES | '/NO-XPATH' || DataNodeNotFoundException
295 'invalid xpath' | DATASPACE_NAME | ANCHOR_FOR_DATA_NODES_WITH_LEAVES | 'INVALID XPATH' || CpsPathException
298 @Sql([CLEAR_DATA, SET_DATA])
299 def 'Update data node leaves.'() {
300 when: 'update is performed for leaves'
301 objectUnderTest.updateDataLeaves(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES,
302 '/parent-200/child-201', ['leaf-value': 'new'])
303 then: 'leaves are updated for selected data node'
304 def updatedFragment = fragmentRepository.getById(DATA_NODE_202_FRAGMENT_ID)
305 def updatedLeaves = getLeavesMap(updatedFragment)
306 assert updatedLeaves.size() == 1
307 assert updatedLeaves.'leaf-value' == 'new'
308 and: 'existing child entry remains as is'
309 def childFragment = updatedFragment.childFragments.iterator().next()
310 def childLeaves = getLeavesMap(childFragment)
311 assert childFragment.id == CHILD_OF_DATA_NODE_202_FRAGMENT_ID
312 assert childLeaves.'leaf-value' == 'original'
315 @Sql([CLEAR_DATA, SET_DATA])
316 def 'Update data leaves error scenario: #scenario.'() {
317 when: 'attempt to update data node for #scenario'
318 objectUnderTest.updateDataLeaves(dataspaceName, anchorName, xpath, ['leaf-name': 'leaf-value'])
319 then: 'a #expectedException is thrown'
320 thrown(expectedException)
321 where: 'the following data is used'
322 scenario | dataspaceName | anchorName | xpath || expectedException
323 'non-existing dataspace' | 'NO DATASPACE' | 'not relevant' | '/not relevant' || DataspaceNotFoundException
324 'non-existing anchor' | DATASPACE_NAME | 'NO ANCHOR' | '/not relevant' || AnchorNotFoundException
325 'non-existing xpath' | DATASPACE_NAME | ANCHOR_FOR_DATA_NODES_WITH_LEAVES | '/NON-EXISTING-XPATH' || DataNodeNotFoundException
328 @Sql([CLEAR_DATA, SET_DATA])
329 def 'Update data node and descendants by removing descendants.'() {
330 given: 'data node object with leaves updated, no children'
331 def submittedDataNode = buildDataNode('/parent-200/child-201', ['leaf-value': 'new'], [])
332 when: 'update data nodes and descendants is performed'
333 objectUnderTest.updateDataNodeAndDescendants(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, submittedDataNode)
334 then: 'leaves have been updated for selected data node'
335 def updatedFragment = fragmentRepository.getById(DATA_NODE_202_FRAGMENT_ID)
336 def updatedLeaves = getLeavesMap(updatedFragment)
337 assert updatedLeaves.size() == 1
338 assert updatedLeaves.'leaf-value' == 'new'
339 and: 'updated entry has no children'
340 updatedFragment.childFragments.isEmpty()
341 and: 'previously attached child entry is removed from database'
342 fragmentRepository.findById(CHILD_OF_DATA_NODE_202_FRAGMENT_ID).isEmpty()
345 @Sql([CLEAR_DATA, SET_DATA])
346 def 'Update data node and descendants with new descendants'() {
347 given: 'data node object with leaves updated, having child with old content'
348 def submittedDataNode = buildDataNode('/parent-200/child-201', ['leaf-value': 'new'], [
349 buildDataNode('/parent-200/child-201/grand-child', ['leaf-value': 'original'], [])
351 when: 'update is performed including descendants'
352 objectUnderTest.updateDataNodeAndDescendants(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, submittedDataNode)
353 then: 'leaves have been updated for selected data node'
354 def updatedFragment = fragmentRepository.getById(DATA_NODE_202_FRAGMENT_ID)
355 def updatedLeaves = getLeavesMap(updatedFragment)
356 assert updatedLeaves.size() == 1
357 assert updatedLeaves.'leaf-value' == 'new'
358 and: 'existing child entry is not updated as content is same'
359 def childFragment = updatedFragment.childFragments.iterator().next()
360 childFragment.xpath == '/parent-200/child-201/grand-child'
361 def childLeaves = getLeavesMap(childFragment)
362 assert childLeaves.'leaf-value' == 'original'
365 @Sql([CLEAR_DATA, SET_DATA])
366 def 'Update data node and descendants with same descendants but changed leaf value.'() {
367 given: 'data node object with leaves updated, having child with old content'
368 def submittedDataNode = buildDataNode('/parent-200/child-201', ['leaf-value': 'new'], [
369 buildDataNode('/parent-200/child-201/grand-child', ['leaf-value': 'new'], [])
371 when: 'update is performed including descendants'
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: 'existing child entry is updated with the new content'
379 def childFragment = updatedFragment.childFragments.iterator().next()
380 childFragment.xpath == '/parent-200/child-201/grand-child'
381 def childLeaves = getLeavesMap(childFragment)
382 assert childLeaves.'leaf-value' == 'new'
385 @Sql([CLEAR_DATA, SET_DATA])
386 def 'Update data node and descendants with different descendants xpath'() {
387 given: 'data node object with leaves updated, having child with old content'
388 def submittedDataNode = buildDataNode('/parent-200/child-201', ['leaf-value': 'new'], [
389 buildDataNode('/parent-200/child-201/grand-child-new', ['leaf-value': 'new'], [])
391 when: 'update is performed including descendants'
392 objectUnderTest.updateDataNodeAndDescendants(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, submittedDataNode)
393 then: 'leaves have been updated for selected data node'
394 def updatedFragment = fragmentRepository.getById(DATA_NODE_202_FRAGMENT_ID)
395 def updatedLeaves = getLeavesMap(updatedFragment)
396 assert updatedLeaves.size() == 1
397 assert updatedLeaves.'leaf-value' == 'new'
398 and: 'previously attached child entry is removed from database'
399 fragmentRepository.findById(CHILD_OF_DATA_NODE_202_FRAGMENT_ID).isEmpty()
400 and: 'new child entry is persisted'
401 def childFragment = updatedFragment.childFragments.iterator().next()
402 childFragment.xpath == '/parent-200/child-201/grand-child-new'
403 def childLeaves = getLeavesMap(childFragment)
404 assert childLeaves.'leaf-value' == 'new'
407 @Sql([CLEAR_DATA, SET_DATA])
408 def 'Update data node and descendants error scenario: #scenario.'() {
409 given: 'data node object'
410 def submittedDataNode = buildDataNode(xpath, ['leaf-name': 'leaf-value'], [])
411 when: 'attempt to update data node for #scenario'
412 objectUnderTest.updateDataNodeAndDescendants(dataspaceName, anchorName, submittedDataNode)
413 then: 'a #expectedException is thrown'
414 thrown(expectedException)
415 where: 'the following data is used'
416 scenario | dataspaceName | anchorName | xpath || expectedException
417 'non-existing dataspace' | 'NO DATASPACE' | 'not relevant' | '/not relevant' || DataspaceNotFoundException
418 'non-existing anchor' | DATASPACE_NAME | 'NO ANCHOR' | '/not relevant' || AnchorNotFoundException
419 'non-existing xpath' | DATASPACE_NAME | ANCHOR_FOR_DATA_NODES_WITH_LEAVES | '/NON-EXISTING-XPATH' || DataNodeNotFoundException
420 'invalid xpath' | DATASPACE_NAME | ANCHOR_FOR_DATA_NODES_WITH_LEAVES | 'INVALID XPATH' || CpsPathException
423 @Sql([CLEAR_DATA, SET_DATA])
424 def 'Update existing list with #scenario.'() {
425 given: 'a parent having a list of data nodes containing: #originalKeys (ech list element has a child too)'
426 def parentXpath = '/parent-3'
427 if (originalKeys.size() > 0) {
428 def originalListEntriesAsDataNodes = createChildListAllHavingAttributeValue(parentXpath, 'original value', originalKeys, true)
429 objectUnderTest.addListElements(DATASPACE_NAME, ANCHOR_NAME1, parentXpath, originalListEntriesAsDataNodes)
431 and: 'each original list element has one child'
432 def originalParentFragment = fragmentRepository.getById(PARENT_3_FRAGMENT_ID)
433 originalParentFragment.childFragments.each {assert it.childFragments.size() == 1 }
434 when: 'it is updated with #scenario'
435 def replacementListEntriesAsDataNodes = createChildListAllHavingAttributeValue(parentXpath, 'new value', replacementKeys, false)
436 objectUnderTest.replaceListContent(DATASPACE_NAME, ANCHOR_NAME1, parentXpath, replacementListEntriesAsDataNodes)
437 then: 'the result list ONLY contains the expected replacement elements'
438 def parentFragment = fragmentRepository.getById(PARENT_3_FRAGMENT_ID)
439 def allChildXpaths = parentFragment.childFragments.collect { it.xpath }
440 def expectedListEntriesAfterUpdateAsXpaths = keysToXpaths(parentXpath, replacementKeys)
441 assert allChildXpaths.size() == replacementKeys.size()
442 assert allChildXpaths.containsAll(expectedListEntriesAfterUpdateAsXpaths)
443 and: 'all the list elements have the new values'
444 assert parentFragment.childFragments.stream().allMatch(childFragment -> childFragment.attributes.contains('new value'))
445 and: 'there are no more grandchildren as none of the replacement list entries had a child'
446 parentFragment.childFragments.each {assert it.childFragments.size() == 0 }
447 where: 'the following replacement lists are applied'
448 scenario | originalKeys | replacementKeys
449 'one existing entry only' | [] | ['NEW']
450 'multiple new entries' | [] | ['NEW1', 'NEW2']
451 'one new entry only (existing entries are deleted)' | ['A', 'B'] | ['NEW1', 'NEW2']
452 'one existing on new entry' | ['A', 'B'] | ['A', 'NEW']
453 'one existing entry only' | ['A', 'B'] | ['A']
456 @Sql([CLEAR_DATA, SET_DATA])
457 def 'Replacing existing list element with attributes and (grand)child.'() {
458 given: 'a parent with list elements A and B with attribute and grandchild tagged as "org"'
459 def parentXpath = '/parent-3'
460 def originalListEntriesAsDataNodes = createChildListAllHavingAttributeValue(parentXpath, 'org', ['A','B'], true)
461 objectUnderTest.addListElements(DATASPACE_NAME, ANCHOR_NAME1, parentXpath, originalListEntriesAsDataNodes)
462 when: 'A is replaced with an entry with attribute and grandchild tagged tagged as "new" (B is not in replacement list)'
463 def replacementListEntriesAsDataNodes = createChildListAllHavingAttributeValue(parentXpath, 'new', ['A'], true)
464 objectUnderTest.replaceListContent(DATASPACE_NAME, ANCHOR_NAME1, parentXpath, replacementListEntriesAsDataNodes)
465 then: 'The updated fragment has a child-list with ONLY element "A"'
466 def parentFragment = fragmentRepository.getById(PARENT_3_FRAGMENT_ID)
467 parentFragment.childFragments.size() == 1
468 def childListElementA = parentFragment.childFragments[0]
469 childListElementA.xpath == "/parent-3/child-list[@key='A']"
470 and: 'element "A" has an attribute with the "new" (tag) value'
471 childListElementA.attributes == '{"attr1": "new"}'
472 and: 'element "A" has a only one (grand)child'
473 childListElementA.childFragments.size() == 1
474 and: 'the grandchild is the new grandchild (tag)'
475 def grandChild = childListElementA.childFragments[0]
476 grandChild.xpath == "/parent-3/child-list[@key='A']/new-grand-child"
477 and: 'the grandchild has an attribute with the "new" (tag) value'
478 grandChild.attributes == '{"attr1": "new"}'
481 @Sql([CLEAR_DATA, SET_DATA])
482 def 'Replace list element for a parent (parent-1) with existing one (non-list) child'() {
483 when: 'a list element is added under the parent'
484 def replacementListEntriesAsDataNodes = createChildListAllHavingAttributeValue(XPATH_DATA_NODE_WITH_DESCENDANTS, 'new', ['A','B'], false)
485 objectUnderTest.replaceListContent(DATASPACE_NAME, ANCHOR_NAME1, XPATH_DATA_NODE_WITH_DESCENDANTS, replacementListEntriesAsDataNodes)
486 then: 'the parent will have 3 children after the replacement'
487 def parentFragment = fragmentRepository.getById(ID_DATA_NODE_WITH_DESCENDANTS)
488 parentFragment.childFragments.size() == 3
489 def xpaths = parentFragment.childFragments.collect {it.xpath}
490 and: 'one of the children is the original child fragment'
491 xpaths.contains('/parent-1/child-1')
492 and: 'it has the two new list elements'
493 xpaths.containsAll("/parent-1/child-list[@key='A']", "/parent-1/child-list[@key='B']")
496 @Sql([CLEAR_DATA, SET_DATA])
497 def 'Replace list content using unknown parent'() {
498 given: 'list element as a collection of data nodes'
499 def listElementCollection = toDataNodes(['irrelevant'])
500 when: 'attempt to replace list elements under unknown parent node'
501 objectUnderTest.replaceListContent(DATASPACE_NAME, ANCHOR_NAME3, '/unknown', listElementCollection)
502 then: 'a datanode not found exception is thrown'
503 thrown(DataNodeNotFoundException)
506 @Sql([CLEAR_DATA, SET_DATA])
507 def 'Replace list content with empty collection is not supported'() {
508 when: 'attempt to replace list elements with empty collection'
509 objectUnderTest.replaceListContent(DATASPACE_NAME, ANCHOR_NAME3, '/parent-203', [])
510 then: 'a CPS admin exception is thrown'
511 def thrown = thrown(CpsAdminException)
512 assert thrown.message == 'Invalid list replacement'
515 @Sql([CLEAR_DATA, SET_DATA])
516 def 'Delete list scenario: #scenario.'() {
517 when: 'deleting list is executed for: #scenario.'
518 objectUnderTest.deleteListDataNode(DATASPACE_NAME, ANCHOR_NAME3, targetXpaths)
519 then: 'only the expected children remain'
520 def parentFragment = fragmentRepository.getById(parentFragmentId)
521 def remainingChildXpaths = parentFragment.childFragments.collect { it.xpath }
522 assert remainingChildXpaths.size() == expectedRemainingChildXpaths.size()
523 assert remainingChildXpaths.containsAll(expectedRemainingChildXpaths)
524 where: 'following parameters were used'
525 scenario | targetXpaths | parentFragmentId || expectedRemainingChildXpaths
526 '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']"]
527 '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']"]
528 'whole list' | '/parent-203/child-204' | LIST_DATA_NODE_PARENT203_FRAGMENT_ID || ['/parent-203/child-203']
529 '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']"]
532 @Sql([CLEAR_DATA, SET_DATA])
533 def 'Delete data nodes with "/"-token in list key value: #scenario. (CPS-1409)'() {
534 given: 'a data nodes with list-element child with "/" in index value (and grandchild)'
535 def grandChild = new DataNodeBuilder().withXpath(deleteTestGrandChildXPath).build()
536 def child = new DataNodeBuilder().withXpath(deleteTestChildXpath).withChildDataNodes([grandChild]).build()
537 objectUnderTest.addChildDataNode(DATASPACE_NAME, ANCHOR_NAME3, deleteTestParentXPath, child)
538 and: 'number of children before delete is stored'
539 def numberOfChildrenBeforeDelete = objectUnderTest.getDataNode(DATASPACE_NAME, ANCHOR_NAME3, pathToParentOfDeletedNode, INCLUDE_ALL_DESCENDANTS).childDataNodes.size()
540 when: 'target node is deleted'
541 objectUnderTest.deleteDataNode(DATASPACE_NAME, ANCHOR_NAME3, deleteTarget)
542 then: 'one child has been deleted'
543 def numberOfChildrenAfterDelete = objectUnderTest.getDataNode(DATASPACE_NAME, ANCHOR_NAME3, pathToParentOfDeletedNode, INCLUDE_ALL_DESCENDANTS).childDataNodes.size()
544 assert numberOfChildrenAfterDelete == numberOfChildrenBeforeDelete - 1
546 scenario | deleteTarget | pathToParentOfDeletedNode
547 'list element with /' | deleteTestChildXpath | deleteTestParentXPath
548 'child of list element' | deleteTestGrandChildXPath | deleteTestChildXpath
551 @Sql([CLEAR_DATA, SET_DATA])
552 def 'Delete list error scenario: #scenario.'() {
553 when: 'attempting to delete scenario: #scenario.'
554 objectUnderTest.deleteListDataNode(DATASPACE_NAME, ANCHOR_NAME3, targetXpaths)
555 then: 'a DataNodeNotFoundException is thrown'
556 thrown(DataNodeNotFoundException)
557 where: 'following parameters were used'
558 scenario | targetXpaths
559 'whole list, parent node does not exist' | '/unknown/some-child'
560 'list element, parent node does not exist' | '/unknown/child-204[@key="A"]'
561 'whole list does not exist' | '/parent-200/unknown'
562 'list element, list does not exist' | '/parent-200/unknown[@key="C"]'
563 'list element, element does not exist' | '/parent-203/child-204[@key="C"]'
564 'valid datanode but not a list' | '/parent-200/child-202'
567 @Sql([CLEAR_DATA, SET_DATA])
568 def 'Delete data node by xpath #scenario.'() {
569 given: 'a valid data node'
571 and: 'data nodes are deleted'
572 objectUnderTest.deleteDataNode(DATASPACE_NAME, ANCHOR_NAME3, xpathForDeletion)
573 when: 'verify data nodes are removed'
574 objectUnderTest.getDataNode(DATASPACE_NAME, ANCHOR_NAME3, xpathForDeletion, INCLUDE_ALL_DESCENDANTS)
576 thrown(DataNodeNotFoundException)
577 and: 'some related object is not deleted'
578 if (xpathSurvivor!=null) {
579 dataNode = objectUnderTest.getDataNode(DATASPACE_NAME, ANCHOR_NAME3, xpathSurvivor, INCLUDE_ALL_DESCENDANTS)
580 assert dataNode.xpath == xpathSurvivor
582 where: 'following parameters were used'
583 scenario | xpathForDeletion || xpathSurvivor
584 'child data node, parent still exists' | '/parent-206/child-206' || '/parent-206'
585 'list element, sibling still exists' | '/parent-206/child-206/grand-child-206[@key="A"]' || "/parent-206/child-206/grand-child-206[@key='X']"
586 'container node' | '/parent-206' || null
587 'container list node' | '/parent-206[@key="A"]' || "/parent-206[@key='B']"
588 'root node with xpath /' | '/' || null
589 'root node with xpath passed as blank' | '' || null
592 @Sql([CLEAR_DATA, SET_DATA])
593 def 'Delete data node error scenario: #scenario.'() {
594 when: 'data node is deleted'
595 objectUnderTest.deleteDataNode(DATASPACE_NAME, ANCHOR_NAME3, datanodeXpath)
596 then: 'a #expectedException is thrown'
597 thrown(expectedException)
598 where: 'the following parameters were used'
599 scenario | datanodeXpath | expectedException
600 'valid data node, non existent child node' | '/parent-203/child-non-existent' | DataNodeNotFoundException
601 'invalid list element' | '/parent-206/child-206/grand-child-206@key="A"]' | PathParsingException
604 @Sql([CLEAR_DATA, SET_DATA])
605 def 'Delete data node for an anchor.'() {
606 given: 'a data-node exists for an anchor'
607 assert fragmentsExistInDB(DATASPACE_1001_ID, ANCHOR_3003_ID)
608 when: 'data nodes are deleted '
609 objectUnderTest.deleteDataNodes(DATASPACE_NAME, ANCHOR_NAME3)
610 then: 'all data-nodes are deleted successfully'
611 assert !fragmentsExistInDB(DATASPACE_1001_ID, ANCHOR_3003_ID)
614 def fragmentsExistInDB(dataSpaceId, anchorId) {
615 !fragmentRepository.findRootsByDataspaceAndAnchor(dataSpaceId, anchorId).isEmpty()
618 static Collection<DataNode> toDataNodes(xpaths) {
619 return xpaths.collect { new DataNodeBuilder().withXpath(it).build() }
623 static DataNode buildDataNode(xpath, leaves, childDataNodes) {
624 return dataNodeBuilder.withXpath(xpath).withLeaves(leaves).withChildDataNodes(childDataNodes).build()
627 static Map<String, Object> getLeavesMap(FragmentEntity fragmentEntity) {
628 return jsonObjectMapper.convertJsonString(fragmentEntity.attributes, Map<String, Object>.class)
631 def static assertLeavesMaps(actualLeavesMap, expectedLeavesMap) {
632 expectedLeavesMap.forEach((key, value) -> {
633 def actualValue = actualLeavesMap[key]
634 if (value instanceof Collection<?> && actualValue instanceof Collection<?>) {
635 assert value.size() == actualValue.size()
636 assert value.containsAll(actualValue)
638 assert value == actualValue
644 def static treeToFlatMapByXpath(Map<String, DataNode> flatMap, DataNode dataNodeTree) {
645 flatMap.put(dataNodeTree.xpath, dataNodeTree)
646 dataNodeTree.childDataNodes
647 .forEach(childDataNode -> treeToFlatMapByXpath(flatMap, childDataNode))
651 def keysToXpaths(parent, Collection keys) {
652 return keys.collect { "${parent}/child-list[@key='${it}']".toString() }
655 def static createDataNodeTree(String... xpaths) {
656 def dataNodeBuilder = new DataNodeBuilder().withXpath(xpaths[0])
657 if (xpaths.length > 1) {
658 def xPathsDescendant = Arrays.copyOfRange(xpaths, 1, xpaths.length)
659 def childDataNode = createDataNodeTree(xPathsDescendant)
660 dataNodeBuilder.withChildDataNodes(ImmutableSet.of(childDataNode))
662 dataNodeBuilder.build()
665 def getFragmentByXpath(dataspaceName, anchorName, xpath) {
666 def dataspace = dataspaceRepository.getByName(dataspaceName)
667 def anchor = anchorRepository.getByDataspaceAndName(dataspace, anchorName)
668 return fragmentRepository.findByDataspaceAndAnchorAndXpath(dataspace, anchor, xpath).orElseThrow()
672 def createChildListAllHavingAttributeValue(parentXpath, tag, Collection keys, boolean addGrandChild) {
673 def listElementAsDataNodes = keysToXpaths(parentXpath, keys).collect {
674 new DataNodeBuilder()
676 .withLeaves([attr1: tag])
680 listElementAsDataNodes.each {it.childDataNodes = [createGrandChild(it.xpath, tag)]}
682 return listElementAsDataNodes
685 def createGrandChild(parentXPath, tag) {
686 new DataNodeBuilder()
687 .withXpath("${parentXPath}/${tag}-grand-child")
688 .withLeaves([attr1: tag])