- def 'Replace list content of #scenario.'() {
- given: 'list element as a collection of data nodes'
- def listElementCollection = toDataNodes(listElementXpaths)
- when: 'list elements are replaced within the existing parent node'
- objectUnderTest.replaceListContent(DATASPACE_NAME, ANCHOR_NAME3, parentXpath, listElementCollection)
- then: 'list elements are updated as expected, non-list element remains as is'
- def parentFragment = fragmentRepository.getById(listElementFragmentID)
- def allChildXpaths = parentFragment.getChildFragments().collect { it.getXpath() }
- assert allChildXpaths.size() == expectedChildXpaths.size()
- assert allChildXpaths.containsAll(expectedChildXpaths)
- where: 'following parameters were used'
- scenario | listElementXpaths | parentXpath | listElementFragmentID || expectedChildXpaths
- 'existing list element with non existing key' | ['/parent-201/child-204[@key="B"]'] | '/parent-201' | LIST_DATA_NODE_PARENT201_FRAGMENT_ID || ['/parent-201/child-203', '/parent-201/child-204[@key="B"]']
- 'non existing list element with non existing key' | ['/parent-201/child-205[@key="1"]'] | '/parent-201' | LIST_DATA_NODE_PARENT201_FRAGMENT_ID || ['/parent-201/child-203', '/parent-201/child-204[@key="A"]', '/parent-201/child-204[@key="X"]', '/parent-201/child-205[@key="1"]']
- 'list element with 1 existing key' | ['/parent-201/child-204[@key="X"]'] | '/parent-201' | LIST_DATA_NODE_PARENT201_FRAGMENT_ID || ['/parent-201/child-203', '/parent-201/child-204[@key="X"]']
- 'list element with combined keys' | ['/parent-202/child-205[@key="A"]'] | '/parent-202' | LIST_DATA_NODE_PARENT202_FRAGMENT_ID || ['/parent-202/child-206[@key="A"]', '/parent-202/child-205[@key="A"]']
- 'grandchild list element' | ['/parent-200/child-202/grand-child-202[@key="E"]'] | '/parent-200/child-202' | LIST_DATA_NODE_CHILD202_FRAGMENT_ID || ['/parent-200/child-202/grand-child-202[@key="E"]']
- 'list element with two list elements' | ['/parent-201/child-204[@key="new X"]', '/parent-201/child-204[@key="Y"]'] | '/parent-201' | LIST_DATA_NODE_PARENT201_FRAGMENT_ID || ['/parent-201/child-203', '/parent-201/child-204[@key="new X"]', '/parent-201/child-204[@key="Y"]']
- 'list element with compounded list element' | ['/parent-202/child-205[@key="A" and @key2="B"]'] | '/parent-202' | LIST_DATA_NODE_PARENT202_FRAGMENT_ID || ['/parent-202/child-206[@key="A"]', '/parent-202/child-205[@key="A" and @key2="B"]']
- 'list element with list element with parent with key value' | ['/parent-204[@key="L"]/child-210[@key="N"]'] | '/parent-204[@key="L"]' | LIST_DATA_NODE_PARENT204_FRAGMENT_ID || ['/parent-204[@key="L"]/child-210[@key="N"]']
+ 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']