diff options
author | 2025-03-14 14:14:03 +0000 | |
---|---|---|
committer | 2025-03-14 14:14:03 +0000 | |
commit | 7a67aa410fbdaaed0cf5b5110512c442bfb400ff (patch) | |
tree | ce9d1d732400a33635a9186d051774fbefbb47d4 /cps-service/src | |
parent | ab6e02b126ca359cc0a57a569219321897abb4ba (diff) | |
parent | 6d5849d012c1a2fe2177d332610a1b99bd44fe40 (diff) |
Merge "Add attribute-axis to CPS query nodes rest API"
Diffstat (limited to 'cps-service/src')
3 files changed, 34 insertions, 2 deletions
diff --git a/cps-service/src/main/java/org/onap/cps/impl/CpsFacadeImpl.java b/cps-service/src/main/java/org/onap/cps/impl/CpsFacadeImpl.java index 4ac0d5d8e8..35a03685b6 100644 --- a/cps-service/src/main/java/org/onap/cps/impl/CpsFacadeImpl.java +++ b/cps-service/src/main/java/org/onap/cps/impl/CpsFacadeImpl.java @@ -23,6 +23,7 @@ package org.onap.cps.impl; import java.util.Collection; import java.util.List; import java.util.Map; +import java.util.Set; import lombok.RequiredArgsConstructor; import org.onap.cps.api.CpsDataService; import org.onap.cps.api.CpsFacade; @@ -30,6 +31,8 @@ import org.onap.cps.api.CpsQueryService; import org.onap.cps.api.model.DataNode; import org.onap.cps.api.parameters.FetchDescendantsOption; import org.onap.cps.api.parameters.PaginationOption; +import org.onap.cps.cpspath.parser.CpsPathQuery; +import org.onap.cps.cpspath.parser.CpsPathUtil; import org.onap.cps.utils.DataMapper; import org.springframework.stereotype.Service; @@ -66,6 +69,13 @@ public class CpsFacadeImpl implements CpsFacade { final String anchorName, final String cpsPath, final FetchDescendantsOption fetchDescendantsOption) { + final CpsPathQuery cpsPathQuery = CpsPathUtil.getCpsPathQuery(cpsPath); + if (cpsPathQuery.hasAttributeAxis()) { + final String attributeName = cpsPathQuery.getAttributeAxisAttributeName(); + final Set<Object> attributeValues = + cpsQueryService.queryDataLeaf(dataspaceName, anchorName, cpsPath, Object.class); + return dataMapper.toAttributeMaps(attributeName, attributeValues); + } final Collection<DataNode> dataNodes = cpsQueryService.queryDataNodes(dataspaceName, anchorName, cpsPath, fetchDescendantsOption); return dataMapper.toDataMaps(dataspaceName, anchorName, dataNodes); diff --git a/cps-service/src/main/java/org/onap/cps/utils/DataMapper.java b/cps-service/src/main/java/org/onap/cps/utils/DataMapper.java index 6e7eff9132..29d61ffcc4 100644 --- a/cps-service/src/main/java/org/onap/cps/utils/DataMapper.java +++ b/cps-service/src/main/java/org/onap/cps/utils/DataMapper.java @@ -107,6 +107,17 @@ public class DataMapper { } /** + * Converts list of attributes values to a list of data maps. + * @param attributeName attribute name + * @param attributeValues attribute values + * @return a list of maps representing the attribute values + */ + public List<Map<String, Object>> toAttributeMaps(final String attributeName, + final Collection<Object> attributeValues) { + return attributeValues.stream().map(attributeValue -> Map.of(attributeName, attributeValue)).toList(); + } + + /** * Convert a collection of data nodes to a data map. * * @param anchor the anchor diff --git a/cps-service/src/test/groovy/org/onap/cps/impl/CpsFacadeImplSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/impl/CpsFacadeImplSpec.groovy index c754970518..4351631ee1 100644 --- a/cps-service/src/test/groovy/org/onap/cps/impl/CpsFacadeImplSpec.groovy +++ b/cps-service/src/test/groovy/org/onap/cps/impl/CpsFacadeImplSpec.groovy @@ -75,15 +75,26 @@ class CpsFacadeImplSpec extends Specification { def 'Execute anchor query.'() { given: 'the cps query service returns two data nodes' - mockCpsQueryService.queryDataNodes('my dataspace', 'my anchor', 'my cps path', myFetchDescendantsOption) >> [ dataNode1, dataNode2] + mockCpsQueryService.queryDataNodes('my dataspace', 'my anchor', '/my/path', myFetchDescendantsOption) >> [ dataNode1, dataNode2] when: 'get data node by dataspace and anchor' - def result = objectUnderTest.executeAnchorQuery('my dataspace', 'my anchor', 'my cps path', myFetchDescendantsOption) + def result = objectUnderTest.executeAnchorQuery('my dataspace', 'my anchor', '/my/path', myFetchDescendantsOption) then: 'all nodes (from the query service result) are returned' assert result.size() == 2 assert result[0].keySet()[0] == 'prefix1:path1' assert result[1].keySet()[0] == 'prefix2:path2' } + def 'Execute anchor query with attribute-axis.'() { + given: 'the cps query service returns two attribute values' + mockCpsQueryService.queryDataLeaf('my dataspace', 'my anchor', '/my/path/@myAttribute', Object) >> ['value1', 'value2'] + when: 'get data using attribute axis' + def result = objectUnderTest.executeAnchorQuery('my dataspace', 'my anchor', '/my/path/@myAttribute', myFetchDescendantsOption) + then: 'attribute values (from the query service result) are returned' + assert result.size() == 2 + assert result[0] == ['myAttribute': 'value1'] + assert result[1] == ['myAttribute': 'value2'] + } + def 'Execute dataspace query.'() { given: 'the cps query service returns two data nodes (on two different anchors)' mockCpsQueryService.queryDataNodesAcrossAnchors('my dataspace', 'my cps path', myFetchDescendantsOption, myPaginationOption) >> [ dataNode1, dataNode2, dataNode3 ] |