Merge "Fix code smell"
[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  *  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.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21 package org.onap.cps.spi.impl
22
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
33
34 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
35 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
36
37 class CpsDataPersistenceQueryDataNodeSpec extends CpsPersistenceSpecBase {
38
39     @Autowired
40     CpsDataPersistenceService objectUnderTest
41
42     static final Gson GSON = new GsonBuilder().create()
43
44     static final String SET_DATA = '/data/fragment.sql'
45     static final String XPATH_DATA_NODE_WITH_DESCENDANTS = '/parent-1'
46
47     static DataNode existingDataNode
48     static DataNode existingChildDataNode
49
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']
55     ]
56
57     static {
58         existingDataNode = createDataNodeTree(XPATH_DATA_NODE_WITH_DESCENDANTS)
59         existingChildDataNode = createDataNodeTree('/parent-1/child-1')
60     }
61
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))
68         }
69         dataNodeBuilder.build()
70     }
71
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))
76         return flatMap
77     }
78
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
92     }
93
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'
99             result.isEmpty()
100         where: 'following cps queries are performed'
101             scenario                           | cpsPath
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]'
105     }
106
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
119     }
120
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]
130             }
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']
135     }
136
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]
146             }
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']
153     }
154
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]
164             }
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]']
170     }
171
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'
179             scenario                             | cpsPath
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"]'
182     }
183 }