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