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 com.google.common.collect.ImmutableSet
24 import com.google.gson.Gson
25 import com.google.gson.GsonBuilder
26 import org.onap.cps.spi.CpsDataPersistenceService
27 import org.onap.cps.spi.FetchDescendantsOption
28 import org.onap.cps.spi.exceptions.CpsPathException
29 import org.onap.cps.spi.model.DataNode
30 import org.onap.cps.spi.model.DataNodeBuilder
31 import org.springframework.beans.factory.annotation.Autowired
32 import org.springframework.test.context.jdbc.Sql
34 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
35 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
37 class CpsDataPersistenceQueryDataNodeSpec extends CpsPersistenceSpecBase {
40 CpsDataPersistenceService objectUnderTest
42 static final Gson GSON = new GsonBuilder().create()
44 static final String SET_DATA = '/data/fragment.sql'
45 static final String XPATH_DATA_NODE_WITH_DESCENDANTS = '/parent-1'
47 static DataNode existingDataNode
48 static DataNode existingChildDataNode
50 def expectedLeavesByXpathMap = [
51 '/parent-100' : ['parent-leaf': 'parent-leaf value'],
52 '/parent-100/child-001' : ['first-child-leaf': 'first-child-leaf value'],
53 '/parent-100/child-002' : ['second-child-leaf': 'second-child-leaf value'],
54 '/parent-100/child-002/grand-child': ['grand-child-leaf': 'grand-child-leaf value']
58 existingDataNode = createDataNodeTree(XPATH_DATA_NODE_WITH_DESCENDANTS)
59 existingChildDataNode = createDataNodeTree('/parent-1/child-1')
62 static def createDataNodeTree(String... xpaths) {
63 def dataNodeBuilder = new DataNodeBuilder().withXpath(xpaths[0])
64 if (xpaths.length > 1) {
65 def xPathsDescendant = Arrays.copyOfRange(xpaths, 1, xpaths.length)
66 def childDataNode = createDataNodeTree(xPathsDescendant)
67 dataNodeBuilder.withChildDataNodes(ImmutableSet.of(childDataNode))
69 dataNodeBuilder.build()
72 def static treeToFlatMapByXpath(Map<String, DataNode> flatMap, DataNode dataNodeTree) {
73 flatMap.put(dataNodeTree.getXpath(), dataNodeTree)
74 dataNodeTree.getChildDataNodes()
75 .forEach(childDataNode -> treeToFlatMapByXpath(flatMap, childDataNode))
79 @Sql([CLEAR_DATA, SET_DATA])
80 def 'Cps Path query for single leaf value with type: #type.'() {
81 when: 'a query is executed to get a data node by the given cps path'
82 def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, cpsPath, includeDescendantsOption)
83 then: 'the correct data is returned'
84 def leaves = '[common-leaf-name:common-leaf value, common-leaf-name-int:5.0]'
85 DataNode dataNode = result.stream().findFirst().get()
86 dataNode.getLeaves().toString() == leaves
87 dataNode.getChildDataNodes().size() == expectedNumberOfChidlNodes
88 where: 'the following data is used'
89 type | cpsPath | includeDescendantsOption || expectedNumberOfChidlNodes
90 'String and no descendants' | '/parent-200/child-202[@common-leaf-name=\'common-leaf value\']' | OMIT_DESCENDANTS || 0
91 'Integer and descendants' | '/parent-200/child-202[@common-leaf-name-int=5]' | INCLUDE_ALL_DESCENDANTS || 1
94 @Sql([CLEAR_DATA, SET_DATA])
95 def 'Query for attribute by cps path with cps paths that return no data because of #scenario.'() {
96 when: 'a query is executed to get datanodes for the given cps path'
97 def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, cpsPath, FetchDescendantsOption.OMIT_DESCENDANTS)
98 then: 'no data is returned'
100 where: 'following cps queries are performed'
102 'cps path is incomplete' | '/parent-200[@common-leaf-name-int=5]'
103 'leaf value does not exist' | '/parent-200/child-202[@common-leaf-name=\'does not exist\']'
104 'incomplete end of xpath prefix' | '/parent-200/child-20[@common-leaf-name-int=5]'
107 @Sql([CLEAR_DATA, SET_DATA])
108 def 'Cps Path query using descendant anywhere and #type (further) descendants.'() {
109 when: 'a query is executed to get a data node by the given cps path'
110 def cpsPath = '//child-202'
111 def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, cpsPath, includeDescendantsOption)
112 then: 'the data node has the correct number of children'
113 DataNode dataNode = result.stream().findFirst().get()
114 dataNode.getChildDataNodes().size() == expectedNumberOfChildNodes
115 where: 'the following data is used'
116 type | includeDescendantsOption || expectedNumberOfChildNodes
117 'omit' | OMIT_DESCENDANTS || 0
118 'include' | INCLUDE_ALL_DESCENDANTS || 1
121 @Sql([CLEAR_DATA, SET_DATA])
122 def 'Cps Path query using descendant anywhere with #scenario '() {
123 when: 'a query is executed to get a data node by the given cps path'
124 def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, cpsPath, OMIT_DESCENDANTS)
125 then: 'the correct number of data nodes are retrieved'
126 result.size() == expectedXPaths.size()
127 and: 'xpaths of the retrieved data nodes are as expected'
128 for(int i = 0; i<result.size(); i++) {
129 assert result[i].getXpath() == expectedXPaths[i]
131 where: 'the following data is used'
132 scenario | cpsPath || expectedXPaths
133 'fully unique descendant name' | '//grand-child-202' || ['/parent-200/child-202/grand-child-202']
134 'descendant name match end of other node' | '//child-202' || ['/parent-200/child-202','/parent-201/child-202']
137 @Sql([CLEAR_DATA, SET_DATA])
138 def 'Cps Path query using descendant anywhere with #scenario condition(s) for a container element.'() {
139 when: 'a query is executed to get a data node by the given cps path'
140 def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, cpsPath, OMIT_DESCENDANTS)
141 then: 'the correct number of data nodes are retrieved'
142 result.size() == expectedXPaths.size()
143 and: 'xpaths of the retrieved data nodes are as expected'
144 for(int i = 0; i<result.size(); i++) {
145 assert result[i].getXpath() == expectedXPaths[i]
147 where: 'the following data is used'
148 scenario | cpsPath || expectedXPaths
149 'one leaf' | '//child-202[@common-leaf-name-int=5]' || ['/parent-200/child-202','/parent-201/child-202']
150 'trailing "and" is ignored' | '//child-202[@common-leaf-name-int=5 and]' || ['/parent-200/child-202','/parent-201/child-202']
151 'more than one leaf' | '//child-202[@common-leaf-name-int=5 and @common-leaf-name="common-leaf value"]' || ['/parent-200/child-202']
152 'leaves reversed in order' | '//child-202[@common-leaf-name="common-leaf value" and @common-leaf-name-int=5]' || ['/parent-200/child-202']
155 @Sql([CLEAR_DATA, SET_DATA])
156 def 'Cps Path query using descendant anywhere with #scenario condition(s) for a list element.'() {
157 when: 'a query is executed to get a data node by the given cps path'
158 def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, cpsPath, OMIT_DESCENDANTS)
159 then: 'the correct number of data nodes are retrieved'
160 result.size() == expectedXPaths.size()
161 and: 'xpaths of the retrieved data nodes are as expected'
162 for(int i = 0; i<result.size(); i++) {
163 assert result[i].getXpath() == expectedXPaths[i]
165 where: 'the following data is used'
166 scenario | cpsPath || expectedXPaths
167 '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]']
168 'one non key leaf' | '//child-203[@other-leaf="other value"]' || ['/parent-201/child-203[@key1="A" and @key2=2]']
169 '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]']
172 @Sql([CLEAR_DATA, SET_DATA])
173 def 'Cps Path query error scenario using descendant anywhere ends with yang list containing %scenario '() {
174 when: 'a query is executed to get a data node by the given cps path'
175 objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, cpsPath, OMIT_DESCENDANTS)
176 then: 'exception is thrown'
177 thrown(CpsPathException)
178 where: 'the following data is used'
180 'one of the leaf without value' | '//child-202[@common-leaf-name-int=5 and @another-attribute"]'
181 'more than one leaf separated by or' | '//child-202[@common-leaf-name-int=5 or @common-leaf-name="common-leaf value"]'