Merge "Leaf String value comparison matches mix of single and double quotes"
[cps.git] / cps-ri / src / test / groovy / org / onap / cps / spi / impl / CpsDataPersistenceServiceSpec.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.entities.FragmentEntity
28 import org.onap.cps.spi.exceptions.AlreadyDefinedException
29 import org.onap.cps.spi.exceptions.AnchorNotFoundException
30 import org.onap.cps.spi.exceptions.DataNodeNotFoundException
31 import org.onap.cps.spi.exceptions.DataspaceNotFoundException
32 import org.onap.cps.spi.model.DataNode
33 import org.onap.cps.spi.model.DataNodeBuilder
34 import org.springframework.beans.factory.annotation.Autowired
35 import org.springframework.dao.DataIntegrityViolationException
36 import org.springframework.test.context.jdbc.Sql
37 import spock.lang.Unroll
38
39 import javax.validation.ConstraintViolationException
40
41 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
42 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
43
44 class CpsDataPersistenceServiceSpec extends CpsPersistenceSpecBase {
45
46     @Autowired
47     CpsDataPersistenceService objectUnderTest
48
49     static final Gson GSON = new GsonBuilder().create()
50
51     static final String SET_DATA = '/data/fragment.sql'
52     static final long ID_DATA_NODE_WITH_DESCENDANTS = 4001
53     static final String XPATH_DATA_NODE_WITH_DESCENDANTS = '/parent-1'
54     static final String XPATH_DATA_NODE_WITH_LEAVES = '/parent-100'
55     static final long UPDATE_DATA_NODE_FRAGMENT_ID = 4202L
56     static final long UPDATE_DATA_NODE_SUB_FRAGMENT_ID = 4203L
57
58     static final DataNode newDataNode = new DataNodeBuilder().build()
59     static DataNode existingDataNode
60     static DataNode existingChildDataNode
61
62     def expectedLeavesByXpathMap = [
63             '/parent-100'                      : ['parent-leaf': 'parent-leaf value'],
64             '/parent-100/child-001'            : ['first-child-leaf': 'first-child-leaf value'],
65             '/parent-100/child-002'            : ['second-child-leaf': 'second-child-leaf value'],
66             '/parent-100/child-002/grand-child': ['grand-child-leaf': 'grand-child-leaf value']
67     ]
68
69     static {
70         existingDataNode = createDataNodeTree(XPATH_DATA_NODE_WITH_DESCENDANTS)
71         existingChildDataNode = createDataNodeTree('/parent-1/child-1')
72     }
73
74     @Sql([CLEAR_DATA, SET_DATA])
75     def 'StoreDataNode with descendants.'() {
76         when: 'a fragment with descendants is stored'
77             def parentXpath = "/parent-new"
78             def childXpath = "/parent-new/child-new"
79             def grandChildXpath = "/parent-new/child-new/grandchild-new"
80             objectUnderTest.storeDataNode(DATASPACE_NAME, ANCHOR_NAME1,
81                     createDataNodeTree(parentXpath, childXpath, grandChildXpath))
82         then: 'it can be retrieved by its xpath'
83             def parentFragment = getFragmentByXpath(DATASPACE_NAME, ANCHOR_NAME1, parentXpath)
84         and: 'it contains the children'
85             parentFragment.childFragments.size() == 1
86             def childFragment = parentFragment.childFragments[0]
87             childFragment.xpath == childXpath
88         and: "and its children's children"
89             childFragment.childFragments.size() == 1
90             def grandchildFragment = childFragment.childFragments[0]
91             grandchildFragment.xpath == grandChildXpath
92     }
93
94     @Sql([CLEAR_DATA, SET_DATA])
95     def 'Store data node for multiple anchors using the same schema.'() {
96         def xpath = "/parent-new"
97         given: 'a fragment is stored for an anchor'
98             objectUnderTest.storeDataNode(DATASPACE_NAME, ANCHOR_NAME1, createDataNodeTree(xpath))
99         when: 'another fragment is stored for an other anchor, using the same schema set'
100             objectUnderTest.storeDataNode(DATASPACE_NAME, ANCHOR_NAME3, createDataNodeTree(xpath))
101         then: 'both fragments can be retrieved by their xpath'
102             def fragment1 = getFragmentByXpath(DATASPACE_NAME, ANCHOR_NAME1, xpath)
103             fragment1.anchor.name == ANCHOR_NAME1
104             fragment1.xpath == xpath
105             def fragment2 = getFragmentByXpath(DATASPACE_NAME, ANCHOR_NAME3, xpath)
106             fragment2.anchor.name == ANCHOR_NAME3
107             fragment2.xpath == xpath
108     }
109
110     @Unroll
111     @Sql([CLEAR_DATA, SET_DATA])
112     def 'Store datanode error scenario: #scenario.'() {
113         when: 'attempt to store a data node with #scenario'
114             objectUnderTest.storeDataNode(dataspaceName, anchorName, dataNode)
115         then: 'a #expectedException is thrown'
116             thrown(expectedException)
117         where: 'the following data is used'
118             scenario                    | dataspaceName  | anchorName     | dataNode         || expectedException
119             'dataspace does not exist'  | 'unknown'      | 'not-relevant' | newDataNode      || DataspaceNotFoundException
120             'schema set does not exist' | DATASPACE_NAME | 'unknown'      | newDataNode      || AnchorNotFoundException
121             'anchor already exists'     | DATASPACE_NAME | ANCHOR_NAME1   | newDataNode      || ConstraintViolationException
122             'datanode already exists'   | DATASPACE_NAME | ANCHOR_NAME1   | existingDataNode || AlreadyDefinedException
123     }
124
125     @Sql([CLEAR_DATA, SET_DATA])
126     def 'Add a child to a Fragment that already has a child.'() {
127         given: ' a new child node'
128             def newChild = createDataNodeTree('xpath for new child')
129         when: 'the child is added to an existing parent with 1 child'
130             objectUnderTest.addChildDataNode(DATASPACE_NAME, ANCHOR_NAME1, XPATH_DATA_NODE_WITH_DESCENDANTS, newChild)
131         then: 'the parent is now has to 2 children'
132             def expectedExistingChildPath = '/parent-1/child-1'
133             def parentFragment = fragmentRepository.findById(ID_DATA_NODE_WITH_DESCENDANTS).orElseThrow()
134             parentFragment.getChildFragments().size() == 2
135         and: 'it still has the old child'
136             parentFragment.getChildFragments().find({ it.xpath == expectedExistingChildPath })
137         and: 'it has the new child'
138             parentFragment.getChildFragments().find({ it.xpath == newChild.xpath })
139     }
140
141     @Unroll
142     @Sql([CLEAR_DATA, SET_DATA])
143     def 'Add child error scenario: #scenario.'() {
144         when: 'attempt to add a child data node with #scenario'
145             objectUnderTest.addChildDataNode(DATASPACE_NAME, ANCHOR_NAME1, parentXpath, dataNode)
146         then: 'a #expectedException is thrown'
147             thrown(expectedException)
148         where: 'the following data is used'
149             scenario                 | parentXpath                      | dataNode              || expectedException
150             'parent does not exist'  | 'unknown'                        | newDataNode           || DataNodeNotFoundException
151             'already existing child' | XPATH_DATA_NODE_WITH_DESCENDANTS | existingChildDataNode || DataIntegrityViolationException
152     }
153
154     static def createDataNodeTree(String... xpaths) {
155         def dataNodeBuilder = new DataNodeBuilder().withXpath(xpaths[0])
156         if (xpaths.length > 1) {
157             def xPathsDescendant = Arrays.copyOfRange(xpaths, 1, xpaths.length)
158             def childDataNode = createDataNodeTree(xPathsDescendant)
159             dataNodeBuilder.withChildDataNodes(ImmutableSet.of(childDataNode))
160         }
161         dataNodeBuilder.build()
162     }
163
164     def getFragmentByXpath(dataspaceName, anchorName, xpath) {
165         def dataspace = dataspaceRepository.getByName(dataspaceName)
166         def anchor = anchorRepository.getByDataspaceAndName(dataspace, anchorName)
167         return fragmentRepository.findByDataspaceAndAnchorAndXpath(dataspace, anchor, xpath).orElseThrow()
168     }
169
170     @Unroll
171     @Sql([CLEAR_DATA, SET_DATA])
172     def 'Get data node by xpath without descendants.'() {
173         when: 'data node is requested'
174             def result = objectUnderTest.getDataNode(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES,
175                     inputXPath, OMIT_DESCENDANTS)
176         then: 'data node is returned with no descendants'
177             assert result.getXpath() == XPATH_DATA_NODE_WITH_LEAVES
178         and: 'expected leaves'
179             assert result.getChildDataNodes().size() == 0
180             assertLeavesMaps(result.getLeaves(), expectedLeavesByXpathMap[XPATH_DATA_NODE_WITH_LEAVES])
181         where: 'the following data is used'
182             scenario                      | inputXPath
183             'some xpath'                  |'/parent-100'
184             'root xpath'                  |'/'
185             'empty xpath'                 |''
186     }
187
188     @Unroll
189     @Sql([CLEAR_DATA, SET_DATA])
190     def 'Get data node by xpath with all descendants.'() {
191         when: 'data node is requested with all descendants'
192             def result = objectUnderTest.getDataNode(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES,
193                     inputXPath, INCLUDE_ALL_DESCENDANTS)
194             def mappedResult = treeToFlatMapByXpath(new HashMap<>(), result)
195         then: 'data node is returned with all the descendants populated'
196             assert mappedResult.size() == 4
197             assert result.getChildDataNodes().size() == 2
198             assert mappedResult.get('/parent-100/child-001').getChildDataNodes().size() == 0
199             assert mappedResult.get('/parent-100/child-002').getChildDataNodes().size() == 1
200         and: 'extracted leaves maps are matching expected'
201             mappedResult.forEach(
202                     (inputXPath, dataNode) -> assertLeavesMaps(dataNode.getLeaves(), expectedLeavesByXpathMap[inputXPath]))
203         where: 'the following data is used'
204             scenario                      | inputXPath
205             'some xpath'                  |'/parent-100'
206             'root xpath'                  |'/'
207             'empty xpath'                 |''
208     }
209
210     def static assertLeavesMaps(actualLeavesMap, expectedLeavesMap) {
211         expectedLeavesMap.forEach((key, value) -> {
212             def actualValue = actualLeavesMap[key]
213             if (value instanceof Collection<?> && actualValue instanceof Collection<?>) {
214                 assert value.size() == actualValue.size()
215                 assert value.containsAll(actualValue)
216             } else {
217                 assert value == actualValue
218             }
219         }
220         )
221         return true
222     }
223
224     def static treeToFlatMapByXpath(Map<String, DataNode> flatMap, DataNode dataNodeTree) {
225         flatMap.put(dataNodeTree.getXpath(), dataNodeTree)
226         dataNodeTree.getChildDataNodes()
227                 .forEach(childDataNode -> treeToFlatMapByXpath(flatMap, childDataNode))
228         return flatMap
229     }
230
231     @Unroll
232     @Sql([CLEAR_DATA, SET_DATA])
233     def 'Get data node error scenario: #scenario.'() {
234         when: 'attempt to get data node with #scenario'
235             objectUnderTest.getDataNode(dataspaceName, anchorName, xpath, OMIT_DESCENDANTS)
236         then: 'a #expectedException is thrown'
237             thrown(expectedException)
238         where: 'the following data is used'
239             scenario                 | dataspaceName  | anchorName                        | xpath          || expectedException
240             'non-existing dataspace' | 'NO DATASPACE' | 'not relevant'                    | 'not relevant' || DataspaceNotFoundException
241             'non-existing anchor'    | DATASPACE_NAME | 'NO ANCHOR'                       | 'not relevant' || AnchorNotFoundException
242             'non-existing xpath'     | DATASPACE_NAME | ANCHOR_FOR_DATA_NODES_WITH_LEAVES | 'NO XPATH'     || DataNodeNotFoundException
243     }
244
245     @Sql([CLEAR_DATA, SET_DATA])
246     def 'Update data node leaves.'() {
247         when: 'update is performed for leaves'
248             objectUnderTest.updateDataLeaves(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES,
249                     "/parent-200/child-201", ['leaf-value': 'new'])
250         then: 'leaves are updated for selected data node'
251             def updatedFragment = fragmentRepository.getOne(UPDATE_DATA_NODE_FRAGMENT_ID)
252             def updatedLeaves = getLeavesMap(updatedFragment)
253             assert updatedLeaves.size() == 1
254             assert updatedLeaves.'leaf-value' == 'new'
255         and: 'existing child entry remains as is'
256             def childFragment = updatedFragment.getChildFragments().iterator().next()
257             def childLeaves = getLeavesMap(childFragment)
258             assert childFragment.getId() == UPDATE_DATA_NODE_SUB_FRAGMENT_ID
259             assert childLeaves.'leaf-value' == 'original'
260     }
261
262     @Unroll
263     @Sql([CLEAR_DATA, SET_DATA])
264     def 'Update data leaves error scenario: #scenario.'() {
265         when: 'attempt to update data node for #scenario'
266             objectUnderTest.updateDataLeaves(dataspaceName, anchorName, xpath, ['leaf-name': 'leaf-value'])
267         then: 'a #expectedException is thrown'
268             thrown(expectedException)
269         where: 'the following data is used'
270             scenario                 | dataspaceName  | anchorName                        | xpath                || expectedException
271             'non-existing dataspace' | 'NO DATASPACE' | 'not relevant'                    | 'not relevant'       || DataspaceNotFoundException
272             'non-existing anchor'    | DATASPACE_NAME | 'NO ANCHOR'                       | 'not relevant'       || AnchorNotFoundException
273             'non-existing xpath'     | DATASPACE_NAME | ANCHOR_FOR_DATA_NODES_WITH_LEAVES | 'NON-EXISTING XPATH' || DataNodeNotFoundException
274     }
275
276     @Sql([CLEAR_DATA, SET_DATA])
277     def 'Replace data node tree with descendants removal.'() {
278         given: 'data node object with leaves updated, no children'
279             def submittedDataNode = buildDataNode("/parent-200/child-201", ['leaf-value': 'new'], [])
280         when: 'replace data node tree is performed'
281             objectUnderTest.replaceDataNodeTree(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, submittedDataNode)
282         then: 'leaves have been updated for selected data node'
283             def updatedFragment = fragmentRepository.getOne(UPDATE_DATA_NODE_FRAGMENT_ID)
284             def updatedLeaves = getLeavesMap(updatedFragment)
285             assert updatedLeaves.size() == 1
286             assert updatedLeaves.'leaf-value' == 'new'
287         and: 'updated entry has no children'
288             updatedFragment.getChildFragments().isEmpty()
289         and: 'previously attached child entry is removed from database'
290             fragmentRepository.findById(UPDATE_DATA_NODE_SUB_FRAGMENT_ID).isEmpty()
291     }
292
293     @Sql([CLEAR_DATA, SET_DATA])
294     def 'Replace data node tree with descendants.'() {
295         given: 'data node object with leaves updated, having child with old content'
296             def submittedDataNode = buildDataNode("/parent-200/child-201", ['leaf-value': 'new'], [
297                     buildDataNode("/parent-200/child-201/grand-child", ['leaf-value': 'original'], [])
298             ])
299         when: 'update is performed including descendants'
300             objectUnderTest.replaceDataNodeTree(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, submittedDataNode)
301         then: 'leaves have been updated for selected data node'
302             def updatedFragment = fragmentRepository.getOne(UPDATE_DATA_NODE_FRAGMENT_ID)
303             def updatedLeaves = getLeavesMap(updatedFragment)
304             assert updatedLeaves.size() == 1
305             assert updatedLeaves.'leaf-value' == 'new'
306         and: 'previously attached child entry is removed from database'
307             fragmentRepository.findById(UPDATE_DATA_NODE_SUB_FRAGMENT_ID).isEmpty()
308         and: 'new child entry with same content is created'
309             def childFragment = updatedFragment.getChildFragments().iterator().next()
310             def childLeaves = getLeavesMap(childFragment)
311             assert childFragment.getId() != UPDATE_DATA_NODE_SUB_FRAGMENT_ID
312             assert childLeaves.'leaf-value' == 'original'
313     }
314
315     @Unroll
316     @Sql([CLEAR_DATA, SET_DATA])
317     def 'Replace data node tree error scenario: #scenario.'() {
318         given: 'data node object'
319             def submittedDataNode = buildDataNode(xpath, ['leaf-name': 'leaf-value'], [])
320         when: 'attempt to update data node for #scenario'
321             objectUnderTest.replaceDataNodeTree(dataspaceName, anchorName, submittedDataNode)
322         then: 'a #expectedException is thrown'
323             thrown(expectedException)
324         where: 'the following data is used'
325             scenario                 | dataspaceName  | anchorName                        | xpath                || expectedException
326             'non-existing dataspace' | 'NO DATASPACE' | 'not relevant'                    | 'not relevant'       || DataspaceNotFoundException
327             'non-existing anchor'    | DATASPACE_NAME | 'NO ANCHOR'                       | 'not relevant'       || AnchorNotFoundException
328             'non-existing xpath'     | DATASPACE_NAME | ANCHOR_FOR_DATA_NODES_WITH_LEAVES | 'NON-EXISTING XPATH' || DataNodeNotFoundException
329     }
330
331     static DataNode buildDataNode(xpath, leaves, childDataNodes) {
332         return new DataNodeBuilder().withXpath(xpath).withLeaves(leaves).withChildDataNodes(childDataNodes).build()
333     }
334
335     static Map<String, Object> getLeavesMap(FragmentEntity fragmentEntity) {
336         return GSON.fromJson(fragmentEntity.getAttributes(), Map<String, Object>.class)
337     }
338 }