a33d9fa5e4db81d957f887f9b347cb867861bf3a
[cps.git] / integration-test / src / test / groovy / org / onap / cps / integration / functional / CpsQueryServiceIntegrationSpec.groovy
1 /*
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.integration.functional
22
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 org.springframework.test.context.jdbc.Sql
28
29 import java.util.stream.Collectors
30
31 import static org.onap.cps.spi.FetchDescendantsOption.DIRECT_CHILDREN_ONLY
32 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
33 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
34 import static org.onap.cps.spi.FetchDescendantsOption.getFetchDescendantsOption
35
36 class CpsQueryServiceIntegrationSpec extends FunctionalSpecBase {
37
38     CpsQueryService objectUnderTest
39
40     def setup() { objectUnderTest = cpsQueryService }
41
42     def 'Query bookstore using CPS path where #scenario.'() {
43         when: 'query data nodes for bookstore container'
44             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE, BOOKSTORE_ANCHOR, cpsPath, INCLUDE_ALL_DESCENDANTS)
45         then: 'the result contains expected number of nodes'
46             assert result.size() == expectedResultSize
47         and: 'the result contains the expected leaf values'
48             result.leaves.forEach( dataNodeLeaves -> {
49                 expectedLeaves.forEach( (expectedLeafKey,expectedLeafValue) -> {
50                     assert dataNodeLeaves[expectedLeafKey] == expectedLeafValue
51                 })
52             })
53         where:
54             scenario                                      | cpsPath                                    || expectedResultSize | expectedLeaves
55             'the and condition is used'                   | '//books[@lang="English" and @price=15]'   || 2                  | [lang:"English", price:15]
56             'the and is used where result does not exist' | '//books[@lang="English" and @price=1000]' || 0                  | []
57     }
58
59     def 'Query for attribute by cps path of type ancestor with #scenario.'() {
60         when: 'the given cps path is parsed'
61             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE, BOOKSTORE_ANCHOR, cpsPath, OMIT_DESCENDANTS)
62         then: 'the xpaths of the retrieved data nodes are as expected'
63             assert result.xpath.sort() == expectedXPaths.sort()
64         where: 'the following data is used'
65             scenario                                    | cpsPath                                               || expectedXPaths
66             'multiple list-ancestors'                   | '//books/ancestor::categories'                        || ["/bookstore/categories[@code='1']", "/bookstore/categories[@code='2']", "/bookstore/categories[@code='3']"]
67             'one ancestor with list value'              | '//books/ancestor::categories[@code="1"]'             || ["/bookstore/categories[@code='1']"]
68             'top ancestor'                              | '//books/ancestor::bookstore'                         || ["/bookstore"]
69             'list with index value in the xpath prefix' | '//categories[@code="1"]/books/ancestor::bookstore'   || ["/bookstore"]
70             'ancestor with parent list'                 | '//books/ancestor::bookstore/categories'              || ["/bookstore/categories[@code='1']", "/bookstore/categories[@code='2']", "/bookstore/categories[@code='3']"]
71             'ancestor with parent'                      | '//books/ancestor::bookstore/categories[@code="2"]'   || ["/bookstore/categories[@code='2']"]
72             'ancestor combined with text condition'     | '//books/title[text()="Matilda"]/ancestor::bookstore' || ["/bookstore"]
73             'ancestor with parent that does not exist'  | '//books/ancestor::parentDoesNoExist/categories'      || []
74             'ancestor does not exist'                   | '//books/ancestor::ancestorDoesNotExist'              || []
75     }
76
77     def 'Query for attribute by cps path of type ancestor with #scenario descendants.'() {
78         when: 'the given cps path is parsed'
79             def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE, BOOKSTORE_ANCHOR, '//books/ancestor::bookstore', fetchDescendantsOption)
80         then: 'the xpaths of the retrieved data nodes are as expected'
81             assert countDataNodesInTree(result) == expectedNumberOfNodes
82         where: 'the following data is used'
83             scenario | fetchDescendantsOption  || expectedNumberOfNodes
84             'no'     | OMIT_DESCENDANTS        || 1
85             'direct' | DIRECT_CHILDREN_ONLY    || 4
86             'all'    | INCLUDE_ALL_DESCENDANTS || 8
87     }
88
89     def 'Cps Path query with syntax error throws a CPS Path Exception.'() {
90         when: 'trying to execute a query with a syntax (parsing) error'
91             objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE, BOOKSTORE_ANCHOR, 'cpsPath that cannot be parsed' , OMIT_DESCENDANTS)
92         then: 'a cps path exception is thrown'
93             thrown(CpsPathException)
94     }
95
96 }