Fixed inconsistent data issue with replaceNode
[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  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS,
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  *
19  *  SPDX-License-Identifier: Apache-2.0
20  *  ============LICENSE_END=========================================================
21  */
22 package org.onap.cps.spi.impl
23
24 import org.onap.cps.spi.exceptions.ConcurrencyException
25
26 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
27 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
28
29 import com.google.common.collect.ImmutableSet
30 import com.google.gson.Gson
31 import com.google.gson.GsonBuilder
32 import org.onap.cps.spi.CpsDataPersistenceService
33 import org.onap.cps.spi.entities.FragmentEntity
34 import org.onap.cps.spi.exceptions.AlreadyDefinedException
35 import org.onap.cps.spi.exceptions.AnchorNotFoundException
36 import org.onap.cps.spi.exceptions.DataNodeNotFoundException
37 import org.onap.cps.spi.exceptions.DataspaceNotFoundException
38 import org.onap.cps.spi.model.DataNode
39 import org.onap.cps.spi.model.DataNodeBuilder
40 import org.springframework.beans.factory.annotation.Autowired
41 import org.springframework.test.context.jdbc.Sql
42
43 import javax.validation.ConstraintViolationException
44
45 class CpsDataPersistenceServiceSpec extends CpsPersistenceSpecBase {
46
47     @Autowired
48     CpsDataPersistenceService objectUnderTest
49
50     static final Gson GSON = new GsonBuilder().create()
51
52     static final String SET_DATA = '/data/fragment.sql'
53     static final long ID_DATA_NODE_WITH_DESCENDANTS = 4001
54     static final String XPATH_DATA_NODE_WITH_DESCENDANTS = '/parent-1'
55     static final String XPATH_DATA_NODE_WITH_LEAVES = '/parent-100'
56     static final long UPDATE_DATA_NODE_FRAGMENT_ID = 4202L
57     static final long UPDATE_DATA_NODE_SUB_FRAGMENT_ID = 4203L
58     static final long LIST_DATA_NODE_PARENT_FRAGMENT_ID = 4206L
59
60     static final DataNode newDataNode = new DataNodeBuilder().build()
61     static DataNode existingDataNode
62     static DataNode existingChildDataNode
63
64     def expectedLeavesByXpathMap = [
65             '/parent-100'                      : ['parent-leaf': 'parent-leaf value'],
66             '/parent-100/child-001'            : ['first-child-leaf': 'first-child-leaf value'],
67             '/parent-100/child-002'            : ['second-child-leaf': 'second-child-leaf value'],
68             '/parent-100/child-002/grand-child': ['grand-child-leaf': 'grand-child-leaf value']
69     ]
70
71     static {
72         existingDataNode = createDataNodeTree(XPATH_DATA_NODE_WITH_DESCENDANTS)
73         existingChildDataNode = createDataNodeTree('/parent-1/child-1')
74     }
75
76     @Sql([CLEAR_DATA, SET_DATA])
77     def 'StoreDataNode with descendants.'() {
78         when: 'a fragment with descendants is stored'
79             def parentXpath = "/parent-new"
80             def childXpath = "/parent-new/child-new"
81             def grandChildXpath = "/parent-new/child-new/grandchild-new"
82             objectUnderTest.storeDataNode(DATASPACE_NAME, ANCHOR_NAME1,
83                     createDataNodeTree(parentXpath, childXpath, grandChildXpath))
84         then: 'it can be retrieved by its xpath'
85             def parentFragment = getFragmentByXpath(DATASPACE_NAME, ANCHOR_NAME1, parentXpath)
86         and: 'it contains the children'
87             parentFragment.childFragments.size() == 1
88             def childFragment = parentFragment.childFragments[0]
89             childFragment.xpath == childXpath
90         and: "and its children's children"
91             childFragment.childFragments.size() == 1
92             def grandchildFragment = childFragment.childFragments[0]
93             grandchildFragment.xpath == grandChildXpath
94     }
95
96     @Sql([CLEAR_DATA, SET_DATA])
97     def 'Store data node for multiple anchors using the same schema.'() {
98         def xpath = "/parent-new"
99         given: 'a fragment is stored for an anchor'
100             objectUnderTest.storeDataNode(DATASPACE_NAME, ANCHOR_NAME1, createDataNodeTree(xpath))
101         when: 'another fragment is stored for an other anchor, using the same schema set'
102             objectUnderTest.storeDataNode(DATASPACE_NAME, ANCHOR_NAME3, createDataNodeTree(xpath))
103         then: 'both fragments can be retrieved by their xpath'
104             def fragment1 = getFragmentByXpath(DATASPACE_NAME, ANCHOR_NAME1, xpath)
105             fragment1.anchor.name == ANCHOR_NAME1
106             fragment1.xpath == xpath
107             def fragment2 = getFragmentByXpath(DATASPACE_NAME, ANCHOR_NAME3, xpath)
108             fragment2.anchor.name == ANCHOR_NAME3
109             fragment2.xpath == xpath
110     }
111
112     @Sql([CLEAR_DATA, SET_DATA])
113     def 'Store datanode error scenario: #scenario.'() {
114         when: 'attempt to store a data node with #scenario'
115             objectUnderTest.storeDataNode(dataspaceName, anchorName, dataNode)
116         then: 'a #expectedException is thrown'
117             thrown(expectedException)
118         where: 'the following data is used'
119             scenario                    | dataspaceName  | anchorName     | dataNode         || expectedException
120             'dataspace does not exist'  | 'unknown'      | 'not-relevant' | newDataNode      || DataspaceNotFoundException
121             'schema set does not exist' | DATASPACE_NAME | 'unknown'      | newDataNode      || AnchorNotFoundException
122             'anchor already exists'     | DATASPACE_NAME | ANCHOR_NAME1   | newDataNode      || ConstraintViolationException
123             'datanode already exists'   | DATASPACE_NAME | ANCHOR_NAME1   | existingDataNode || AlreadyDefinedException
124     }
125
126     @Sql([CLEAR_DATA, SET_DATA])
127     def 'Add a child to a Fragment that already has a child.'() {
128         given: ' a new child node'
129             def newChild = createDataNodeTree('xpath for new child')
130         when: 'the child is added to an existing parent with 1 child'
131             objectUnderTest.addChildDataNode(DATASPACE_NAME, ANCHOR_NAME1, XPATH_DATA_NODE_WITH_DESCENDANTS, newChild)
132         then: 'the parent is now has to 2 children'
133             def expectedExistingChildPath = '/parent-1/child-1'
134             def parentFragment = fragmentRepository.findById(ID_DATA_NODE_WITH_DESCENDANTS).orElseThrow()
135             parentFragment.getChildFragments().size() == 2
136         and: 'it still has the old child'
137             parentFragment.getChildFragments().find({ it.xpath == expectedExistingChildPath })
138         and: 'it has the new child'
139             parentFragment.getChildFragments().find({ it.xpath == newChild.xpath })
140     }
141
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 || AlreadyDefinedException
152     }
153
154     @Sql([CLEAR_DATA, SET_DATA])
155     def 'Add list-node fragment with multiple elements.'() {
156         given: 'list node data fragment as a collection of data nodes'
157             def listNodeXpaths = ['/parent-201/child-204[@key="B"]', '/parent-201/child-204[@key="C"]']
158             def listNodeCollection = buildDataNodeCollection(listNodeXpaths)
159         when: 'list-node elements added to existing parent node'
160             objectUnderTest.addListDataNodes(DATASPACE_NAME, ANCHOR_NAME3, '/parent-201', listNodeCollection)
161         then: 'new entries successfully persisted, parent node now contains 5 children (2 new + 3 existing before)'
162             def parentFragment = fragmentRepository.getOne(LIST_DATA_NODE_PARENT_FRAGMENT_ID)
163             def allChildXpaths = parentFragment.getChildFragments().collect { it.getXpath() }
164             assert allChildXpaths.size() == 5
165             assert allChildXpaths.containsAll(listNodeXpaths)
166     }
167
168     @Sql([CLEAR_DATA, SET_DATA])
169     def 'Add list-node fragment error scenario: #scenario.'() {
170         given: 'list node data fragment as a collection of data nodes'
171             def listNodeCollection = buildDataNodeCollection(listNodeXpaths)
172         when: 'list-node elements added to existing parent node'
173             objectUnderTest.addListDataNodes(DATASPACE_NAME, ANCHOR_NAME3, parentNodeXpath, listNodeCollection)
174         then: 'a #expectedException is thrown'
175             thrown(expectedException)
176         where: 'following parameters were used'
177             scenario                     | parentNodeXpath | listNodeXpaths                      || expectedException
178             'parent node does not exist' | '/unknown'      | ['irrelevant']                      || DataNodeNotFoundException
179             'already existing fragment'  | '/parent-201'   | ['/parent-201/child-204[@key="A"]'] || AlreadyDefinedException
180
181     }
182
183     static def createDataNodeTree(String... xpaths) {
184         def dataNodeBuilder = new DataNodeBuilder().withXpath(xpaths[0])
185         if (xpaths.length > 1) {
186             def xPathsDescendant = Arrays.copyOfRange(xpaths, 1, xpaths.length)
187             def childDataNode = createDataNodeTree(xPathsDescendant)
188             dataNodeBuilder.withChildDataNodes(ImmutableSet.of(childDataNode))
189         }
190         dataNodeBuilder.build()
191     }
192
193     def getFragmentByXpath(dataspaceName, anchorName, xpath) {
194         def dataspace = dataspaceRepository.getByName(dataspaceName)
195         def anchor = anchorRepository.getByDataspaceAndName(dataspace, anchorName)
196         return fragmentRepository.findByDataspaceAndAnchorAndXpath(dataspace, anchor, xpath).orElseThrow()
197     }
198
199     @Sql([CLEAR_DATA, SET_DATA])
200     def 'Get data node by xpath without descendants.'() {
201         when: 'data node is requested'
202             def result = objectUnderTest.getDataNode(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES,
203                     inputXPath, OMIT_DESCENDANTS)
204         then: 'data node is returned with no descendants'
205             assert result.getXpath() == XPATH_DATA_NODE_WITH_LEAVES
206         and: 'expected leaves'
207             assert result.getChildDataNodes().size() == 0
208             assertLeavesMaps(result.getLeaves(), expectedLeavesByXpathMap[XPATH_DATA_NODE_WITH_LEAVES])
209         where: 'the following data is used'
210             scenario      | inputXPath
211             'some xpath'  | '/parent-100'
212             'root xpath'  | '/'
213             'empty xpath' | ''
214     }
215
216     @Sql([CLEAR_DATA, SET_DATA])
217     def 'Get data node by xpath with all descendants.'() {
218         when: 'data node is requested with all descendants'
219             def result = objectUnderTest.getDataNode(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES,
220                     inputXPath, INCLUDE_ALL_DESCENDANTS)
221             def mappedResult = treeToFlatMapByXpath(new HashMap<>(), result)
222         then: 'data node is returned with all the descendants populated'
223             assert mappedResult.size() == 4
224             assert result.getChildDataNodes().size() == 2
225             assert mappedResult.get('/parent-100/child-001').getChildDataNodes().size() == 0
226             assert mappedResult.get('/parent-100/child-002').getChildDataNodes().size() == 1
227         and: 'extracted leaves maps are matching expected'
228             mappedResult.forEach(
229                     (xPath, dataNode) -> assertLeavesMaps(dataNode.getLeaves(), expectedLeavesByXpathMap[xPath]))
230         where: 'the following data is used'
231             scenario      | inputXPath
232             'some xpath'  | '/parent-100'
233             'root xpath'  | '/'
234             'empty xpath' | ''
235     }
236
237     @Sql([CLEAR_DATA, SET_DATA])
238     def 'Get data node error scenario: #scenario.'() {
239         when: 'attempt to get data node with #scenario'
240             objectUnderTest.getDataNode(dataspaceName, anchorName, xpath, OMIT_DESCENDANTS)
241         then: 'a #expectedException is thrown'
242             thrown(expectedException)
243         where: 'the following data is used'
244             scenario                 | dataspaceName  | anchorName                        | xpath          || expectedException
245             'non-existing dataspace' | 'NO DATASPACE' | 'not relevant'                    | 'not relevant' || DataspaceNotFoundException
246             'non-existing anchor'    | DATASPACE_NAME | 'NO ANCHOR'                       | 'not relevant' || AnchorNotFoundException
247             'non-existing xpath'     | DATASPACE_NAME | ANCHOR_FOR_DATA_NODES_WITH_LEAVES | 'NO XPATH'     || DataNodeNotFoundException
248     }
249
250     @Sql([CLEAR_DATA, SET_DATA])
251     def 'Update data node leaves.'() {
252         when: 'update is performed for leaves'
253             objectUnderTest.updateDataLeaves(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES,
254                     "/parent-200/child-201", ['leaf-value': 'new'])
255         then: 'leaves are updated for selected data node'
256             def updatedFragment = fragmentRepository.getOne(UPDATE_DATA_NODE_FRAGMENT_ID)
257             def updatedLeaves = getLeavesMap(updatedFragment)
258             assert updatedLeaves.size() == 1
259             assert updatedLeaves.'leaf-value' == 'new'
260         and: 'existing child entry remains as is'
261             def childFragment = updatedFragment.getChildFragments().iterator().next()
262             def childLeaves = getLeavesMap(childFragment)
263             assert childFragment.getId() == UPDATE_DATA_NODE_SUB_FRAGMENT_ID
264             assert childLeaves.'leaf-value' == 'original'
265     }
266
267     @Sql([CLEAR_DATA, SET_DATA])
268     def 'Update data leaves error scenario: #scenario.'() {
269         when: 'attempt to update data node for #scenario'
270             objectUnderTest.updateDataLeaves(dataspaceName, anchorName, xpath, ['leaf-name': 'leaf-value'])
271         then: 'a #expectedException is thrown'
272             thrown(expectedException)
273         where: 'the following data is used'
274             scenario                 | dataspaceName  | anchorName                        | xpath                || expectedException
275             'non-existing dataspace' | 'NO DATASPACE' | 'not relevant'                    | 'not relevant'       || DataspaceNotFoundException
276             'non-existing anchor'    | DATASPACE_NAME | 'NO ANCHOR'                       | 'not relevant'       || AnchorNotFoundException
277             'non-existing xpath'     | DATASPACE_NAME | ANCHOR_FOR_DATA_NODES_WITH_LEAVES | 'NON-EXISTING XPATH' || DataNodeNotFoundException
278     }
279
280     @Sql([CLEAR_DATA, SET_DATA])
281     def 'Replace data node tree with descendants removal.'() {
282         given: 'data node object with leaves updated, no children'
283             def submittedDataNode = buildDataNode("/parent-200/child-201", ['leaf-value': 'new'], [])
284         when: 'replace data node tree is performed'
285             objectUnderTest.replaceDataNodeTree(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, submittedDataNode)
286         then: 'leaves have been updated for selected data node'
287             def updatedFragment = fragmentRepository.getOne(UPDATE_DATA_NODE_FRAGMENT_ID)
288             def updatedLeaves = getLeavesMap(updatedFragment)
289             assert updatedLeaves.size() == 1
290             assert updatedLeaves.'leaf-value' == 'new'
291         and: 'updated entry has no children'
292             updatedFragment.getChildFragments().isEmpty()
293         and: 'previously attached child entry is removed from database'
294             fragmentRepository.findById(UPDATE_DATA_NODE_SUB_FRAGMENT_ID).isEmpty()
295     }
296
297     @Sql([CLEAR_DATA, SET_DATA])
298     def 'Replace data node tree with descendants.'() {
299         given: 'data node object with leaves updated, having child with old content'
300             def submittedDataNode = buildDataNode("/parent-200/child-201", ['leaf-value': 'new'], [
301                     buildDataNode("/parent-200/child-201/grand-child", ['leaf-value': 'original'], [])
302             ])
303         when: 'update is performed including descendants'
304             objectUnderTest.replaceDataNodeTree(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, submittedDataNode)
305         then: 'leaves have been updated for selected data node'
306             def updatedFragment = fragmentRepository.getOne(UPDATE_DATA_NODE_FRAGMENT_ID)
307             def updatedLeaves = getLeavesMap(updatedFragment)
308             assert updatedLeaves.size() == 1
309             assert updatedLeaves.'leaf-value' == 'new'
310         and: 'existing child entry is not updated as content is same'
311             def childFragment = updatedFragment.getChildFragments().iterator().next()
312             childFragment.getXpath() == '/parent-200/child-201/grand-child'
313             def childLeaves = getLeavesMap(childFragment)
314             assert childLeaves.'leaf-value' == 'original'
315     }
316
317     @Sql([CLEAR_DATA, SET_DATA])
318     def 'Replace data node tree with same descendants but changed leaf value.'() {
319         given: 'data node object with leaves updated, having child with old content'
320             def submittedDataNode = buildDataNode("/parent-200/child-201", ['leaf-value': 'new'], [
321                     buildDataNode("/parent-200/child-201/grand-child", ['leaf-value': 'new'], [])
322             ])
323         when: 'update is performed including descendants'
324             objectUnderTest.replaceDataNodeTree(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, submittedDataNode)
325         then: 'leaves have been updated for selected data node'
326             def updatedFragment = fragmentRepository.getOne(UPDATE_DATA_NODE_FRAGMENT_ID)
327             def updatedLeaves = getLeavesMap(updatedFragment)
328             assert updatedLeaves.size() == 1
329             assert updatedLeaves.'leaf-value' == 'new'
330         and: 'existing child entry is updated with the new content'
331             def childFragment = updatedFragment.getChildFragments().iterator().next()
332             childFragment.getXpath() == '/parent-200/child-201/grand-child'
333             def childLeaves = getLeavesMap(childFragment)
334             assert childLeaves.'leaf-value' == 'new'
335     }
336
337     @Sql([CLEAR_DATA, SET_DATA])
338     def 'Replace data node tree with different descendants xpath'() {
339         given: 'data node object with leaves updated, having child with old content'
340             def submittedDataNode = buildDataNode("/parent-200/child-201", ['leaf-value': 'new'], [
341                     buildDataNode("/parent-200/child-201/grand-child-new", ['leaf-value': 'new'], [])
342             ])
343         when: 'update is performed including descendants'
344             objectUnderTest.replaceDataNodeTree(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, submittedDataNode)
345         then: 'leaves have been updated for selected data node'
346             def updatedFragment = fragmentRepository.getOne(UPDATE_DATA_NODE_FRAGMENT_ID)
347             def updatedLeaves = getLeavesMap(updatedFragment)
348             assert updatedLeaves.size() == 1
349             assert updatedLeaves.'leaf-value' == 'new'
350         and: 'previously attached child entry is removed from database'
351             fragmentRepository.findById(UPDATE_DATA_NODE_SUB_FRAGMENT_ID).isEmpty()
352         and: 'new child entry is persisted'
353             def childFragment = updatedFragment.getChildFragments().iterator().next()
354             childFragment.getXpath() == '/parent-200/child-201/grand-child-new'
355             def childLeaves = getLeavesMap(childFragment)
356             assert childLeaves.'leaf-value' == 'new'
357     }
358
359     @Sql([CLEAR_DATA, SET_DATA])
360     def 'Replace data node tree error scenario: #scenario.'() {
361         given: 'data node object'
362             def submittedDataNode = buildDataNode(xpath, ['leaf-name': 'leaf-value'], [])
363         when: 'attempt to update data node for #scenario'
364             objectUnderTest.replaceDataNodeTree(dataspaceName, anchorName, submittedDataNode)
365         then: 'a #expectedException is thrown'  
366             thrown(expectedException)
367         where: 'the following data is used'
368             scenario                 | dataspaceName  | anchorName                        | xpath                || expectedException
369             'non-existing dataspace' | 'NO DATASPACE' | 'not relevant'                    | 'not relevant'       || DataspaceNotFoundException
370             'non-existing anchor'    | DATASPACE_NAME | 'NO ANCHOR'                       | 'not relevant'       || AnchorNotFoundException
371             'non-existing xpath'     | DATASPACE_NAME | ANCHOR_FOR_DATA_NODES_WITH_LEAVES | 'NON-EXISTING XPATH' || DataNodeNotFoundException
372     }
373
374     @Sql([CLEAR_DATA, SET_DATA])
375     def 'Replace list-node content of #scenario.'() {
376         given: 'list node data fragment as a collection of data nodes'
377             def listNodeCollection = buildDataNodeCollection(listNodeXpaths)
378         when: 'list-node elements replaced within the existing parent node'
379             objectUnderTest.replaceListDataNodes(DATASPACE_NAME, ANCHOR_NAME3, '/parent-201', listNodeCollection)
380         then: 'child list elements are updated as expected, non-list element remains as is'
381             def parentFragment = fragmentRepository.getOne(LIST_DATA_NODE_PARENT_FRAGMENT_ID)
382             def allChildXpaths = parentFragment.getChildFragments().collect { it.getXpath() }
383             assert allChildXpaths.size() == expectedChildXpaths.size()
384             assert allChildXpaths.containsAll(expectedChildXpaths)
385         where: 'following parameters were used'
386             scenario                 | listNodeXpaths                      || expectedChildXpaths
387             'existing list-node'     | ['/parent-201/child-204[@key="B"]'] || ['/parent-201/child-203', '/parent-201/child-204[@key="B"]']
388             'non-existing list-node' | ['/parent-201/child-205[@key="1"]'] || ['/parent-201/child-203', '/parent-201/child-204[@key="A"]', '/parent-201/child-204[@key="X"]', '/parent-201/child-205[@key="1"]']
389     }
390
391     @Sql([CLEAR_DATA, SET_DATA])
392     def 'Replace list-node fragment error scenario: #scenario.'() {
393         given: 'list node data fragment as a collection of data nodes'
394             def listNodeCollection = buildDataNodeCollection(listNodeXpaths)
395         when: 'list-node elements were replaced under existing parent node'
396             objectUnderTest.replaceListDataNodes(DATASPACE_NAME, ANCHOR_NAME3, parentNodeXpath, listNodeCollection)
397         then: 'a #expectedException is thrown'
398             thrown(expectedException)
399         where: 'following parameters were used'
400             scenario                     | parentNodeXpath | listNodeXpaths || expectedException
401             'parent node does not exist' | '/unknown'      | ['irrelevant'] || DataNodeNotFoundException
402     }
403
404     static Collection<DataNode> buildDataNodeCollection(xpaths) {
405         return xpaths.collect { new DataNodeBuilder().withXpath(it).build() }
406     }
407
408     static DataNode buildDataNode(xpath, leaves, childDataNodes) {
409         return new DataNodeBuilder().withXpath(xpath).withLeaves(leaves).withChildDataNodes(childDataNodes).build()
410     }
411
412     static Map<String, Object> getLeavesMap(FragmentEntity fragmentEntity) {
413         return GSON.fromJson(fragmentEntity.getAttributes(), Map<String, Object>.class)
414     }
415
416     def static assertLeavesMaps(actualLeavesMap, expectedLeavesMap) {
417         expectedLeavesMap.forEach((key, value) -> {
418             def actualValue = actualLeavesMap[key]
419             if (value instanceof Collection<?> && actualValue instanceof Collection<?>) {
420                 assert value.size() == actualValue.size()
421                 assert value.containsAll(actualValue)
422             } else {
423                 assert value == actualValue
424             }
425         })
426         return true
427     }
428
429     def static treeToFlatMapByXpath(Map<String, DataNode> flatMap, DataNode dataNodeTree) {
430         flatMap.put(dataNodeTree.getXpath(), dataNodeTree)
431         dataNodeTree.getChildDataNodes()
432                 .forEach(childDataNode -> treeToFlatMapByXpath(flatMap, childDataNode))
433         return flatMap
434     }
435
436 }