Implement ends with cps path query to support multiple attributes with 'and' condition
[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 import spock.lang.Unroll
34
35 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
36 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
37
38 class CpsDataPersistenceQueryDataNodeSpec extends CpsPersistenceSpecBase {
39
40     @Autowired
41     CpsDataPersistenceService objectUnderTest
42
43     static final Gson GSON = new GsonBuilder().create()
44
45     static final String SET_DATA = '/data/fragment.sql'
46     static final String XPATH_DATA_NODE_WITH_DESCENDANTS = '/parent-1'
47
48     static DataNode existingDataNode
49     static DataNode existingChildDataNode
50
51     def expectedLeavesByXpathMap = [
52             '/parent-100'                      : ['parent-leaf': 'parent-leaf value'],
53             '/parent-100/child-001'            : ['first-child-leaf': 'first-child-leaf value'],
54             '/parent-100/child-002'            : ['second-child-leaf': 'second-child-leaf value'],
55             '/parent-100/child-002/grand-child': ['grand-child-leaf': 'grand-child-leaf value']
56     ]
57
58     static {
59         existingDataNode = createDataNodeTree(XPATH_DATA_NODE_WITH_DESCENDANTS)
60         existingChildDataNode = createDataNodeTree('/parent-1/child-1')
61     }
62
63     static def createDataNodeTree(String... xpaths) {
64         def dataNodeBuilder = new DataNodeBuilder().withXpath(xpaths[0])
65         if (xpaths.length > 1) {
66             def xPathsDescendant = Arrays.copyOfRange(xpaths, 1, xpaths.length)
67             def childDataNode = createDataNodeTree(xPathsDescendant)
68             dataNodeBuilder.withChildDataNodes(ImmutableSet.of(childDataNode))
69         }
70         dataNodeBuilder.build()
71     }
72
73     def static treeToFlatMapByXpath(Map<String, DataNode> flatMap, DataNode dataNodeTree) {
74         flatMap.put(dataNodeTree.getXpath(), dataNodeTree)
75         dataNodeTree.getChildDataNodes()
76                 .forEach(childDataNode -> treeToFlatMapByXpath(flatMap, childDataNode))
77         return flatMap
78     }
79
80
81     @Unroll
82     @Sql([CLEAR_DATA, SET_DATA])
83     def 'Cps Path query for single leaf value with type: #type.'() {
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, includeDescendantsOption)
86         then: 'the correct data is returned'
87             def leaves = '[common-leaf-name:common-leaf value, common-leaf-name-int:5.0]'
88             DataNode dataNode = result.stream().findFirst().get()
89             dataNode.getLeaves().toString() == leaves
90             dataNode.getChildDataNodes().size() == expectedNumberOfChidlNodes
91         where: 'the following data is used'
92             type                        | cpsPath                                                          | includeDescendantsOption || expectedNumberOfChidlNodes
93             'String and no descendants' | '/parent-200/child-202[@common-leaf-name=\'common-leaf value\']' | OMIT_DESCENDANTS         || 0
94             'Integer and descendants'   | '/parent-200/child-202[@common-leaf-name-int=5]'                 | INCLUDE_ALL_DESCENDANTS  || 1
95     }
96
97     @Unroll
98     @Sql([CLEAR_DATA, SET_DATA])
99     def 'Query for attribute by cps path with cps paths that return no data because of #scenario.'() {
100         when: 'a query is executed to get datanodes for the given cps path'
101             def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, cpsPath, FetchDescendantsOption.OMIT_DESCENDANTS)
102         then: 'no data is returned'
103             result.isEmpty()
104         where: 'following cps queries are performed'
105             scenario                           | cpsPath
106             'cps path is incomplete'           | '/parent-200[@common-leaf-name-int=5]'
107             'leaf value does not exist'        | '/parent-200/child-202[@common-leaf-name=\'does not exist\']'
108             'incomplete end of xpath prefix'   | '/parent-200/child-20[@common-leaf-name-int=5]'
109     }
110
111     @Unroll
112     @Sql([CLEAR_DATA, SET_DATA])
113     def 'Cps Path query using descendant anywhere and #type (further) descendants.'() {
114         when: 'a query is executed to get a data node by the given cps path'
115             def cpsPath = '//child-202'
116             def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, cpsPath, includeDescendantsOption)
117         then: 'the data node has the correct number of children'
118             DataNode dataNode = result.stream().findFirst().get()
119             dataNode.getChildDataNodes().size() == expectedNumberOfChildNodes
120         where: 'the following data is used'
121             type      | includeDescendantsOption || expectedNumberOfChildNodes
122             'omit'    | OMIT_DESCENDANTS         || 0
123             'include' | INCLUDE_ALL_DESCENDANTS  || 1
124     }
125
126     @Unroll
127     @Sql([CLEAR_DATA, SET_DATA])
128     def 'Cps Path query using descendant anywhere with %scenario '() {
129         when: 'a query is executed to get a data node by the given cps path'
130             def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, cpsPath, OMIT_DESCENDANTS)
131         then: 'the correct number of data nodes are retrieved'
132             result.size() == expectedXPaths.size()
133         and: 'xpaths of the retrieved data nodes are as expected'
134             for(int i = 0; i<result.size(); i++) {
135                 result[i].getXpath() == expectedXPaths[i]
136             }
137         where: 'the following data is used'
138             scenario                                  | cpsPath             || expectedXPaths
139             'fully unique descendant name'            | '//grand-child-202' || ['/parent-200/child-202/grand-child-202']
140             'descendant name match end of other node' | '//child-202'       || ['/parent-200/child-202','/parent-201/child-202']
141     }
142
143     @Unroll
144     @Sql([CLEAR_DATA, SET_DATA])
145     def 'Cps Path query using descendant anywhere ends with yang list containing %scenario '() {
146         when: 'a query is executed to get a data node by the given cps path'
147             def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, cpsPath, OMIT_DESCENDANTS)
148         then: 'the correct number of data nodes are retrieved'
149             result.size() == expectedXPaths.size()
150         and: 'xpaths of the retrieved data nodes are as expected'
151             for(int i = 0; i<result.size(); i++) {
152                 result[i].getXpath() == expectedXPaths[i]
153             }
154         where: 'the following data is used'
155             scenario                       | cpsPath                                                                          || expectedXPaths
156             'one attribute'                | '//child-202[@common-leaf-name-int=5]'                                           || ['/parent-200/child-202','/parent-201/child-202']
157             'trailing "and" is ignored'    | '//child-202[@common-leaf-name-int=5 and]'                                       || ['/parent-200/child-202','/parent-201/child-202']
158             'more than one attribute'      | '//child-202[@common-leaf-name-int=5 and @common-leaf-name="common-leaf value"]' || ['/parent-200/child-202']
159             'attributes reversed in order' | '//child-202[@common-leaf-name="common-leaf value" and @common-leaf-name-int=5]' || ['/parent-200/child-202']
160     }
161
162     @Unroll
163     @Sql([CLEAR_DATA, SET_DATA])
164     def 'Cps Path query error scenario using descendant anywhere ends with yang list containing %scenario '() {
165         when: 'a query is executed to get a data node by the given cps path'
166             def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, cpsPath, OMIT_DESCENDANTS)
167         then: 'exception is thrown'
168             thrown(CpsPathException)
169         where: 'the following data is used'
170             scenario                                  | cpsPath
171             'one of the attributes without value'     | '//child-202[@common-leaf-name-int=5 and @another-attribute"]'
172             'more than one attribute separated by or' | '//child-202[@common-leaf-name-int=5 or @common-leaf-name="common-leaf value"]'
173     }
174 }