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.FetchDescendantsOption
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
30 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
31 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
33 class CpsDataPersistenceQueryDataNodeSpec extends CpsPersistenceSpecBase {
36 CpsDataPersistenceService objectUnderTest
38 static final String SET_DATA = '/data/fragment.sql'
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_DATA_NODES_WITH_LEAVES, cpsPath, includeDescendantsOption)
44 then: 'the correct data is returned'
45 def leaves = '[common-leaf-name:common-leaf value, common-leaf-name-int:5.0]'
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' | '/parent-200/child-202[@common-leaf-name=\'common-leaf value\']' | OMIT_DESCENDANTS || 0
52 'Integer and descendants' | '/parent-200/child-202[@common-leaf-name-int=5]' | INCLUDE_ALL_DESCENDANTS || 1
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_DATA_NODES_WITH_LEAVES, cpsPath, FetchDescendantsOption.OMIT_DESCENDANTS)
59 then: 'no data is returned'
61 where: 'following cps queries are performed'
63 'cps path is incomplete' | '/parent-200[@common-leaf-name-int=5]'
64 'leaf value does not exist' | '/parent-200/child-202[@common-leaf-name=\'does not exist\']'
65 'incomplete end of xpath prefix' | '/parent-200/child-20[@common-leaf-name-int=5]'
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 = '//child-202'
72 def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, 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
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_DATA_NODES_WITH_LEAVES, 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]
92 where: 'the following data is used'
93 scenario | cpsPath || expectedXPaths
94 'fully unique descendant name' | '//grand-child-202' || ['/parent-200/child-202/grand-child-202']
95 'descendant name match end of other node' | '//child-202' || ['/parent-200/child-202', '/parent-201/child-202']
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_DATA_NODES_WITH_LEAVES, 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]
108 where: 'the following data is used'
109 scenario | cpsPath || expectedXPaths
110 'one leaf' | '//child-202[@common-leaf-name-int=5]' || ['/parent-200/child-202','/parent-201/child-202']
111 'trailing "and" is ignored' | '//child-202[@common-leaf-name-int=5 and]' || ['/parent-200/child-202','/parent-201/child-202']
112 'more than one leaf' | '//child-202[@common-leaf-name-int=5 and @common-leaf-name="common-leaf value"]' || ['/parent-200/child-202']
113 'leaves reversed in order' | '//child-202[@common-leaf-name="common-leaf value" and @common-leaf-name-int=5]' || ['/parent-200/child-202']
116 @Sql([CLEAR_DATA, SET_DATA])
117 def 'Cps Path query using descendant anywhere with #scenario condition(s) for a list element.'() {
118 when: 'a query is executed to get a data node by the given cps path'
119 def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, cpsPath, OMIT_DESCENDANTS)
120 then: 'the correct number of data nodes are retrieved'
121 result.size() == expectedXPaths.size()
122 and: 'xpaths of the retrieved data nodes are as expected'
123 for(int i = 0; i<result.size(); i++) {
124 assert result[i].getXpath() == expectedXPaths[i]
126 where: 'the following data is used'
127 scenario | cpsPath || expectedXPaths
128 'one partial key leaf' | '//child-203[@key1="A"]' || ['/parent-201/child-203[@key1="A" and @key2=1]','/parent-201/child-203[@key1="A" and @key2=2]']
129 'one non key leaf' | '//child-203[@other-leaf="other value"]' || ['/parent-201/child-203[@key1="A" and @key2=2]']
130 'mix of partial key and non key leaf' | '//child-203[@key1="A" and @other-leaf="leaf value"]' || ['/parent-201/child-203[@key1="A" and @key2=1]']
133 @Sql([CLEAR_DATA, SET_DATA])
134 def 'Cps Path query error scenario using descendant anywhere ends with yang list containing %scenario '() {
135 when: 'a query is executed to get a data node by the given cps path'
136 objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, cpsPath, OMIT_DESCENDANTS)
137 then: 'exception is thrown'
138 thrown(CpsPathException)
139 where: 'the following data is used'
141 'one of the leaf without value' | '//child-202[@common-leaf-name-int=5 and @another-attribute"]'
142 'more than one leaf separated by or' | '//child-202[@common-leaf-name-int=5 or @common-leaf-name="common-leaf value"]'
145 @Sql([CLEAR_DATA, SET_DATA])
146 def 'Query for attribute by cps path of type ancestor with #scenario.'() {
147 when: 'the given cps path is parsed'
148 def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_NAME1, cpsPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS)
149 then: 'the xpaths of the retrieved data nodes are as expected'
150 result.size() == expectedXPaths.size()
151 for (int i = 0; i < result.size(); i++) {
152 assert result[i].getXpath() == expectedXPaths[i]
154 where: 'the following data is used'
155 scenario | cpsPath || expectedXPaths
156 'multiple list-ancestors' | '//books/ancestor::categories' || ['/bookstore/books/categories[@name="SciFi"]', '/bookstore/magazines/categories[@name="kids"]']
157 'one ancestor value' | '//books/ancestor::books' || ['/bookstore/books']
158 'top ancestor' | '//books/ancestor::bookstore' || ['/bookstore']
159 'list with index value in the xpath prefix' | '//categories[@name="kids"]/books/ancestor::bookstore' || ['/bookstore']
160 'ancestor with parent' | '//books/ancestor::/bookstore/magazines' || ['/bookstore/magazines']
161 'ancestor with parent that does not exist' | '//books/ancestor::/parentDoesNoExist/magazines' || []
162 'ancestor does not exist' | '//books/ancestor::ancestorDoesNotExist' || []