CPS-265 - updating cps path to support include-descendants option.
[cps.git] / cps-ri / src / test / groovy / org / onap / cps / spi / impl / CpsDataPersistenceServiceSpec.groovy
index a6e4701..8001c65 100644 (file)
  */
 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.FetchDescendantsOption
 import org.onap.cps.spi.entities.FragmentEntity
 import org.onap.cps.spi.exceptions.AnchorNotFoundException
 import org.onap.cps.spi.exceptions.DataNodeNotFoundException
@@ -37,6 +35,9 @@ import org.springframework.dao.DataIntegrityViolationException
 import org.springframework.test.context.jdbc.Sql
 import spock.lang.Unroll
 
+import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
+import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
+
 class CpsDataPersistenceServiceSpec extends CpsPersistenceSpecBase {
 
     @Autowired
@@ -87,6 +88,7 @@ class CpsDataPersistenceServiceSpec extends CpsPersistenceSpecBase {
             grandchildFragment.xpath == grandChildXpath
     }
 
+    @Unroll
     @Sql([CLEAR_DATA, SET_DATA])
     def 'Store datanode error scenario: #scenario.'() {
         when: 'attempt to store a data node with #scenario'
@@ -116,6 +118,7 @@ class CpsDataPersistenceServiceSpec extends CpsPersistenceSpecBase {
             parentFragment.getChildFragments().find({ it.xpath == newChild.xpath })
     }
 
+    @Unroll
     @Sql([CLEAR_DATA, SET_DATA])
     def 'Add child error scenario: #scenario.'() {
         when: 'attempt to add a child data node with #scenario'
@@ -283,7 +286,7 @@ class CpsDataPersistenceServiceSpec extends CpsPersistenceSpecBase {
     @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 +305,35 @@ class CpsDataPersistenceServiceSpec extends CpsPersistenceSpecBase {
     static Map<String, Object> getLeavesMap(FragmentEntity fragmentEntity) {
         return GSON.fromJson(fragmentEntity.getAttributes(), Map<String, Object>.class)
     }
+
+    @Unroll
+    @Sql([CLEAR_DATA, SET_DATA])
+    def 'Cps Path query for single leaf value with type: #type.'() {
+        when: 'a query is executed to get a data node by the given cps path'
+            def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, cpsPath, includeDescendantsOption)
+        then: 'the correct data is returned'
+            def leaves ='[common-leaf-name:common-leaf-value, common-leaf-name-int:5.0]'
+            DataNode dataNode = result.stream().findFirst().get()
+            dataNode.getLeaves().toString() == leaves
+            dataNode.getChildDataNodes().size() == expectedNumberOfChidlNodes
+        where: 'the following data is used'
+            type                         | cpsPath                                                          | includeDescendantsOption | expectedNumberOfChidlNodes
+            'String and no descendants'  | '/parent-200/child-202[@common-leaf-name=\'common-leaf-value\']' | OMIT_DESCENDANTS         | 0
+            'Integer and descendants'    | '/parent-200/child-202[@common-leaf-name-int=5]'                 | INCLUDE_ALL_DESCENDANTS  | 1
+    }
+
+    @Unroll
+    @Sql([CLEAR_DATA, SET_DATA])
+    def 'Query for attribute by cps path with cps paths that return no data because of #scenario.'() {
+        when: 'a query is executed to get datanodes for the given cps path'
+            def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, cpsPath, FetchDescendantsOption.OMIT_DESCENDANTS)
+        then: 'no data is returned'
+            result.isEmpty()
+        where: 'following cps queries are performed'
+            scenario                         | cpsPath
+            'cps path is incomplete'         | '/parent-200[@common-leaf-name-int=5]'
+            'missing / at beginning of path' | 'parent-200/child-202[@common-leaf-name-int=5]'
+            'leaf value does not exist'      | '/parent-200/child-202[@common-leaf-name=\'does not exist\']'
+            'incomplete end of xpath prefix' | '/parent-200/child-20[@common-leaf-name-int=5]'
+    }
 }