summaryrefslogtreecommitdiffstats
path: root/cps-ri
diff options
context:
space:
mode:
Diffstat (limited to 'cps-ri')
-rwxr-xr-x[-rw-r--r--]cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java11
-rwxr-xr-xcps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java9
-rwxr-xr-x[-rw-r--r--]cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceServiceSpec.groovy20
-rwxr-xr-x[-rw-r--r--]cps-ri/src/test/resources/data/fragment.sql14
4 files changed, 41 insertions, 13 deletions
diff --git a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java
index fa13c7d14d..a02b193d9e 100644..100755
--- a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java
+++ b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java
@@ -130,7 +130,12 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService
final String xpath) {
final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
- return fragmentRepository.getByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity, xpath);
+ if (isRootXpath(xpath)) {
+ return fragmentRepository.getFirstByDataspaceAndAnchor(dataspaceEntity, anchorEntity);
+ } else {
+ return fragmentRepository.getByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity,
+ xpath);
+ }
}
@Override
@@ -205,4 +210,8 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService
fragmentEntity.setChildFragments(Collections.emptySet());
fragmentRepository.save(fragmentEntity);
}
+
+ private boolean isRootXpath(final String xpath) {
+ return "/".equals(xpath) || "".equals(xpath);
+ }
}
diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java
index b896fe86d0..74d04613d9 100755
--- a/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java
+++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java
@@ -48,6 +48,15 @@ public interface FragmentRepository extends JpaRepository<FragmentEntity, Long>
.orElseThrow(() -> new DataNodeNotFoundException(dataspaceEntity.getName(), anchorEntity.getName(), xpath));
}
+ Optional<FragmentEntity> findFirstByDataspaceAndAnchor(@NonNull DataspaceEntity dataspaceEntity,
+ @NonNull AnchorEntity anchorEntity);
+
+ default FragmentEntity getFirstByDataspaceAndAnchor(@NonNull DataspaceEntity dataspaceEntity,
+ @NonNull AnchorEntity anchorEntity) {
+ return findFirstByDataspaceAndAnchor(dataspaceEntity, anchorEntity)
+ .orElseThrow(() -> new DataNodeNotFoundException(dataspaceEntity.getName(), anchorEntity.getName()));
+ }
+
@Modifying
@Query("DELETE FROM FragmentEntity fe WHERE fe.anchor IN (:anchors)")
void deleteByAnchorIn(@NotNull @Param("anchors") Collection<AnchorEntity> anchorEntities);
diff --git a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceServiceSpec.groovy b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceServiceSpec.groovy
index a47bd65d02..ea6b26923a 100644..100755
--- a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceServiceSpec.groovy
+++ b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceServiceSpec.groovy
@@ -167,23 +167,30 @@ class CpsDataPersistenceServiceSpec extends CpsPersistenceSpecBase {
return fragmentRepository.findByDataspaceAndAnchorAndXpath(dataspace, anchor, xpath).orElseThrow()
}
+ @Unroll
@Sql([CLEAR_DATA, SET_DATA])
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' |''
}
+ @Unroll
@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
@@ -192,9 +199,12 @@ class CpsDataPersistenceServiceSpec extends CpsPersistenceSpecBase {
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])
- )
+ (inputXPath, dataNode) -> assertLeavesMaps(dataNode.getLeaves(), expectedLeavesByXpathMap[inputXPath]))
+ where: 'the following data is used'
+ scenario | inputXPath
+ 'some xpath' |'/parent-100'
+ 'root xpath' |'/'
+ 'empty xpath' |''
}
def static assertLeavesMaps(actualLeavesMap, expectedLeavesMap) {
diff --git a/cps-ri/src/test/resources/data/fragment.sql b/cps-ri/src/test/resources/data/fragment.sql
index b6dc2ca307..d252fbbb59 100644..100755
--- a/cps-ri/src/test/resources/data/fragment.sql
+++ b/cps-ri/src/test/resources/data/fragment.sql
@@ -8,13 +8,13 @@ INSERT INTO ANCHOR (ID, NAME, DATASPACE_ID, SCHEMA_SET_ID) VALUES
(3001, 'ANCHOR-001', 1001, 2001),
(3003, 'ANCHOR-003', 1001, 2001);
-INSERT INTO FRAGMENT (ID, XPATH, ANCHOR_ID, PARENT_ID, DATASPACE_ID) VALUES
- (4001, '/parent-1', 3001, null, 1001),
- (4002, '/parent-2', 3001, null, 1001),
- (4003, '/parent-3', 3001, null, 1001),
- (4004, '/parent-1/child-1', 3001, 4001, 1001),
- (4005, '/parent-2/child-2', 3001, 4002, 1001),
- (4006, '/parent-1/child-1/grandchild-1', 3001, 4004, 1001);
+INSERT INTO FRAGMENT (ID, DATASPACE_ID, ANCHOR_ID, PARENT_ID, XPATH) VALUES
+ (4001, 1001, 3001, null, '/parent-1'),
+ (4002, 1001, 3001, null, '/parent-2'),
+ (4003, 1001, 3001, null, '/parent-3'),
+ (4004, 1001, 3001, 4001, '/parent-1/child-1'),
+ (4005, 1001, 3001, 4002, '/parent-2/child-2'),
+ (4006, 1001, 3001, 4004, '/parent-1/child-1/grandchild-1');
INSERT INTO FRAGMENT (ID, DATASPACE_ID, ANCHOR_ID, PARENT_ID, XPATH, ATTRIBUTES) VALUES
(4101, 1001, 3003, null, '/parent-100', '{"parent-leaf": "parent-leaf value"}'),