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 println(expectedLeaves.toArray())
65 where: 'the following data is used'
66 scenario | cpspath || expectedResultSize | expectedLeaves
67 'the "OR" condition' | '//books[@lang="English" or @price=15]' || 6 | [[lang: "English", price: 15, title: "Annihilation", authors: ["Jeff VanderMeer"], editions: [2014]],
68 [lang: "English", price: 15, title: "The Gruffalo", authors: ["Julia Donaldson"], editions: [1999]],
69 [lang: "English", price: 14, title: "The Light Fantastic", authors: ["Terry Pratchett"], editions: [1986]],
70 [lang: "English", price: 13, title: "Good Omens", authors: ["Terry Pratchett", "Neil Gaiman"], editions: [2006]],
71 [lang: "English", price: 12, title: "The Colour of Magic", authors: ["Terry Pratchett"], editions: [1983]],
72 [lang: "English", price: 10, title: "Matilda", authors: ["Roald Dahl"], editions: [1988, 2000]]]
73 '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]],
74 [lang: "English", price: 15, title: "The Gruffalo", authors: ["Julia Donaldson"], editions: [1999]]]
75 'combination of multiple AND' | '//books[@lang="English" and @price=15 and @edition=1983]' || 0 | []
76 '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]],
77 [lang: "English", price: 10, title: "Matilda", authors: ["Roald Dahl"], editions: [1988, 2000]],
78 [lang: "English", price: 15, title: "The Gruffalo", authors: ["Julia Donaldson"], editions: [1999]]]
79 '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]]]
80 '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]]]
83 def 'Cps Path query for leaf value(s) with #scenario.'() {
84 when: 'a query is executed to get a data node by the given cps path'
85 def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, fetchDescendantsOption)
86 then: 'the correct number of parent nodes are returned'
87 assert result.size() == expectedNumberOfParentNodes
88 and: 'the correct total number of data nodes are returned'
89 assert countDataNodesInTree(result) == expectedTotalNumberOfNodes
90 where: 'the following data is used'
91 scenario | cpsPath | fetchDescendantsOption || expectedNumberOfParentNodes | expectedTotalNumberOfNodes
92 'string and no descendants' | '/bookstore/categories[@code="1"]/books[@title="Matilda"]' | OMIT_DESCENDANTS || 1 | 1
93 'integer and descendants' | '/bookstore/categories[@code="1"]/books[@price=15]' | INCLUDE_ALL_DESCENDANTS || 1 | 1
94 'no condition and no descendants' | '/bookstore/categories' | OMIT_DESCENDANTS || 4 | 4
95 'no condition and level 1 descendants' | '/bookstore' | new FetchDescendantsOption(1) || 1 | 6
96 'no condition and level 2 descendants' | '/bookstore' | new FetchDescendantsOption(2) || 1 | 17
99 def 'Query for attribute by cps path with cps paths that return no data because of #scenario.'() {
100 when: 'a query is executed to get data nodes for the given cps path'
101 def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, OMIT_DESCENDANTS)
102 then: 'no data is returned'
103 assert result.isEmpty()
104 where: 'following cps queries are performed'
106 'cps path is incomplete' | '/bookstore[@title="Matilda"]'
107 'leaf value does not exist' | '/bookstore/categories[@code="1"]/books[@title=\'does not exist\']'
108 'incomplete end of xpath prefix' | '/bookstore/categories/books[@price=15]'
111 def 'Cps Path query using descendant anywhere and #type (further) descendants.'() {
112 when: 'a query is executed to get a data node by the given cps path'
113 def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="1"]', fetchDescendantsOption)
114 then: 'the data node has the correct number of children'
115 assert result[0].childDataNodes.xpath.sort() == expectedChildNodes.sort()
116 where: 'the following data is used'
117 type | fetchDescendantsOption || expectedChildNodes
118 'omit' | OMIT_DESCENDANTS || []
119 'include' | INCLUDE_ALL_DESCENDANTS || ["/bookstore/categories[@code='1']/books[@title='Matilda']",
120 "/bookstore/categories[@code='1']/books[@title='The Gruffalo']"]
123 def 'Cps Path query for all books.'() {
124 when: 'a query is executed to get all books'
125 def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '//books', OMIT_DESCENDANTS)
126 then: 'the expected number of books are returned'
127 assert result.size() == 9
130 def 'Cps Path query using descendant anywhere with #scenario.'() {
131 when: 'a query is executed to get a data node by the given cps path'
132 def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, OMIT_DESCENDANTS)
133 then: 'xpaths of the retrieved data nodes are as expected'
134 def bookTitles = result.collect { it.getLeaves().get('title') }
135 assert bookTitles.sort() == expectedBookTitles.sort()
136 where: 'the following data is used'
137 scenario | cpsPath || expectedBookTitles
138 'string leaf condition' | '//books[@title="Matilda"]' || ["Matilda"]
139 'text condition on leaf' | '//books/title[text()="Matilda"]' || ["Matilda"]
140 'text condition case mismatch' | '//books/title[text()="matilda"]' || []
141 'text condition on int leaf' | '//books/price[text()="10"]' || ["Matilda"]
142 'text condition on leaf-list' | '//books/authors[text()="Terry Pratchett"]' || ["Good Omens", "The Colour of Magic", "The Light Fantastic"]
143 'text condition partial match' | '//books/authors[text()="Terry"]' || []
144 'text condition (existing) empty string' | '//books/lang[text()=""]' || ["A Book with No Language"]
145 'text condition on int leaf-list' | '//books/editions[text()="2000"]' || ["Matilda"]
146 'match of leaf containing /' | '//books[@lang="N/A"]' || ["Logarithm tables"]
147 'text condition on leaf containing /' | '//books/lang[text()="N/A"]' || ["Logarithm tables"]
148 'match of key containing /' | '//books[@title="Debian GNU/Linux"]' || ["Debian GNU/Linux"]
149 'text condition on key containing /' | '//books/title[text()="Debian GNU/Linux"]' || ["Debian GNU/Linux"]
152 def 'Query for attribute by cps path using contains condition #scenario.'() {
153 when: 'a query is executed to get response by the given cps path'
154 def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, OMIT_DESCENDANTS)
155 then: 'the cps-path of queryDataNodes has expected number of nodes'
156 assert result.size() == expectedResultsize
157 and: '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 | expectedResultsize || expectedBookTitles
162 'contains condition with leaf' | '//books[contains(@title,"Mat")]' | 1 || ["Matilda"]
163 'contains condition with case-sensitive' | '//books[contains(@title,"Ti")]' | 0 || []
164 'contains condition with Integer Value' | '//books[contains(@price,"15")]' | 2 || ["Annihilation", "The Gruffalo"]
165 'contains condition with No-value' | '//books[contains(@title,"")]' | 9 || ["A Book with No Language", "Annihilation", "Debian GNU/Linux", "Good Omens", "Logarithm tables", "Matilda", "The Colour of Magic", "The Gruffalo", "The Light Fantastic"]
168 def 'Cps Path query using descendant anywhere with #scenario condition for a container element.'() {
169 when: 'a query is executed to get a data node by the given cps path'
170 def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, OMIT_DESCENDANTS)
171 then: 'book titles from the retrieved data nodes are as expected'
172 def bookTitles = result.collect { it.getLeaves().get('title') }
173 assert bookTitles.sort() == expectedBookTitles.sort()
174 where: 'the following data is used'
175 scenario | cpsPath || expectedBookTitles
176 'one leaf' | '//books[@price=14]' || ['The Light Fantastic']
177 'one text' | '//books/authors[text()="Terry Pratchett"]' || ['Good Omens', 'The Colour of Magic', 'The Light Fantastic']
178 'more than one leaf' | '//books[@price=12 and @lang="English"]' || ['The Colour of Magic']
179 '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']
180 'more than one leaf has "OR" condition with non-json data' | '//books[@title="xyz" or @price=13]' || ['Good Omens']
181 'more than one leaf has multiple AND' | '//books[@lang="English" and @price=13 and @edition=1983]' || []
182 'more than one leaf has multiple OR' | '//books[ @title="Matilda" or @price=15 or @edition=2006]' || ['Annihilation', 'Matilda', 'The Gruffalo']
183 'leaves reversed in order' | '//books[@lang="English" and @price=12]' || ['The Colour of Magic']
184 'more than one leaf has combination of AND/OR' | '//books[@edition=1983 and @price=13 or @title="Good Omens"]' || ['Good Omens']
185 'more than one leaf has OR/AND' | '//books[@title="The Light Fantastic" or @price=11 and @edition=1983]' || ['The Light Fantastic']
186 'leaf and text' | '//books[@price=14]/authors[text()="Terry Pratchett"]' || ['The Light Fantastic']
187 'leaf and contains' | '//books[contains(@price,"13")]' || ['Good Omens']
190 def 'Cps Path query using descendant anywhere with #scenario condition(s) for a list element.'() {
191 when: 'a query is executed to get a data node by the given cps path'
192 def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, INCLUDE_ALL_DESCENDANTS)
193 then: 'xpaths of the retrieved data nodes are as expected'
194 result.xpath.toList() == ["/bookstore/premises/addresses[@house-number='2' and @street='Main Street']"]
195 where: 'the following data is used'
197 'full composite key' | '//addresses[@house-number=2 and @street="Main Street"]'
198 'one partial key leaf' | '//addresses[@house-number=2]'
199 'one non key leaf' | '//addresses[@county="Kildare"]'
200 'mix of partial key and non key leaf' | '//addresses[@street="Main Street" and @county="Kildare"]'
203 def 'Query for attribute by cps path of type ancestor with #scenario.'() {
204 when: 'the given cps path is parsed'
205 def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, OMIT_DESCENDANTS)
206 then: 'the xpaths of the retrieved data nodes are as expected'
207 assert result.xpath.sort() == expectedXPaths.sort()
208 where: 'the following data is used'
209 scenario | cpsPath || expectedXPaths
210 'multiple list-ancestors' | '//books/ancestor::categories' || ["/bookstore/categories[@code='1']", "/bookstore/categories[@code='2']", "/bookstore/categories[@code='3']", "/bookstore/categories[@code='4']"]
211 'one ancestor with list value' | '//books/ancestor::categories[@code="1"]' || ["/bookstore/categories[@code='1']"]
212 'top ancestor' | '//books/ancestor::bookstore' || ["/bookstore"]
213 'list with index value in the xpath prefix' | '//categories[@code="1"]/books/ancestor::bookstore' || ["/bookstore"]
214 'ancestor with parent list' | '//books/ancestor::bookstore/categories' || ["/bookstore/categories[@code='1']", "/bookstore/categories[@code='2']", "/bookstore/categories[@code='3']", "/bookstore/categories[@code='4']"]
215 'ancestor with parent' | '//books/ancestor::bookstore/categories[@code="2"]' || ["/bookstore/categories[@code='2']"]
216 'ancestor combined with text condition' | '//books/title[text()="Matilda"]/ancestor::bookstore' || ["/bookstore"]
217 'ancestor with parent that does not exist' | '//books/ancestor::parentDoesNoExist/categories' || []
218 'ancestor does not exist' | '//books/ancestor::ancestorDoesNotExist' || []
219 'ancestor combined with contains condition' | '//books[contains(@title,"Mat")]/ancestor::bookstore' || ["/bookstore"]
222 def 'Query for attribute by cps path of type ancestor with #scenario descendants.'() {
223 when: 'the given cps path is parsed'
224 def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '//books/ancestor::bookstore', fetchDescendantsOption)
225 then: 'the xpaths of the retrieved data nodes are as expected'
226 assert countDataNodesInTree(result) == expectedNumberOfNodes
227 where: 'the following data is used'
228 scenario | fetchDescendantsOption || expectedNumberOfNodes
229 'no' | OMIT_DESCENDANTS || 1
230 'direct' | DIRECT_CHILDREN_ONLY || 6
231 'all' | INCLUDE_ALL_DESCENDANTS || 17
234 def 'Cps Path query with syntax error throws a CPS Path Exception.'() {
235 when: 'trying to execute a query with a syntax (parsing) error'
236 objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, 'cpsPath that cannot be parsed' , OMIT_DESCENDANTS)
237 then: 'a cps path exception is thrown'
238 thrown(CpsPathException)
242 def 'Cps Path query across anchors with #scenario.'() {
243 when: 'a query is executed to get a data nodes across anchors by the given CpsPath'
244 def result = objectUnderTest.queryDataNodesAcrossAnchors(FUNCTIONAL_TEST_DATASPACE_1, cpsPath, OMIT_DESCENDANTS)
245 then: 'the correct dataspace is queried'
246 assert result.dataspace.toSet() == [FUNCTIONAL_TEST_DATASPACE_1].toSet()
247 and: 'correct anchors are queried'
248 assert result.anchorName.toSet() == [BOOKSTORE_ANCHOR_1, BOOKSTORE_ANCHOR_2].toSet()
249 and: 'the correct number of nodes is returned'
250 assert result.size() == expectedXpathsPerAnchor.size() * NUMBER_OF_ANCHORS_PER_DATASPACE_WITH_BOOKSTORE_DATA
251 and: 'the queried nodes have expected xpaths'
252 assert result.xpath.toSet() == expectedXpathsPerAnchor.toSet()
253 where: 'the following data is used'
254 scenario | cpsPath || expectedXpathsPerAnchor
255 'container node' | '/bookstore' || ["/bookstore"]
256 'list node' | '/bookstore/categories' || ["/bookstore/categories[@code='1']", "/bookstore/categories[@code='2']", "/bookstore/categories[@code='3']", "/bookstore/categories[@code='4']"]
257 'string leaf-condition' | '/bookstore[@bookstore-name="Easons"]' || ["/bookstore"]
258 'integer leaf-condition' | '/bookstore/categories[@code="1"]/books[@price=15]' || ["/bookstore/categories[@code='1']/books[@title='The Gruffalo']"]
259 'multiple list-ancestors' | '//books/ancestor::categories' || ["/bookstore/categories[@code='1']", "/bookstore/categories[@code='2']", "/bookstore/categories[@code='3']", "/bookstore/categories[@code='4']"]
260 'one ancestor with list value' | '//books/ancestor::categories[@code="1"]' || ["/bookstore/categories[@code='1']"]
261 'list with index value in the xpath prefix' | '//categories[@code="1"]/books/ancestor::bookstore' || ["/bookstore"]
262 'ancestor with parent list' | '//books/ancestor::bookstore/categories' || ["/bookstore/categories[@code='1']", "/bookstore/categories[@code='2']", "/bookstore/categories[@code='3']", "/bookstore/categories[@code='4']"]
263 'ancestor with parent list element' | '//books/ancestor::bookstore/categories[@code="2"]' || ["/bookstore/categories[@code='2']"]
264 'ancestor combined with text condition' | '//books/title[text()="Matilda"]/ancestor::bookstore' || ["/bookstore"]
268 def 'Cps Path query across anchors with #scenario descendants.'() {
269 when: 'a query is executed to get a data node by the given cps path'
270 def result = objectUnderTest.queryDataNodesAcrossAnchors(FUNCTIONAL_TEST_DATASPACE_1, '/bookstore', fetchDescendantsOption)
271 then: 'the correct dataspace was queried'
272 assert result.dataspace.toSet() == [FUNCTIONAL_TEST_DATASPACE_1].toSet()
273 and: 'correct number of datanodes are returned'
274 assert countDataNodesInTree(result) == expectedNumberOfNodesPerAnchor * NUMBER_OF_ANCHORS_PER_DATASPACE_WITH_BOOKSTORE_DATA
275 where: 'the following data is used'
276 scenario | fetchDescendantsOption || expectedNumberOfNodesPerAnchor
277 'no' | OMIT_DESCENDANTS || 1
278 'direct' | DIRECT_CHILDREN_ONLY || 6
279 'all' | INCLUDE_ALL_DESCENDANTS || 17
283 def 'Cps Path query across anchors with ancestors and #scenario descendants.'() {
284 when: 'a query is executed to get a data node by the given cps path'
285 def result = objectUnderTest.queryDataNodesAcrossAnchors(FUNCTIONAL_TEST_DATASPACE_1, '//books/ancestor::bookstore', fetchDescendantsOption)
286 then: 'the correct dataspace was queried'
287 assert result.dataspace.toSet() == [FUNCTIONAL_TEST_DATASPACE_1].toSet()
288 and: 'correct number of datanodes are returned'
289 assert countDataNodesInTree(result) == expectedNumberOfNodesPerAnchor * NUMBER_OF_ANCHORS_PER_DATASPACE_WITH_BOOKSTORE_DATA
290 where: 'the following data is used'
291 scenario | fetchDescendantsOption || expectedNumberOfNodesPerAnchor
292 'no' | OMIT_DESCENDANTS || 1
293 'direct' | DIRECT_CHILDREN_ONLY || 6
294 'all' | INCLUDE_ALL_DESCENDANTS || 17
297 def 'Cps Path query across anchors with syntax error throws a CPS Path Exception.'() {
298 when: 'trying to execute a query with a syntax (parsing) error'
299 objectUnderTest.queryDataNodesAcrossAnchors(FUNCTIONAL_TEST_DATASPACE_1, 'cpsPath that cannot be parsed' , OMIT_DESCENDANTS)
300 then: 'a cps path exception is thrown'
301 thrown(CpsPathException)