Merge "Reduce dataspace/anchor lookups in CpsDataPersistenceService"
[cps.git] / cps-ri / src / test / groovy / org / onap / cps / spi / impl / CpsDataPersistenceServiceIntegrationSpec.groovy
index 28916b1..25f19f7 100755 (executable)
@@ -25,7 +25,6 @@ package org.onap.cps.spi.impl
 
 import com.fasterxml.jackson.databind.ObjectMapper
 import com.google.common.collect.ImmutableSet
-import org.onap.cps.cpspath.parser.PathParsingException
 import org.onap.cps.spi.CpsDataPersistenceService
 import org.onap.cps.spi.entities.FragmentEntity
 import org.onap.cps.spi.exceptions.AlreadyDefinedExceptionBatch
@@ -71,13 +70,6 @@ class CpsDataPersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase {
     def static deleteTestChildXpath = "${deleteTestParentXPath}/child-with-slash[@key='a/b']"
     def static deleteTestGrandChildXPath = "${deleteTestChildXpath}/grandChild"
 
-    def expectedLeavesByXpathMap = [
-            '/parent-207'                      : ['parent-leaf': 'parent-leaf value'],
-            '/parent-207/child-001'            : ['first-child-leaf': 'first-child-leaf value'],
-            '/parent-207/child-002'            : ['second-child-leaf': 'second-child-leaf value'],
-            '/parent-207/child-002/grand-child': ['grand-child-leaf': 'grand-child-leaf value']
-    ]
-
     @Sql([CLEAR_DATA, SET_DATA])
     def 'Get all datanodes with descendants .'() {
         when: 'data nodes are retrieved by their xpath'
@@ -188,7 +180,7 @@ class CpsDataPersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase {
         and: 'the (grand)child node of the new list entry is also present'
             def dataspaceEntity = dataspaceRepository.getByName(DATASPACE_NAME)
             def anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, ANCHOR_NAME3)
-            def grandChildFragmentEntity = fragmentRepository.findByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity, grandChild.xpath)
+            def grandChildFragmentEntity = fragmentRepository.findByAnchorAndXpath(anchorEntity, grandChild.xpath)
             assert grandChildFragmentEntity.isPresent()
     }
 
@@ -213,7 +205,7 @@ class CpsDataPersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase {
         and: 'the new entity is inserted correctly'
             def dataspaceEntity = dataspaceRepository.getByName(DATASPACE_NAME)
             def anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, ANCHOR_HAVING_SINGLE_TOP_LEVEL_FRAGMENT)
-            fragmentRepository.findByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity, '/parent-200/child-new2').isPresent()
+            fragmentRepository.findByAnchorAndXpath(anchorEntity, '/parent-200/child-new2').isPresent()
     }
 
     @Sql([CLEAR_DATA, SET_DATA])
@@ -253,8 +245,8 @@ class CpsDataPersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase {
         when: 'trying to execute a query with a syntax (parsing) error'
             objectUnderTest.getDataNodes(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, 'invalid-cps-path/child' , OMIT_DESCENDANTS)
         then: 'exception is thrown'
-            def exceptionThrown = thrown(PathParsingException)
-            assert exceptionThrown.getMessage().contains('failed to parse at line 1 due to extraneous input \'invalid-cps-path\' expecting \'/\'')
+            def exceptionThrown = thrown(CpsPathException)
+            assert exceptionThrown.getDetails() == "failed to parse at line 1 due to extraneous input 'invalid-cps-path' expecting '/'"
     }
 
     @Sql([CLEAR_DATA, SET_DATA])
@@ -288,7 +280,7 @@ class CpsDataPersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase {
         where: 'the following data is used'
             scenario             | dataspaceName  | anchorName                        | xpath           || expectedException
             'non existing xpath' | DATASPACE_NAME | ANCHOR_FOR_DATA_NODES_WITH_LEAVES | '/NO-XPATH'     || DataNodeNotFoundException
-            'invalid Xpath'      | DATASPACE_NAME | ANCHOR_FOR_DATA_NODES_WITH_LEAVES | 'INVALID XPATH' || PathParsingException
+            'invalid Xpath'      | DATASPACE_NAME | ANCHOR_FOR_DATA_NODES_WITH_LEAVES | 'INVALID XPATH' || CpsPathException
     }
 
     @Sql([CLEAR_DATA, SET_DATA])
@@ -667,35 +659,35 @@ class CpsDataPersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase {
         then: 'a #expectedException is thrown'
             thrown(expectedException)
         where: 'the following parameters were used'
-            scenario                                        | datanodeXpath                                    | expectedException
-            'valid data node, non existent child node'      | '/parent-203/child-non-existent'                 | DataNodeNotFoundException
-            'invalid list element'                          | '/parent-206/child-206/grand-child-206@key="A"]' | PathParsingException
+            scenario                                   | datanodeXpath                                    | expectedException
+            'valid data node, non existent child node' | '/parent-203/child-non-existent'                 | DataNodeNotFoundException
+            'invalid list element'                     | '/parent-206/child-206/grand-child-206@key="A"]' | CpsPathException
     }
 
     @Sql([CLEAR_DATA, SET_DATA])
     def 'Delete data node for an anchor.'() {
         given: 'a data-node exists for an anchor'
-            assert fragmentsExistInDB(1001, 3003)
+            assert fragmentsExistInDB(3003)
         when: 'data nodes are deleted '
             objectUnderTest.deleteDataNodes(DATASPACE_NAME, ANCHOR_NAME3)
         then: 'all data-nodes are deleted successfully'
-            assert !fragmentsExistInDB(1001, 3003)
+            assert !fragmentsExistInDB(3003)
     }
 
     @Sql([CLEAR_DATA, SET_DATA])
     def 'Delete data node for multiple anchors.'() {
         given: 'a data-node exists for an anchor'
-            assert fragmentsExistInDB(1001, 3001)
-            assert fragmentsExistInDB(1001, 3003)
+            assert fragmentsExistInDB(3001)
+            assert fragmentsExistInDB(3003)
         when: 'data nodes are deleted '
             objectUnderTest.deleteDataNodes(DATASPACE_NAME, ['ANCHOR-001', 'ANCHOR-003'])
         then: 'all data-nodes are deleted successfully'
-            assert !fragmentsExistInDB(1001, 3001)
-            assert !fragmentsExistInDB(1001, 3003)
+            assert !fragmentsExistInDB(3001)
+            assert !fragmentsExistInDB(3003)
     }
 
-    def fragmentsExistInDB(dataSpaceId, anchorId) {
-        !fragmentRepository.findRootsByDataspaceAndAnchor(dataSpaceId, anchorId).isEmpty()
+    def fragmentsExistInDB(anchorId) {
+        fragmentRepository.existsByAnchorId(anchorId)
     }
 
     static Collection<DataNode> toDataNodes(xpaths) {
@@ -711,19 +703,6 @@ class CpsDataPersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase {
         return jsonObjectMapper.convertJsonString(fragmentEntity.attributes, Map<String, Object>.class)
     }
 
-    def static assertLeavesMaps(actualLeavesMap, expectedLeavesMap) {
-        expectedLeavesMap.forEach((key, value) -> {
-            def actualValue = actualLeavesMap[key]
-            if (value instanceof Collection<?> && actualValue instanceof Collection<?>) {
-                assert value.size() == actualValue.size()
-                assert value.containsAll(actualValue)
-            } else {
-                assert value == actualValue
-            }
-        })
-        return true
-    }
-
     def static treeToFlatMapByXpath(Map<String, DataNode> flatMap, DataNode dataNodeTree) {
         flatMap.put(dataNodeTree.xpath, dataNodeTree)
         dataNodeTree.childDataNodes
@@ -757,10 +736,9 @@ class CpsDataPersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase {
     def getFragmentByXpath(dataspaceName, anchorName, xpath) {
         def dataspace = dataspaceRepository.getByName(dataspaceName)
         def anchor = anchorRepository.getByDataspaceAndName(dataspace, anchorName)
-        return fragmentRepository.findByDataspaceAndAnchorAndXpath(dataspace, anchor, xpath).orElseThrow()
+        return fragmentRepository.findByAnchorAndXpath(anchor, xpath).orElseThrow()
     }
 
-
     def createChildListAllHavingAttributeValue(parentXpath, tag, Collection keys, boolean addGrandChild) {
         def listElementAsDataNodes = keysToXpaths(parentXpath, keys).collect {
                 new DataNodeBuilder()