diff options
author | Ruslan Kashapov <ruslan.kashapov@pantheon.tech> | 2021-05-14 12:04:34 +0300 |
---|---|---|
committer | Rishi Chail <rishi.chail@est.tech> | 2021-05-24 15:36:55 +0000 |
commit | b0e78acb6aefdcb5866955802099e6091b2a6952 (patch) | |
tree | 8f06ec09b2d9fba00dc8dfe4fbbe639330417c15 /cps-ri/src | |
parent | 940d8e4a950d8ce8075a712d9dc3852de828e5b5 (diff) |
Replace list-node content (part 1): CPS Service and persistence layers
Issue-ID: CPS-362
Change-Id: I669c9fc6ef67c1992fe95e17a765f0c616b00f7e
Signed-off-by: Ruslan Kashapov <ruslan.kashapov@pantheon.tech>
Diffstat (limited to 'cps-ri/src')
-rw-r--r-- | cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java | 25 | ||||
-rwxr-xr-x | cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceServiceSpec.groovy | 30 |
2 files changed, 55 insertions, 0 deletions
diff --git a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java index ae399a1381..10440924e9 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java @@ -35,6 +35,7 @@ import java.util.Map; import java.util.Set; import java.util.regex.Pattern; import java.util.stream.Collectors; +import javax.transaction.Transactional; import org.onap.cps.spi.CpsDataPersistenceService; import org.onap.cps.spi.FetchDescendantsOption; import org.onap.cps.spi.entities.AnchorEntity; @@ -253,6 +254,30 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService fragmentRepository.save(fragmentEntity); } + @Override + @Transactional + public void replaceListDataNodes(final String dataspaceName, final String anchorName, final String parentNodeXpath, + final Collection<DataNode> dataNodes) { + final var parentEntity = getFragmentByXpath(dataspaceName, anchorName, parentNodeXpath); + final var firstChildNodeXpath = dataNodes.iterator().next().getXpath(); + final var listNodeXpath = firstChildNodeXpath.substring(0, firstChildNodeXpath.lastIndexOf("[")); + removeListNodeDescendants(parentEntity, listNodeXpath); + final Set<FragmentEntity> childFragmentEntities = dataNodes.stream().map( + dataNode -> convertToFragmentWithAllDescendants( + parentEntity.getDataspace(), parentEntity.getAnchor(), dataNode) + ).collect(Collectors.toUnmodifiableSet()); + parentEntity.getChildFragments().addAll(childFragmentEntities); + fragmentRepository.save(parentEntity); + } + + private void removeListNodeDescendants(final FragmentEntity parentFragmentEntity, final String listNodeXpath) { + final String listNodeXpathPrefix = listNodeXpath + "["; + if (parentFragmentEntity.getChildFragments() + .removeIf(fragment -> fragment.getXpath().startsWith(listNodeXpathPrefix))) { + fragmentRepository.save(parentFragmentEntity); + } + } + private void removeExistingDescendants(final FragmentEntity fragmentEntity) { fragmentEntity.setChildFragments(Collections.emptySet()); fragmentRepository.save(fragmentEntity); diff --git a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceServiceSpec.groovy b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceServiceSpec.groovy index 0f0b1b4302..1f2ea6d364 100755 --- a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceServiceSpec.groovy +++ b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceServiceSpec.groovy @@ -328,6 +328,36 @@ class CpsDataPersistenceServiceSpec extends CpsPersistenceSpecBase { 'non-existing xpath' | DATASPACE_NAME | ANCHOR_FOR_DATA_NODES_WITH_LEAVES | 'NON-EXISTING XPATH' || DataNodeNotFoundException } + @Sql([CLEAR_DATA, SET_DATA]) + def 'Replace list-node content of #scenario.'() { + given: 'list node data fragment as a collection of data nodes' + def listNodeCollection = buildDataNodeCollection(listNodeXpaths) + when: 'list-node elements replaced within the existing parent node' + objectUnderTest.replaceListDataNodes(DATASPACE_NAME, ANCHOR_NAME3, '/parent-201', listNodeCollection) + then: 'child list elements are updated as expected, non-list element remains as is' + def parentFragment = fragmentRepository.getOne(LIST_DATA_NODE_PARENT_FRAGMENT_ID) + def allChildXpaths = parentFragment.getChildFragments().collect { it.getXpath() } + assert allChildXpaths.size() == expectedChildXpaths.size() + assert allChildXpaths.containsAll(expectedChildXpaths) + where: 'following parameters were used' + scenario | listNodeXpaths || expectedChildXpaths + 'existing list-node' | ['/parent-201/child-204[@key="B"]'] || ['/parent-201/child-203', '/parent-201/child-204[@key="B"]'] + 'non-existing list-node' | ['/parent-201/child-205[@key="1"]'] || ['/parent-201/child-203', '/parent-201/child-204[@key="A"]', '/parent-201/child-204[@key="X"]', '/parent-201/child-205[@key="1"]'] + } + + @Sql([CLEAR_DATA, SET_DATA]) + def 'Replace list-node fragment error scenario: #scenario.'() { + given: 'list node data fragment as a collection of data nodes' + def listNodeCollection = buildDataNodeCollection(listNodeXpaths) + when: 'list-node elements were replaced under existing parent node' + objectUnderTest.replaceListDataNodes(DATASPACE_NAME, ANCHOR_NAME3, parentNodeXpath, listNodeCollection) + then: 'a #expectedException is thrown' + thrown(expectedException) + where: 'following parameters were used' + scenario | parentNodeXpath | listNodeXpaths || expectedException + 'parent node does not exist' | '/unknown' | ['irrelevant'] || DataNodeNotFoundException + } + static Collection<DataNode> buildDataNodeCollection(xpaths) { return xpaths.collect { new DataNodeBuilder().withXpath(it).build() } } |