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