+ def 'Update existing list with #scenario.'() {
+ given: 'a parent having a list of data nodes containing: #originalKeys (ech list element has a child too)'
+ def parentXpath = '/parent-3'
+ if (originalKeys.size() > 0) {
+ def originalListEntriesAsDataNodes = createChildListAllHavingAttributeValue(parentXpath, 'original value', originalKeys, true)
+ objectUnderTest.addListElements(DATASPACE_NAME, ANCHOR_NAME1, parentXpath, originalListEntriesAsDataNodes)
+ }
+ and: 'each original list element has one child'
+ def originalParentFragment = fragmentRepository.getById(PARENT_3_FRAGMENT_ID)
+ originalParentFragment.childFragments.each {assert it.childFragments.size() == 1 }
+ when: 'it is updated with #scenario'
+ def replacementListEntriesAsDataNodes = createChildListAllHavingAttributeValue(parentXpath, 'new value', replacementKeys, false)
+ objectUnderTest.replaceListContent(DATASPACE_NAME, ANCHOR_NAME1, parentXpath, replacementListEntriesAsDataNodes)
+ then: 'the result list ONLY contains the expected replacement elements'
+ def parentFragment = fragmentRepository.getById(PARENT_3_FRAGMENT_ID)
+ def allChildXpaths = parentFragment.childFragments.collect { it.xpath }
+ def expectedListEntriesAfterUpdateAsXpaths = keysToXpaths(parentXpath, replacementKeys)
+ assert allChildXpaths.size() == replacementKeys.size()
+ assert allChildXpaths.containsAll(expectedListEntriesAfterUpdateAsXpaths)
+ and: 'all the list elements have the new values'
+ assert parentFragment.childFragments.stream().allMatch(childFragment -> childFragment.attributes.contains('new value'))
+ and: 'there are no more grandchildren as none of the replacement list entries had a child'
+ parentFragment.childFragments.each {assert it.childFragments.size() == 0 }
+ where: 'the following replacement lists are applied'
+ scenario | originalKeys | replacementKeys
+ 'one existing entry only' | [] | ['NEW']
+ 'multiple new entries' | [] | ['NEW1', 'NEW2']
+ 'one new entry only (existing entries are deleted)' | ['A', 'B'] | ['NEW1', 'NEW2']
+ 'one existing on new entry' | ['A', 'B'] | ['A', 'NEW']
+ 'one existing entry only' | ['A', 'B'] | ['A']
+ }
+
+ @Sql([CLEAR_DATA, SET_DATA])
+ def 'Replacing existing list element with attributes and (grand)child.'() {
+ given: 'a parent with list elements A and B with attribute and grandchild tagged as "org"'
+ def parentXpath = '/parent-3'
+ def originalListEntriesAsDataNodes = createChildListAllHavingAttributeValue(parentXpath, 'org', ['A','B'], true)
+ objectUnderTest.addListElements(DATASPACE_NAME, ANCHOR_NAME1, parentXpath, originalListEntriesAsDataNodes)
+ when: 'A is replaced with an entry with attribute and grandchild tagged tagged as "new" (B is not in replacement list)'
+ def replacementListEntriesAsDataNodes = createChildListAllHavingAttributeValue(parentXpath, 'new', ['A'], true)
+ objectUnderTest.replaceListContent(DATASPACE_NAME, ANCHOR_NAME1, parentXpath, replacementListEntriesAsDataNodes)
+ then: 'The updated fragment has a child-list with ONLY element "A"'
+ def parentFragment = fragmentRepository.getById(PARENT_3_FRAGMENT_ID)
+ parentFragment.childFragments.size() == 1
+ def childListElementA = parentFragment.childFragments[0]
+ childListElementA.xpath == "/parent-3/child-list[@key='A']"
+ and: 'element "A" has an attribute with the "new" (tag) value'
+ childListElementA.attributes == '{"attr1": "new"}'
+ and: 'element "A" has a only one (grand)child'
+ childListElementA.childFragments.size() == 1
+ and: 'the grandchild is the new grandchild (tag)'
+ def grandChild = childListElementA.childFragments[0]
+ grandChild.xpath == "/parent-3/child-list[@key='A']/new-grand-child"
+ and: 'the grandchild has an attribute with the "new" (tag) value'
+ grandChild.attributes == '{"attr1": "new"}'
+ }
+
+ @Sql([CLEAR_DATA, SET_DATA])
+ def 'Replace list element for a parent (parent-1) with existing one (non-list) child'() {
+ when: 'a list element is added under the parent'
+ def replacementListEntriesAsDataNodes = createChildListAllHavingAttributeValue(XPATH_DATA_NODE_WITH_DESCENDANTS, 'new', ['A','B'], false)
+ objectUnderTest.replaceListContent(DATASPACE_NAME, ANCHOR_NAME1, XPATH_DATA_NODE_WITH_DESCENDANTS, replacementListEntriesAsDataNodes)
+ then: 'the parent will have 3 children after the replacement'
+ def parentFragment = fragmentRepository.getById(ID_DATA_NODE_WITH_DESCENDANTS)
+ parentFragment.childFragments.size() == 3
+ def xpaths = parentFragment.childFragments.collect {it.xpath}
+ and: 'one of the children is the original child fragment'
+ xpaths.contains('/parent-1/child-1')
+ and: 'it has the two new list elements'
+ xpaths.containsAll("/parent-1/child-list[@key='A']", "/parent-1/child-list[@key='B']")
+ }
+
+ @Sql([CLEAR_DATA, SET_DATA])
+ def 'Replace list content using unknown parent'() {
+ given: 'list element as a collection of data nodes'
+ def listElementCollection = toDataNodes(['irrelevant'])
+ when: 'attempt to replace list elements under unknown parent node'
+ objectUnderTest.replaceListContent(DATASPACE_NAME, ANCHOR_NAME3, '/unknown', listElementCollection)
+ then: 'a datanode not found exception is thrown'
+ thrown(DataNodeNotFoundException)
+ }
+
+ @Sql([CLEAR_DATA, SET_DATA])
+ def 'Replace list content with empty collection is not supported'() {
+ when: 'attempt to replace list elements with empty collection'
+ objectUnderTest.replaceListContent(DATASPACE_NAME, ANCHOR_NAME3, '/parent-203', [])
+ then: 'a CPS admin exception is thrown'
+ def thrown = thrown(CpsAdminException)
+ assert thrown.message == 'Invalid list replacement'
+ }
+
+ @Sql([CLEAR_DATA, SET_DATA])
+ def 'Delete list scenario: #scenario.'() {
+ when: 'deleting list is executed for: #scenario.'
+ objectUnderTest.deleteListDataNode(DATASPACE_NAME, ANCHOR_NAME3, targetXpaths)
+ then: 'only the expected children remain'
+ def parentFragment = fragmentRepository.getById(parentFragmentId)
+ def remainingChildXpaths = parentFragment.childFragments.collect { it.xpath }
+ assert remainingChildXpaths.size() == expectedRemainingChildXpaths.size()
+ assert remainingChildXpaths.containsAll(expectedRemainingChildXpaths)