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