Prepare for next CPS-NCMP release
[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.exceptions.CpsPathException
29
30 import static org.onap.cps.spi.FetchDescendantsOption.DIRECT_CHILDREN_ONLY
31 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
32 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
33
34 class CpsQueryServiceIntegrationSpec extends FunctionalSpecBase {
35
36     CpsQueryService objectUnderTest
37
38     def setup() { objectUnderTest = cpsQueryService }
39
40     def 'Query bookstore using CPS path where #scenario.'() {
41         when: 'query data nodes for bookstore container'
42             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, INCLUDE_ALL_DESCENDANTS)
43         then: 'the result contains expected number of nodes'
44             assert result.size() == expectedResultSize
45         and: 'the result contains the expected leaf values'
46             result.leaves.forEach( dataNodeLeaves -> {
47                 expectedLeaves.forEach( (expectedLeafKey,expectedLeafValue) -> {
48                     assert dataNodeLeaves[expectedLeafKey] == expectedLeafValue
49                 })
50             })
51         where:
52             scenario                                      | cpsPath                                    || expectedResultSize | expectedLeaves
53             'the AND condition is used'                   | '//books[@lang="English" and @price=15]'   || 2                  | [lang:"English", price:15]
54             'the AND is used where result does not exist' | '//books[@lang="English" and @price=1000]' || 0                  | []
55     }
56
57     def 'Cps Path query using combinations of OR operator #scenario.'() {
58         when: 'a query is executed to get response by the given cps path'
59             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, OMIT_DESCENDANTS)
60         then: 'the result contains expected number of nodes'
61             assert result.size() == expectedResultSize
62         and: 'the cps-path of queryDataNodes has the expectedLeaves'
63             assert result.leaves.sort() == expectedLeaves.sort()
64         where: 'the following data is used'
65             scenario                                | cpsPath                                                          || expectedResultSize | expectedLeaves
66             'the "OR" condition'                    | '//books[@lang="English" or @price=15]'                          || 6                  | [[lang: "English", price: 15, title: "Annihilation", authors: ["Jeff VanderMeer"], editions: [2014]],
67                                                                                                                                                 [lang: "English", price: 15, title: "The Gruffalo", authors: ["Julia Donaldson"], editions: [1999]],
68                                                                                                                                                 [lang: "English", price: 14, title: "The Light Fantastic", authors: ["Terry Pratchett"], editions: [1986]],
69                                                                                                                                                 [lang: "English", price: 13, title: "Good Omens", authors: ["Terry Pratchett", "Neil Gaiman"], editions: [2006]],
70                                                                                                                                                 [lang: "English", price: 12, title: "The Colour of Magic", authors: ["Terry Pratchett"], editions: [1983]],
71                                                                                                                                                 [lang: "English", price: 10, title: "Matilda", authors: ["Roald Dahl"], editions: [1988, 2000]]]
72             'the "OR" condition with non-json data' | '//books[@title="xyz" or @price=15]'                             || 2                  | [[lang: "English", price: 15, title: "Annihilation", authors: ["Jeff VanderMeer"], editions: [2014]],
73                                                                                                                                                 [lang: "English", price: 15, title: "The Gruffalo", authors: ["Julia Donaldson"], editions: [1999]]]
74             'combination of multiple AND'           | '//books[@lang="English" and @price=15 and @edition=1983]'       || 0                  | []
75             'combination of multiple OR'            | '//books[ @title="Matilda" or @price=15 or @edition=1983]'       || 3                  | [[lang: "English", price: 15, title: "Annihilation", authors: ["Jeff VanderMeer"], editions: [2014]],
76                                                                                                                                                 [lang: "English", price: 10, title: "Matilda", authors: ["Roald Dahl"], editions: [1988, 2000]],
77                                                                                                                                                 [lang: "English", price: 15, title: "The Gruffalo", authors: ["Julia Donaldson"], editions: [1999]]]
78             'combination of AND/OR'                 | '//books[@edition=1983 and @price=15 or @title="Good Omens"]'    || 1                  | [[lang: "English", price: 13, title: "Good Omens", authors: ["Terry Pratchett", "Neil Gaiman"], editions: [2006]]]
79             'combination of OR/AND'                 | '//books[@title="Annihilation" or @price=39 and @lang="arabic"]' || 1                  | [[lang: "English", price: 15, title: "Annihilation", authors: ["Jeff VanderMeer"], editions: [2014]]]
80     }
81
82     def 'cps-path query using combinations of Comparative Operators #scenario.'() {
83         when: 'a query is executed to get response by the given cpsPath'
84             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, OMIT_DESCENDANTS)
85         then: 'the result contains expected number of nodes'
86             assert result.size() == expectedResultSize
87         and: 'xpaths of the retrieved data nodes are as expected'
88             def bookTitles = result.collect { it.getLeaves().get('title') }
89             assert bookTitles.sort() == expectedBookTitles.sort()
90         where: 'the following data is used'
91             scenario                                         | cpsPath                                                            || expectedResultSize | expectedBookTitles
92             'the ">" condition'                              | '//books[@price>13 ]'                                              || 5                  | ['A Book with No Language', 'Annihilation', 'Debian GNU/Linux', 'The Gruffalo', 'The Light Fantastic']
93             'the "<" condition '                             | '//books[@price<15]'                                               || 5                  | ['Good Omens', 'Logarithm tables', 'Matilda', 'The Colour of Magic', 'The Light Fantastic']
94             'the "<=" condition'                             | '//books[@price<=15]'                                              || 7                  | ['Annihilation', 'Good Omens', 'Logarithm tables', 'Matilda', 'The Colour of Magic', 'The Gruffalo', 'The Light Fantastic']
95             'the ">=" condition'                             | '//books[@price>=20]'                                              || 2                  | ['A Book with No Language', 'Debian GNU/Linux']
96             'the "<" condition  where result does not exist' | '//books[@price<5]'                                                || 0                  | []
97             'the ">" condition  where result does not exist' | '//books[@price>1000]'                                             || 0                  | []
98             'the ">" condition with AND condition'           | '//books[@price>13 and @title="A Book with No Language"]'          || 1                  | ['A Book with No Language']
99             'the "<" condition with OR condition'            | '//books[@price<10 or @lang="German"]'                             || 1                  | ['Debian GNU/Linux']
100             'the "<=" condition with AND/OR condition'       | '//books[@price<=15 and @title="Annihilation" or @lang="Spanish"]' || 1                  | ['Annihilation']
101             'the ">=" condition with OR/AND condition'       | '//books[@price>=13 or @lang="Spanish" and @title="Good Omens"]'   || 6                  | ['A Book with No Language', 'Annihilation', 'Good Omens', 'Debian GNU/Linux', 'The Gruffalo', 'The Light Fantastic']
102             'Mix of integer and string condition '           | '//books[@lang="German" and @price>38]'                            || 1                  | ['Debian GNU/Linux']
103     }
104
105     def 'Cps Path query for leaf value(s) with #scenario.'() {
106         when: 'a query is executed to get a data node by the given cps path'
107             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, fetchDescendantsOption)
108         then: 'the correct number of parent nodes are returned'
109             assert result.size() == expectedNumberOfParentNodes
110         and: 'the correct total number of data nodes are returned'
111             assert countDataNodesInTree(result) == expectedTotalNumberOfNodes
112         where: 'the following data is used'
113             scenario                               | cpsPath                                                    | fetchDescendantsOption         || expectedNumberOfParentNodes | expectedTotalNumberOfNodes
114             'string and no descendants'            | '/bookstore/categories[@code="1"]/books[@title="Matilda"]' | OMIT_DESCENDANTS               || 1                           | 1
115             'integer and descendants'              | '/bookstore/categories[@code="1"]/books[@price=15]'        | INCLUDE_ALL_DESCENDANTS        || 1                           | 1
116             'no condition and no descendants'      | '/bookstore/categories'                                    | OMIT_DESCENDANTS               || 4                           | 4
117             'no condition and level 1 descendants' | '/bookstore'                                               | new FetchDescendantsOption(1)  || 1                           | 6
118             'no condition and level 2 descendants' | '/bookstore'                                               | new FetchDescendantsOption(2)  || 1                           | 17
119     }
120
121     def 'Query for attribute by cps path with cps paths that return no data because of #scenario.'() {
122         when: 'a query is executed to get data nodes for the given cps path'
123             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, OMIT_DESCENDANTS)
124         then: 'no data is returned'
125             assert result.isEmpty()
126         where: 'following cps queries are performed'
127             scenario                         | cpsPath
128             'cps path is incomplete'         | '/bookstore[@title="Matilda"]'
129             'leaf value does not exist'      | '/bookstore/categories[@code="1"]/books[@title=\'does not exist\']'
130             'incomplete end of xpath prefix' | '/bookstore/categories/books[@price=15]'
131     }
132
133     def 'Cps Path query using descendant anywhere and #type (further) descendants.'() {
134         when: 'a query is executed to get a data node by the given cps path'
135             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="1"]', fetchDescendantsOption)
136         then: 'the data node has the correct number of children'
137             assert result[0].childDataNodes.xpath.sort() == expectedChildNodes.sort()
138         where: 'the following data is used'
139             type      | fetchDescendantsOption   || expectedChildNodes
140             'omit'    | OMIT_DESCENDANTS         || []
141             'include' | INCLUDE_ALL_DESCENDANTS  || ["/bookstore/categories[@code='1']/books[@title='Matilda']",
142                                                      "/bookstore/categories[@code='1']/books[@title='The Gruffalo']"]
143     }
144
145     def 'Cps Path query for all books.'() {
146         when: 'a query is executed to get all books'
147             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '//books', OMIT_DESCENDANTS)
148         then: 'the expected number of books are returned'
149             assert result.size() == 9
150     }
151
152     def 'Cps Path query using descendant anywhere with #scenario.'() {
153         when: 'a query is executed to get a data node by the given cps path'
154             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, OMIT_DESCENDANTS)
155         then: 'xpaths of the retrieved data nodes are as expected'
156             def bookTitles = result.collect { it.getLeaves().get('title') }
157             assert bookTitles.sort() == expectedBookTitles.sort()
158         where: 'the following data is used'
159             scenario                                 | cpsPath                                     || expectedBookTitles
160             'string leaf condition'                  | '//books[@title="Matilda"]'                 || ["Matilda"]
161             'text condition on leaf'                 | '//books/title[text()="Matilda"]'           || ["Matilda"]
162             'text condition case mismatch'           | '//books/title[text()="matilda"]'           || []
163             'text condition on int leaf'             | '//books/price[text()="10"]'                || ["Matilda"]
164             'text condition on leaf-list'            | '//books/authors[text()="Terry Pratchett"]' || ["Good Omens", "The Colour of Magic", "The Light Fantastic"]
165             'text condition partial match'           | '//books/authors[text()="Terry"]'           || []
166             'text condition (existing) empty string' | '//books/lang[text()=""]'                   || ["A Book with No Language"]
167             'text condition on int leaf-list'        | '//books/editions[text()="2000"]'           || ["Matilda"]
168             'match of leaf containing /'             | '//books[@lang="N/A"]'                      || ["Logarithm tables"]
169             'text condition on leaf containing /'    | '//books/lang[text()="N/A"]'                || ["Logarithm tables"]
170             'match of key containing /'              | '//books[@title="Debian GNU/Linux"]'        || ["Debian GNU/Linux"]
171             'text condition on key containing /'     | '//books/title[text()="Debian GNU/Linux"]'  || ["Debian GNU/Linux"]
172     }
173
174     def 'Query for attribute by cps path using contains condition #scenario.'() {
175         when: 'a query is executed to get response by the given cps path'
176             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, INCLUDE_ALL_DESCENDANTS)
177         then: 'xpaths of 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             'contains condition with leaf'           | '//books[contains(@title,"Mat")]' || ["Matilda"]
183             'contains condition with case-sensitive' | '//books[contains(@title,"Ti")]'  || []
184             'contains condition with Integer Value'  | '//books[contains(@price,"15")]'  || ["Annihilation", "The Gruffalo"]
185             'contains condition with No-value'       | '//books[contains(@title,"")]'    || ["A Book with No Language", "Annihilation", "Debian GNU/Linux", "Good Omens", "Logarithm tables", "Matilda", "The Colour of Magic", "The Gruffalo", "The Light Fantastic"]
186     }
187
188     def 'Cps Path query using descendant anywhere with #scenario condition for a container element.'() {
189         when: 'a query is executed to get a data node by the given cps path'
190             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, OMIT_DESCENDANTS)
191         then: 'book titles from the retrieved data nodes are as expected'
192             def bookTitles = result.collect { it.getLeaves().get('title') }
193             assert bookTitles.sort() == expectedBookTitles.sort()
194         where: 'the following data is used'
195             scenario                                                   | cpsPath                                                                || expectedBookTitles
196             'one leaf'                                                 | '//books[@price=14]'                                                   || ['The Light Fantastic']
197             'one leaf with ">" condition'                              | '//books[@price>14]'                                                   || ['A Book with No Language', 'Annihilation', 'Debian GNU/Linux', 'The Gruffalo']
198             'one text'                                                 | '//books/authors[text()="Terry Pratchett"]'                            || ['Good Omens', 'The Colour of Magic', 'The Light Fantastic']
199             'more than one leaf'                                       | '//books[@price=12 and @lang="English"]'                               || ['The Colour of Magic']
200             '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']
201             'more than one leaf has "OR" condition with non-json data' | '//books[@title="xyz" or @price=13]'                                   || ['Good Omens']
202             'more than one leaf has multiple AND'                      | '//books[@lang="English" and @price=13 and @edition=1983]'             || []
203             'more than one leaf has multiple OR'                       | '//books[ @title="Matilda" or @price=15 or @edition=2006]'             || ['Annihilation', 'Matilda', 'The Gruffalo']
204             'leaves reversed in order'                                 | '//books[@lang="English" and @price=12]'                               || ['The Colour of Magic']
205             'more than one leaf has combination of AND/OR'             | '//books[@edition=1983 and @price=13 or @title="Good Omens"]'          || ['Good Omens']
206             'more than one leaf has OR/AND'                            | '//books[@title="The Light Fantastic" or @price=11 and @edition=1983]' || ['The Light Fantastic']
207             'leaf and text'                                            | '//books[@price=14]/authors[text()="Terry Pratchett"]'                 || ['The Light Fantastic']
208             'leaf and contains'                                        | '//books[contains(@price,"13")]'                                       || ['Good Omens']
209     }
210
211     def 'Cps Path query using descendant anywhere with #scenario condition(s) for a list element.'() {
212         when: 'a query is executed to get a data node by the given cps path'
213             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, INCLUDE_ALL_DESCENDANTS)
214         then: 'xpaths of the retrieved data nodes are as expected'
215             result.xpath.toList() == ["/bookstore/premises/addresses[@house-number='2' and @street='Main Street']"]
216         where: 'the following data is used'
217             scenario                              | cpsPath
218             'full composite key'                  | '//addresses[@house-number=2 and @street="Main Street"]'
219             'one partial key leaf'                | '//addresses[@house-number=2]'
220             'one non key leaf'                    | '//addresses[@county="Kildare"]'
221             'mix of partial key and non key leaf' | '//addresses[@street="Main Street" and @county="Kildare"]'
222     }
223
224     def 'Query for attribute by cps path of type ancestor with #scenario.'() {
225         when: 'the given cps path is parsed'
226             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, OMIT_DESCENDANTS)
227         then: 'the xpaths of the retrieved data nodes are as expected'
228             assert result.xpath.sort() == expectedXPaths.sort()
229         where: 'the following data is used'
230             scenario                                    | cpsPath                                               || expectedXPaths
231             'multiple list-ancestors'                   | '//books/ancestor::categories'                        || ["/bookstore/categories[@code='1']", "/bookstore/categories[@code='2']", "/bookstore/categories[@code='3']", "/bookstore/categories[@code='4']"]
232             'one ancestor with list value'              | '//books/ancestor::categories[@code="1"]'             || ["/bookstore/categories[@code='1']"]
233             'top ancestor'                              | '//books/ancestor::bookstore'                         || ["/bookstore"]
234             'list with index value in the xpath prefix' | '//categories[@code="1"]/books/ancestor::bookstore'   || ["/bookstore"]
235             'ancestor with parent list'                 | '//books/ancestor::bookstore/categories'              || ["/bookstore/categories[@code='1']", "/bookstore/categories[@code='2']", "/bookstore/categories[@code='3']", "/bookstore/categories[@code='4']"]
236             'ancestor with parent'                      | '//books/ancestor::bookstore/categories[@code="2"]'   || ["/bookstore/categories[@code='2']"]
237             'ancestor combined with text condition'     | '//books/title[text()="Matilda"]/ancestor::bookstore' || ["/bookstore"]
238             'ancestor with parent that does not exist'  | '//books/ancestor::parentDoesNoExist/categories'      || []
239             'ancestor does not exist'                   | '//books/ancestor::ancestorDoesNotExist'              || []
240             'ancestor combined with contains condition' | '//books[contains(@title,"Mat")]/ancestor::bookstore' || ["/bookstore"]
241     }
242
243     def 'Query for attribute by cps path of type ancestor with #scenario descendants.'() {
244         when: 'the given cps path is parsed'
245             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '//books/ancestor::bookstore', fetchDescendantsOption)
246         then: 'the xpaths of the retrieved data nodes are as expected'
247             assert countDataNodesInTree(result) == expectedNumberOfNodes
248         where: 'the following data is used'
249             scenario | fetchDescendantsOption  || expectedNumberOfNodes
250             'no'     | OMIT_DESCENDANTS        || 1
251             'direct' | DIRECT_CHILDREN_ONLY    || 6
252             'all'    | INCLUDE_ALL_DESCENDANTS || 17
253     }
254
255     def 'Cps Path query with #scenario throws a CPS Path Exception.'() {
256         when: 'trying to execute a query with a syntax (parsing) error'
257             objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, OMIT_DESCENDANTS)
258         then: 'a cps path exception is thrown'
259             thrown(CpsPathException)
260         where: 'the following data is used'
261             scenario                           | cpsPath
262             'cpsPath that cannot be parsed'    | 'cpsPath that cannot be parsed'
263             'String with comparative operator' | '//books[@lang>"German" and @price>10]'
264     }
265
266     def 'Cps Path query across anchors with #scenario.'() {
267         when: 'a query is executed to get a data nodes across anchors by the given CpsPath'
268             def result = objectUnderTest.queryDataNodesAcrossAnchors(FUNCTIONAL_TEST_DATASPACE_1, cpsPath, OMIT_DESCENDANTS)
269         then: 'the correct dataspace is queried'
270             assert result.dataspace.toSet() == [FUNCTIONAL_TEST_DATASPACE_1].toSet()
271         and: 'correct anchors are queried'
272             assert result.anchorName.toSet() == [BOOKSTORE_ANCHOR_1, BOOKSTORE_ANCHOR_2].toSet()
273         and: 'the correct number of nodes is returned'
274             assert result.size() == expectedXpathsPerAnchor.size() * NUMBER_OF_ANCHORS_PER_DATASPACE_WITH_BOOKSTORE_DATA
275         and: 'the queried nodes have expected xpaths'
276             assert result.xpath.toSet() == expectedXpathsPerAnchor.toSet()
277         where: 'the following data is used'
278             scenario                                    | cpsPath                                               || expectedXpathsPerAnchor
279             'container node'                            | '/bookstore'                                          || ["/bookstore"]
280             'list node'                                 | '/bookstore/categories'                               || ["/bookstore/categories[@code='1']", "/bookstore/categories[@code='2']", "/bookstore/categories[@code='3']", "/bookstore/categories[@code='4']"]
281             'string leaf-condition'                     | '/bookstore[@bookstore-name="Easons"]'                || ["/bookstore"]
282             'integer leaf-condition'                    | '/bookstore/categories[@code="1"]/books[@price=15]'   || ["/bookstore/categories[@code='1']/books[@title='The Gruffalo']"]
283             'multiple list-ancestors'                   | '//books/ancestor::categories'                        || ["/bookstore/categories[@code='1']", "/bookstore/categories[@code='2']", "/bookstore/categories[@code='3']", "/bookstore/categories[@code='4']"]
284             'one ancestor with list value'              | '//books/ancestor::categories[@code="1"]'             || ["/bookstore/categories[@code='1']"]
285             'list with index value in the xpath prefix' | '//categories[@code="1"]/books/ancestor::bookstore'   || ["/bookstore"]
286             'ancestor with parent list'                 | '//books/ancestor::bookstore/categories'              || ["/bookstore/categories[@code='1']", "/bookstore/categories[@code='2']", "/bookstore/categories[@code='3']", "/bookstore/categories[@code='4']"]
287             'ancestor with parent list element'         | '//books/ancestor::bookstore/categories[@code="2"]'   || ["/bookstore/categories[@code='2']"]
288             'ancestor combined with text condition'     | '//books/title[text()="Matilda"]/ancestor::bookstore' || ["/bookstore"]
289     }
290
291     def 'Cps Path query across anchors with #scenario descendants.'() {
292         when: 'a query is executed to get a data node by the given cps path'
293             def result = objectUnderTest.queryDataNodesAcrossAnchors(FUNCTIONAL_TEST_DATASPACE_1, '/bookstore', fetchDescendantsOption)
294         then: 'the correct dataspace was queried'
295             assert result.dataspace.toSet() == [FUNCTIONAL_TEST_DATASPACE_1].toSet()
296         and: 'correct number of datanodes are returned'
297             assert countDataNodesInTree(result) == expectedNumberOfNodesPerAnchor * NUMBER_OF_ANCHORS_PER_DATASPACE_WITH_BOOKSTORE_DATA
298         where: 'the following data is used'
299             scenario | fetchDescendantsOption  || expectedNumberOfNodesPerAnchor
300             'no'     | OMIT_DESCENDANTS        || 1
301             'direct' | DIRECT_CHILDREN_ONLY    || 6
302             'all'    | INCLUDE_ALL_DESCENDANTS || 17
303     }
304
305     def 'Cps Path query across anchors with ancestors and #scenario descendants.'() {
306         when: 'a query is executed to get a data node by the given cps path'
307             def result = objectUnderTest.queryDataNodesAcrossAnchors(FUNCTIONAL_TEST_DATASPACE_1, '//books/ancestor::bookstore', fetchDescendantsOption)
308         then: 'the correct dataspace was queried'
309             assert result.dataspace.toSet() == [FUNCTIONAL_TEST_DATASPACE_1].toSet()
310         and: 'correct number of datanodes are returned'
311             assert countDataNodesInTree(result) == expectedNumberOfNodesPerAnchor * NUMBER_OF_ANCHORS_PER_DATASPACE_WITH_BOOKSTORE_DATA
312         where: 'the following data is used'
313             scenario | fetchDescendantsOption  || expectedNumberOfNodesPerAnchor
314             'no'     | OMIT_DESCENDANTS        || 1
315             'direct' | DIRECT_CHILDREN_ONLY    || 6
316             'all'    | INCLUDE_ALL_DESCENDANTS || 17
317     }
318
319     def 'Cps Path query across anchors with syntax error throws a CPS Path Exception.'() {
320         when: 'trying to execute a query with a syntax (parsing) error'
321             objectUnderTest.queryDataNodesAcrossAnchors(FUNCTIONAL_TEST_DATASPACE_1, 'cpsPath that cannot be parsed' , OMIT_DESCENDANTS)
322         then: 'a cps path exception is thrown'
323             thrown(CpsPathException)
324     }
325
326     def 'Cps Path querys with all descendants including descendants that are list entries: #scenario.'() {
327         when: 'a query is executed to get a data node by the given cps path'
328             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, INCLUDE_ALL_DESCENDANTS)
329         then: 'correct number of datanodes are returned'
330             assert countDataNodesInTree(result) == expectedNumberOfDataNodes
331         where:
332             scenario                              | cpsPath                                 || expectedNumberOfDataNodes
333             'absolute path all list entries'      | '/bookstore/categories'                 || 13
334             'absolute path 1 list entry by key'   | '/bookstore/categories[@code="3"]'      || 5
335             'absolute path 1 list entry by name'  | '/bookstore/categories[@name="Comedy"]' || 5
336             'relative path all list entries'      | '//categories'                          || 13
337             'relative path 1 list entry by key'   | '//categories[@code="3"]'               || 5
338             'relative path 1 list entry by leaf'  | '//categories[@name="Comedy"]'          || 5
339             'incomplete absolute path'            | '/categories'                           || 0
340             'incomplete absolute 1 list entry'    | '/categories[@code="3"]'                || 0
341     }
342
343     def 'Cps Path query contains #wildcard.'() {
344         when: 'a query is executed with a wildcard in the given cps path'
345             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, INCLUDE_ALL_DESCENDANTS)
346         then: 'no results are returned, as Cps Path query does not interpret wildcard characters'
347             assert result.isEmpty()
348         where:
349             wildcard                                   | cpsPath
350             '  sql wildcard in parent path list index' | '/bookstore/categories[@code="%"]/books'
351             'regex wildcard in parent path list index' | '/bookstore/categories[@code=".*"]/books'
352             '  sql wildcard in leaf-condition'         | '/bookstore/categories[@code="1"]/books[@title="%"]'
353             'regex wildcard in leaf-condition'         | '/bookstore/categories[@code="1"]/books[@title=".*"]'
354             '  sql wildcard in text-condition'         | '/bookstore/categories[@code="1"]/books/title[text()="%"]'
355             'regex wildcard in text-condition'         | '/bookstore/categories[@code="1"]/books/title[text()=".*"]'
356             '  sql wildcard in contains-condition'     | '/bookstore/categories[@code="1"]/books[contains(@title, "%")]'
357             'regex wildcard in contains-condition'     | '/bookstore/categories[@code="1"]/books[contains(@title, ".*")]'
358     }
359
360     def 'Cps Path query can return a data node containing [@ in xpath #scenario.'() {
361         given: 'a book with special characters [@ and ] in title'
362             cpsDataService.saveData(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, "/bookstore/categories[@code='1']", '{"books": [ {"title":"[@hello=world]"} ] }', OffsetDateTime.now())
363         when: 'a query is executed'
364             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, OMIT_DESCENDANTS)
365         then: 'the node is returned'
366             assert result.size() == 1
367         cleanup: 'the new datanode'
368             cpsDataService.deleteDataNode(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, "/bookstore/categories[@code='1']/books[@title='[@hello=world]']", OffsetDateTime.now())
369         where:
370             scenario             || cpsPath
371             'leaf-condition'     || "/bookstore/categories[@code='1']/books[@title='[@hello=world]']"
372             'text-condition'     || "/bookstore/categories[@code='1']/books/title[text()='[@hello=world]']"
373             'contains-condition' || "/bookstore/categories[@code='1']/books[contains(@title, '[@hello=world]')]"
374     }
375 }