212686e917d0e80348f6f3f6e36feabff04cccfc
[cps.git] /
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2023-2025 OpenInfra Foundation Europe. All rights reserved.
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
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.integration.functional.cps
23
24 import static org.onap.cps.api.parameters.FetchDescendantsOption.DIRECT_CHILDREN_ONLY
25 import static org.onap.cps.api.parameters.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
26 import static org.onap.cps.api.parameters.FetchDescendantsOption.OMIT_DESCENDANTS
27 import static org.onap.cps.api.parameters.PaginationOption.NO_PAGINATION
28
29 import java.time.OffsetDateTime
30 import org.onap.cps.api.CpsQueryService
31 import org.onap.cps.integration.base.FunctionalSpecBase
32 import org.onap.cps.api.parameters.FetchDescendantsOption
33 import org.onap.cps.api.parameters.PaginationOption
34 import org.onap.cps.api.exceptions.CpsPathException
35
36 class QueryServiceIntegrationSpec extends FunctionalSpecBase {
37
38     CpsQueryService objectUnderTest
39
40     def setup() { objectUnderTest = cpsQueryService }
41
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
51                 })
52             })
53         where:
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                  | []
57     }
58
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
64         where:
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
69             'non-existing attribute'  | '//books/@non-existing'                       || 0
70     }
71
72     def 'Query data leaf with type #leafType using CPS path.'() {
73         given: 'a cps path query for two books, returning only #leafName'
74             def cpsPath = '//books[@title="Matilda" or @title="Good Omens"]/@' + leafName
75         when: 'query data leaf for bookstore container'
76             def results = objectUnderTest.queryDataLeaf(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, leafType)
77         then: 'the result contains the expected leaf values'
78             assert results == expectedResults as Set
79         where:
80             leafName    | leafType      || expectedResults
81             'lang'      | String.class  || ['English']
82             'price'     | Integer.class || [13, 20]
83             'editions'  | List.class    || [[1988, 2000], [2006]]
84     }
85
86     def 'Query data leaf using CPS path with ancestor axis.'() {
87         given: 'a cps path query that will return the names of the categories of two books'
88             def cpsPath = '//books[@title="Matilda" or @title="Good Omens"]/ancestor::categories/@name'
89         when: 'query data leaf for bookstore container'
90             def result = objectUnderTest.queryDataLeaf(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, String.class)
91         then: 'the result contains the expected leaf values'
92             assert result == ['Children', 'Comedy'] as Set
93     }
94
95     def 'Attempt to query data leaf without specifying leaf name gives an error.'() {
96         given: 'a cps path without an attribute axis'
97             def cpsPathWithoutAttributeAxis = '//books'
98         when: 'query data leaf is called without attribute axis in cps path'
99             objectUnderTest.queryDataLeaf(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPathWithoutAttributeAxis, String.class)
100         then: 'illegal argument exception is thrown'
101             thrown(IllegalArgumentException)
102     }
103
104     def 'Cps Path query using comparative and boolean operators.'() {
105         given: 'a cps path query in the discount category'
106             def cpsPath = "/bookstore/categories[@code='5']/books" + leafCondition
107         when: 'a query is executed to get response by the given cps path'
108             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1,
109                     cpsPath, OMIT_DESCENDANTS)
110         then: 'the cps-path of queryDataNodes has the expectedLeaves'
111             def bookPrices = result.collect { it.getLeaves().get('price') }
112             assert bookPrices.sort() == expectedBookPrices.sort()
113         where: 'the following data is used'
114             leafCondition                                 || expectedBookPrices
115             '[@price = 5]'                                || [5]
116             '[@price < 5]'                                || [1, 2, 3, 4]
117             '[@price > 5]'                                || [6, 7, 8, 9, 10]
118             '[@price <= 5]'                               || [1, 2, 3, 4, 5]
119             '[@price >= 5]'                               || [5, 6, 7, 8, 9, 10]
120             '[@price > 10]'                               || []
121             '[@price = 3 or @price = 7]'                  || [3, 7]
122             '[@price = 3 and @price = 7]'                 || []
123             '[@price > 3 and @price <= 6]'                || [4, 5, 6]
124             '[@price < 3 or @price > 8]'                  || [1, 2, 9, 10]
125             '[@price = 1 or @price = 3 or @price = 5]'    || [1, 3, 5]
126             '[@price = 1 or @price >= 8 and @price < 10]' || [1, 8, 9]
127             '[@price >= 3 and @price <= 5 or @price > 9]' || [3, 4, 5, 10]
128     }
129
130     def 'Cps Path query for leaf value(s) with #scenario.'() {
131         when: 'a query is executed to get a data node by the given cps path'
132             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, fetchDescendantsOption)
133         then: 'the correct number of parent nodes are returned'
134             assert result.size() == expectedNumberOfParentNodes
135         and: 'the correct total number of data nodes are returned'
136             assert countDataNodesInTree(result) == expectedTotalNumberOfNodes
137         where: 'the following data is used'
138             scenario                               | cpsPath                                                    | fetchDescendantsOption         || expectedNumberOfParentNodes | expectedTotalNumberOfNodes
139             'string and no descendants'            | '/bookstore/categories[@code="1"]/books[@title="Matilda"]' | OMIT_DESCENDANTS               || 1                           | 1
140             'integer and descendants'              | '/bookstore/categories[@code="1"]/books[@price=15]'        | INCLUDE_ALL_DESCENDANTS        || 1                           | 1
141             'no condition and no descendants'      | '/bookstore/categories'                                    | OMIT_DESCENDANTS               || 5                           | 5
142             'no condition and level 1 descendants' | '/bookstore'                                               | new FetchDescendantsOption(1)  || 1                           | 7
143             'no condition and level 2 descendants' | '/bookstore'                                               | new FetchDescendantsOption(2)  || 1                           | 28
144     }
145
146     def 'Query for attribute by cps path with cps paths that return no data because of #scenario.'() {
147         when: 'a query is executed to get data nodes for the given cps path'
148             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, OMIT_DESCENDANTS)
149         then: 'no data is returned'
150             assert result.isEmpty()
151         where: 'following cps queries are performed'
152             scenario                         | cpsPath
153             'cps path is incomplete'         | '/bookstore[@title="Matilda"]'
154             'leaf value does not exist'      | '/bookstore/categories[@code="1"]/books[@title=\'does not exist\']'
155             'incomplete end of xpath prefix' | '/bookstore/categories/books[@price=15]'
156     }
157
158     def 'Cps Path query using descendant anywhere and #type (further) descendants.'() {
159         when: 'a query is executed to get a data node by the given cps path'
160             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="1"]', fetchDescendantsOption)
161         then: 'the data node has the correct number of children'
162             assert result[0].childDataNodes.xpath.sort() == expectedChildNodes.sort()
163         where: 'the following data is used'
164             type      | fetchDescendantsOption   || expectedChildNodes
165             'omit'    | OMIT_DESCENDANTS         || []
166             'include' | INCLUDE_ALL_DESCENDANTS  || ["/bookstore/categories[@code='1']/books[@title='Matilda']",
167                                                      "/bookstore/categories[@code='1']/books[@title='The Gruffalo']"]
168     }
169
170     def 'Cps Path query for all books.'() {
171         when: 'a query is executed to get all books'
172             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '//books', OMIT_DESCENDANTS)
173         then: 'the expected number of books are returned'
174             assert result.size() == 19
175     }
176
177     def 'Cps Path query using descendant anywhere with #scenario.'() {
178         when: 'a query is executed to get a data node by the given cps path'
179             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, OMIT_DESCENDANTS)
180         then: 'xpaths of the retrieved data nodes are as expected'
181             def bookTitles = result.collect { it.getLeaves().get('title') }
182             assert bookTitles.sort() == expectedBookTitles.sort()
183         where: 'the following data is used'
184             scenario                                 | cpsPath                                     || expectedBookTitles
185             'string leaf condition'                  | '//books[@title="Matilda"]'                 || ["Matilda"]
186             'text condition on leaf'                 | '//books/title[text()="Matilda"]'           || ["Matilda"]
187             'text condition case mismatch'           | '//books/title[text()="matilda"]'           || []
188             'text condition on int leaf'             | '//books/price[text()="20"]'                || ["A Book with No Language", "Matilda"]
189             'text condition on leaf-list'            | '//books/authors[text()="Terry Pratchett"]' || ["Good Omens", "The Colour of Magic", "The Light Fantastic"]
190             'text condition partial match'           | '//books/authors[text()="Terry"]'           || []
191             'text condition (existing) empty string' | '//books/lang[text()=""]'                   || ["A Book with No Language"]
192             'text condition on int leaf-list'        | '//books/editions[text()="2000"]'           || ["Matilda"]
193             'match of leaf containing /'             | '//books[@lang="N/A"]'                      || ["Logarithm tables"]
194             'text condition on leaf containing /'    | '//books/lang[text()="N/A"]'                || ["Logarithm tables"]
195             'match of key containing /'              | '//books[@title="Debian GNU/Linux"]'        || ["Debian GNU/Linux"]
196             'text condition on key containing /'     | '//books/title[text()="Debian GNU/Linux"]'  || ["Debian GNU/Linux"]
197     }
198
199     def 'Query for attribute by cps path using contains condition #scenario.'() {
200         when: 'a query is executed to get response by the given cps path'
201             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, INCLUDE_ALL_DESCENDANTS)
202         then: 'xpaths of the retrieved data nodes are as expected'
203             def bookTitles = result.collect { it.getLeaves().get('title') }
204             assert bookTitles.sort() == expectedBookTitles.sort()
205         where: 'the following data is used'
206             scenario                                 | cpsPath                           || expectedBookTitles
207             'contains condition with leaf'           | '//books[contains(@title,"Mat")]' || ["Matilda"]
208             'contains condition with case-sensitive' | '//books[contains(@title,"Ti")]'  || []
209             'contains condition with Integer Value'  | '//books[contains(@price,"15")]'  || ["Annihilation", "The Gruffalo"]
210     }
211
212     def 'Query for attribute by cps path using contains condition with no value.'() {
213         when: 'a query is executed to get response by the given cps path'
214             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '//books[contains(@title,"")]', OMIT_DESCENDANTS)
215         then: 'all books are returned'
216             assert result.size() == 19
217     }
218
219     def 'Cps Path query using descendant anywhere with #scenario condition for a container element.'() {
220         when: 'a query is executed to get a data node by the given cps path'
221             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, OMIT_DESCENDANTS)
222         then: 'book titles from the retrieved data nodes are as expected'
223             def bookTitles = result.collect { it.getLeaves().get('title') }
224             assert bookTitles.sort() == expectedBookTitles.sort()
225         where: 'the following data is used'
226             scenario                                                   | cpsPath                                                                || expectedBookTitles
227             'one leaf'                                                 | '//books[@price=14]'                                                   || ['The Light Fantastic']
228             'one leaf with ">" condition'                              | '//books[@price>14]'                                                   || ['A Book with No Language', 'Annihilation', 'Debian GNU/Linux', 'Matilda', 'The Gruffalo']
229             'one text'                                                 | '//books/authors[text()="Terry Pratchett"]'                            || ['Good Omens', 'The Colour of Magic', 'The Light Fantastic']
230             'more than one leaf'                                       | '//books[@price=12 and @lang="English"]'                               || ['The Colour of Magic']
231             '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']
232             'more than one leaf has "OR" condition with non-json data' | '//books[@title="xyz" or @price=13]'                                   || ['Good Omens']
233             'more than one leaf has multiple AND'                      | '//books[@lang="English" and @price=13 and @edition=1983]'             || []
234             'more than one leaf has multiple OR'                       | '//books[ @title="Matilda" or @price=15 or @edition=2006]'             || ['Annihilation', 'Matilda', 'The Gruffalo']
235             'leaves reversed in order'                                 | '//books[@lang="English" and @price=12]'                               || ['The Colour of Magic']
236             'more than one leaf has combination of AND/OR'             | '//books[@edition=1983 and @price=13 or @title="Good Omens"]'          || ['Good Omens']
237             'more than one leaf has OR/AND'                            | '//books[@title="The Light Fantastic" or @price=11 and @edition=1983]' || ['The Light Fantastic']
238             'leaf and text'                                            | '//books[@price=14]/authors[text()="Terry Pratchett"]'                 || ['The Light Fantastic']
239             'leaf and contains'                                        | '//books[contains(@price,"13")]'                                       || ['Good Omens']
240     }
241
242     def 'Cps Path query using descendant anywhere with #scenario condition(s) for a list element.'() {
243         when: 'a query is executed to get a data node by the given cps path'
244             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, INCLUDE_ALL_DESCENDANTS)
245         then: 'xpaths of the retrieved data nodes are as expected'
246             result.xpath.toList() == ["/bookstore/premises/addresses[@house-number='2' and @street='Main Street']"]
247         where: 'the following data is used'
248             scenario                              | cpsPath
249             'full composite key'                  | '//addresses[@house-number=2 and @street="Main Street"]'
250             'one partial key leaf'                | '//addresses[@house-number=2]'
251             'one non key leaf'                    | '//addresses[@county="Kildare"]'
252             'mix of partial key and non key leaf' | '//addresses[@street="Main Street" and @county="Kildare"]'
253     }
254
255     def 'Query for attribute by cps path of type ancestor with #scenario.'() {
256         when: 'the given cps path is parsed'
257             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, OMIT_DESCENDANTS)
258         then: 'the xpaths of the retrieved data nodes are as expected'
259             assert result.xpath.sort() == expectedXPaths.sort()
260         where: 'the following data is used'
261             scenario                                    | cpsPath                                               || expectedXPaths
262             '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']"]
263             'one ancestor with list value'              | '//books/ancestor::categories[@code="1"]'             || ["/bookstore/categories[@code='1']"]
264             'top ancestor'                              | '//books/ancestor::bookstore'                         || ["/bookstore"]
265             'list with index value in the xpath prefix' | '//categories[@code="1"]/books/ancestor::bookstore'   || ["/bookstore"]
266             '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']"]
267             'ancestor with parent'                      | '//books/ancestor::bookstore/categories[@code="2"]'   || ["/bookstore/categories[@code='2']"]
268             'ancestor combined with text condition'     | '//books/title[text()="Matilda"]/ancestor::bookstore' || ["/bookstore"]
269             'ancestor with parent that does not exist'  | '//books/ancestor::parentDoesNoExist/categories'      || []
270             'ancestor does not exist'                   | '//books/ancestor::ancestorDoesNotExist'              || []
271             'ancestor combined with contains condition' | '//books[contains(@title,"Mat")]/ancestor::bookstore' || ["/bookstore"]
272             'ancestor is the same as search target'     | '//books/ancestor::books'                             || []
273     }
274
275     def 'Query for attribute by cps path of type ancestor with #scenario descendants.'() {
276         when: 'the given cps path is parsed'
277             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '//books/ancestor::bookstore', fetchDescendantsOption)
278         then: 'the xpaths of the retrieved data nodes are as expected'
279             assert countDataNodesInTree(result) == expectedNumberOfNodes
280         where: 'the following data is used'
281             scenario | fetchDescendantsOption  || expectedNumberOfNodes
282             'no'     | OMIT_DESCENDANTS        || 1
283             'direct' | DIRECT_CHILDREN_ONLY    || 7
284             'all'    | INCLUDE_ALL_DESCENDANTS || 28
285     }
286
287     def 'Cps Path query with #scenario throws a CPS Path Exception.'() {
288         when: 'trying to execute a query with a syntax (parsing) error'
289             objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, OMIT_DESCENDANTS)
290         then: 'a cps path exception is thrown'
291             thrown(CpsPathException)
292         where: 'the following data is used'
293             scenario                           | cpsPath
294             'cpsPath that cannot be parsed'    | 'cpsPath that cannot be parsed'
295             'String with comparative operator' | '//books[@lang>"German" and @price>10]'
296     }
297
298     def 'Cps Path query across anchors with #scenario.'() {
299         when: 'a query is executed to get a data nodes across anchors by the given CpsPath'
300             def result = objectUnderTest.queryDataNodesAcrossAnchors(FUNCTIONAL_TEST_DATASPACE_1, cpsPath, OMIT_DESCENDANTS, NO_PAGINATION)
301         then: 'the correct dataspace is queried'
302             assert result.dataspace.toSet() == [FUNCTIONAL_TEST_DATASPACE_1].toSet()
303         and: 'correct anchors are queried'
304             assert result.anchorName.toSet() == [BOOKSTORE_ANCHOR_1, BOOKSTORE_ANCHOR_2].toSet()
305         and: 'the correct number of nodes is returned'
306             assert result.size() == expectedXpathsPerAnchor.size() * NUMBER_OF_ANCHORS_PER_DATASPACE_WITH_BOOKSTORE_JSON_DATA
307         and: 'the queried nodes have expected xpaths'
308             assert result.xpath.toSet() == expectedXpathsPerAnchor.toSet()
309         where: 'the following data is used'
310             scenario                                    | cpsPath                                               || expectedXpathsPerAnchor
311             'container node'                            | '/bookstore'                                          || ["/bookstore"]
312             'list node'                                 | '/bookstore/categories'                               || ["/bookstore/categories[@code='1']", "/bookstore/categories[@code='2']", "/bookstore/categories[@code='3']", "/bookstore/categories[@code='4']", "/bookstore/categories[@code='5']"]
313             'integer leaf-condition'                    | '/bookstore/categories[@code="1"]/books[@price=15]'   || ["/bookstore/categories[@code='1']/books[@title='The Gruffalo']"]
314             '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']"]
315             'one ancestor with list value'              | '//books/ancestor::categories[@code="1"]'             || ["/bookstore/categories[@code='1']"]
316             'list with index value in the xpath prefix' | '//categories[@code="1"]/books/ancestor::bookstore'   || ["/bookstore"]
317             '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']"]
318             'ancestor with parent list element'         | '//books/ancestor::bookstore/categories[@code="2"]'   || ["/bookstore/categories[@code='2']"]
319             'ancestor combined with text condition'     | '//books/title[text()="Matilda"]/ancestor::bookstore' || ["/bookstore"]
320     }
321
322     def 'Cps Path query across anchors with #scenario descendants.'() {
323         when: 'a query is executed to get a data node by the given cps path'
324             def result = objectUnderTest.queryDataNodesAcrossAnchors(FUNCTIONAL_TEST_DATASPACE_1, '/bookstore', fetchDescendantsOption, NO_PAGINATION)
325         then: 'the correct dataspace was queried'
326             assert result.dataspace.toSet() == [FUNCTIONAL_TEST_DATASPACE_1].toSet()
327         and: 'correct number of datanodes are returned'
328             assert countDataNodesInTree(result) == expectedNumberOfNodesPerAnchor * NUMBER_OF_ANCHORS_PER_DATASPACE_WITH_BOOKSTORE_JSON_DATA
329         where: 'the following data is used'
330             scenario | fetchDescendantsOption  || expectedNumberOfNodesPerAnchor
331             'no'     | OMIT_DESCENDANTS        || 1
332             'direct' | DIRECT_CHILDREN_ONLY    || 7
333             'all'    | INCLUDE_ALL_DESCENDANTS || 28
334     }
335
336     def 'Cps Path query across anchors with ancestors and #scenario descendants.'() {
337         when: 'a query is executed to get a data node by the given cps path'
338             def result = objectUnderTest.queryDataNodesAcrossAnchors(FUNCTIONAL_TEST_DATASPACE_1, '//books/ancestor::bookstore', fetchDescendantsOption, NO_PAGINATION)
339         then: 'the correct dataspace was queried'
340             assert result.dataspace.toSet() == [FUNCTIONAL_TEST_DATASPACE_1].toSet()
341         and: 'correct number of datanodes are returned'
342             assert countDataNodesInTree(result) == expectedNumberOfNodesPerAnchor * NUMBER_OF_ANCHORS_PER_DATASPACE_WITH_BOOKSTORE_JSON_DATA
343         where: 'the following data is used'
344             scenario | fetchDescendantsOption  || expectedNumberOfNodesPerAnchor
345             'no'     | OMIT_DESCENDANTS        || 1
346             'direct' | DIRECT_CHILDREN_ONLY    || 7
347             'all'    | INCLUDE_ALL_DESCENDANTS || 28
348     }
349
350     def 'Cps Path query across anchors with syntax error throws a CPS Path Exception.'() {
351         when: 'trying to execute a query with a syntax (parsing) error'
352             objectUnderTest.queryDataNodesAcrossAnchors(FUNCTIONAL_TEST_DATASPACE_1, 'cpsPath that cannot be parsed' , OMIT_DESCENDANTS, NO_PAGINATION)
353         then: 'a cps path exception is thrown'
354             thrown(CpsPathException)
355     }
356
357     def 'Cps Path querys with all descendants including descendants that are list entries: #scenario.'() {
358         when: 'a query is executed to get a data node by the given cps path'
359             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, INCLUDE_ALL_DESCENDANTS)
360         then: 'correct number of datanodes are returned'
361             assert countDataNodesInTree(result) == expectedNumberOfDataNodes
362         where:
363             scenario                              | cpsPath                                 || expectedNumberOfDataNodes
364             'absolute path all list entries'      | '/bookstore/categories'                 || 24
365             'absolute path 1 list entry by key'   | '/bookstore/categories[@code="3"]'      || 5
366             'absolute path 1 list entry by name'  | '/bookstore/categories[@name="Comedy"]' || 5
367             'relative path all list entries'      | '//categories'                          || 24
368             'relative path 1 list entry by key'   | '//categories[@code="3"]'               || 5
369             'relative path 1 list entry by leaf'  | '//categories[@name="Comedy"]'          || 5
370             'incomplete absolute path'            | '/categories'                           || 0
371             'incomplete absolute 1 list entry'    | '/categories[@code="3"]'                || 0
372     }
373
374     def 'Cps Path query contains #wildcard.'() {
375         when: 'a query is executed with a wildcard in the given cps path'
376             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, INCLUDE_ALL_DESCENDANTS)
377         then: 'no results are returned, as Cps Path query does not interpret wildcard characters'
378             assert result.isEmpty()
379         where:
380             wildcard                                   | cpsPath
381             '  sql wildcard in parent path list index' | '/bookstore/categories[@code="%"]/books'
382             'regex wildcard in parent path list index' | '/bookstore/categories[@code=".*"]/books'
383             '  sql wildcard in leaf-condition'         | '/bookstore/categories[@code="1"]/books[@title="%"]'
384             'regex wildcard in leaf-condition'         | '/bookstore/categories[@code="1"]/books[@title=".*"]'
385             '  sql wildcard in text-condition'         | '/bookstore/categories[@code="1"]/books/title[text()="%"]'
386             'regex wildcard in text-condition'         | '/bookstore/categories[@code="1"]/books/title[text()=".*"]'
387             '  sql wildcard in contains-condition'     | '/bookstore/categories[@code="1"]/books[contains(@title, "%")]'
388             'regex wildcard in contains-condition'     | '/bookstore/categories[@code="1"]/books[contains(@title, ".*")]'
389     }
390
391     def 'Cps Path query can return a data node containing [@ in xpath #scenario.'() {
392         given: 'a book with special characters [@ and ] in title'
393             cpsDataService.saveData(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, "/bookstore/categories[@code='1']", '{"books": [ {"title":"[@hello=world]"} ] }', OffsetDateTime.now())
394         when: 'a query is executed'
395             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, OMIT_DESCENDANTS)
396         then: 'the node is returned'
397             assert result.size() == 1
398         cleanup: 'the new datanode'
399             cpsDataService.deleteDataNode(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, "/bookstore/categories[@code='1']/books[@title='[@hello=world]']", OffsetDateTime.now())
400         where:
401             scenario             || cpsPath
402             'leaf-condition'     || "/bookstore/categories[@code='1']/books[@title='[@hello=world]']"
403             'text-condition'     || "/bookstore/categories[@code='1']/books/title[text()='[@hello=world]']"
404             'contains-condition' || "/bookstore/categories[@code='1']/books[contains(@title, '[@hello=world]')]"
405     }
406
407     def 'Cps Path get and query can handle apostrophe inside #quotes.'() {
408         given: 'a book with special characters in title'
409             cpsDataService.saveData(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, "/bookstore/categories[@code='1']",
410                     '{"books": [ {"title":"I\'m escaping"} ] }', OffsetDateTime.now())
411         when: 'a query is executed'
412             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, OMIT_DESCENDANTS)
413         then: 'the node is returned'
414             assert result.size() == 1
415             assert result[0].xpath == "/bookstore/categories[@code='1']/books[@title='I''m escaping']"
416         cleanup: 'the new datanode'
417             cpsDataService.deleteDataNode(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, "/bookstore/categories[@code='1']/books[@title='I''m escaping']", OffsetDateTime.now())
418         where:
419             quotes               || cpsPath
420             'single quotes'      || "/bookstore/categories[@code='1']/books[@title='I''m escaping']"
421             'double quotes'      || '/bookstore/categories[@code="1"]/books[@title="I\'m escaping"]'
422             'text-condition'     || "/bookstore/categories[@code='1']/books/title[text()='I''m escaping']"
423             'contains-condition' || "/bookstore/categories[@code='1']/books[contains(@title, 'I''m escaping')]"
424     }
425
426     def 'Cps Path query across anchors using pagination option with #scenario.'() {
427         when: 'a query is executed to get a data nodes across anchors by the given CpsPath and pagination option'
428             def result = objectUnderTest.queryDataNodesAcrossAnchors(FUNCTIONAL_TEST_DATASPACE_1, '/bookstore', OMIT_DESCENDANTS, new PaginationOption(pageIndex, pageSize))
429         then: 'correct bookstore names are queried'
430             def bookstoreNames = result.collect { it.getLeaves().get('bookstore-name') }
431             assert bookstoreNames.toSet() == expectedBookstoreNames.toSet()
432         and: 'the correct number of page size is returned'
433             assert result.size() == expectedPageSize
434         and: 'the queried nodes have expected anchor names'
435             assert result.anchorName.toSet() == expectedAnchors.toSet()
436         where: 'the following data is used'
437             scenario                       | pageIndex | pageSize || expectedPageSize || expectedAnchors                          || expectedBookstoreNames
438             '1st page with one anchor'     | 1         | 1        || 1                || [BOOKSTORE_ANCHOR_1]                     || ['Easons-1']
439             '1st page with two anchor'     | 1         | 2        || 2                || [BOOKSTORE_ANCHOR_1, BOOKSTORE_ANCHOR_2] || ['Easons-1', 'Easons-2']
440             '2nd page'                     | 2         | 1        || 1                || [BOOKSTORE_ANCHOR_2]                     || ['Easons-2']
441             'no 2nd page due to page size' | 2         | 2        || 0                || []                                       || []
442     }
443
444     def 'Cps Path query across anchors using pagination option for ancestor axis.'() {
445         when: 'a query is executed to get a data nodes across anchors by the given CpsPath and pagination option'
446             def result = objectUnderTest.queryDataNodesAcrossAnchors(FUNCTIONAL_TEST_DATASPACE_1, '//books/ancestor::categories', INCLUDE_ALL_DESCENDANTS, new PaginationOption(1, 2))
447         then: 'correct category codes are queried'
448             def categoryNames = result.collect { it.getLeaves().get('name') }
449             assert categoryNames.toSet() == ['Discount books', 'Computing', 'Comedy', 'Thriller', 'Children'].toSet()
450         and: 'the queried nodes have expected anchors'
451             assert result.anchorName.toSet() == [BOOKSTORE_ANCHOR_1, BOOKSTORE_ANCHOR_2].toSet()
452     }
453
454     def 'Count number of anchors for given dataspace name and cps path'() {
455         expect: '/bookstore is present in two anchors'
456             assert objectUnderTest.countAnchorsForDataspaceAndCpsPath(FUNCTIONAL_TEST_DATASPACE_1, '/bookstore') == 2
457     }
458
459     def 'Cps Path query across anchors using no pagination'() {
460         when: 'a query is executed to get a data nodes across anchors by the given CpsPath and pagination option'
461             def result = objectUnderTest.queryDataNodesAcrossAnchors(FUNCTIONAL_TEST_DATASPACE_1, '/bookstore', OMIT_DESCENDANTS, NO_PAGINATION)
462         then: 'all bookstore names are queried'
463             def bookstoreNames = result.collect { it.getLeaves().get('bookstore-name') }
464             assert bookstoreNames.toSet() == ['Easons-1', 'Easons-2'].toSet()
465         and: 'the correct number of page size is returned'
466             assert result.size() == 2
467         and: 'the queried nodes have expected bookstore names'
468             assert result.anchorName.toSet() == [BOOKSTORE_ANCHOR_1, BOOKSTORE_ANCHOR_2].toSet()
469     }
470
471     def 'Query data nodes with a limit of #limit.' () {
472         when: 'a query for data nodes is executed with a result limit'
473             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories', OMIT_DESCENDANTS, limit)
474         then: 'the expected number of nodes is returned'
475             assert countDataNodesInTree(result) == expectedNumberOfResults
476         where: 'the following parameters are used'
477             limit || expectedNumberOfResults
478             1     || 1
479             2     || 2
480             0     || 5
481             -1    || 5
482     }
483
484     def 'Query data leaf with a limit of #limit.' () {
485         when: 'a query for data leaf is executed with a result limit'
486             def result = objectUnderTest.queryDataLeaf(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories/@name', limit, String)
487         then: 'the expected number of leaf values is returned'
488             assert result.size() == expectedNumberOfResults
489         where: 'the following parameters are used'
490             limit || expectedNumberOfResults
491             1     || 1
492             2     || 2
493             0     || 5
494             -1    || 5
495     }
496 }