Merge "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 java.util.stream.Collectors
32
33 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
34 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
35
36 class CpsDataPersistenceQueryDataNodeSpec extends CpsPersistenceSpecBase {
37
38     @Autowired
39     CpsDataPersistenceService objectUnderTest
40
41     static final String SET_DATA = '/data/cps-path-query.sql'
42
43     @Sql([CLEAR_DATA, SET_DATA])
44     def 'Cps Path query for leaf value(s) with : #scenario.'() {
45         when: 'a query is executed to get a data node by the given cps path'
46             def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_SHOP_EXAMPLE, cpsPath, fetchDescendantsOption)
47         then: 'the correct number of parent nodes are returned'
48             result.size() == expectedNumberOfParentNodes
49         then: 'the correct data is returned'
50             result.each {
51                 assert it.getChildDataNodes().size() == expectedNumberOfChildNodes
52             }
53         where: 'the following data is used'
54             scenario                          | cpsPath                                                      | fetchDescendantsOption         || expectedNumberOfParentNodes | expectedNumberOfChildNodes
55             'String and no descendants'       | '/shops/shop[@id=1]/categories[@code=1]/book[@title="Dune"]' | OMIT_DESCENDANTS               || 1                           | 0
56             'Integer and descendants'         | '/shops/shop[@id=1]/categories[@code=1]/book[@price=5]'      | INCLUDE_ALL_DESCENDANTS        || 1                           | 1
57             'No condition no descendants'     | '/shops/shop[@id=1]/categories'                              | OMIT_DESCENDANTS               || 3                           | 0
58             'Integer and level 1 descendants' | '/shops'                                                     | new FetchDescendantsOption(1)  || 1                           | 5
59             'Integer and level 2 descendants' | '/shops/shop[@id=1]'                                         | new FetchDescendantsOption(2)  || 1                           | 3
60     }
61
62     @Sql([CLEAR_DATA, SET_DATA])
63     def 'Query for attribute by cps path with cps paths that return no data because of #scenario.'() {
64         when: 'a query is executed to get data nodes for the given cps path'
65             def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_SHOP_EXAMPLE, cpsPath, OMIT_DESCENDANTS)
66         then: 'no data is returned'
67             result.isEmpty()
68         where: 'following cps queries are performed'
69             scenario                         | cpsPath
70             'cps path is incomplete'         | '/shops[@title="Dune"]'
71             'leaf value does not exist'      | '/shops/shop[@id=1]/categories[@code=1]/book[@title=\'does not exist\']'
72             'incomplete end of xpath prefix' | '/shops/shop[@id=1]/categories/book[@price=15]'
73     }
74
75     @Sql([CLEAR_DATA, SET_DATA])
76     def 'Cps Path query using descendant anywhere and #type (further) descendants.'() {
77         when: 'a query is executed to get a data node by the given cps path'
78             def cpsPath = '//categories[@code=1]'
79             def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_SHOP_EXAMPLE, cpsPath, includeDescendantsOption)
80         then: 'the data node has the correct number of children'
81             def dataNode = result.stream().findFirst().get()
82             dataNode.getChildDataNodes().size() == expectedNumberOfChildNodes
83         where: 'the following data is used'
84             type      | includeDescendantsOption || expectedNumberOfChildNodes
85             'omit'    | OMIT_DESCENDANTS         || 0
86             'include' | INCLUDE_ALL_DESCENDANTS  || 1
87     }
88
89     @Sql([CLEAR_DATA, SET_DATA])
90     def 'Cps Path query using descendant anywhere with #scenario '() {
91         when: 'a query is executed to get a data node by the given cps path'
92             def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_SHOP_EXAMPLE, cpsPath, OMIT_DESCENDANTS)
93         then: 'the correct number of data nodes are retrieved'
94             result.size() == expectedXPaths.size()
95         and: 'xpaths of the retrieved data nodes are as expected'
96             for (int i = 0; i < result.size(); i++) {
97                 assert result[i].getXpath() == expectedXPaths[i]
98             }
99         where: 'the following data is used'
100             scenario                                                 | cpsPath                                                || expectedXPaths
101             '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']"]
102             'descendant name match end of other node'                | '//book'                                               || ["/shops/shop[@id='1']/categories[@code='1']/book", "/shops/shop[@id='1']/categories[@code='2']/book"]
103             'descendant with text condition on leaf'                 | '//book/title[text()="Chapters"]'                      || ["/shops/shop[@id='1']/categories[@code='2']/book"]
104             'descendant with text condition case mismatch'           | '//book/title[text()="chapters"]'                      || []
105             'descendant with text condition on int leaf'             | '//book/price[text()="5"]'                             || ["/shops/shop[@id='1']/categories[@code='1']/book"]
106             'descendant with text condition on leaf-list'            | '//book/labels[text()="special offer"]'                || ["/shops/shop[@id='1']/categories[@code='1']/book"]
107             'descendant with text condition partial match'           | '//book/labels[text()="special"]'                      || []
108             'descendant with text condition (existing) empty string' | '//book/labels[text()=""]'                             || ["/shops/shop[@id='1']/categories[@code='1']/book"]
109             'descendant with text condition on int leaf-list'        | '//book/editions[text()="2000"]'                       || ["/shops/shop[@id='1']/categories[@code='2']/book"]
110             'descendant name match of leaf containing /'             | '//categories/type[text()="text/with/slash"]'          || ["/shops/shop[@id='1']/categories[@code='string/with/slash/']"]
111             'descendant with text condition on leaf containing /'    | '//categories[@code=\'string/with/slash\']'            || ["/shops/shop[@id='1']/categories[@code='string/with/slash/']"]
112             'descendant with text condition on leaf containing ['    | '//book/author[@Address="String[with]square[bracket]"]'|| []
113     }
114
115     @Sql([CLEAR_DATA, SET_DATA])
116     def 'Cps Path query using descendant anywhere with #scenario condition(s) for a container element.'() {
117         when: 'a query is executed to get a data node by the given cps path'
118             def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_SHOP_EXAMPLE, cpsPath, OMIT_DESCENDANTS)
119         then: 'the correct number of data nodes are retrieved'
120             result.size() == expectedXPaths.size()
121         and: 'xpaths of the retrieved data nodes are as expected'
122             for (int i = 0; i < result.size(); i++) {
123                 assert result[i].getXpath() == expectedXPaths[i]
124             }
125         where: 'the following data is used'
126             scenario                   | cpsPath                                               || expectedXPaths
127             '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']"]
128             'more than one leaf'       | '//author[@FirstName="Joe" and @Surname="Bloggs"]'    || ["/shops/shop[@id='1']/categories[@code='1']/book/author[@FirstName='Joe' and @Surname='Bloggs']"]
129             'leaves reversed in order' | '//author[@Surname="Bloggs" and @FirstName="Joe"]'    || ["/shops/shop[@id='1']/categories[@code='1']/book/author[@FirstName='Joe' and @Surname='Bloggs']"]
130             'leaf and text condition'  | '//author[@FirstName="Joe"]/Surname[text()="Bloggs"]' || ["/shops/shop[@id='1']/categories[@code='1']/book/author[@FirstName='Joe' and @Surname='Bloggs']"]
131     }
132
133     @Sql([CLEAR_DATA, SET_DATA])
134     def 'Cps Path query using descendant anywhere with #scenario condition(s) for a list element.'() {
135         when: 'a query is executed to get a data node by the given cps path'
136             def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_SHOP_EXAMPLE, cpsPath, OMIT_DESCENDANTS)
137         then: 'the correct number of data nodes are retrieved'
138             result.size() == expectedXPaths.size()
139         and: 'xpaths of the retrieved data nodes are as expected'
140             for (int i = 0; i < result.size(); i++) {
141                 assert result[i].getXpath() == expectedXPaths[i]
142             }
143         where: 'the following data is used'
144             scenario                              | cpsPath                                        || expectedXPaths
145             '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']"]
146             'one non key leaf'                    | '//author[@title="Dune"]'                      || ["/shops/shop[@id='1']/categories[@code='1']/book/author[@FirstName='Joe' and @Surname='Bloggs']"]
147             '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']"]
148     }
149
150     @Sql([CLEAR_DATA, SET_DATA])
151     def 'Query for attribute by cps path of type ancestor with #scenario.'() {
152         when: 'the given cps path is parsed'
153             def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_SHOP_EXAMPLE, cpsPath, INCLUDE_ALL_DESCENDANTS)
154         then: 'the xpaths of the retrieved data nodes are as expected'
155             result.size() == expectedXPaths.size()
156             if (result.size() > 0) {
157                 def resultXpaths = result.stream().map(it -> it.xpath).collect(Collectors.toSet())
158                 resultXpaths.containsAll(expectedXPaths)
159                 result.each {
160                     assert it.childDataNodes.size() == expectedNumberOfChildren
161                 }
162             }
163         where: 'the following data is used'
164             scenario                                    | cpsPath                                              || expectedXPaths                                                                               || expectedNumberOfChildren
165             'multiple list-ancestors'                   | '//book/ancestor::categories'                        || ["/shops/shop[@id='1']/categories[@code='2']", "/shops/shop[@id='1']/categories[@code='1']"] || 1
166             'one ancestor with list value'              | '//book/ancestor::categories[@code=1]'               || ["/shops/shop[@id='1']/categories[@code='1']"]                                               || 1
167             'top ancestor'                              | '//shop[@id=1]/ancestor::shops'                      || ['/shops']                                                                                   || 5
168             'list with index value in the xpath prefix' | '//categories[@code=1]/book/ancestor::shop[@id=1]'   || ["/shops/shop[@id='1']"]                                                                     || 3
169             'ancestor with parent list'                 | '//book/ancestor::shop[@id=1]/categories[@code=2]'   || ["/shops/shop[@id='1']/categories[@code='2']"]                                               || 1
170             'ancestor with parent'                      | '//phonenumbers[@type="mob"]/ancestor::info/contact' || ["/shops/shop[@id='3']/info/contact"]                                                        || 3
171             'ancestor combined with text condition'     | '//book/title[text()="Dune"]/ancestor::shop'         || ["/shops/shop[@id='1']"]                                                                     || 3
172             'ancestor with parent that does not exist'  | '//book/ancestor::parentDoesNoExist/categories'      || []                                                                                           || null
173             'ancestor does not exist'                   | '//book/ancestor::ancestorDoesNotExist'              || []                                                                                           || null
174     }
175
176     def 'Cps Path query with syntax error throws a CPS Path Exception.'() {
177         when: 'trying to execute a query with a syntax (parsing) error'
178             objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_SHOP_EXAMPLE, 'cpsPath that cannot be parsed' , OMIT_DESCENDANTS)
179         then: 'a cps path exception is thrown'
180             thrown(CpsPathException)
181     }
182
183 }