2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation
4 * Modifications Copyright (C) 2021 Pantheon.tech
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.onap.cps.spi.impl
22 import com.google.common.collect.ImmutableSet
23 import org.onap.cps.spi.CpsDataPersistenceService
24 import org.onap.cps.spi.exceptions.AnchorNotFoundException
25 import org.onap.cps.spi.exceptions.DataNodeNotFoundException
26 import org.onap.cps.spi.exceptions.DataspaceNotFoundException
27 import org.onap.cps.spi.model.DataNode
28 import org.onap.cps.spi.model.DataNodeBuilder
29 import org.springframework.beans.factory.annotation.Autowired
30 import org.springframework.dao.DataIntegrityViolationException
31 import org.springframework.test.context.jdbc.Sql
32 import spock.lang.Unroll
34 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
35 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
37 class CpsDataPersistenceServiceSpec extends CpsPersistenceSpecBase {
40 CpsDataPersistenceService objectUnderTest
42 static final String SET_DATA = '/data/fragment.sql'
43 static final long ID_DATA_NODE_WITH_DESCENDANTS = 4001
44 static final String XPATH_DATA_NODE_WITH_DESCENDANTS = '/parent-1'
45 static final String XPATH_DATA_NODE_WITH_LEAVES = '/parent-100'
47 static final DataNode newDataNode = new DataNodeBuilder().build()
48 static DataNode existingDataNode
49 static DataNode existingChildDataNode
51 static Map<String, Map<String, Object>> expectedLeavesByXpathMap = [
52 '/parent-100' : ["x": "y"],
53 '/parent-100/child-001' : ["a": "b", "c": ["d", "e", "f"]],
54 '/parent-100/child-002' : ["g": "h", "i": ["j", "k"]],
55 '/parent-100/child-002/grand-child': ["l": "m", "n": ["o", "p"]]
59 existingDataNode = createDataNodeTree(XPATH_DATA_NODE_WITH_DESCENDANTS)
60 existingChildDataNode = createDataNodeTree('/parent-1/child-1')
63 @Sql([CLEAR_DATA, SET_DATA])
64 def 'StoreDataNode with descendants.'() {
65 when: 'a fragment with descendants is stored'
66 def parentXpath = "/parent-new"
67 def childXpath = "/parent-new/child-new"
68 def grandChildXpath = "/parent-new/child-new/grandchild-new"
69 objectUnderTest.storeDataNode(DATASPACE_NAME, ANCHOR_NAME1,
70 createDataNodeTree(parentXpath, childXpath, grandChildXpath))
71 then: 'it can be retrieved by its xpath'
72 def parentFragment = getFragmentByXpath(DATASPACE_NAME, ANCHOR_NAME1, parentXpath)
73 and: 'it contains the children'
74 parentFragment.childFragments.size() == 1
75 def childFragment = parentFragment.childFragments[0]
76 childFragment.xpath == childXpath
77 and: "and its children's children"
78 childFragment.childFragments.size() == 1
79 def grandchildFragment = childFragment.childFragments[0]
80 grandchildFragment.xpath == grandChildXpath
83 @Sql([CLEAR_DATA, SET_DATA])
84 def 'Store datanode error scenario: #scenario.'() {
85 when: 'attempt to store a data node with #scenario'
86 objectUnderTest.storeDataNode(dataspaceName, anchorName, dataNode)
87 then: 'a #expectedException is thrown'
88 thrown(expectedException)
89 where: 'the following data is used'
90 scenario | dataspaceName | anchorName | dataNode || expectedException
91 'dataspace does not exist' | 'unknown' | 'not-relevant' | newDataNode || DataspaceNotFoundException
92 'schema set does not exist' | DATASPACE_NAME | 'unknown' | newDataNode || AnchorNotFoundException
93 'anchor already exists' | DATASPACE_NAME | ANCHOR_NAME1 | existingDataNode || DataIntegrityViolationException
96 @Sql([CLEAR_DATA, SET_DATA])
97 def 'Add a child to a Fragment that already has a child.'() {
98 given: ' a new child node'
99 def newChild = createDataNodeTree('xpath for new child')
100 when: 'the child is added to an existing parent with 1 child'
101 objectUnderTest.addChildDataNode(DATASPACE_NAME, ANCHOR_NAME1, XPATH_DATA_NODE_WITH_DESCENDANTS, newChild)
102 then: 'the parent is now has to 2 children'
103 def expectedExistingChildPath = '/parent-1/child-1'
104 def parentFragment = fragmentRepository.findById(ID_DATA_NODE_WITH_DESCENDANTS).orElseThrow()
105 parentFragment.getChildFragments().size() == 2
106 and: 'it still has the old child'
107 parentFragment.getChildFragments().find({ it.xpath == expectedExistingChildPath })
108 and: 'it has the new child'
109 parentFragment.getChildFragments().find({ it.xpath == newChild.xpath })
112 @Sql([CLEAR_DATA, SET_DATA])
113 def 'Add child error scenario: #scenario.'() {
114 when: 'attempt to add a child data node with #scenario'
115 objectUnderTest.addChildDataNode(DATASPACE_NAME, ANCHOR_NAME1, parentXpath, dataNode)
116 then: 'a #expectedException is thrown'
117 thrown(expectedException)
118 where: 'the following data is used'
119 scenario | parentXpath | dataNode || expectedException
120 'parent does not exist' | 'unknown' | newDataNode || DataNodeNotFoundException
121 'already existing child' | XPATH_DATA_NODE_WITH_DESCENDANTS | existingChildDataNode || DataIntegrityViolationException
124 static def createDataNodeTree(String... xpaths) {
125 def dataNodeBuilder = new DataNodeBuilder().withXpath(xpaths[0])
126 if (xpaths.length > 1) {
127 def xPathsDescendant = Arrays.copyOfRange(xpaths, 1, xpaths.length)
128 def childDataNode = createDataNodeTree(xPathsDescendant)
129 dataNodeBuilder.withChildDataNodes(ImmutableSet.of(childDataNode))
131 dataNodeBuilder.build()
134 def getFragmentByXpath(dataspaceName, anchorName, xpath) {
135 def dataspace = dataspaceRepository.getByName(dataspaceName)
136 def anchor = anchorRepository.getByDataspaceAndName(dataspace, anchorName)
137 return fragmentRepository.findByDataspaceAndAnchorAndXpath(dataspace, anchor, xpath).orElseThrow()
140 @Sql([CLEAR_DATA, SET_DATA])
141 def 'Get data node by xpath without descendants.'() {
142 when: 'data node is requested'
143 def result = objectUnderTest.getDataNode(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES,
144 XPATH_DATA_NODE_WITH_LEAVES, OMIT_DESCENDANTS)
145 then: 'data node is returned with no descendants'
146 assert result.getXpath() == XPATH_DATA_NODE_WITH_LEAVES
147 and: 'expected leaves'
148 assert result.getChildDataNodes().size() == 0
149 assertLeavesMaps(result.getLeaves(), expectedLeavesByXpathMap[XPATH_DATA_NODE_WITH_LEAVES])
152 @Sql([CLEAR_DATA, SET_DATA])
153 def 'Get data node by xpath with all descendants.'() {
154 when: 'data node is requested with all descendants'
155 def result = objectUnderTest.getDataNode(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES,
156 XPATH_DATA_NODE_WITH_LEAVES, INCLUDE_ALL_DESCENDANTS)
157 def mappedResult = treeToFlatMapByXpath(new HashMap<>(), result)
158 then: 'data node is returned with all the descendants populated'
159 assert mappedResult.size() == 4
160 assert result.getChildDataNodes().size() == 2
161 assert mappedResult.get('/parent-100/child-001').getChildDataNodes().size() == 0
162 assert mappedResult.get('/parent-100/child-002').getChildDataNodes().size() == 1
163 and: 'extracted leaves maps are matching expected'
164 mappedResult.forEach(
166 assertLeavesMaps(dataNode.getLeaves(), expectedLeavesByXpathMap[xpath])
170 def static assertLeavesMaps(actualLeavesMap, expectedLeavesMap) {
171 expectedLeavesMap.forEach((key, value) -> {
172 def actualValue = actualLeavesMap[key]
173 if (value instanceof Collection<?> && actualValue instanceof Collection<?>) {
174 assert value.size() == actualValue.size()
175 assert value.containsAll(actualValue)
177 assert value == actualValue
184 def static treeToFlatMapByXpath(Map<String, DataNode> flatMap, DataNode dataNodeTree) {
185 flatMap.put(dataNodeTree.getXpath(), dataNodeTree)
186 dataNodeTree.getChildDataNodes()
187 .forEach(childDataNode -> treeToFlatMapByXpath(flatMap, childDataNode))
192 @Sql([CLEAR_DATA, SET_DATA])
193 def 'Get data node error scenario: #scenario.'() {
194 when: 'attempt to get data node with #scenario'
195 objectUnderTest.getDataNode(dataspaceName, anchorName, xpath, OMIT_DESCENDANTS)
196 then: 'a #expectedException is thrown'
197 thrown(expectedException)
198 where: 'the following data is used'
199 scenario | dataspaceName | anchorName | xpath || expectedException
200 'non-existing dataspace' | 'NO DATASPACE' | 'not relevant' | 'not relevant' || DataspaceNotFoundException
201 'non-existing anchor' | DATASPACE_NAME | 'NO ANCHOR' | 'not relevant' || AnchorNotFoundException
202 'non-existing xpath' | DATASPACE_NAME | ANCHOR_FOR_DATA_NODES_WITH_LEAVES | 'NO XPATH' || DataNodeNotFoundException