/*
- * ============LICENSE_START=======================================================
+ * ============LICENSE_START=======================================================
* Copyright (C) 2021 Nordix Foundation
* Modifications Copyright (C) 2021 Pantheon.tech
+ * Modifications Copyright (C) 2021 Bell Canada.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*/
package org.onap.cps.spi.impl
+import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
+import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
+
import com.google.common.collect.ImmutableSet
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import org.onap.cps.spi.CpsDataPersistenceService
-import org.onap.cps.spi.FetchDescendantsOption
import org.onap.cps.spi.entities.FragmentEntity
+import org.onap.cps.spi.exceptions.AlreadyDefinedException
import org.onap.cps.spi.exceptions.AnchorNotFoundException
import org.onap.cps.spi.exceptions.DataNodeNotFoundException
import org.onap.cps.spi.exceptions.DataspaceNotFoundException
import org.onap.cps.spi.model.DataNode
import org.onap.cps.spi.model.DataNodeBuilder
import org.springframework.beans.factory.annotation.Autowired
-import org.springframework.dao.DataIntegrityViolationException
import org.springframework.test.context.jdbc.Sql
-import spock.lang.Unroll
-import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
-import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
+import javax.validation.ConstraintViolationException
class CpsDataPersistenceServiceSpec extends CpsPersistenceSpecBase {
static final String XPATH_DATA_NODE_WITH_LEAVES = '/parent-100'
static final long UPDATE_DATA_NODE_FRAGMENT_ID = 4202L
static final long UPDATE_DATA_NODE_SUB_FRAGMENT_ID = 4203L
+ static final long LIST_DATA_NODE_PARENT_FRAGMENT_ID = 4206L
static final DataNode newDataNode = new DataNodeBuilder().build()
static DataNode existingDataNode
static DataNode existingChildDataNode
def expectedLeavesByXpathMap = [
- '/parent-100' : ['parent-leaf': 'parent-leaf-value'],
- '/parent-100/child-001' : ['first-child-leaf': 'first-child-leaf-value'],
- '/parent-100/child-002' : ['second-child-leaf': 'second-child-leaf-value'],
- '/parent-100/child-002/grand-child': ['grand-child-leaf': 'grand-child-leaf-value']
+ '/parent-100' : ['parent-leaf': 'parent-leaf value'],
+ '/parent-100/child-001' : ['first-child-leaf': 'first-child-leaf value'],
+ '/parent-100/child-002' : ['second-child-leaf': 'second-child-leaf value'],
+ '/parent-100/child-002/grand-child': ['grand-child-leaf': 'grand-child-leaf value']
]
static {
grandchildFragment.xpath == grandChildXpath
}
- @Unroll
+ @Sql([CLEAR_DATA, SET_DATA])
+ def 'Store data node for multiple anchors using the same schema.'() {
+ def xpath = "/parent-new"
+ given: 'a fragment is stored for an anchor'
+ objectUnderTest.storeDataNode(DATASPACE_NAME, ANCHOR_NAME1, createDataNodeTree(xpath))
+ when: 'another fragment is stored for an other anchor, using the same schema set'
+ objectUnderTest.storeDataNode(DATASPACE_NAME, ANCHOR_NAME3, createDataNodeTree(xpath))
+ then: 'both fragments can be retrieved by their xpath'
+ def fragment1 = getFragmentByXpath(DATASPACE_NAME, ANCHOR_NAME1, xpath)
+ fragment1.anchor.name == ANCHOR_NAME1
+ fragment1.xpath == xpath
+ def fragment2 = getFragmentByXpath(DATASPACE_NAME, ANCHOR_NAME3, xpath)
+ fragment2.anchor.name == ANCHOR_NAME3
+ fragment2.xpath == xpath
+ }
+
@Sql([CLEAR_DATA, SET_DATA])
def 'Store datanode error scenario: #scenario.'() {
when: 'attempt to store a data node with #scenario'
scenario | dataspaceName | anchorName | dataNode || expectedException
'dataspace does not exist' | 'unknown' | 'not-relevant' | newDataNode || DataspaceNotFoundException
'schema set does not exist' | DATASPACE_NAME | 'unknown' | newDataNode || AnchorNotFoundException
- 'anchor already exists' | DATASPACE_NAME | ANCHOR_NAME1 | existingDataNode || DataIntegrityViolationException
+ 'anchor already exists' | DATASPACE_NAME | ANCHOR_NAME1 | newDataNode || ConstraintViolationException
+ 'datanode already exists' | DATASPACE_NAME | ANCHOR_NAME1 | existingDataNode || AlreadyDefinedException
}
@Sql([CLEAR_DATA, SET_DATA])
parentFragment.getChildFragments().find({ it.xpath == newChild.xpath })
}
- @Unroll
@Sql([CLEAR_DATA, SET_DATA])
def 'Add child error scenario: #scenario.'() {
when: 'attempt to add a child data node with #scenario'
where: 'the following data is used'
scenario | parentXpath | dataNode || expectedException
'parent does not exist' | 'unknown' | newDataNode || DataNodeNotFoundException
- 'already existing child' | XPATH_DATA_NODE_WITH_DESCENDANTS | existingChildDataNode || DataIntegrityViolationException
+ 'already existing child' | XPATH_DATA_NODE_WITH_DESCENDANTS | existingChildDataNode || AlreadyDefinedException
+ }
+
+ @Sql([CLEAR_DATA, SET_DATA])
+ def 'Add list-node fragment with multiple elements.'() {
+ given: 'list node data fragment as a collection of data nodes'
+ def listNodeXpaths = ['/parent-201/child-204[@key="B"]', '/parent-201/child-204[@key="C"]']
+ def listNodeCollection = buildDataNodeCollection(listNodeXpaths)
+ when: 'list-node elements added to existing parent node'
+ objectUnderTest.addListDataNodes(DATASPACE_NAME, ANCHOR_NAME3, '/parent-201', listNodeCollection)
+ then: 'new entries successfully persisted, parent node now contains 5 children (2 new + 3 existing before)'
+ def parentFragment = fragmentRepository.getOne(LIST_DATA_NODE_PARENT_FRAGMENT_ID)
+ def allChildXpaths = parentFragment.getChildFragments().collect { it.getXpath() }
+ assert allChildXpaths.size() == 5
+ assert allChildXpaths.containsAll(listNodeXpaths)
+ }
+
+ @Sql([CLEAR_DATA, SET_DATA])
+ def 'Add list-node fragment error scenario: #scenario.'() {
+ given: 'list node data fragment as a collection of data nodes'
+ def listNodeCollection = buildDataNodeCollection(listNodeXpaths)
+ when: 'list-node elements added to existing parent node'
+ objectUnderTest.addListDataNodes(DATASPACE_NAME, ANCHOR_NAME3, parentNodeXpath, listNodeCollection)
+ then: 'a #expectedException is thrown'
+ thrown(expectedException)
+ where: 'following parameters were used'
+ scenario | parentNodeXpath | listNodeXpaths || expectedException
+ 'parent node does not exist' | '/unknown' | ['irrelevant'] || DataNodeNotFoundException
+ 'already existing fragment' | '/parent-201' | ['/parent-201/child-204[@key="A"]'] || AlreadyDefinedException
+
}
static def createDataNodeTree(String... xpaths) {
def 'Get data node by xpath without descendants.'() {
when: 'data node is requested'
def result = objectUnderTest.getDataNode(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES,
- XPATH_DATA_NODE_WITH_LEAVES, OMIT_DESCENDANTS)
+ inputXPath, OMIT_DESCENDANTS)
then: 'data node is returned with no descendants'
assert result.getXpath() == XPATH_DATA_NODE_WITH_LEAVES
and: 'expected leaves'
assert result.getChildDataNodes().size() == 0
assertLeavesMaps(result.getLeaves(), expectedLeavesByXpathMap[XPATH_DATA_NODE_WITH_LEAVES])
+ where: 'the following data is used'
+ scenario | inputXPath
+ 'some xpath' | '/parent-100'
+ 'root xpath' | '/'
+ 'empty xpath' | ''
}
@Sql([CLEAR_DATA, SET_DATA])
def 'Get data node by xpath with all descendants.'() {
when: 'data node is requested with all descendants'
def result = objectUnderTest.getDataNode(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES,
- XPATH_DATA_NODE_WITH_LEAVES, INCLUDE_ALL_DESCENDANTS)
+ inputXPath, INCLUDE_ALL_DESCENDANTS)
def mappedResult = treeToFlatMapByXpath(new HashMap<>(), result)
then: 'data node is returned with all the descendants populated'
assert mappedResult.size() == 4
assert mappedResult.get('/parent-100/child-002').getChildDataNodes().size() == 1
and: 'extracted leaves maps are matching expected'
mappedResult.forEach(
- (xpath, dataNode) ->
- assertLeavesMaps(dataNode.getLeaves(), expectedLeavesByXpathMap[xpath])
- )
- }
-
- def static assertLeavesMaps(actualLeavesMap, expectedLeavesMap) {
- expectedLeavesMap.forEach((key, value) -> {
- def actualValue = actualLeavesMap[key]
- if (value instanceof Collection<?> && actualValue instanceof Collection<?>) {
- assert value.size() == actualValue.size()
- assert value.containsAll(actualValue)
- } else {
- assert value == actualValue
- }
- }
- )
- return true
- }
-
- def static treeToFlatMapByXpath(Map<String, DataNode> flatMap, DataNode dataNodeTree) {
- flatMap.put(dataNodeTree.getXpath(), dataNodeTree)
- dataNodeTree.getChildDataNodes()
- .forEach(childDataNode -> treeToFlatMapByXpath(flatMap, childDataNode))
- return flatMap
+ (xPath, dataNode) -> assertLeavesMaps(dataNode.getLeaves(), expectedLeavesByXpathMap[xPath]))
+ where: 'the following data is used'
+ scenario | inputXPath
+ 'some xpath' | '/parent-100'
+ 'root xpath' | '/'
+ 'empty xpath' | ''
}
- @Unroll
@Sql([CLEAR_DATA, SET_DATA])
def 'Get data node error scenario: #scenario.'() {
when: 'attempt to get data node with #scenario'
assert childLeaves.'leaf-value' == 'original'
}
- @Unroll
@Sql([CLEAR_DATA, SET_DATA])
def 'Update data leaves error scenario: #scenario.'() {
when: 'attempt to update data node for #scenario'
assert childLeaves.'leaf-value' == 'original'
}
- @Unroll
@Sql([CLEAR_DATA, SET_DATA])
def 'Replace data node tree error scenario: #scenario.'() {
given: 'data node object'
'non-existing xpath' | DATASPACE_NAME | ANCHOR_FOR_DATA_NODES_WITH_LEAVES | 'NON-EXISTING XPATH' || DataNodeNotFoundException
}
+ @Sql([CLEAR_DATA, SET_DATA])
+ def 'Replace list-node content of #scenario.'() {
+ given: 'list node data fragment as a collection of data nodes'
+ def listNodeCollection = buildDataNodeCollection(listNodeXpaths)
+ when: 'list-node elements replaced within the existing parent node'
+ objectUnderTest.replaceListDataNodes(DATASPACE_NAME, ANCHOR_NAME3, '/parent-201', listNodeCollection)
+ then: 'child list elements are updated as expected, non-list element remains as is'
+ def parentFragment = fragmentRepository.getOne(LIST_DATA_NODE_PARENT_FRAGMENT_ID)
+ def allChildXpaths = parentFragment.getChildFragments().collect { it.getXpath() }
+ assert allChildXpaths.size() == expectedChildXpaths.size()
+ assert allChildXpaths.containsAll(expectedChildXpaths)
+ where: 'following parameters were used'
+ scenario | listNodeXpaths || expectedChildXpaths
+ 'existing list-node' | ['/parent-201/child-204[@key="B"]'] || ['/parent-201/child-203', '/parent-201/child-204[@key="B"]']
+ '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"]']
+ }
+
+ @Sql([CLEAR_DATA, SET_DATA])
+ def 'Replace list-node fragment error scenario: #scenario.'() {
+ given: 'list node data fragment as a collection of data nodes'
+ def listNodeCollection = buildDataNodeCollection(listNodeXpaths)
+ when: 'list-node elements were replaced under existing parent node'
+ objectUnderTest.replaceListDataNodes(DATASPACE_NAME, ANCHOR_NAME3, parentNodeXpath, listNodeCollection)
+ then: 'a #expectedException is thrown'
+ thrown(expectedException)
+ where: 'following parameters were used'
+ scenario | parentNodeXpath | listNodeXpaths || expectedException
+ 'parent node does not exist' | '/unknown' | ['irrelevant'] || DataNodeNotFoundException
+ }
+
+ static Collection<DataNode> buildDataNodeCollection(xpaths) {
+ return xpaths.collect { new DataNodeBuilder().withXpath(it).build() }
+ }
+
static DataNode buildDataNode(xpath, leaves, childDataNodes) {
return new DataNodeBuilder().withXpath(xpath).withLeaves(leaves).withChildDataNodes(childDataNodes).build()
}
return GSON.fromJson(fragmentEntity.getAttributes(), Map<String, Object>.class)
}
- @Unroll
- @Sql([CLEAR_DATA, SET_DATA])
- def 'Cps Path query for single leaf value with type: #type.'() {
- when: 'a query is executed to get a data node by the given cps path'
- def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, cpsPath, includeDescendantsOption)
- then: 'the correct data is returned'
- def leaves ='[common-leaf-name:common-leaf-value, common-leaf-name-int:5.0]'
- DataNode dataNode = result.stream().findFirst().get()
- dataNode.getLeaves().toString() == leaves
- dataNode.getChildDataNodes().size() == expectedNumberOfChidlNodes
- where: 'the following data is used'
- type | cpsPath | includeDescendantsOption | expectedNumberOfChidlNodes
- 'String and no descendants' | '/parent-200/child-202[@common-leaf-name=\'common-leaf-value\']' | OMIT_DESCENDANTS | 0
- 'Integer and descendants' | '/parent-200/child-202[@common-leaf-name-int=5]' | INCLUDE_ALL_DESCENDANTS | 1
+ def static assertLeavesMaps(actualLeavesMap, expectedLeavesMap) {
+ expectedLeavesMap.forEach((key, value) -> {
+ def actualValue = actualLeavesMap[key]
+ if (value instanceof Collection<?> && actualValue instanceof Collection<?>) {
+ assert value.size() == actualValue.size()
+ assert value.containsAll(actualValue)
+ } else {
+ assert value == actualValue
+ }
+ })
+ return true
}
- @Unroll
- @Sql([CLEAR_DATA, SET_DATA])
- def 'Query for attribute by cps path with cps paths that return no data because of #scenario.'() {
- when: 'a query is executed to get datanodes for the given cps path'
- def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, cpsPath, FetchDescendantsOption.OMIT_DESCENDANTS)
- then: 'no data is returned'
- result.isEmpty()
- where: 'following cps queries are performed'
- scenario | cpsPath
- 'cps path is incomplete' | '/parent-200[@common-leaf-name-int=5]'
- 'missing / at beginning of path' | 'parent-200/child-202[@common-leaf-name-int=5]'
- 'leaf value does not exist' | '/parent-200/child-202[@common-leaf-name=\'does not exist\']'
- 'incomplete end of xpath prefix' | '/parent-200/child-20[@common-leaf-name-int=5]'
+ def static treeToFlatMapByXpath(Map<String, DataNode> flatMap, DataNode dataNodeTree) {
+ flatMap.put(dataNodeTree.getXpath(), dataNodeTree)
+ dataNodeTree.getChildDataNodes()
+ .forEach(childDataNode -> treeToFlatMapByXpath(flatMap, childDataNode))
+ return flatMap
}
+
}