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