Investigate and update Spock version
[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
80     @Sql([CLEAR_DATA, SET_DATA])
81     def 'Cps Path query for single leaf value with type: #type.'() {
82         when: 'a query is executed to get a data node by the given cps path'
83             def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, cpsPath, includeDescendantsOption)
84         then: 'the correct data is returned'
85             def leaves = '[common-leaf-name:common-leaf value, common-leaf-name-int:5.0]'
86             DataNode dataNode = result.stream().findFirst().get()
87             dataNode.getLeaves().toString() == leaves
88             dataNode.getChildDataNodes().size() == expectedNumberOfChidlNodes
89         where: 'the following data is used'
90             type                        | cpsPath                                                          | includeDescendantsOption || expectedNumberOfChidlNodes
91             'String and no descendants' | '/parent-200/child-202[@common-leaf-name=\'common-leaf value\']' | OMIT_DESCENDANTS         || 0
92             'Integer and descendants'   | '/parent-200/child-202[@common-leaf-name-int=5]'                 | INCLUDE_ALL_DESCENDANTS  || 1
93     }
94
95     @Sql([CLEAR_DATA, SET_DATA])
96     def 'Query for attribute by cps path with cps paths that return no data because of #scenario.'() {
97         when: 'a query is executed to get datanodes for the given cps path'
98             def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, cpsPath, FetchDescendantsOption.OMIT_DESCENDANTS)
99         then: 'no data is returned'
100             result.isEmpty()
101         where: 'following cps queries are performed'
102             scenario                           | cpsPath
103             'cps path is incomplete'           | '/parent-200[@common-leaf-name-int=5]'
104             'leaf value does not exist'        | '/parent-200/child-202[@common-leaf-name=\'does not exist\']'
105             'incomplete end of xpath prefix'   | '/parent-200/child-20[@common-leaf-name-int=5]'
106     }
107
108     @Sql([CLEAR_DATA, SET_DATA])
109     def 'Cps Path query using descendant anywhere and #type (further) descendants.'() {
110         when: 'a query is executed to get a data node by the given cps path'
111             def cpsPath = '//child-202'
112             def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, cpsPath, includeDescendantsOption)
113         then: 'the data node has the correct number of children'
114             DataNode dataNode = result.stream().findFirst().get()
115             dataNode.getChildDataNodes().size() == expectedNumberOfChildNodes
116         where: 'the following data is used'
117             type      | includeDescendantsOption || expectedNumberOfChildNodes
118             'omit'    | OMIT_DESCENDANTS         || 0
119             'include' | INCLUDE_ALL_DESCENDANTS  || 1
120     }
121
122     @Sql([CLEAR_DATA, SET_DATA])
123     def 'Cps Path query using descendant anywhere with %scenario '() {
124         when: 'a query is executed to get a data node by the given cps path'
125             def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, cpsPath, OMIT_DESCENDANTS)
126         then: 'the correct number of data nodes are retrieved'
127             result.size() == expectedXPaths.size()
128         and: 'xpaths of the retrieved data nodes are as expected'
129             for(int i = 0; i<result.size(); i++) {
130                 result[i].getXpath() == expectedXPaths[i]
131             }
132         where: 'the following data is used'
133             scenario                                  | cpsPath             || expectedXPaths
134             'fully unique descendant name'            | '//grand-child-202' || ['/parent-200/child-202/grand-child-202']
135             'descendant name match end of other node' | '//child-202'       || ['/parent-200/child-202','/parent-201/child-202']
136     }
137
138     @Sql([CLEAR_DATA, SET_DATA])
139     def 'Cps Path query using descendant anywhere ends with yang list containing %scenario '() {
140         when: 'a query is executed to get a data node by the given cps path'
141             def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, cpsPath, OMIT_DESCENDANTS)
142         then: 'the correct number of data nodes are retrieved'
143             result.size() == expectedXPaths.size()
144         and: 'xpaths of the retrieved data nodes are as expected'
145             for(int i = 0; i<result.size(); i++) {
146                 result[i].getXpath() == expectedXPaths[i]
147             }
148         where: 'the following data is used'
149             scenario                       | cpsPath                                                                          || expectedXPaths
150             'one attribute'                | '//child-202[@common-leaf-name-int=5]'                                           || ['/parent-200/child-202','/parent-201/child-202']
151             'trailing "and" is ignored'    | '//child-202[@common-leaf-name-int=5 and]'                                       || ['/parent-200/child-202','/parent-201/child-202']
152             'more than one attribute'      | '//child-202[@common-leaf-name-int=5 and @common-leaf-name="common-leaf value"]' || ['/parent-200/child-202']
153             'attributes reversed in order' | '//child-202[@common-leaf-name="common-leaf value" and @common-leaf-name-int=5]' || ['/parent-200/child-202']
154     }
155
156     @Sql([CLEAR_DATA, SET_DATA])
157     def 'Cps Path query error scenario using descendant anywhere ends with yang list containing %scenario '() {
158         when: 'a query is executed to get a data node by the given cps path'
159             def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, cpsPath, OMIT_DESCENDANTS)
160         then: 'exception is thrown'
161             thrown(CpsPathException)
162         where: 'the following data is used'
163             scenario                                  | cpsPath
164             'one of the attributes without value'     | '//child-202[@common-leaf-name-int=5 and @another-attribute"]'
165             'more than one attribute separated by or' | '//child-202[@common-leaf-name-int=5 or @common-leaf-name="common-leaf value"]'
166     }
167 }