Merge "Implement cps path query to get ancestor by schema node identifier Cleaned...
[cps.git] / cps-ri / src / test / groovy / org / onap / cps / spi / impl / CpsDataPersistenceServiceSpec.groovy
old mode 100644 (file)
new mode 100755 (executable)
index a6e4701..f632e02
@@ -2,6 +2,7 @@
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2021 Nordix Foundation
  *  Modifications Copyright (C) 2021 Pantheon.tech
+ *  Modifications Copyright (C) 2021 Bell Canada.
  *  ================================================================================
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
  */
 package org.onap.cps.spi.impl
 
-import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
-import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
-
 import com.google.common.collect.ImmutableSet
 import com.google.gson.Gson
 import com.google.gson.GsonBuilder
 import org.onap.cps.spi.CpsDataPersistenceService
 import org.onap.cps.spi.entities.FragmentEntity
+import org.onap.cps.spi.exceptions.AlreadyDefinedException
 import org.onap.cps.spi.exceptions.AnchorNotFoundException
 import org.onap.cps.spi.exceptions.DataNodeNotFoundException
 import org.onap.cps.spi.exceptions.DataspaceNotFoundException
@@ -35,7 +34,11 @@ import org.onap.cps.spi.model.DataNodeBuilder
 import org.springframework.beans.factory.annotation.Autowired
 import org.springframework.dao.DataIntegrityViolationException
 import org.springframework.test.context.jdbc.Sql
-import spock.lang.Unroll
+
+import javax.validation.ConstraintViolationException
+
+import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
+import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
 
 class CpsDataPersistenceServiceSpec extends CpsPersistenceSpecBase {
 
@@ -56,10 +59,10 @@ class CpsDataPersistenceServiceSpec extends CpsPersistenceSpecBase {
     static DataNode existingChildDataNode
 
     def expectedLeavesByXpathMap = [
-            '/parent-100'                      : ['parent-leaf': 'parent-leaf-value'],
-            '/parent-100/child-001'            : ['first-child-leaf': 'first-child-leaf-value'],
-            '/parent-100/child-002'            : ['second-child-leaf': 'second-child-leaf-value'],
-            '/parent-100/child-002/grand-child': ['grand-child-leaf': 'grand-child-leaf-value']
+            '/parent-100'                      : ['parent-leaf': 'parent-leaf value'],
+            '/parent-100/child-001'            : ['first-child-leaf': 'first-child-leaf value'],
+            '/parent-100/child-002'            : ['second-child-leaf': 'second-child-leaf value'],
+            '/parent-100/child-002/grand-child': ['grand-child-leaf': 'grand-child-leaf value']
     ]
 
     static {
@@ -87,6 +90,22 @@ class CpsDataPersistenceServiceSpec extends CpsPersistenceSpecBase {
             grandchildFragment.xpath == grandChildXpath
     }
 
+    @Sql([CLEAR_DATA, SET_DATA])
+    def 'Store data node for multiple anchors using the same schema.'() {
+        def xpath = "/parent-new"
+        given: 'a fragment is stored for an anchor'
+            objectUnderTest.storeDataNode(DATASPACE_NAME, ANCHOR_NAME1, createDataNodeTree(xpath))
+        when: 'another fragment is stored for an other anchor, using the same schema set'
+            objectUnderTest.storeDataNode(DATASPACE_NAME, ANCHOR_NAME3, createDataNodeTree(xpath))
+        then: 'both fragments can be retrieved by their xpath'
+            def fragment1 = getFragmentByXpath(DATASPACE_NAME, ANCHOR_NAME1, xpath)
+            fragment1.anchor.name == ANCHOR_NAME1
+            fragment1.xpath == xpath
+            def fragment2 = getFragmentByXpath(DATASPACE_NAME, ANCHOR_NAME3, xpath)
+            fragment2.anchor.name == ANCHOR_NAME3
+            fragment2.xpath == xpath
+    }
+
     @Sql([CLEAR_DATA, SET_DATA])
     def 'Store datanode error scenario: #scenario.'() {
         when: 'attempt to store a data node with #scenario'
@@ -97,7 +116,8 @@ class CpsDataPersistenceServiceSpec extends CpsPersistenceSpecBase {
             scenario                    | dataspaceName  | anchorName     | dataNode         || expectedException
             'dataspace does not exist'  | 'unknown'      | 'not-relevant' | newDataNode      || DataspaceNotFoundException
             'schema set does not exist' | DATASPACE_NAME | 'unknown'      | newDataNode      || AnchorNotFoundException
-            'anchor already exists'     | DATASPACE_NAME | ANCHOR_NAME1   | existingDataNode || DataIntegrityViolationException
+            'anchor already exists'     | DATASPACE_NAME | ANCHOR_NAME1   | newDataNode      || ConstraintViolationException
+            'datanode already exists'   | DATASPACE_NAME | ANCHOR_NAME1   | existingDataNode || AlreadyDefinedException
     }
 
     @Sql([CLEAR_DATA, SET_DATA])
@@ -148,19 +168,24 @@ class CpsDataPersistenceServiceSpec extends CpsPersistenceSpecBase {
     def 'Get data node by xpath without descendants.'() {
         when: 'data node is requested'
             def result = objectUnderTest.getDataNode(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES,
-                    XPATH_DATA_NODE_WITH_LEAVES, OMIT_DESCENDANTS)
+                    inputXPath, OMIT_DESCENDANTS)
         then: 'data node is returned with no descendants'
             assert result.getXpath() == XPATH_DATA_NODE_WITH_LEAVES
         and: 'expected leaves'
             assert result.getChildDataNodes().size() == 0
             assertLeavesMaps(result.getLeaves(), expectedLeavesByXpathMap[XPATH_DATA_NODE_WITH_LEAVES])
+        where: 'the following data is used'
+            scenario                      | inputXPath
+            'some xpath'                  |'/parent-100'
+            'root xpath'                  |'/'
+            'empty xpath'                 |''
     }
 
     @Sql([CLEAR_DATA, SET_DATA])
     def 'Get data node by xpath with all descendants.'() {
         when: 'data node is requested with all descendants'
             def result = objectUnderTest.getDataNode(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES,
-                    XPATH_DATA_NODE_WITH_LEAVES, INCLUDE_ALL_DESCENDANTS)
+                    inputXPath, INCLUDE_ALL_DESCENDANTS)
             def mappedResult = treeToFlatMapByXpath(new HashMap<>(), result)
         then: 'data node is returned with all the descendants populated'
             assert mappedResult.size() == 4
@@ -169,33 +194,14 @@ class CpsDataPersistenceServiceSpec extends CpsPersistenceSpecBase {
             assert mappedResult.get('/parent-100/child-002').getChildDataNodes().size() == 1
         and: 'extracted leaves maps are matching expected'
             mappedResult.forEach(
-                    (xpath, dataNode) ->
-                            assertLeavesMaps(dataNode.getLeaves(), expectedLeavesByXpathMap[xpath])
-            )
-    }
-
-    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.getXpath(), dataNodeTree)
-        dataNodeTree.getChildDataNodes()
-                .forEach(childDataNode -> treeToFlatMapByXpath(flatMap, childDataNode))
-        return flatMap
+                    (xPath, dataNode) -> assertLeavesMaps(dataNode.getLeaves(), expectedLeavesByXpathMap[xPath]))
+        where: 'the following data is used'
+            scenario                      | inputXPath
+            'some xpath'                  |'/parent-100'
+            'root xpath'                  |'/'
+            'empty xpath'                 |''
     }
 
-    @Unroll
     @Sql([CLEAR_DATA, SET_DATA])
     def 'Get data node error scenario: #scenario.'() {
         when: 'attempt to get data node with #scenario'
@@ -226,7 +232,6 @@ class CpsDataPersistenceServiceSpec extends CpsPersistenceSpecBase {
             assert childLeaves.'leaf-value' == 'original'
     }
 
-    @Unroll
     @Sql([CLEAR_DATA, SET_DATA])
     def 'Update data leaves error scenario: #scenario.'() {
         when: 'attempt to update data node for #scenario'
@@ -279,11 +284,10 @@ class CpsDataPersistenceServiceSpec extends CpsPersistenceSpecBase {
             assert childLeaves.'leaf-value' == 'original'
     }
 
-    @Unroll
     @Sql([CLEAR_DATA, SET_DATA])
     def 'Replace data node tree error scenario: #scenario.'() {
         given: 'data node object'
-            def submittedDataNode = buildDataNode(xpath, ['leaf-name':'leaf-value'], [])
+            def submittedDataNode = buildDataNode(xpath, ['leaf-name': 'leaf-value'], [])
         when: 'attempt to update data node for #scenario'
             objectUnderTest.replaceDataNodeTree(dataspaceName, anchorName, submittedDataNode)
         then: 'a #expectedException is thrown'
@@ -302,4 +306,25 @@ class CpsDataPersistenceServiceSpec extends CpsPersistenceSpecBase {
     static Map<String, Object> getLeavesMap(FragmentEntity fragmentEntity) {
         return GSON.fromJson(fragmentEntity.getAttributes(), 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.getXpath(), dataNodeTree)
+        dataNodeTree.getChildDataNodes()
+            .forEach(childDataNode -> treeToFlatMapByXpath(flatMap, childDataNode))
+        return flatMap
+    }
+
 }