2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2023 Nordix Foundation
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the 'License');
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an 'AS IS' BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.integration.functional
23 import org.onap.cps.api.CpsQueryService
24 import org.onap.cps.integration.base.FunctionalSpecBase
25 import org.onap.cps.spi.FetchDescendantsOption
26 import org.onap.cps.spi.exceptions.CpsPathException
27 import spock.lang.Ignore
29 import static org.onap.cps.spi.FetchDescendantsOption.DIRECT_CHILDREN_ONLY
30 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
31 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
33 class CpsQueryServiceIntegrationSpec extends FunctionalSpecBase {
35 CpsQueryService objectUnderTest
37 def setup() { objectUnderTest = cpsQueryService }
39 def 'Query bookstore using CPS path where #scenario.'() {
40 when: 'query data nodes for bookstore container'
41 def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, INCLUDE_ALL_DESCENDANTS)
42 then: 'the result contains expected number of nodes'
43 assert result.size() == expectedResultSize
44 and: 'the result contains the expected leaf values'
45 result.leaves.forEach( dataNodeLeaves -> {
46 expectedLeaves.forEach( (expectedLeafKey,expectedLeafValue) -> {
47 assert dataNodeLeaves[expectedLeafKey] == expectedLeafValue
51 scenario | cpsPath || expectedResultSize | expectedLeaves
52 'the AND condition is used' | '//books[@lang="English" and @price=15]' || 2 | [lang:"English", price:15]
53 'the AND is used where result does not exist' | '//books[@lang="English" and @price=1000]' || 0 | []
56 def 'Cps Path query for leaf value(s) with #scenario.'() {
57 when: 'a query is executed to get a data node by the given cps path'
58 def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, fetchDescendantsOption)
59 then: 'the correct number of parent nodes are returned'
60 assert result.size() == expectedNumberOfParentNodes
61 and: 'the correct total number of data nodes are returned'
62 assert countDataNodesInTree(result) == expectedTotalNumberOfNodes
63 where: 'the following data is used'
64 scenario | cpsPath | fetchDescendantsOption || expectedNumberOfParentNodes | expectedTotalNumberOfNodes
65 'string and no descendants' | '/bookstore/categories[@code="1"]/books[@title="Matilda"]' | OMIT_DESCENDANTS || 1 | 1
66 'integer and descendants' | '/bookstore/categories[@code="1"]/books[@price=15]' | INCLUDE_ALL_DESCENDANTS || 1 | 1
67 'no condition and no descendants' | '/bookstore/categories' | OMIT_DESCENDANTS || 4 | 4
68 'no condition and level 1 descendants' | '/bookstore' | new FetchDescendantsOption(1) || 1 | 6
69 'no condition and level 2 descendants' | '/bookstore' | new FetchDescendantsOption(2) || 1 | 17
72 def 'Query for attribute by cps path with cps paths that return no data because of #scenario.'() {
73 when: 'a query is executed to get data nodes for the given cps path'
74 def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, OMIT_DESCENDANTS)
75 then: 'no data is returned'
76 assert result.isEmpty()
77 where: 'following cps queries are performed'
79 'cps path is incomplete' | '/bookstore[@title="Matilda"]'
80 'leaf value does not exist' | '/bookstore/categories[@code="1"]/books[@title=\'does not exist\']'
81 'incomplete end of xpath prefix' | '/bookstore/categories/books[@price=15]'
84 def 'Cps Path query using descendant anywhere and #type (further) descendants.'() {
85 when: 'a query is executed to get a data node by the given cps path'
86 def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '/bookstore/categories[@code="1"]', fetchDescendantsOption)
87 then: 'the data node has the correct number of children'
88 assert result[0].childDataNodes.xpath.sort() == expectedChildNodes.sort()
89 where: 'the following data is used'
90 type | fetchDescendantsOption || expectedChildNodes
91 'omit' | OMIT_DESCENDANTS || []
92 'include' | INCLUDE_ALL_DESCENDANTS || ["/bookstore/categories[@code='1']/books[@title='Matilda']",
93 "/bookstore/categories[@code='1']/books[@title='The Gruffalo']"]
96 def 'Cps Path query for all books.'() {
97 when: 'a query is executed to get all books'
98 def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '//books', OMIT_DESCENDANTS)
99 then: 'the expected number of books are returned'
100 assert result.size() == 9
103 def 'Cps Path query using descendant anywhere with #scenario.'() {
104 when: 'a query is executed to get a data node by the given cps path'
105 def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, OMIT_DESCENDANTS)
106 then: 'xpaths of the retrieved data nodes are as expected'
107 def bookTitles = result.collect { it.getLeaves().get('title') }
108 assert bookTitles.sort() == expectedBookTitles.sort()
109 where: 'the following data is used'
110 scenario | cpsPath || expectedBookTitles
111 'string leaf condition' | '//books[@title="Matilda"]' || ["Matilda"]
112 'text condition on leaf' | '//books/title[text()="Matilda"]' || ["Matilda"]
113 'text condition case mismatch' | '//books/title[text()="matilda"]' || []
114 'text condition on int leaf' | '//books/price[text()="10"]' || ["Matilda"]
115 'text condition on leaf-list' | '//books/authors[text()="Terry Pratchett"]' || ["Good Omens", "The Colour of Magic", "The Light Fantastic"]
116 'text condition partial match' | '//books/authors[text()="Terry"]' || []
117 'text condition (existing) empty string' | '//books/lang[text()=""]' || ["A Book with No Language"]
118 'text condition on int leaf-list' | '//books/editions[text()="2000"]' || ["Matilda"]
119 'match of leaf containing /' | '//books[@lang="N/A"]' || ["Logarithm tables"]
120 'text condition on leaf containing /' | '//books/lang[text()="N/A"]' || ["Logarithm tables"]
121 'match of key containing /' | '//books[@title="Debian GNU/Linux"]' || ["Debian GNU/Linux"]
122 'text condition on key containing /' | '//books/title[text()="Debian GNU/Linux"]' || ["Debian GNU/Linux"]
125 def 'Cps Path query using descendant anywhere with #scenario condition for a container element.'() {
126 when: 'a query is executed to get a data node by the given cps path'
127 def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, OMIT_DESCENDANTS)
128 then: 'book titles from the retrieved data nodes are as expected'
129 def bookTitles = result.collect { it.getLeaves().get('title') }
130 assert bookTitles.sort() == expectedBookTitles.sort()
131 where: 'the following data is used'
132 scenario | cpsPath || expectedBookTitles
133 'one leaf' | '//books[@price=14]' || ['The Light Fantastic']
134 'one text' | '//books/authors[text()="Terry Pratchett"]' || ['Good Omens', 'The Colour of Magic', 'The Light Fantastic']
135 'more than one leaf' | '//books[@price=12 and @lang="English"]' || ['The Colour of Magic']
136 'leaves reversed in order' | '//books[@lang="English" and @price=12]' || ['The Colour of Magic']
137 'leaf and text' | '//books[@price=14]/authors[text()="Terry Pratchett"]' || ['The Light Fantastic']
140 def 'Cps Path query using descendant anywhere with #scenario condition(s) for a list element.'() {
141 when: 'a query is executed to get a data node by the given cps path'
142 def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, INCLUDE_ALL_DESCENDANTS)
143 then: 'xpaths of the retrieved data nodes are as expected'
144 result.xpath.toList() == ["/bookstore/premises/addresses[@house-number='2' and @street='Main Street']"]
145 where: 'the following data is used'
147 'full composite key' | '//addresses[@house-number=2 and @street="Main Street"]'
148 'one partial key leaf' | '//addresses[@house-number=2]'
149 'one non key leaf' | '//addresses[@county="Kildare"]'
150 'mix of partial key and non key leaf' | '//addresses[@street="Main Street" and @county="Kildare"]'
153 def 'Query for attribute by cps path of type ancestor with #scenario.'() {
154 when: 'the given cps path is parsed'
155 def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, cpsPath, OMIT_DESCENDANTS)
156 then: 'the xpaths of the retrieved data nodes are as expected'
157 assert result.xpath.sort() == expectedXPaths.sort()
158 where: 'the following data is used'
159 scenario | cpsPath || expectedXPaths
160 'multiple list-ancestors' | '//books/ancestor::categories' || ["/bookstore/categories[@code='1']", "/bookstore/categories[@code='2']", "/bookstore/categories[@code='3']", "/bookstore/categories[@code='4']"]
161 'one ancestor with list value' | '//books/ancestor::categories[@code="1"]' || ["/bookstore/categories[@code='1']"]
162 'top ancestor' | '//books/ancestor::bookstore' || ["/bookstore"]
163 'list with index value in the xpath prefix' | '//categories[@code="1"]/books/ancestor::bookstore' || ["/bookstore"]
164 'ancestor with parent list' | '//books/ancestor::bookstore/categories' || ["/bookstore/categories[@code='1']", "/bookstore/categories[@code='2']", "/bookstore/categories[@code='3']", "/bookstore/categories[@code='4']"]
165 'ancestor with parent' | '//books/ancestor::bookstore/categories[@code="2"]' || ["/bookstore/categories[@code='2']"]
166 'ancestor combined with text condition' | '//books/title[text()="Matilda"]/ancestor::bookstore' || ["/bookstore"]
167 'ancestor with parent that does not exist' | '//books/ancestor::parentDoesNoExist/categories' || []
168 'ancestor does not exist' | '//books/ancestor::ancestorDoesNotExist' || []
171 def 'Query for attribute by cps path of type ancestor with #scenario descendants.'() {
172 when: 'the given cps path is parsed'
173 def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, '//books/ancestor::bookstore', fetchDescendantsOption)
174 then: 'the xpaths of the retrieved data nodes are as expected'
175 assert countDataNodesInTree(result) == expectedNumberOfNodes
176 where: 'the following data is used'
177 scenario | fetchDescendantsOption || expectedNumberOfNodes
178 'no' | OMIT_DESCENDANTS || 1
179 'direct' | DIRECT_CHILDREN_ONLY || 6
180 'all' | INCLUDE_ALL_DESCENDANTS || 17
183 def 'Cps Path query with syntax error throws a CPS Path Exception.'() {
184 when: 'trying to execute a query with a syntax (parsing) error'
185 objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE_1, BOOKSTORE_ANCHOR_1, 'cpsPath that cannot be parsed' , OMIT_DESCENDANTS)
186 then: 'a cps path exception is thrown'
187 thrown(CpsPathException)
191 def 'Cps Path query across anchors with #scenario.'() {
192 when: 'a query is executed to get a data nodes across anchors by the given CpsPath'
193 def result = objectUnderTest.queryDataNodesAcrossAnchors(FUNCTIONAL_TEST_DATASPACE_1, cpsPath, OMIT_DESCENDANTS)
194 then: 'the correct dataspace is queried'
195 assert result.dataspace.toSet() == [FUNCTIONAL_TEST_DATASPACE_1].toSet()
196 and: 'correct anchors are queried'
197 assert result.anchorName.toSet() == [BOOKSTORE_ANCHOR_1, BOOKSTORE_ANCHOR_2].toSet()
198 and: 'the correct number of nodes is returned'
199 assert result.size() == expectedXpathsPerAnchor.size() * NUMBER_OF_ANCHORS_PER_DATASPACE_WITH_BOOKSTORE_DATA
200 and: 'the queried nodes have expected xpaths'
201 assert result.xpath.toSet() == expectedXpathsPerAnchor.toSet()
202 where: 'the following data is used'
203 scenario | cpsPath || expectedXpathsPerAnchor
204 'container node' | '/bookstore' || ["/bookstore"]
205 'list node' | '/bookstore/categories' || ["/bookstore/categories[@code='1']", "/bookstore/categories[@code='2']", "/bookstore/categories[@code='3']", "/bookstore/categories[@code='4']"]
206 'string leaf-condition' | '/bookstore[@bookstore-name="Easons"]' || ["/bookstore"]
207 'integer leaf-condition' | '/bookstore/categories[@code="1"]/books[@price=15]' || ["/bookstore/categories[@code='1']/books[@title='The Gruffalo']"]
208 'multiple list-ancestors' | '//books/ancestor::categories' || ["/bookstore/categories[@code='1']", "/bookstore/categories[@code='2']", "/bookstore/categories[@code='3']", "/bookstore/categories[@code='4']"]
209 'one ancestor with list value' | '//books/ancestor::categories[@code="1"]' || ["/bookstore/categories[@code='1']"]
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 list element' | '//books/ancestor::bookstore/categories[@code="2"]' || ["/bookstore/categories[@code='2']"]
213 'ancestor combined with text condition' | '//books/title[text()="Matilda"]/ancestor::bookstore' || ["/bookstore"]
217 def 'Cps Path query across anchors with #scenario descendants.'() {
218 when: 'a query is executed to get a data node by the given cps path'
219 def result = objectUnderTest.queryDataNodesAcrossAnchors(FUNCTIONAL_TEST_DATASPACE_1, '/bookstore', fetchDescendantsOption)
220 then: 'the correct dataspace was queried'
221 assert result.dataspace.toSet() == [FUNCTIONAL_TEST_DATASPACE_1].toSet()
222 and: 'correct number of datanodes are returned'
223 assert countDataNodesInTree(result) == expectedNumberOfNodesPerAnchor * NUMBER_OF_ANCHORS_PER_DATASPACE_WITH_BOOKSTORE_DATA
224 where: 'the following data is used'
225 scenario | fetchDescendantsOption || expectedNumberOfNodesPerAnchor
226 'no' | OMIT_DESCENDANTS || 1
227 'direct' | DIRECT_CHILDREN_ONLY || 6
228 'all' | INCLUDE_ALL_DESCENDANTS || 17
232 def 'Cps Path query across anchors with ancestors and #scenario descendants.'() {
233 when: 'a query is executed to get a data node by the given cps path'
234 def result = objectUnderTest.queryDataNodesAcrossAnchors(FUNCTIONAL_TEST_DATASPACE_1, '//books/ancestor::bookstore', fetchDescendantsOption)
235 then: 'the correct dataspace was queried'
236 assert result.dataspace.toSet() == [FUNCTIONAL_TEST_DATASPACE_1].toSet()
237 and: 'correct number of datanodes are returned'
238 assert countDataNodesInTree(result) == expectedNumberOfNodesPerAnchor * NUMBER_OF_ANCHORS_PER_DATASPACE_WITH_BOOKSTORE_DATA
239 where: 'the following data is used'
240 scenario | fetchDescendantsOption || expectedNumberOfNodesPerAnchor
241 'no' | OMIT_DESCENDANTS || 1
242 'direct' | DIRECT_CHILDREN_ONLY || 6
243 'all' | INCLUDE_ALL_DESCENDANTS || 17
246 def 'Cps Path query across anchors with syntax error throws a CPS Path Exception.'() {
247 when: 'trying to execute a query with a syntax (parsing) error'
248 objectUnderTest.queryDataNodesAcrossAnchors(FUNCTIONAL_TEST_DATASPACE_1, 'cpsPath that cannot be parsed' , OMIT_DESCENDANTS)
249 then: 'a cps path exception is thrown'
250 thrown(CpsPathException)