Added depth parameter in query nodes API.
[cps.git] / cps-ri / src / test / groovy / org / onap / cps / spi / impl / CpsDataPersistenceQueryDataNodeSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2022 Nordix Foundation
4  *  Modifications Copyright (C) 2021 Pantheon.tech
5  *  Modifications Copyright (C) 2021 Bell Canada.
6  *  Modifications Copyright (C) 2023 TechMahindra Ltd.
7  *  ================================================================================
8  *  Licensed under the Apache License, Version 2.0 (the "License");
9  *  you may not use this file except in compliance with the License.
10  *  You may obtain a copy of the License at
11  *
12  *        http://www.apache.org/licenses/LICENSE-2.0
13  *
14  *  Unless required by applicable law or agreed to in writing, software
15  *  distributed under the License is distributed on an "AS IS" BASIS,
16  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  *  See the License for the specific language governing permissions and
18  *  limitations under the License.
19  *
20  *  SPDX-License-Identifier: Apache-2.0
21  *  ============LICENSE_END=========================================================
22  */
23 package org.onap.cps.spi.impl
24
25 import org.onap.cps.spi.CpsDataPersistenceService
26 import org.onap.cps.spi.FetchDescendantsOption
27 import org.onap.cps.spi.exceptions.CpsPathException
28 import org.springframework.beans.factory.annotation.Autowired
29 import org.springframework.test.context.jdbc.Sql
30
31 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
32 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
33
34 class CpsDataPersistenceQueryDataNodeSpec extends CpsPersistenceSpecBase {
35
36     @Autowired
37     CpsDataPersistenceService objectUnderTest
38
39     static final String SET_DATA = '/data/cps-path-query.sql'
40
41     @Sql([CLEAR_DATA, SET_DATA])
42     def 'Cps Path query for leaf value(s) with : #scenario.'() {
43         when: 'a query is executed to get a data node by the given cps path'
44             def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_SHOP_EXAMPLE, cpsPath, fetchDescendantsOption)
45         then: 'the correct number of parent nodes are returned'
46             result.size() == expectedNumberOfParentNodes
47         then: 'the correct data is returned'
48             result.each {
49                 assert it.getChildDataNodes().size() == expectedNumberOfChildNodes
50             }
51         where: 'the following data is used'
52             scenario                          | cpsPath                                                      | fetchDescendantsOption         || expectedNumberOfParentNodes | expectedNumberOfChildNodes
53             'String and no descendants'       | '/shops/shop[@id=1]/categories[@code=1]/book[@title="Dune"]' | OMIT_DESCENDANTS               || 1                           | 0
54             'Integer and descendants'         | '/shops/shop[@id=1]/categories[@code=1]/book[@price=5]'      | INCLUDE_ALL_DESCENDANTS        || 1                           | 1
55             'No condition no descendants'     | '/shops/shop[@id=1]/categories'                              | OMIT_DESCENDANTS               || 3                           | 0
56             'Integer and level 1 descendants' | '/shops'                                                     | new FetchDescendantsOption(1)  || 1                           | 5
57             'Integer and level 2 descendants' | '/shops/shop[@id=1]'                                         | new FetchDescendantsOption(2)  || 1                           | 3
58     }
59
60     @Sql([CLEAR_DATA, SET_DATA])
61     def 'Query for attribute by cps path with cps paths that return no data because of #scenario.'() {
62         when: 'a query is executed to get data nodes for the given cps path'
63             def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_SHOP_EXAMPLE, cpsPath, OMIT_DESCENDANTS)
64         then: 'no data is returned'
65             result.isEmpty()
66         where: 'following cps queries are performed'
67             scenario                         | cpsPath
68             'cps path is incomplete'         | '/shops[@title="Dune"]'
69             'leaf value does not exist'      | '/shops/shop[@id=1]/categories[@code=1]/book[@title=\'does not exist\']'
70             'incomplete end of xpath prefix' | '/shops/shop[@id=1]/categories/book[@price=15]'
71     }
72
73     @Sql([CLEAR_DATA, SET_DATA])
74     def 'Cps Path query using descendant anywhere and #type (further) descendants.'() {
75         when: 'a query is executed to get a data node by the given cps path'
76             def cpsPath = '//categories[@code=1]'
77             def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_SHOP_EXAMPLE, cpsPath, includeDescendantsOption)
78         then: 'the data node has the correct number of children'
79             def dataNode = result.stream().findFirst().get()
80             dataNode.getChildDataNodes().size() == expectedNumberOfChildNodes
81         where: 'the following data is used'
82             type      | includeDescendantsOption || expectedNumberOfChildNodes
83             'omit'    | OMIT_DESCENDANTS         || 0
84             'include' | INCLUDE_ALL_DESCENDANTS  || 1
85     }
86
87     @Sql([CLEAR_DATA, SET_DATA])
88     def 'Cps Path query using descendant anywhere with #scenario '() {
89         when: 'a query is executed to get a data node by the given cps path'
90             def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_SHOP_EXAMPLE, cpsPath, OMIT_DESCENDANTS)
91         then: 'the correct number of data nodes are retrieved'
92             result.size() == expectedXPaths.size()
93         and: 'xpaths of the retrieved data nodes are as expected'
94             for (int i = 0; i < result.size(); i++) {
95                 assert result[i].getXpath() == expectedXPaths[i]
96             }
97         where: 'the following data is used'
98             scenario                                                 | cpsPath                                                || expectedXPaths
99             'fully unique descendant name'                           | '//categories[@code=2]'                                || ["/shops/shop[@id='1']/categories[@code='2']", "/shops/shop[@id='2']/categories[@code='1']", "/shops/shop[@id='2']/categories[@code='2']"]
100             'descendant name match end of other node'                | '//book'                                               || ["/shops/shop[@id='1']/categories[@code='1']/book", "/shops/shop[@id='1']/categories[@code='2']/book"]
101             'descendant with text condition on leaf'                 | '//book/title[text()="Chapters"]'                      || ["/shops/shop[@id='1']/categories[@code='2']/book"]
102             'descendant with text condition case mismatch'           | '//book/title[text()="chapters"]'                      || []
103             'descendant with text condition on int leaf'             | '//book/price[text()="5"]'                             || ["/shops/shop[@id='1']/categories[@code='1']/book"]
104             'descendant with text condition on leaf-list'            | '//book/labels[text()="special offer"]'                || ["/shops/shop[@id='1']/categories[@code='1']/book"]
105             'descendant with text condition partial match'           | '//book/labels[text()="special"]'                      || []
106             'descendant with text condition (existing) empty string' | '//book/labels[text()=""]'                             || ["/shops/shop[@id='1']/categories[@code='1']/book"]
107             'descendant with text condition on int leaf-list'        | '//book/editions[text()="2000"]'                       || ["/shops/shop[@id='1']/categories[@code='2']/book"]
108             'descendant name match of leaf containing /'             | '//categories/type[text()="text/with/slash"]'          || ["/shops/shop[@id='1']/categories[@code='string/with/slash/']"]
109             'descendant with text condition on leaf containing /'    | '//categories[@code=\'string/with/slash\']'            || ["/shops/shop[@id='1']/categories[@code='string/with/slash/']"]
110             'descendant with text condition on leaf containing ['    | '//book/author[@Address="String[with]square[bracket]"]'|| []
111     }
112
113     @Sql([CLEAR_DATA, SET_DATA])
114     def 'Cps Path query using descendant anywhere with #scenario condition(s) for a container element.'() {
115         when: 'a query is executed to get a data node by the given cps path'
116             def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_SHOP_EXAMPLE, cpsPath, OMIT_DESCENDANTS)
117         then: 'the correct number of data nodes are retrieved'
118             result.size() == expectedXPaths.size()
119         and: 'xpaths of the retrieved data nodes are as expected'
120             for (int i = 0; i < result.size(); i++) {
121                 assert result[i].getXpath() == expectedXPaths[i]
122             }
123         where: 'the following data is used'
124             scenario                   | cpsPath                                               || expectedXPaths
125             'one leaf'                 | '//author[@FirstName="Joe"]'                          || ["/shops/shop[@id='1']/categories[@code='1']/book/author[@FirstName='Joe' and @Surname='Bloggs']", "/shops/shop[@id='1']/categories[@code='2']/book/author[@FirstName='Joe' and @Surname='Smith']"]
126             'more than one leaf'       | '//author[@FirstName="Joe" and @Surname="Bloggs"]'    || ["/shops/shop[@id='1']/categories[@code='1']/book/author[@FirstName='Joe' and @Surname='Bloggs']"]
127             'leaves reversed in order' | '//author[@Surname="Bloggs" and @FirstName="Joe"]'    || ["/shops/shop[@id='1']/categories[@code='1']/book/author[@FirstName='Joe' and @Surname='Bloggs']"]
128             'leaf and text condition'  | '//author[@FirstName="Joe"]/Surname[text()="Bloggs"]' || ["/shops/shop[@id='1']/categories[@code='1']/book/author[@FirstName='Joe' and @Surname='Bloggs']"]
129     }
130
131     @Sql([CLEAR_DATA, SET_DATA])
132     def 'Cps Path query using descendant anywhere with #scenario condition(s) for a list element.'() {
133         when: 'a query is executed to get a data node by the given cps path'
134             def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_SHOP_EXAMPLE, cpsPath, OMIT_DESCENDANTS)
135         then: 'the correct number of data nodes are retrieved'
136             result.size() == expectedXPaths.size()
137         and: 'xpaths of the retrieved data nodes are as expected'
138             for (int i = 0; i < result.size(); i++) {
139                 assert result[i].getXpath() == expectedXPaths[i]
140             }
141         where: 'the following data is used'
142             scenario                              | cpsPath                                        || expectedXPaths
143             'one partial key leaf'                | '//author[@FirstName="Joe"]'                   || ["/shops/shop[@id='1']/categories[@code='1']/book/author[@FirstName='Joe' and @Surname='Bloggs']", "/shops/shop[@id='1']/categories[@code='2']/book/author[@FirstName='Joe' and @Surname='Smith']"]
144             'one non key leaf'                    | '//author[@title="Dune"]'                      || ["/shops/shop[@id='1']/categories[@code='1']/book/author[@FirstName='Joe' and @Surname='Bloggs']"]
145             'mix of partial key and non key leaf' | '//author[@FirstName="Joe" and @title="Dune"]' || ["/shops/shop[@id='1']/categories[@code='1']/book/author[@FirstName='Joe' and @Surname='Bloggs']"]
146     }
147
148     @Sql([CLEAR_DATA, SET_DATA])
149     def 'Query for attribute by cps path of type ancestor with #scenario.'() {
150         when: 'the given cps path is parsed'
151             def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_SHOP_EXAMPLE, cpsPath, INCLUDE_ALL_DESCENDANTS)
152         then: 'the xpaths of the retrieved data nodes are as expected'
153             result.size() == expectedXPaths.size()
154             for (int i = 0; i < result.size(); i++) {
155                 assert result[i].getXpath() == expectedXPaths[i]
156                 assert result[i].childDataNodes.size() == expectedNumberOfChildren[i]
157             }
158         where: 'the following data is used'
159             scenario                                    | cpsPath                                              || expectedXPaths                                                                               || expectedNumberOfChildren
160             'multiple list-ancestors'                   | '//book/ancestor::categories'                        || ["/shops/shop[@id='1']/categories[@code='1']", "/shops/shop[@id='1']/categories[@code='2']"] || [1, 1]
161             'one ancestor with list value'              | '//book/ancestor::categories[@code=1]'               || ["/shops/shop[@id='1']/categories[@code='1']"]                                               || [1]
162             'top ancestor'                              | '//shop[@id=1]/ancestor::shops'                      || ['/shops']                                                                                   || [5]
163             'list with index value in the xpath prefix' | '//categories[@code=1]/book/ancestor::shop[@id=1]'   || ["/shops/shop[@id='1']"]                                                                     || [3]
164             'ancestor with parent list'                 | '//book/ancestor::shop[@id=1]/categories[@code=2]'   || ["/shops/shop[@id='1']/categories[@code='2']"]                                               || [1]
165             'ancestor with parent'                      | '//phonenumbers[@type="mob"]/ancestor::info/contact' || ["/shops/shop[@id='3']/info/contact"]                                                        || [3]
166             'ancestor combined with text condition'     | '//book/title[text()="Dune"]/ancestor::shop'         || ["/shops/shop[@id='1']"]                                                                     || [3]
167             'ancestor with parent that does not exist'  | '//book/ancestor::parentDoesNoExist/categories'      || []                                                                                           || []
168             'ancestor does not exist'                   | '//book/ancestor::ancestorDoesNotExist'              || []                                                                                           || []
169     }
170
171     def 'Cps Path query with syntax error throws a CPS Path Exception.'() {
172         when: 'trying to execute a query with a syntax (parsing) error'
173             objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_SHOP_EXAMPLE, 'cpsPath that cannot be parsed' , OMIT_DESCENDANTS)
174         then: 'exception is thrown'
175             thrown(CpsPathException)
176     }
177
178 }