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
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
24 import org.onap.cps.api.CpsQueryService
25 import org.onap.cps.integration.base.FunctionalSpecBase
26 import org.onap.cps.spi.FetchDescendantsOption
27 import org.onap.cps.spi.exceptions.CpsPathException
28 import spock.lang.Ignore
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
34 class CpsQueryServiceIntegrationSpec extends FunctionalSpecBase {
36 CpsQueryService objectUnderTest
38 def setup() { objectUnderTest = cpsQueryService }
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
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 | []
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]]]
82 def 'Cps Path query for leaf value(s) with #scenario.'() {
83 when: 'a query is executed to get a data node by the given cps path'
84 def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, fetchDescendantsOption)
85 then: 'the correct number of parent nodes are returned'
86 assert result.size() == expectedNumberOfParentNodes
87 and: 'the correct total number of data nodes are returned'
88 assert countDataNodesInTree(result) == expectedTotalNumberOfNodes
89 where: 'the following data is used'
90 scenario | cpsPath | fetchDescendantsOption || expectedNumberOfParentNodes | expectedTotalNumberOfNodes
91 'string and no descendants' | '/bookstore/categories[@code="1"]/books[@title="Matilda"]' | OMIT_DESCENDANTS || 1 | 1
92 'integer and descendants' | '/bookstore/categories[@code="1"]/books[@price=15]' | INCLUDE_ALL_DESCENDANTS || 1 | 1
93 'no condition and no descendants' | '/bookstore/categories' | OMIT_DESCENDANTS || 4 | 4
94 'no condition and level 1 descendants' | '/bookstore' | new FetchDescendantsOption(1) || 1 | 6
95 'no condition and level 2 descendants' | '/bookstore' | new FetchDescendantsOption(2) || 1 | 17
98 def 'Query for attribute by cps path with cps paths that return no data because of #scenario.'() {
99 when: 'a query is executed to get data nodes for the given cps path'
100 def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, OMIT_DESCENDANTS)
101 then: 'no data is returned'
102 assert result.isEmpty()
103 where: 'following cps queries are performed'
105 'cps path is incomplete' | '/bookstore[@title="Matilda"]'
106 'leaf value does not exist' | '/bookstore/categories[@code="1"]/books[@title=\'does not exist\']'
107 'incomplete end of xpath prefix' | '/bookstore/categories/books[@price=15]'
110 def 'Cps Path query using descendant anywhere and #type (further) descendants.'() {
111 when: 'a query is executed to get a data node by the given cps path'
112 def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="1"]', fetchDescendantsOption)
113 then: 'the data node has the correct number of children'
114 assert result[0].childDataNodes.xpath.sort() == expectedChildNodes.sort()
115 where: 'the following data is used'
116 type | fetchDescendantsOption || expectedChildNodes
117 'omit' | OMIT_DESCENDANTS || []
118 'include' | INCLUDE_ALL_DESCENDANTS || ["/bookstore/categories[@code='1']/books[@title='Matilda']",
119 "/bookstore/categories[@code='1']/books[@title='The Gruffalo']"]
122 def 'Cps Path query for all books.'() {
123 when: 'a query is executed to get all books'
124 def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '//books', OMIT_DESCENDANTS)
125 then: 'the expected number of books are returned'
126 assert result.size() == 9
129 def 'Cps Path query using descendant anywhere with #scenario.'() {
130 when: 'a query is executed to get a data node by the given cps path'
131 def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, OMIT_DESCENDANTS)
132 then: 'xpaths of the retrieved data nodes are as expected'
133 def bookTitles = result.collect { it.getLeaves().get('title') }
134 assert bookTitles.sort() == expectedBookTitles.sort()
135 where: 'the following data is used'
136 scenario | cpsPath || expectedBookTitles
137 'string leaf condition' | '//books[@title="Matilda"]' || ["Matilda"]
138 'text condition on leaf' | '//books/title[text()="Matilda"]' || ["Matilda"]
139 'text condition case mismatch' | '//books/title[text()="matilda"]' || []
140 'text condition on int leaf' | '//books/price[text()="10"]' || ["Matilda"]
141 'text condition on leaf-list' | '//books/authors[text()="Terry Pratchett"]' || ["Good Omens", "The Colour of Magic", "The Light Fantastic"]
142 'text condition partial match' | '//books/authors[text()="Terry"]' || []
143 'text condition (existing) empty string' | '//books/lang[text()=""]' || ["A Book with No Language"]
144 'text condition on int leaf-list' | '//books/editions[text()="2000"]' || ["Matilda"]
145 'match of leaf containing /' | '//books[@lang="N/A"]' || ["Logarithm tables"]
146 'text condition on leaf containing /' | '//books/lang[text()="N/A"]' || ["Logarithm tables"]
147 'match of key containing /' | '//books[@title="Debian GNU/Linux"]' || ["Debian GNU/Linux"]
148 'text condition on key containing /' | '//books/title[text()="Debian GNU/Linux"]' || ["Debian GNU/Linux"]
151 def 'Query for attribute by cps path using contains condition #scenario.'() {
152 when: 'a query is executed to get response by the given cps path'
153 def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, INCLUDE_ALL_DESCENDANTS)
154 then: 'xpaths of the retrieved data nodes are as expected'
155 def bookTitles = result.collect { it.getLeaves().get('title') }
156 assert bookTitles.sort() == expectedBookTitles.sort()
157 where: 'the following data is used'
158 scenario | cpsPath || expectedBookTitles
159 'contains condition with leaf' | '//books[contains(@title,"Mat")]' || ["Matilda"]
160 'contains condition with case-sensitive' | '//books[contains(@title,"Ti")]' || []
161 'contains condition with Integer Value' | '//books[contains(@price,"15")]' || ["Annihilation", "The Gruffalo"]
162 '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"]
165 def 'Cps Path query using descendant anywhere with #scenario condition for a container element.'() {
166 when: 'a query is executed to get a data node by the given cps path'
167 def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, OMIT_DESCENDANTS)
168 then: 'book titles from the retrieved data nodes are as expected'
169 def bookTitles = result.collect { it.getLeaves().get('title') }
170 assert bookTitles.sort() == expectedBookTitles.sort()
171 where: 'the following data is used'
172 scenario | cpsPath || expectedBookTitles
173 'one leaf' | '//books[@price=14]' || ['The Light Fantastic']
174 'one text' | '//books/authors[text()="Terry Pratchett"]' || ['Good Omens', 'The Colour of Magic', 'The Light Fantastic']
175 'more than one leaf' | '//books[@price=12 and @lang="English"]' || ['The Colour of Magic']
176 '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']
177 'more than one leaf has "OR" condition with non-json data' | '//books[@title="xyz" or @price=13]' || ['Good Omens']
178 'more than one leaf has multiple AND' | '//books[@lang="English" and @price=13 and @edition=1983]' || []
179 'more than one leaf has multiple OR' | '//books[ @title="Matilda" or @price=15 or @edition=2006]' || ['Annihilation', 'Matilda', 'The Gruffalo']
180 'leaves reversed in order' | '//books[@lang="English" and @price=12]' || ['The Colour of Magic']
181 'more than one leaf has combination of AND/OR' | '//books[@edition=1983 and @price=13 or @title="Good Omens"]' || ['Good Omens']
182 'more than one leaf has OR/AND' | '//books[@title="The Light Fantastic" or @price=11 and @edition=1983]' || ['The Light Fantastic']
183 'leaf and text' | '//books[@price=14]/authors[text()="Terry Pratchett"]' || ['The Light Fantastic']
184 'leaf and contains' | '//books[contains(@price,"13")]' || ['Good Omens']
187 def 'Cps Path query using descendant anywhere with #scenario condition(s) for a list element.'() {
188 when: 'a query is executed to get a data node by the given cps path'
189 def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, INCLUDE_ALL_DESCENDANTS)
190 then: 'xpaths of the retrieved data nodes are as expected'
191 result.xpath.toList() == ["/bookstore/premises/addresses[@house-number='2' and @street='Main Street']"]
192 where: 'the following data is used'
194 'full composite key' | '//addresses[@house-number=2 and @street="Main Street"]'
195 'one partial key leaf' | '//addresses[@house-number=2]'
196 'one non key leaf' | '//addresses[@county="Kildare"]'
197 'mix of partial key and non key leaf' | '//addresses[@street="Main Street" and @county="Kildare"]'
200 def 'Query for attribute by cps path of type ancestor with #scenario.'() {
201 when: 'the given cps path is parsed'
202 def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, OMIT_DESCENDANTS)
203 then: 'the xpaths of the retrieved data nodes are as expected'
204 assert result.xpath.sort() == expectedXPaths.sort()
205 where: 'the following data is used'
206 scenario | cpsPath || expectedXPaths
207 'multiple list-ancestors' | '//books/ancestor::categories' || ["/bookstore/categories[@code='1']", "/bookstore/categories[@code='2']", "/bookstore/categories[@code='3']", "/bookstore/categories[@code='4']"]
208 'one ancestor with list value' | '//books/ancestor::categories[@code="1"]' || ["/bookstore/categories[@code='1']"]
209 'top ancestor' | '//books/ancestor::bookstore' || ["/bookstore"]
210 'list with index value in the xpath prefix' | '//categories[@code="1"]/books/ancestor::bookstore' || ["/bookstore"]
211 'ancestor with parent list' | '//books/ancestor::bookstore/categories' || ["/bookstore/categories[@code='1']", "/bookstore/categories[@code='2']", "/bookstore/categories[@code='3']", "/bookstore/categories[@code='4']"]
212 'ancestor with parent' | '//books/ancestor::bookstore/categories[@code="2"]' || ["/bookstore/categories[@code='2']"]
213 'ancestor combined with text condition' | '//books/title[text()="Matilda"]/ancestor::bookstore' || ["/bookstore"]
214 'ancestor with parent that does not exist' | '//books/ancestor::parentDoesNoExist/categories' || []
215 'ancestor does not exist' | '//books/ancestor::ancestorDoesNotExist' || []
216 'ancestor combined with contains condition' | '//books[contains(@title,"Mat")]/ancestor::bookstore' || ["/bookstore"]
219 def 'Query for attribute by cps path of type ancestor with #scenario descendants.'() {
220 when: 'the given cps path is parsed'
221 def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '//books/ancestor::bookstore', fetchDescendantsOption)
222 then: 'the xpaths of the retrieved data nodes are as expected'
223 assert countDataNodesInTree(result) == expectedNumberOfNodes
224 where: 'the following data is used'
225 scenario | fetchDescendantsOption || expectedNumberOfNodes
226 'no' | OMIT_DESCENDANTS || 1
227 'direct' | DIRECT_CHILDREN_ONLY || 6
228 'all' | INCLUDE_ALL_DESCENDANTS || 17
231 def 'Cps Path query with syntax error throws a CPS Path Exception.'() {
232 when: 'trying to execute a query with a syntax (parsing) error'
233 objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, 'cpsPath that cannot be parsed' , OMIT_DESCENDANTS)
234 then: 'a cps path exception is thrown'
235 thrown(CpsPathException)
239 def 'Cps Path query across anchors with #scenario.'() {
240 when: 'a query is executed to get a data nodes across anchors by the given CpsPath'
241 def result = objectUnderTest.queryDataNodesAcrossAnchors(FUNCTIONAL_TEST_DATASPACE_1, cpsPath, OMIT_DESCENDANTS)
242 then: 'the correct dataspace is queried'
243 assert result.dataspace.toSet() == [FUNCTIONAL_TEST_DATASPACE_1].toSet()
244 and: 'correct anchors are queried'
245 assert result.anchorName.toSet() == [BOOKSTORE_ANCHOR_1, BOOKSTORE_ANCHOR_2].toSet()
246 and: 'the correct number of nodes is returned'
247 assert result.size() == expectedXpathsPerAnchor.size() * NUMBER_OF_ANCHORS_PER_DATASPACE_WITH_BOOKSTORE_DATA
248 and: 'the queried nodes have expected xpaths'
249 assert result.xpath.toSet() == expectedXpathsPerAnchor.toSet()
250 where: 'the following data is used'
251 scenario | cpsPath || expectedXpathsPerAnchor
252 'container node' | '/bookstore' || ["/bookstore"]
253 'list node' | '/bookstore/categories' || ["/bookstore/categories[@code='1']", "/bookstore/categories[@code='2']", "/bookstore/categories[@code='3']", "/bookstore/categories[@code='4']"]
254 'string leaf-condition' | '/bookstore[@bookstore-name="Easons"]' || ["/bookstore"]
255 'integer leaf-condition' | '/bookstore/categories[@code="1"]/books[@price=15]' || ["/bookstore/categories[@code='1']/books[@title='The Gruffalo']"]
256 'multiple list-ancestors' | '//books/ancestor::categories' || ["/bookstore/categories[@code='1']", "/bookstore/categories[@code='2']", "/bookstore/categories[@code='3']", "/bookstore/categories[@code='4']"]
257 'one ancestor with list value' | '//books/ancestor::categories[@code="1"]' || ["/bookstore/categories[@code='1']"]
258 'list with index value in the xpath prefix' | '//categories[@code="1"]/books/ancestor::bookstore' || ["/bookstore"]
259 'ancestor with parent list' | '//books/ancestor::bookstore/categories' || ["/bookstore/categories[@code='1']", "/bookstore/categories[@code='2']", "/bookstore/categories[@code='3']", "/bookstore/categories[@code='4']"]
260 'ancestor with parent list element' | '//books/ancestor::bookstore/categories[@code="2"]' || ["/bookstore/categories[@code='2']"]
261 'ancestor combined with text condition' | '//books/title[text()="Matilda"]/ancestor::bookstore' || ["/bookstore"]
265 def 'Cps Path query across anchors with #scenario descendants.'() {
266 when: 'a query is executed to get a data node by the given cps path'
267 def result = objectUnderTest.queryDataNodesAcrossAnchors(FUNCTIONAL_TEST_DATASPACE_1, '/bookstore', fetchDescendantsOption)
268 then: 'the correct dataspace was queried'
269 assert result.dataspace.toSet() == [FUNCTIONAL_TEST_DATASPACE_1].toSet()
270 and: 'correct number of datanodes are returned'
271 assert countDataNodesInTree(result) == expectedNumberOfNodesPerAnchor * NUMBER_OF_ANCHORS_PER_DATASPACE_WITH_BOOKSTORE_DATA
272 where: 'the following data is used'
273 scenario | fetchDescendantsOption || expectedNumberOfNodesPerAnchor
274 'no' | OMIT_DESCENDANTS || 1
275 'direct' | DIRECT_CHILDREN_ONLY || 6
276 'all' | INCLUDE_ALL_DESCENDANTS || 17
280 def 'Cps Path query across anchors with ancestors and #scenario descendants.'() {
281 when: 'a query is executed to get a data node by the given cps path'
282 def result = objectUnderTest.queryDataNodesAcrossAnchors(FUNCTIONAL_TEST_DATASPACE_1, '//books/ancestor::bookstore', fetchDescendantsOption)
283 then: 'the correct dataspace was queried'
284 assert result.dataspace.toSet() == [FUNCTIONAL_TEST_DATASPACE_1].toSet()
285 and: 'correct number of datanodes are returned'
286 assert countDataNodesInTree(result) == expectedNumberOfNodesPerAnchor * NUMBER_OF_ANCHORS_PER_DATASPACE_WITH_BOOKSTORE_DATA
287 where: 'the following data is used'
288 scenario | fetchDescendantsOption || expectedNumberOfNodesPerAnchor
289 'no' | OMIT_DESCENDANTS || 1
290 'direct' | DIRECT_CHILDREN_ONLY || 6
291 'all' | INCLUDE_ALL_DESCENDANTS || 17
294 def 'Cps Path query across anchors with syntax error throws a CPS Path Exception.'() {
295 when: 'trying to execute a query with a syntax (parsing) error'
296 objectUnderTest.queryDataNodesAcrossAnchors(FUNCTIONAL_TEST_DATASPACE_1, 'cpsPath that cannot be parsed' , OMIT_DESCENDANTS)
297 then: 'a cps path exception is thrown'
298 thrown(CpsPathException)
301 def 'Cps Path querys with all descendants including descendants that are list entries: #scenario.'() {
302 when: 'a query is executed to get a data node by the given cps path'
303 def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, INCLUDE_ALL_DESCENDANTS)
304 then: 'correct number of datanodes are returned'
305 assert countDataNodesInTree(result) == expectedNumberOfDataNodes
307 scenario | cpsPath || expectedNumberOfDataNodes
308 'absolute path all list entries' | '/bookstore/categories' || 13
309 'absolute path 1 list entry by key' | '/bookstore/categories[@code="3"]' || 5
310 'absolute path 1 list entry by name' | '/bookstore/categories[@name="Comedy"]' || 5
311 'relative path all list entries' | '//categories' || 13
312 'relative path 1 list entry by key' | '//categories[@code="3"]' || 5
313 'relative path 1 list entry by leaf' | '//categories[@name="Comedy"]' || 5
314 'incomplete absolute path' | '/categories' || 0
315 'incomplete absolute 1 list entry' | '/categories[@code="3"]' || 0