2  *  ============LICENSE_START=======================================================
 
   3  *  Copyright (C) 2023-2025 Nordix Foundation
 
   4  *  Modifications Copyright (C) 2023-2025 TechMahindra Ltd
 
   5  *  ================================================================================
 
   6  *  Licensed under the Apache License, Version 2.0 (the 'License');
 
   7  *  you may not use this file except in compliance with the License.
 
   8  *  You may obtain a copy of the License at
 
  10  *        http://www.apache.org/licenses/LICENSE-2.0
 
  12  *  Unless required by applicable law or agreed to in writing, software
 
  13  *  distributed under the License is distributed on an 'AS IS' BASIS,
 
  14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  15  *  See the License for the specific language governing permissions and
 
  16  *  limitations under the License.
 
  18  *  SPDX-License-Identifier: Apache-2.0
 
  19  *  ============LICENSE_END=========================================================
 
  22 package org.onap.cps.integration.functional.cps
 
  24 import java.time.OffsetDateTime
 
  25 import org.onap.cps.api.CpsQueryService
 
  26 import org.onap.cps.integration.base.FunctionalSpecBase
 
  27 import org.onap.cps.api.parameters.FetchDescendantsOption
 
  28 import org.onap.cps.api.parameters.PaginationOption
 
  29 import org.onap.cps.api.exceptions.CpsPathException
 
  31 import static org.onap.cps.api.parameters.FetchDescendantsOption.DIRECT_CHILDREN_ONLY
 
  32 import static org.onap.cps.api.parameters.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
 
  33 import static org.onap.cps.api.parameters.FetchDescendantsOption.OMIT_DESCENDANTS
 
  34 import static org.onap.cps.api.parameters.PaginationOption.NO_PAGINATION
 
  36 class QueryServiceIntegrationSpec extends FunctionalSpecBase {
 
  38     CpsQueryService objectUnderTest
 
  40     def setup() { objectUnderTest = cpsQueryService }
 
  42     def 'Query bookstore using CPS path where #scenario.'() {
 
  43         when: 'query data nodes for bookstore container'
 
  44             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, INCLUDE_ALL_DESCENDANTS)
 
  45         then: 'the result contains expected number of nodes'
 
  46             assert result.size() == expectedResultSize
 
  47         and: 'the result contains the expected leaf values'
 
  48             result.leaves.forEach( dataNodeLeaves -> {
 
  49                 expectedLeaves.forEach( (expectedLeafKey,expectedLeafValue) -> {
 
  50                     assert dataNodeLeaves[expectedLeafKey] == expectedLeafValue
 
  54             scenario                                      | cpsPath                                    || expectedResultSize | expectedLeaves
 
  55             'the AND condition is used'                   | '//books[@lang="English" and @price=15]'   || 2                  | [lang:"English", price:15]
 
  56             'the AND is used where result does not exist' | '//books[@lang="English" and @price=1000]' || 0                  | []
 
  59     def 'Query data leaf using CPS path for #scenario.'() {
 
  60         when: 'query data leaf for bookstore container'
 
  61             def result = objectUnderTest.queryDataLeaf(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, Object.class)
 
  62         then: 'the result contains the expected number of leaf values'
 
  63             assert result.size() == expectedUniqueBooksTitles
 
  65             scenario                  | cpsPath                                       || expectedUniqueBooksTitles
 
  66             'all books'               | '//books/@title'                              || 19
 
  67             'all books in a category' | '/bookstore/categories[@code=5]/books/@title' || 10
 
  68             'non-existing path'       | '/non-existing/@title'                        || 0
 
  71     def 'Query data leaf with type #leafType using CPS path.'() {
 
  72         given: 'a cps path query for two books, returning only #leafName'
 
  73             def cpsPath = '//books[@title="Matilda" or @title="Good Omens"]/@' + leafName
 
  74         when: 'query data leaf for bookstore container'
 
  75             def results = objectUnderTest.queryDataLeaf(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, leafType)
 
  76         then: 'the result contains the expected leaf values'
 
  77             assert results == expectedResults as Set
 
  79             leafName    | leafType      || expectedResults
 
  80             'lang'      | String.class  || ['English']
 
  81             'price'     | Number.class  || [13, 20]
 
  82             'editions'  | List.class    || [[1988, 2000], [2006]]
 
  85     def 'Query data leaf using CPS path with ancestor axis.'() {
 
  86         given: 'a cps path query that will return the names of the categories of two books'
 
  87             def cpsPath = '//books[@title="Matilda" or @title="Good Omens"]/ancestor::categories/@name'
 
  88         when: 'query data leaf for bookstore container'
 
  89             def result = objectUnderTest.queryDataLeaf(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, String.class)
 
  90         then: 'the result contains the expected leaf values'
 
  91             assert result == ['Children', 'Comedy'] as Set
 
  94     def 'Cps Path query using comparative and boolean operators.'() {
 
  95         given: 'a cps path query in the discount category'
 
  96             def cpsPath = "/bookstore/categories[@code='5']/books" + leafCondition
 
  97         when: 'a query is executed to get response by the given cps path'
 
  98             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1,
 
  99                     cpsPath, OMIT_DESCENDANTS)
 
 100         then: 'the cps-path of queryDataNodes has the expectedLeaves'
 
 101             def bookPrices = result.collect { it.getLeaves().get('price') }
 
 102             assert bookPrices.sort() == expectedBookPrices.sort()
 
 103         where: 'the following data is used'
 
 104             leafCondition                                 || expectedBookPrices
 
 105             '[@price = 5]'                                || [5]
 
 106             '[@price < 5]'                                || [1, 2, 3, 4]
 
 107             '[@price > 5]'                                || [6, 7, 8, 9, 10]
 
 108             '[@price <= 5]'                               || [1, 2, 3, 4, 5]
 
 109             '[@price >= 5]'                               || [5, 6, 7, 8, 9, 10]
 
 110             '[@price > 10]'                               || []
 
 111             '[@price = 3 or @price = 7]'                  || [3, 7]
 
 112             '[@price = 3 and @price = 7]'                 || []
 
 113             '[@price > 3 and @price <= 6]'                || [4, 5, 6]
 
 114             '[@price < 3 or @price > 8]'                  || [1, 2, 9, 10]
 
 115             '[@price = 1 or @price = 3 or @price = 5]'    || [1, 3, 5]
 
 116             '[@price = 1 or @price >= 8 and @price < 10]' || [1, 8, 9]
 
 117             '[@price >= 3 and @price <= 5 or @price > 9]' || [3, 4, 5, 10]
 
 120     def 'Cps Path query for leaf value(s) with #scenario.'() {
 
 121         when: 'a query is executed to get a data node by the given cps path'
 
 122             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, fetchDescendantsOption)
 
 123         then: 'the correct number of parent nodes are returned'
 
 124             assert result.size() == expectedNumberOfParentNodes
 
 125         and: 'the correct total number of data nodes are returned'
 
 126             assert countDataNodesInTree(result) == expectedTotalNumberOfNodes
 
 127         where: 'the following data is used'
 
 128             scenario                               | cpsPath                                                    | fetchDescendantsOption         || expectedNumberOfParentNodes | expectedTotalNumberOfNodes
 
 129             'string and no descendants'            | '/bookstore/categories[@code="1"]/books[@title="Matilda"]' | OMIT_DESCENDANTS               || 1                           | 1
 
 130             'integer and descendants'              | '/bookstore/categories[@code="1"]/books[@price=15]'        | INCLUDE_ALL_DESCENDANTS        || 1                           | 1
 
 131             'no condition and no descendants'      | '/bookstore/categories'                                    | OMIT_DESCENDANTS               || 5                           | 5
 
 132             'no condition and level 1 descendants' | '/bookstore'                                               | new FetchDescendantsOption(1)  || 1                           | 7
 
 133             'no condition and level 2 descendants' | '/bookstore'                                               | new FetchDescendantsOption(2)  || 1                           | 28
 
 136     def 'Query for attribute by cps path with cps paths that return no data because of #scenario.'() {
 
 137         when: 'a query is executed to get data nodes for the given cps path'
 
 138             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, OMIT_DESCENDANTS)
 
 139         then: 'no data is returned'
 
 140             assert result.isEmpty()
 
 141         where: 'following cps queries are performed'
 
 143             'cps path is incomplete'         | '/bookstore[@title="Matilda"]'
 
 144             'leaf value does not exist'      | '/bookstore/categories[@code="1"]/books[@title=\'does not exist\']'
 
 145             'incomplete end of xpath prefix' | '/bookstore/categories/books[@price=15]'
 
 148     def 'Cps Path query using descendant anywhere and #type (further) descendants.'() {
 
 149         when: 'a query is executed to get a data node by the given cps path'
 
 150             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="1"]', fetchDescendantsOption)
 
 151         then: 'the data node has the correct number of children'
 
 152             assert result[0].childDataNodes.xpath.sort() == expectedChildNodes.sort()
 
 153         where: 'the following data is used'
 
 154             type      | fetchDescendantsOption   || expectedChildNodes
 
 155             'omit'    | OMIT_DESCENDANTS         || []
 
 156             'include' | INCLUDE_ALL_DESCENDANTS  || ["/bookstore/categories[@code='1']/books[@title='Matilda']",
 
 157                                                      "/bookstore/categories[@code='1']/books[@title='The Gruffalo']"]
 
 160     def 'Cps Path query for all books.'() {
 
 161         when: 'a query is executed to get all books'
 
 162             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '//books', OMIT_DESCENDANTS)
 
 163         then: 'the expected number of books are returned'
 
 164             assert result.size() == 19
 
 167     def 'Cps Path query using descendant anywhere with #scenario.'() {
 
 168         when: 'a query is executed to get a data node by the given cps path'
 
 169             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, OMIT_DESCENDANTS)
 
 170         then: 'xpaths of the retrieved data nodes are as expected'
 
 171             def bookTitles = result.collect { it.getLeaves().get('title') }
 
 172             assert bookTitles.sort() == expectedBookTitles.sort()
 
 173         where: 'the following data is used'
 
 174             scenario                                 | cpsPath                                     || expectedBookTitles
 
 175             'string leaf condition'                  | '//books[@title="Matilda"]'                 || ["Matilda"]
 
 176             'text condition on leaf'                 | '//books/title[text()="Matilda"]'           || ["Matilda"]
 
 177             'text condition case mismatch'           | '//books/title[text()="matilda"]'           || []
 
 178             'text condition on int leaf'             | '//books/price[text()="20"]'                || ["A Book with No Language", "Matilda"]
 
 179             'text condition on leaf-list'            | '//books/authors[text()="Terry Pratchett"]' || ["Good Omens", "The Colour of Magic", "The Light Fantastic"]
 
 180             'text condition partial match'           | '//books/authors[text()="Terry"]'           || []
 
 181             'text condition (existing) empty string' | '//books/lang[text()=""]'                   || ["A Book with No Language"]
 
 182             'text condition on int leaf-list'        | '//books/editions[text()="2000"]'           || ["Matilda"]
 
 183             'match of leaf containing /'             | '//books[@lang="N/A"]'                      || ["Logarithm tables"]
 
 184             'text condition on leaf containing /'    | '//books/lang[text()="N/A"]'                || ["Logarithm tables"]
 
 185             'match of key containing /'              | '//books[@title="Debian GNU/Linux"]'        || ["Debian GNU/Linux"]
 
 186             'text condition on key containing /'     | '//books/title[text()="Debian GNU/Linux"]'  || ["Debian GNU/Linux"]
 
 189     def 'Query for attribute by cps path using contains condition #scenario.'() {
 
 190         when: 'a query is executed to get response by the given cps path'
 
 191             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, INCLUDE_ALL_DESCENDANTS)
 
 192         then: 'xpaths of the retrieved data nodes are as expected'
 
 193             def bookTitles = result.collect { it.getLeaves().get('title') }
 
 194             assert bookTitles.sort() == expectedBookTitles.sort()
 
 195         where: 'the following data is used'
 
 196             scenario                                 | cpsPath                           || expectedBookTitles
 
 197             'contains condition with leaf'           | '//books[contains(@title,"Mat")]' || ["Matilda"]
 
 198             'contains condition with case-sensitive' | '//books[contains(@title,"Ti")]'  || []
 
 199             'contains condition with Integer Value'  | '//books[contains(@price,"15")]'  || ["Annihilation", "The Gruffalo"]
 
 202     def 'Query for attribute by cps path using contains condition with no value.'() {
 
 203         when: 'a query is executed to get response by the given cps path'
 
 204             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '//books[contains(@title,"")]', OMIT_DESCENDANTS)
 
 205         then: 'all books are returned'
 
 206             assert result.size() == 19
 
 209     def 'Cps Path query using descendant anywhere with #scenario condition for a container element.'() {
 
 210         when: 'a query is executed to get a data node by the given cps path'
 
 211             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, OMIT_DESCENDANTS)
 
 212         then: 'book titles from the retrieved data nodes are as expected'
 
 213             def bookTitles = result.collect { it.getLeaves().get('title') }
 
 214             assert bookTitles.sort() == expectedBookTitles.sort()
 
 215         where: 'the following data is used'
 
 216             scenario                                                   | cpsPath                                                                || expectedBookTitles
 
 217             'one leaf'                                                 | '//books[@price=14]'                                                   || ['The Light Fantastic']
 
 218             'one leaf with ">" condition'                              | '//books[@price>14]'                                                   || ['A Book with No Language', 'Annihilation', 'Debian GNU/Linux', 'Matilda', 'The Gruffalo']
 
 219             'one text'                                                 | '//books/authors[text()="Terry Pratchett"]'                            || ['Good Omens', 'The Colour of Magic', 'The Light Fantastic']
 
 220             'more than one leaf'                                       | '//books[@price=12 and @lang="English"]'                               || ['The Colour of Magic']
 
 221             'more than one leaf has "OR" condition'                    | '//books[@lang="English" or @price=15]'                                || ['Annihilation', 'Good Omens', 'Matilda', 'The Colour of Magic', 'The Gruffalo', 'The Light Fantastic']
 
 222             'more than one leaf has "OR" condition with non-json data' | '//books[@title="xyz" or @price=13]'                                   || ['Good Omens']
 
 223             'more than one leaf has multiple AND'                      | '//books[@lang="English" and @price=13 and @edition=1983]'             || []
 
 224             'more than one leaf has multiple OR'                       | '//books[ @title="Matilda" or @price=15 or @edition=2006]'             || ['Annihilation', 'Matilda', 'The Gruffalo']
 
 225             'leaves reversed in order'                                 | '//books[@lang="English" and @price=12]'                               || ['The Colour of Magic']
 
 226             'more than one leaf has combination of AND/OR'             | '//books[@edition=1983 and @price=13 or @title="Good Omens"]'          || ['Good Omens']
 
 227             'more than one leaf has OR/AND'                            | '//books[@title="The Light Fantastic" or @price=11 and @edition=1983]' || ['The Light Fantastic']
 
 228             'leaf and text'                                            | '//books[@price=14]/authors[text()="Terry Pratchett"]'                 || ['The Light Fantastic']
 
 229             'leaf and contains'                                        | '//books[contains(@price,"13")]'                                       || ['Good Omens']
 
 232     def 'Cps Path query using descendant anywhere with #scenario condition(s) for a list element.'() {
 
 233         when: 'a query is executed to get a data node by the given cps path'
 
 234             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, INCLUDE_ALL_DESCENDANTS)
 
 235         then: 'xpaths of the retrieved data nodes are as expected'
 
 236             result.xpath.toList() == ["/bookstore/premises/addresses[@house-number='2' and @street='Main Street']"]
 
 237         where: 'the following data is used'
 
 239             'full composite key'                  | '//addresses[@house-number=2 and @street="Main Street"]'
 
 240             'one partial key leaf'                | '//addresses[@house-number=2]'
 
 241             'one non key leaf'                    | '//addresses[@county="Kildare"]'
 
 242             'mix of partial key and non key leaf' | '//addresses[@street="Main Street" and @county="Kildare"]'
 
 245     def 'Query for attribute by cps path of type ancestor with #scenario.'() {
 
 246         when: 'the given cps path is parsed'
 
 247             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, OMIT_DESCENDANTS)
 
 248         then: 'the xpaths of the retrieved data nodes are as expected'
 
 249             assert result.xpath.sort() == expectedXPaths.sort()
 
 250         where: 'the following data is used'
 
 251             scenario                                    | cpsPath                                               || expectedXPaths
 
 252             'multiple list-ancestors'                   | '//books/ancestor::categories'                        || ["/bookstore/categories[@code='1']", "/bookstore/categories[@code='2']", "/bookstore/categories[@code='3']", "/bookstore/categories[@code='4']", "/bookstore/categories[@code='5']"]
 
 253             'one ancestor with list value'              | '//books/ancestor::categories[@code="1"]'             || ["/bookstore/categories[@code='1']"]
 
 254             'top ancestor'                              | '//books/ancestor::bookstore'                         || ["/bookstore"]
 
 255             'list with index value in the xpath prefix' | '//categories[@code="1"]/books/ancestor::bookstore'   || ["/bookstore"]
 
 256             'ancestor with parent list'                 | '//books/ancestor::bookstore/categories'              || ["/bookstore/categories[@code='1']", "/bookstore/categories[@code='2']", "/bookstore/categories[@code='3']", "/bookstore/categories[@code='4']", "/bookstore/categories[@code='5']"]
 
 257             'ancestor with parent'                      | '//books/ancestor::bookstore/categories[@code="2"]'   || ["/bookstore/categories[@code='2']"]
 
 258             'ancestor combined with text condition'     | '//books/title[text()="Matilda"]/ancestor::bookstore' || ["/bookstore"]
 
 259             'ancestor with parent that does not exist'  | '//books/ancestor::parentDoesNoExist/categories'      || []
 
 260             'ancestor does not exist'                   | '//books/ancestor::ancestorDoesNotExist'              || []
 
 261             'ancestor combined with contains condition' | '//books[contains(@title,"Mat")]/ancestor::bookstore' || ["/bookstore"]
 
 262             'ancestor is the same as search target'     | '//books/ancestor::books'                             || []
 
 265     def 'Query for attribute by cps path of type ancestor with #scenario descendants.'() {
 
 266         when: 'the given cps path is parsed'
 
 267             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '//books/ancestor::bookstore', fetchDescendantsOption)
 
 268         then: 'the xpaths of the retrieved data nodes are as expected'
 
 269             assert countDataNodesInTree(result) == expectedNumberOfNodes
 
 270         where: 'the following data is used'
 
 271             scenario | fetchDescendantsOption  || expectedNumberOfNodes
 
 272             'no'     | OMIT_DESCENDANTS        || 1
 
 273             'direct' | DIRECT_CHILDREN_ONLY    || 7
 
 274             'all'    | INCLUDE_ALL_DESCENDANTS || 28
 
 277     def 'Cps Path query with #scenario throws a CPS Path Exception.'() {
 
 278         when: 'trying to execute a query with a syntax (parsing) error'
 
 279             objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, OMIT_DESCENDANTS)
 
 280         then: 'a cps path exception is thrown'
 
 281             thrown(CpsPathException)
 
 282         where: 'the following data is used'
 
 284             'cpsPath that cannot be parsed'    | 'cpsPath that cannot be parsed'
 
 285             'String with comparative operator' | '//books[@lang>"German" and @price>10]'
 
 288     def 'Cps Path query across anchors with #scenario.'() {
 
 289         when: 'a query is executed to get a data nodes across anchors by the given CpsPath'
 
 290             def result = objectUnderTest.queryDataNodesAcrossAnchors(FUNCTIONAL_TEST_DATASPACE_1, cpsPath, OMIT_DESCENDANTS, NO_PAGINATION)
 
 291         then: 'the correct dataspace is queried'
 
 292             assert result.dataspace.toSet() == [FUNCTIONAL_TEST_DATASPACE_1].toSet()
 
 293         and: 'correct anchors are queried'
 
 294             assert result.anchorName.toSet() == [BOOKSTORE_ANCHOR_1, BOOKSTORE_ANCHOR_2].toSet()
 
 295         and: 'the correct number of nodes is returned'
 
 296             assert result.size() == expectedXpathsPerAnchor.size() * NUMBER_OF_ANCHORS_PER_DATASPACE_WITH_BOOKSTORE_JSON_DATA
 
 297         and: 'the queried nodes have expected xpaths'
 
 298             assert result.xpath.toSet() == expectedXpathsPerAnchor.toSet()
 
 299         where: 'the following data is used'
 
 300             scenario                                    | cpsPath                                               || expectedXpathsPerAnchor
 
 301             'container node'                            | '/bookstore'                                          || ["/bookstore"]
 
 302             'list node'                                 | '/bookstore/categories'                               || ["/bookstore/categories[@code='1']", "/bookstore/categories[@code='2']", "/bookstore/categories[@code='3']", "/bookstore/categories[@code='4']", "/bookstore/categories[@code='5']"]
 
 303             'integer leaf-condition'                    | '/bookstore/categories[@code="1"]/books[@price=15]'   || ["/bookstore/categories[@code='1']/books[@title='The Gruffalo']"]
 
 304             'multiple list-ancestors'                   | '//books/ancestor::categories'                        || ["/bookstore/categories[@code='1']", "/bookstore/categories[@code='2']", "/bookstore/categories[@code='3']", "/bookstore/categories[@code='4']", "/bookstore/categories[@code='5']"]
 
 305             'one ancestor with list value'              | '//books/ancestor::categories[@code="1"]'             || ["/bookstore/categories[@code='1']"]
 
 306             'list with index value in the xpath prefix' | '//categories[@code="1"]/books/ancestor::bookstore'   || ["/bookstore"]
 
 307             'ancestor with parent list'                 | '//books/ancestor::bookstore/categories'              || ["/bookstore/categories[@code='1']", "/bookstore/categories[@code='2']", "/bookstore/categories[@code='3']", "/bookstore/categories[@code='4']", "/bookstore/categories[@code='5']"]
 
 308             'ancestor with parent list element'         | '//books/ancestor::bookstore/categories[@code="2"]'   || ["/bookstore/categories[@code='2']"]
 
 309             'ancestor combined with text condition'     | '//books/title[text()="Matilda"]/ancestor::bookstore' || ["/bookstore"]
 
 312     def 'Cps Path query across anchors with #scenario descendants.'() {
 
 313         when: 'a query is executed to get a data node by the given cps path'
 
 314             def result = objectUnderTest.queryDataNodesAcrossAnchors(FUNCTIONAL_TEST_DATASPACE_1, '/bookstore', fetchDescendantsOption, NO_PAGINATION)
 
 315         then: 'the correct dataspace was queried'
 
 316             assert result.dataspace.toSet() == [FUNCTIONAL_TEST_DATASPACE_1].toSet()
 
 317         and: 'correct number of datanodes are returned'
 
 318             assert countDataNodesInTree(result) == expectedNumberOfNodesPerAnchor * NUMBER_OF_ANCHORS_PER_DATASPACE_WITH_BOOKSTORE_JSON_DATA
 
 319         where: 'the following data is used'
 
 320             scenario | fetchDescendantsOption  || expectedNumberOfNodesPerAnchor
 
 321             'no'     | OMIT_DESCENDANTS        || 1
 
 322             'direct' | DIRECT_CHILDREN_ONLY    || 7
 
 323             'all'    | INCLUDE_ALL_DESCENDANTS || 28
 
 326     def 'Cps Path query across anchors with ancestors and #scenario descendants.'() {
 
 327         when: 'a query is executed to get a data node by the given cps path'
 
 328             def result = objectUnderTest.queryDataNodesAcrossAnchors(FUNCTIONAL_TEST_DATASPACE_1, '//books/ancestor::bookstore', fetchDescendantsOption, NO_PAGINATION)
 
 329         then: 'the correct dataspace was queried'
 
 330             assert result.dataspace.toSet() == [FUNCTIONAL_TEST_DATASPACE_1].toSet()
 
 331         and: 'correct number of datanodes are returned'
 
 332             assert countDataNodesInTree(result) == expectedNumberOfNodesPerAnchor * NUMBER_OF_ANCHORS_PER_DATASPACE_WITH_BOOKSTORE_JSON_DATA
 
 333         where: 'the following data is used'
 
 334             scenario | fetchDescendantsOption  || expectedNumberOfNodesPerAnchor
 
 335             'no'     | OMIT_DESCENDANTS        || 1
 
 336             'direct' | DIRECT_CHILDREN_ONLY    || 7
 
 337             'all'    | INCLUDE_ALL_DESCENDANTS || 28
 
 340     def 'Cps Path query across anchors with syntax error throws a CPS Path Exception.'() {
 
 341         when: 'trying to execute a query with a syntax (parsing) error'
 
 342             objectUnderTest.queryDataNodesAcrossAnchors(FUNCTIONAL_TEST_DATASPACE_1, 'cpsPath that cannot be parsed' , OMIT_DESCENDANTS, NO_PAGINATION)
 
 343         then: 'a cps path exception is thrown'
 
 344             thrown(CpsPathException)
 
 347     def 'Cps Path querys with all descendants including descendants that are list entries: #scenario.'() {
 
 348         when: 'a query is executed to get a data node by the given cps path'
 
 349             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, INCLUDE_ALL_DESCENDANTS)
 
 350         then: 'correct number of datanodes are returned'
 
 351             assert countDataNodesInTree(result) == expectedNumberOfDataNodes
 
 353             scenario                              | cpsPath                                 || expectedNumberOfDataNodes
 
 354             'absolute path all list entries'      | '/bookstore/categories'                 || 24
 
 355             'absolute path 1 list entry by key'   | '/bookstore/categories[@code="3"]'      || 5
 
 356             'absolute path 1 list entry by name'  | '/bookstore/categories[@name="Comedy"]' || 5
 
 357             'relative path all list entries'      | '//categories'                          || 24
 
 358             'relative path 1 list entry by key'   | '//categories[@code="3"]'               || 5
 
 359             'relative path 1 list entry by leaf'  | '//categories[@name="Comedy"]'          || 5
 
 360             'incomplete absolute path'            | '/categories'                           || 0
 
 361             'incomplete absolute 1 list entry'    | '/categories[@code="3"]'                || 0
 
 364     def 'Cps Path query contains #wildcard.'() {
 
 365         when: 'a query is executed with a wildcard in the given cps path'
 
 366             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, INCLUDE_ALL_DESCENDANTS)
 
 367         then: 'no results are returned, as Cps Path query does not interpret wildcard characters'
 
 368             assert result.isEmpty()
 
 371             '  sql wildcard in parent path list index' | '/bookstore/categories[@code="%"]/books'
 
 372             'regex wildcard in parent path list index' | '/bookstore/categories[@code=".*"]/books'
 
 373             '  sql wildcard in leaf-condition'         | '/bookstore/categories[@code="1"]/books[@title="%"]'
 
 374             'regex wildcard in leaf-condition'         | '/bookstore/categories[@code="1"]/books[@title=".*"]'
 
 375             '  sql wildcard in text-condition'         | '/bookstore/categories[@code="1"]/books/title[text()="%"]'
 
 376             'regex wildcard in text-condition'         | '/bookstore/categories[@code="1"]/books/title[text()=".*"]'
 
 377             '  sql wildcard in contains-condition'     | '/bookstore/categories[@code="1"]/books[contains(@title, "%")]'
 
 378             'regex wildcard in contains-condition'     | '/bookstore/categories[@code="1"]/books[contains(@title, ".*")]'
 
 381     def 'Cps Path query can return a data node containing [@ in xpath #scenario.'() {
 
 382         given: 'a book with special characters [@ and ] in title'
 
 383             cpsDataService.saveData(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, "/bookstore/categories[@code='1']", '{"books": [ {"title":"[@hello=world]"} ] }', OffsetDateTime.now())
 
 384         when: 'a query is executed'
 
 385             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, OMIT_DESCENDANTS)
 
 386         then: 'the node is returned'
 
 387             assert result.size() == 1
 
 388         cleanup: 'the new datanode'
 
 389             cpsDataService.deleteDataNode(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, "/bookstore/categories[@code='1']/books[@title='[@hello=world]']", OffsetDateTime.now())
 
 392             'leaf-condition'     || "/bookstore/categories[@code='1']/books[@title='[@hello=world]']"
 
 393             'text-condition'     || "/bookstore/categories[@code='1']/books/title[text()='[@hello=world]']"
 
 394             'contains-condition' || "/bookstore/categories[@code='1']/books[contains(@title, '[@hello=world]')]"
 
 397     def 'Cps Path get and query can handle apostrophe inside #quotes.'() {
 
 398         given: 'a book with special characters in title'
 
 399             cpsDataService.saveData(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, "/bookstore/categories[@code='1']",
 
 400                     '{"books": [ {"title":"I\'m escaping"} ] }', OffsetDateTime.now())
 
 401         when: 'a query is executed'
 
 402             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, OMIT_DESCENDANTS)
 
 403         then: 'the node is returned'
 
 404             assert result.size() == 1
 
 405             assert result[0].xpath == "/bookstore/categories[@code='1']/books[@title='I''m escaping']"
 
 406         cleanup: 'the new datanode'
 
 407             cpsDataService.deleteDataNode(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, "/bookstore/categories[@code='1']/books[@title='I''m escaping']", OffsetDateTime.now())
 
 410             'single quotes'      || "/bookstore/categories[@code='1']/books[@title='I''m escaping']"
 
 411             'double quotes'      || '/bookstore/categories[@code="1"]/books[@title="I\'m escaping"]'
 
 412             'text-condition'     || "/bookstore/categories[@code='1']/books/title[text()='I''m escaping']"
 
 413             'contains-condition' || "/bookstore/categories[@code='1']/books[contains(@title, 'I''m escaping')]"
 
 416     def 'Cps Path query across anchors using pagination option with #scenario.'() {
 
 417         when: 'a query is executed to get a data nodes across anchors by the given CpsPath and pagination option'
 
 418             def result = objectUnderTest.queryDataNodesAcrossAnchors(FUNCTIONAL_TEST_DATASPACE_1, '/bookstore', OMIT_DESCENDANTS, new PaginationOption(pageIndex, pageSize))
 
 419         then: 'correct bookstore names are queried'
 
 420             def bookstoreNames = result.collect { it.getLeaves().get('bookstore-name') }
 
 421             assert bookstoreNames.toSet() == expectedBookstoreNames.toSet()
 
 422         and: 'the correct number of page size is returned'
 
 423             assert result.size() == expectedPageSize
 
 424         and: 'the queried nodes have expected anchor names'
 
 425             assert result.anchorName.toSet() == expectedAnchors.toSet()
 
 426         where: 'the following data is used'
 
 427             scenario                       | pageIndex | pageSize || expectedPageSize || expectedAnchors                          || expectedBookstoreNames
 
 428             '1st page with one anchor'     | 1         | 1        || 1                || [BOOKSTORE_ANCHOR_1]                     || ['Easons-1']
 
 429             '1st page with two anchor'     | 1         | 2        || 2                || [BOOKSTORE_ANCHOR_1, BOOKSTORE_ANCHOR_2] || ['Easons-1', 'Easons-2']
 
 430             '2nd page'                     | 2         | 1        || 1                || [BOOKSTORE_ANCHOR_2]                     || ['Easons-2']
 
 431             'no 2nd page due to page size' | 2         | 2        || 0                || []                                       || []
 
 434     def 'Cps Path query across anchors using pagination option for ancestor axis.'() {
 
 435         when: 'a query is executed to get a data nodes across anchors by the given CpsPath and pagination option'
 
 436             def result = objectUnderTest.queryDataNodesAcrossAnchors(FUNCTIONAL_TEST_DATASPACE_1, '//books/ancestor::categories', INCLUDE_ALL_DESCENDANTS, new PaginationOption(1, 2))
 
 437         then: 'correct category codes are queried'
 
 438             def categoryNames = result.collect { it.getLeaves().get('name') }
 
 439             assert categoryNames.toSet() == ['Discount books', 'Computing', 'Comedy', 'Thriller', 'Children'].toSet()
 
 440         and: 'the queried nodes have expected anchors'
 
 441             assert result.anchorName.toSet() == [BOOKSTORE_ANCHOR_1, BOOKSTORE_ANCHOR_2].toSet()
 
 444     def 'Count number of anchors for given dataspace name and cps path'() {
 
 445         expect: '/bookstore is present in two anchors'
 
 446             assert objectUnderTest.countAnchorsForDataspaceAndCpsPath(FUNCTIONAL_TEST_DATASPACE_1, '/bookstore') == 2
 
 449     def 'Cps Path query across anchors using no pagination'() {
 
 450         when: 'a query is executed to get a data nodes across anchors by the given CpsPath and pagination option'
 
 451             def result = objectUnderTest.queryDataNodesAcrossAnchors(FUNCTIONAL_TEST_DATASPACE_1, '/bookstore', OMIT_DESCENDANTS, NO_PAGINATION)
 
 452         then: 'all bookstore names are queried'
 
 453             def bookstoreNames = result.collect { it.getLeaves().get('bookstore-name') }
 
 454             assert bookstoreNames.toSet() == ['Easons-1', 'Easons-2'].toSet()
 
 455         and: 'the correct number of page size is returned'
 
 456             assert result.size() == 2
 
 457         and: 'the queried nodes have expected bookstore names'
 
 458             assert result.anchorName.toSet() == [BOOKSTORE_ANCHOR_1, BOOKSTORE_ANCHOR_2].toSet()
 
 461     def 'Query data nodes with a limit of #limit.' () {
 
 462         when: 'a query for data nodes is executed with a result limit'
 
 463             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories', OMIT_DESCENDANTS, limit)
 
 464         then: 'the expected number of nodes is returned'
 
 465             assert countDataNodesInTree(result) == expectedNumberOfResults
 
 466         where: 'the following parameters are used'
 
 467             limit || expectedNumberOfResults
 
 474     def 'Query data leaf with a limit of #limit.' () {
 
 475         when: 'a query for data leaf is executed with a result limit'
 
 476             def result = objectUnderTest.queryDataLeaf(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories/@name', limit, String)
 
 477         then: 'the expected number of leaf values is returned'
 
 478             assert result.size() == expectedNumberOfResults
 
 479         where: 'the following parameters are used'
 
 480             limit || expectedNumberOfResults