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
11 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
21 package org.onap.cps.spi.impl
23 import org.onap.cps.spi.CpsDataPersistenceService
24 import org.onap.cps.spi.exceptions.CpsPathException
25 import org.onap.cps.spi.model.DataNode
26 import org.springframework.beans.factory.annotation.Autowired
27 import org.springframework.test.context.jdbc.Sql
29 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
30 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
32 class CpsDataPersistenceQueryDataNodeSpec extends CpsPersistenceSpecBase {
35 CpsDataPersistenceService objectUnderTest
37 static final String SET_DATA = '/data/cps-path-query.sql'
39 @Sql([CLEAR_DATA, SET_DATA])
40 def 'Cps Path query for single leaf value with type: #type.'() {
41 when: 'a query is executed to get a data node by the given cps path'
42 def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_SHOP_EXAMPLE, cpsPath, includeDescendantsOption)
43 then: 'the correct data is returned'
44 def leaves = '[price:15.0, title:Dune]'
45 DataNode dataNode = result.stream().findFirst().get()
46 dataNode.getLeaves().toString() == leaves
47 dataNode.getChildDataNodes().size() == expectedNumberOfChidlNodes
48 where: 'the following data is used'
49 type | cpsPath | includeDescendantsOption || expectedNumberOfChidlNodes
50 'String and no descendants' | '/shops/shop[@id=1]/categories[@code=1]/book[@title="Dune"]' | OMIT_DESCENDANTS || 0
51 'Integer and descendants' | '/shops/shop[@id=1]/categories[@code=1]/book[@price=15]' | INCLUDE_ALL_DESCENDANTS || 1
54 @Sql([CLEAR_DATA, SET_DATA])
55 def 'Query for attribute by cps path with cps paths that return no data because of #scenario.'() {
56 when: 'a query is executed to get datanodes for the given cps path'
57 def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_SHOP_EXAMPLE, cpsPath, OMIT_DESCENDANTS)
58 then: 'no data is returned'
60 where: 'following cps queries are performed'
62 'cps path is incomplete' | '/shops[@title="Dune"]'
63 'leaf value does not exist' | '/shops/shop[@id=1]/categories[@code=1]/book[@title=\'does not exist\']'
64 'incomplete end of xpath prefix' | '/shops/shop[@id=1]/categories/book[@price=15]'
67 @Sql([CLEAR_DATA, SET_DATA])
68 def 'Cps Path query using descendant anywhere and #type (further) descendants.'() {
69 when: 'a query is executed to get a data node by the given cps path'
70 def cpsPath = '//categories[@code=1]'
71 def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_SHOP_EXAMPLE, cpsPath, includeDescendantsOption)
72 then: 'the data node has the correct number of children'
73 DataNode dataNode = result.stream().findFirst().get()
74 dataNode.getChildDataNodes().size() == expectedNumberOfChildNodes
75 where: 'the following data is used'
76 type | includeDescendantsOption || expectedNumberOfChildNodes
77 'omit' | OMIT_DESCENDANTS || 0
78 'include' | INCLUDE_ALL_DESCENDANTS || 1
81 @Sql([CLEAR_DATA, SET_DATA])
82 def 'Cps Path query using descendant anywhere with #scenario '() {
83 when: 'a query is executed to get a data node by the given cps path'
84 def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_SHOP_EXAMPLE, cpsPath, OMIT_DESCENDANTS)
85 then: 'the correct number of data nodes are retrieved'
86 result.size() == expectedXPaths.size()
87 and: 'xpaths of the retrieved data nodes are as expected'
88 for (int i = 0; i < result.size(); i++) {
89 assert result[i].getXpath() == expectedXPaths[i]
91 where: 'the following data is used'
92 scenario | cpsPath || expectedXPaths
93 '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]']
94 'descendant name match end of other node' | '//book' || ['/shops/shop[@id=1]/categories[@code=1]/book', '/shops/shop[@id=1]/categories[@code=2]/book']
97 @Sql([CLEAR_DATA, SET_DATA])
98 def 'Cps Path query using descendant anywhere with #scenario condition(s) for a container element.'() {
99 when: 'a query is executed to get a data node by the given cps path'
100 def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_SHOP_EXAMPLE, cpsPath, OMIT_DESCENDANTS)
101 then: 'the correct number of data nodes are retrieved'
102 result.size() == expectedXPaths.size()
103 and: 'xpaths of the retrieved data nodes are as expected'
104 for (int i = 0; i < result.size(); i++) {
105 assert result[i].getXpath() == expectedXPaths[i]
107 where: 'the following data is used'
108 scenario | cpsPath || expectedXPaths
109 '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"]']
110 'more than one leaf' | '//author[@FirstName="Joe" and @Surname="Bloggs"]' || ['/shops/shop[@id=1]/categories[@code=1]/book/author[@FirstName="Joe" and @Surname="Bloggs"]']
111 'leaves reversed in order' | '//author[@Surname="Bloggs" and @FirstName="Joe"]' || ['/shops/shop[@id=1]/categories[@code=1]/book/author[@FirstName="Joe" and @Surname="Bloggs"]']
114 @Sql([CLEAR_DATA, SET_DATA])
115 def 'Cps Path query using descendant anywhere with #scenario condition(s) for a list element.'() {
116 when: 'a query is executed to get a data node by the given cps path'
117 def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_SHOP_EXAMPLE, cpsPath, OMIT_DESCENDANTS)
118 then: 'the correct number of data nodes are retrieved'
119 result.size() == expectedXPaths.size()
120 and: 'xpaths of the retrieved data nodes are as expected'
121 for (int i = 0; i < result.size(); i++) {
122 assert result[i].getXpath() == expectedXPaths[i]
124 where: 'the following data is used'
125 scenario | cpsPath || expectedXPaths
126 '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"]']
127 'one non key leaf' | '//author[@title="Dune"]' || ['/shops/shop[@id=1]/categories[@code=1]/book/author[@FirstName="Joe" and @Surname="Bloggs"]']
128 '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"]']
131 @Sql([CLEAR_DATA, SET_DATA])
132 def 'Cps Path query error scenario using descendant anywhere ends with yang list containing %scenario '() {
133 when: 'a query is executed to get a data node by the given cps path'
134 objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_SHOP_EXAMPLE, cpsPath, OMIT_DESCENDANTS)
135 then: 'exception is thrown'
136 thrown(CpsPathException)
137 where: 'the following data is used'
139 'one of the leaf without value' | '//categories[@code=1 and @name=]'
140 'more than one leaf separated by or' | '//categories[@code=1 or @name="SciFi"]'
143 @Sql([CLEAR_DATA, SET_DATA])
144 def 'Query for attribute by cps path of type ancestor with #scenario.'() {
145 when: 'the given cps path is parsed'
146 def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_SHOP_EXAMPLE, cpsPath, INCLUDE_ALL_DESCENDANTS)
147 then: 'the xpaths of the retrieved data nodes are as expected'
148 result.size() == expectedXPaths.size()
149 for (int i = 0; i < result.size(); i++) {
150 assert result[i].getXpath() == expectedXPaths[i]
152 where: 'the following data is used'
153 scenario | cpsPath || expectedXPaths
154 'multiple list-ancestors' | '//book/ancestor::categories' || ['/shops/shop[@id=1]/categories[@code=1]', '/shops/shop[@id=1]/categories[@code=2]']
155 'one ancestor with list value' | '//book/ancestor::categories[@code=1]' || ['/shops/shop[@id=1]/categories[@code=1]']
156 'top ancestor' | '//shop[@id=1]/ancestor::shops' || ['/shops']
157 'list with index value in the xpath prefix' | '//categories[@code=1]/book/ancestor::shop[@id=1]' || ['/shops/shop[@id=1]']
158 'ancestor with parent list' | '//book/ancestor::shop[@id=1]/categories[@code=2]' || ['/shops/shop[@id=1]/categories[@code=2]']
159 'ancestor with parent' | '//phonenumbers[@type="mob"]/ancestor::info/contact' || ['/shops/shop[@id=3]/info/contact']
160 'ancestor with parent that does not exist' | '//book/ancestor::/parentDoesNoExist/categories' || []
161 'ancestor does not exist' | '//book/ancestor::ancestorDoesNotExist' || []