From ec061d5caba23c76f0cdc183c1f5a37e0c11b6c7 Mon Sep 17 00:00:00 2001 From: "raviteja.karumuri" Date: Fri, 5 May 2023 14:52:07 +0100 Subject: [CPS] RI: Code Refactoring # Replace '[\s\S]' to '.' as it is same as '.' while grouping in regex # Updated the groovy files to add 'assert' and to replace deprecated method # Removed deprecated 'storeDataNode' method. Issue-ID: CPS-1594 Signed-off-by: raviteja.karumuri Change-Id: Iaa5f8b0cc8d66473c993060a8fc5a54e37e373b1 --- .../spi/impl/CpsDataPersistenceServiceImpl.java | 11 +++------- ...CpsDataPersistenceServiceIntegrationSpec.groovy | 24 +++++++++++----------- .../spi/impl/CpsDataPersistenceServiceSpec.groovy | 9 -------- ...sModulePersistenceServiceIntegrationSpec.groovy | 4 ++-- .../onap/cps/spi/CpsDataPersistenceService.java | 12 ----------- 5 files changed, 17 insertions(+), 43 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 78983ee0df..16ec270c22 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 @@ -82,7 +82,7 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService private final JsonObjectMapper jsonObjectMapper; private final SessionManager sessionManager; - private static final String REG_EX_FOR_OPTIONAL_LIST_INDEX = "(\\[@[\\s\\S]+?])?)"; + private static final String REG_EX_FOR_OPTIONAL_LIST_INDEX = "(\\[@.+?])?)"; private static final String QUERY_ACROSS_ANCHORS = null; private static final AnchorEntity ALL_ANCHORS = null; @@ -170,11 +170,6 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService } } - @Override - public void storeDataNode(final String dataspaceName, final String anchorName, final DataNode dataNode) { - storeDataNodes(dataspaceName, anchorName, Collections.singletonList(dataNode)); - } - @Override public void storeDataNodes(final String dataspaceName, final String anchorName, final Collection dataNodes) { @@ -438,8 +433,8 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService final CpsPathQuery cpsPathQuery) { final Set ancestorXpath = new HashSet<>(); final Pattern pattern = - Pattern.compile("([\\s\\S]*/" + Pattern.quote(cpsPathQuery.getAncestorSchemaNodeIdentifier()) - + REG_EX_FOR_OPTIONAL_LIST_INDEX + "/[\\s\\S]*"); + Pattern.compile("(.*/" + Pattern.quote(cpsPathQuery.getAncestorSchemaNodeIdentifier()) + + REG_EX_FOR_OPTIONAL_LIST_INDEX + "/.*"); for (final FragmentEntity fragmentEntity : fragmentEntities) { final Matcher matcher = pattern.matcher(fragmentEntity.getXpath()); if (matcher.matches()) { diff --git a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceServiceIntegrationSpec.groovy b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceServiceIntegrationSpec.groovy index 93d7662014..67ccc805ae 100755 --- a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceServiceIntegrationSpec.groovy +++ b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceServiceIntegrationSpec.groovy @@ -170,7 +170,7 @@ class CpsDataPersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase { when: 'the new data node (list elements) are added to an existing parent node' objectUnderTest.addMultipleLists(DATASPACE_NAME, ANCHOR_NAME3, '/parent-201', [listElements]) then: 'new entries are successfully persisted, parent node now contains 5 children (2 new + 3 existing before)' - def parentFragment = fragmentRepository.getById(LIST_DATA_NODE_PARENT201_FRAGMENT_ID) + def parentFragment = fragmentRepository.getReferenceById(LIST_DATA_NODE_PARENT201_FRAGMENT_ID) def allChildXpaths = parentFragment.childFragments.collect { it.xpath } assert allChildXpaths.size() == 5 assert allChildXpaths.containsAll(listElementXpaths) @@ -325,7 +325,7 @@ class CpsDataPersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase { objectUnderTest.updateDataLeaves(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, '/parent-200/child-201', ['leaf-value': 'new']) then: 'leaves are updated for selected data node' - def updatedFragment = fragmentRepository.getById(DATA_NODE_202_FRAGMENT_ID) + def updatedFragment = fragmentRepository.getReferenceById(DATA_NODE_202_FRAGMENT_ID) def updatedLeaves = getLeavesMap(updatedFragment) assert updatedLeaves.size() == 1 assert updatedLeaves.'leaf-value' == 'new' @@ -356,7 +356,7 @@ class CpsDataPersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase { when: 'update data nodes and descendants is performed' objectUnderTest.updateDataNodesAndDescendants(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, submittedDataNodes) then: 'leaves have been updated for selected data node' - def updatedFragment = fragmentRepository.getById(DATA_NODE_202_FRAGMENT_ID) + def updatedFragment = fragmentRepository.getReferenceById(DATA_NODE_202_FRAGMENT_ID) def updatedLeaves = getLeavesMap(updatedFragment) assert updatedLeaves.size() == 1 assert updatedLeaves.'leaf-value' == 'new' @@ -375,7 +375,7 @@ class CpsDataPersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase { when: 'update is performed including descendants' objectUnderTest.updateDataNodesAndDescendants(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, submittedDataNodes) then: 'leaves have been updated for selected data node' - def updatedFragment = fragmentRepository.getById(DATA_NODE_202_FRAGMENT_ID) + def updatedFragment = fragmentRepository.getReferenceById(DATA_NODE_202_FRAGMENT_ID) def updatedLeaves = getLeavesMap(updatedFragment) assert updatedLeaves.size() == 1 assert updatedLeaves.'leaf-value' == 'new' @@ -395,7 +395,7 @@ class CpsDataPersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase { when: 'update is performed including descendants' objectUnderTest.updateDataNodesAndDescendants(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, submittedDataNodes) then: 'leaves have been updated for selected data node' - def updatedFragment = fragmentRepository.getById(DATA_NODE_202_FRAGMENT_ID) + def updatedFragment = fragmentRepository.getReferenceById(DATA_NODE_202_FRAGMENT_ID) def updatedLeaves = getLeavesMap(updatedFragment) assert updatedLeaves.size() == 1 assert updatedLeaves.'leaf-value' == 'new' @@ -415,7 +415,7 @@ class CpsDataPersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase { when: 'update is performed including descendants' objectUnderTest.updateDataNodesAndDescendants(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, submittedDataNodes) then: 'leaves have been updated for selected data node' - def updatedFragment = fragmentRepository.getById(DATA_NODE_202_FRAGMENT_ID) + def updatedFragment = fragmentRepository.getReferenceById(DATA_NODE_202_FRAGMENT_ID) def updatedLeaves = getLeavesMap(updatedFragment) assert updatedLeaves.size() == 1 assert updatedLeaves.'leaf-value' == 'new' @@ -451,13 +451,13 @@ class CpsDataPersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase { objectUnderTest.addListElements(DATASPACE_NAME, ANCHOR_NAME1, parentXpath, originalListEntriesAsDataNodes) } and: 'each original list element has one child' - def originalParentFragment = fragmentRepository.getById(PARENT_3_FRAGMENT_ID) + def originalParentFragment = fragmentRepository.getReferenceById(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 parentFragment = fragmentRepository.getReferenceById(PARENT_3_FRAGMENT_ID) def allChildXpaths = parentFragment.childFragments.collect { it.xpath } def expectedListEntriesAfterUpdateAsXpaths = keysToXpaths(parentXpath, replacementKeys) assert allChildXpaths.size() == replacementKeys.size() @@ -485,7 +485,7 @@ class CpsDataPersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase { 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) + def parentFragment = fragmentRepository.getReferenceById(PARENT_3_FRAGMENT_ID) parentFragment.childFragments.size() == 1 def childListElementA = parentFragment.childFragments[0] childListElementA.xpath == "/parent-3/child-list[@key='A']" @@ -506,7 +506,7 @@ class CpsDataPersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase { 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) + def parentFragment = fragmentRepository.getReferenceById(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' @@ -539,7 +539,7 @@ class CpsDataPersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase { when: 'deleting list is executed for: #scenario.' objectUnderTest.deleteListDataNode(DATASPACE_NAME, ANCHOR_NAME3, targetXpaths) and: 'remaining children are fetched' - def parentFragment = fragmentRepository.getById(parentFragmentId) + def parentFragment = fragmentRepository.getReferenceById(parentFragmentId) def remainingChildXpaths = parentFragment.childFragments.collect { it.xpath } then: 'only the expected children remain' assert remainingChildXpaths.size() == expectedRemainingChildXpaths.size() @@ -557,7 +557,7 @@ class CpsDataPersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase { when: 'deleting nodes is executed for: #scenario.' objectUnderTest.deleteDataNodes(DATASPACE_NAME, ANCHOR_NAME3, targetXpaths) and: 'remaining children are fetched' - def parentFragment = fragmentRepository.getById(LIST_DATA_NODE_PARENT203_FRAGMENT_ID) + def parentFragment = fragmentRepository.getReferenceById(LIST_DATA_NODE_PARENT203_FRAGMENT_ID) def remainingChildXpaths = parentFragment.childFragments.collect { it.xpath } then: 'only the expected children remain' assert remainingChildXpaths.size() == expectedRemainingChildXpaths.size() 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 c8e3283a91..8a5838827a 100644 --- 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 @@ -68,15 +68,6 @@ class CpsDataPersistenceServiceSpec extends Specification { 2 * mockFragmentRepository.save(_) } - def 'Store single data node.'() { - given: 'a data node' - def dataNode = new DataNode() - when: 'storing a single data node' - objectUnderTest.storeDataNode('dataspace1', 'anchor1', dataNode) - then: 'the call is redirected to storing a collection of data nodes with just the given data node' - 1 * objectUnderTest.storeDataNodes('dataspace1', 'anchor1', [dataNode]) - } - def 'Handling of StaleStateException (caused by concurrent updates) during update data nodes and descendants.'() { given: 'the system can update one datanode and has two more datanodes that throw an exception while updating' def dataNodes = createDataNodesAndMockRepositoryMethodSupportingThem([ diff --git a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsModulePersistenceServiceIntegrationSpec.groovy b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsModulePersistenceServiceIntegrationSpec.groovy index 53f42f5a9f..3a5d9ef769 100644 --- a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsModulePersistenceServiceIntegrationSpec.groovy +++ b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsModulePersistenceServiceIntegrationSpec.groovy @@ -260,10 +260,10 @@ class CpsModulePersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase objectUnderTest.deleteUnusedYangResourceModules() then: 'any orphaned (not used by any schema set anymore) yang resources are deleted' def orphanedResourceId = 3100L - yangResourceRepository.findById(orphanedResourceId).isPresent() == false + assert !yangResourceRepository.findById(orphanedResourceId).isPresent() and: 'any shared (still in use by other schema set) yang resources still persists' def sharedResourceId = 3003L - yangResourceRepository.findById(sharedResourceId).isPresent() + assert yangResourceRepository.findById(sharedResourceId).isPresent() } @Sql([CLEAR_DATA, SET_DATA]) diff --git a/cps-service/src/main/java/org/onap/cps/spi/CpsDataPersistenceService.java b/cps-service/src/main/java/org/onap/cps/spi/CpsDataPersistenceService.java index 949fbc2c29..d28a3339fe 100644 --- a/cps-service/src/main/java/org/onap/cps/spi/CpsDataPersistenceService.java +++ b/cps-service/src/main/java/org/onap/cps/spi/CpsDataPersistenceService.java @@ -36,18 +36,6 @@ import org.onap.cps.spi.model.DataNode; */ public interface CpsDataPersistenceService { - - /** - * Store a datanode. - * - * @param dataspaceName dataspace name - * @param anchorName anchor name - * @param dataNode data node - * @deprecated Please use {@link #storeDataNodes(String, String, Collection)} as it supports multiple data nodes. - */ - @Deprecated - void storeDataNode(String dataspaceName, String anchorName, DataNode dataNode); - /** * Store multiple datanodes at once. * @param dataspaceName dataspace name -- cgit 1.2.3-korg