diff options
8 files changed, 274 insertions, 85 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 ca279f30c1..fa13c7d14d 100644 --- 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 @@ -2,6 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2021 Nordix Foundation * Modifications Copyright (C) 2021 Pantheon.tech + * Modifications Copyright (C) 2020-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. @@ -143,6 +144,11 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService fragmentEntities = fragmentRepository .getByAnchorAndXpathAndLeafAttributes(anchorEntity.getId(), cpsPathQuery.getXpathPrefix(), cpsPathQuery .getLeafName(), cpsPathQuery.getLeafValue()); + } else if (CpsPathQueryType.XPATH_HAS_DESCENDANT_WITH_LEAF_VALUES.equals(cpsPathQuery.getCpsPathQueryType())) { + final String leafDataAsJson = GSON.toJson(cpsPathQuery.getLeavesData()); + fragmentEntities = fragmentRepository + .getByAnchorAndDescendentNameAndLeafValues(anchorEntity.getId(), + cpsPathQuery.getDescendantName(), leafDataAsJson); } else { fragmentEntities = fragmentRepository .getByAnchorAndXpathEndsInDescendantName(anchorEntity.getId(), cpsPathQuery.getDescendantName()); diff --git a/cps-ri/src/main/java/org/onap/cps/spi/query/CpsPathQuery.java b/cps-ri/src/main/java/org/onap/cps/spi/query/CpsPathQuery.java index ce78c06d4f..c5861bd4b6 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/query/CpsPathQuery.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/query/CpsPathQuery.java @@ -1,7 +1,7 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2021 Nordix Foundation - * Modifications Copyright (C) 2021 Bell Canada. All rights reserved. + * 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. @@ -20,6 +20,8 @@ package org.onap.cps.spi.query; +import java.util.HashMap; +import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; import lombok.AccessLevel; @@ -36,6 +38,7 @@ public class CpsPathQuery { private String leafName; private Object leafValue; private String descendantName; + private Map<String, Object> leavesData; private static final String NON_CAPTURING_GROUP_1_TO_99_YANG_CONTAINERS = "((?:\\/[^\\/]+){1,99})"; @@ -51,6 +54,15 @@ public class CpsPathQuery { private static final Pattern LEAF_STRING_VALUE_PATTERN = Pattern.compile("['\"](.*)['\"]"); + private static final String YANG_MULTIPLE_LEAF_VALUE_EQUALS_CONDITION = "\\[(.*?)\\s{0,9}]"; + + private static final Pattern DESCENDANT_ANYWHERE_PATTERN_WITH_MULTIPLE_LEAF_PATTERN = + Pattern.compile(DESCENDANT_ANYWHERE_PATTERN + YANG_MULTIPLE_LEAF_VALUE_EQUALS_CONDITION); + + private static final String INDIVIDUAL_LEAF_DETAIL_PATTERN = ("\\s{0,9}and\\s{0,9}"); + + private static final Pattern LEAF_VALUE_PATTERN = Pattern.compile("@(\\S+?)=(.*)"); + /** * Returns a cps path query. * @@ -61,11 +73,11 @@ public class CpsPathQuery { Matcher matcher = QUERY_CPS_PATH_WITH_SINGLE_LEAF_PATTERN.matcher(cpsPath); final CpsPathQuery cpsPathQuery = new CpsPathQuery(); if (matcher.matches()) { - cpsPathQuery.setCpsPathQueryType(CpsPathQueryType.XPATH_LEAF_VALUE); - cpsPathQuery.setXpathPrefix(matcher.group(1)); - cpsPathQuery.setLeafName(matcher.group(2)); - cpsPathQuery.setLeafValue(convertLeafValueToCorrectType(matcher.group(3), cpsPath)); - return cpsPathQuery; + return buildCpsPathQueryWithSingleLeafPattern(cpsPath, matcher, cpsPathQuery); + } + matcher = DESCENDANT_ANYWHERE_PATTERN_WITH_MULTIPLE_LEAF_PATTERN.matcher(cpsPath); + if (matcher.matches()) { + return buildCpsQueryForDescendentWithLeafPattern(cpsPath, matcher, cpsPathQuery); } matcher = DESCENDANT_ANYWHERE_PATTERN.matcher(cpsPath); if (matcher.matches()) { @@ -77,6 +89,34 @@ public class CpsPathQuery { String.format("Cannot interpret or parse cps path '%s'.", cpsPath)); } + private static CpsPathQuery buildCpsPathQueryWithSingleLeafPattern(final String cpsPath, final Matcher matcher, + final CpsPathQuery cpsPathQuery) { + cpsPathQuery.setCpsPathQueryType(CpsPathQueryType.XPATH_LEAF_VALUE); + cpsPathQuery.setXpathPrefix(matcher.group(1)); + cpsPathQuery.setLeafName(matcher.group(2)); + cpsPathQuery.setLeafValue(convertLeafValueToCorrectType(matcher.group(3), cpsPath)); + return cpsPathQuery; + } + + private static CpsPathQuery buildCpsQueryForDescendentWithLeafPattern(final String cpsPath, final Matcher matcher, + final CpsPathQuery cpsPathQuery) { + cpsPathQuery.setCpsPathQueryType(CpsPathQueryType.XPATH_HAS_DESCENDANT_WITH_LEAF_VALUES); + cpsPathQuery.setDescendantName(matcher.group(1)); + final Map<String, Object> leafData = new HashMap<>(); + for (final String leafValuePair : matcher.group(2).split(INDIVIDUAL_LEAF_DETAIL_PATTERN)) { + final Matcher descendentMatcher = LEAF_VALUE_PATTERN.matcher(leafValuePair); + if (descendentMatcher.matches()) { + leafData.put(descendentMatcher.group(1), + convertLeafValueToCorrectType(descendentMatcher.group(2), cpsPath)); + } else { + throw new CpsPathException("Invalid cps path.", + String.format("Cannot interpret or parse attributes in cps path '%s'.", cpsPath)); + } + } + cpsPathQuery.setLeavesData(leafData); + return cpsPathQuery; + } + private static Object convertLeafValueToCorrectType(final String leafValueString, final String cpsPath) { final Matcher stringValueWithQuotesMatcher = LEAF_STRING_VALUE_PATTERN.matcher(leafValueString); if (stringValueWithQuotesMatcher.matches()) { diff --git a/cps-ri/src/main/java/org/onap/cps/spi/query/CpsPathQueryType.java b/cps-ri/src/main/java/org/onap/cps/spi/query/CpsPathQueryType.java index abdbfb9fd4..3ae54ba22a 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/query/CpsPathQueryType.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/query/CpsPathQueryType.java @@ -1,6 +1,7 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2021 Nordix Foundation + * Modifications Copyright (C) 2020-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. @@ -28,6 +29,10 @@ public enum CpsPathQueryType { */ XPATH_HAS_DESCENDANT_ANYWHERE, /** + * Xpath descendant anywhere type e.g. //nodeName[@leafName=leafValue] . + */ + XPATH_HAS_DESCENDANT_WITH_LEAF_VALUES, + /** * Xpath leaf value cps path query type e.g. /cps-path[@leafName=leafValue] . */ XPATH_LEAF_VALUE 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 eb2e82b6c9..b896fe86d0 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 @@ -1,7 +1,7 @@ /*-
* ============LICENSE_START=======================================================
* Copyright (C) 2020-201 Nordix Foundation. All rights reserved.
- * Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
+ * Modifications Copyright (C) 2020-2021 Bell Canada. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -66,4 +66,11 @@ public interface FragmentRepository extends JpaRepository<FragmentEntity, Long> // Above query will match the anchor id and last descendant name
List<FragmentEntity> getByAnchorAndXpathEndsInDescendantName(@Param("anchor") int anchorId,
@Param("descendantName") String descendantName);
+
+ @Query(value = "SELECT * FROM FRAGMENT WHERE anchor_id = :anchor AND xpath LIKE CONCAT('%/',:descendantName) "
+ + "AND attributes @> :leafDataAsJson\\:\\:jsonb", nativeQuery = true)
+ // Above query will match the anchor id, last descendant name and all parameters passed into leafDataASJson with the
+ // attribute values of the requested data node eg: {"leaf_name":"value", "another_leaf_name":"another value"}
+ List<FragmentEntity> getByAnchorAndDescendentNameAndLeafValues(@Param("anchor") int anchorId,
+ @Param("descendantName") String descendantName, @Param("leafDataAsJson") String leafDataAsJson);
}
diff --git a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceQueryDataNodeSpec.groovy b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceQueryDataNodeSpec.groovy new file mode 100644 index 0000000000..6a0a6f48ba --- /dev/null +++ b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceQueryDataNodeSpec.groovy @@ -0,0 +1,174 @@ +/* + * ============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. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.cps.spi.impl + +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.exceptions.CpsPathException +import org.onap.cps.spi.model.DataNode +import org.onap.cps.spi.model.DataNodeBuilder +import org.springframework.beans.factory.annotation.Autowired +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 + +class CpsDataPersistenceQueryDataNodeSpec extends CpsPersistenceSpecBase { + + @Autowired + CpsDataPersistenceService objectUnderTest + + static final Gson GSON = new GsonBuilder().create() + + static final String SET_DATA = '/data/fragment.sql' + static final String XPATH_DATA_NODE_WITH_DESCENDANTS = '/parent-1' + + 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'] + ] + + static { + existingDataNode = createDataNodeTree(XPATH_DATA_NODE_WITH_DESCENDANTS) + existingChildDataNode = createDataNodeTree('/parent-1/child-1') + } + + static def createDataNodeTree(String... xpaths) { + def dataNodeBuilder = new DataNodeBuilder().withXpath(xpaths[0]) + if (xpaths.length > 1) { + def xPathsDescendant = Arrays.copyOfRange(xpaths, 1, xpaths.length) + def childDataNode = createDataNodeTree(xPathsDescendant) + dataNodeBuilder.withChildDataNodes(ImmutableSet.of(childDataNode)) + } + dataNodeBuilder.build() + } + + def static treeToFlatMapByXpath(Map<String, DataNode> flatMap, DataNode dataNodeTree) { + flatMap.put(dataNodeTree.getXpath(), dataNodeTree) + dataNodeTree.getChildDataNodes() + .forEach(childDataNode -> treeToFlatMapByXpath(flatMap, childDataNode)) + return flatMap + } + + + @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 + } + + @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]' + '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]' + } + + @Unroll + @Sql([CLEAR_DATA, SET_DATA]) + def 'Cps Path query using descendant anywhere and #type (further) descendants.'() { + when: 'a query is executed to get a data node by the given cps path' + def cpsPath = '//child-202' + def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, cpsPath, includeDescendantsOption) + then: 'the data node has the correct number of children' + DataNode dataNode = result.stream().findFirst().get() + dataNode.getChildDataNodes().size() == expectedNumberOfChildNodes + where: 'the following data is used' + type | includeDescendantsOption || expectedNumberOfChildNodes + 'omit' | OMIT_DESCENDANTS || 0 + 'include' | INCLUDE_ALL_DESCENDANTS || 1 + } + + @Unroll + @Sql([CLEAR_DATA, SET_DATA]) + def 'Cps Path query using descendant anywhere with %scenario '() { + 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, OMIT_DESCENDANTS) + then: 'the correct number of data nodes are retrieved' + result.size() == expectedXPaths.size() + and: 'xpaths of the retrieved data nodes are as expected' + for(int i = 0; i<result.size(); i++) { + result[i].getXpath() == expectedXPaths[i] + } + where: 'the following data is used' + scenario | cpsPath || expectedXPaths + 'fully unique descendant name' | '//grand-child-202' || ['/parent-200/child-202/grand-child-202'] + 'descendant name match end of other node' | '//child-202' || ['/parent-200/child-202','/parent-201/child-202'] + } + + @Unroll + @Sql([CLEAR_DATA, SET_DATA]) + def 'Cps Path query using descendant anywhere ends with yang list containing %scenario '() { + 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, OMIT_DESCENDANTS) + then: 'the correct number of data nodes are retrieved' + result.size() == expectedXPaths.size() + and: 'xpaths of the retrieved data nodes are as expected' + for(int i = 0; i<result.size(); i++) { + result[i].getXpath() == expectedXPaths[i] + } + where: 'the following data is used' + scenario | cpsPath || expectedXPaths + 'one attribute' | '//child-202[@common-leaf-name-int=5]' || ['/parent-200/child-202','/parent-201/child-202'] + 'trailing "and" is ignored' | '//child-202[@common-leaf-name-int=5 and]' || ['/parent-200/child-202','/parent-201/child-202'] + 'more than one attribute' | '//child-202[@common-leaf-name-int=5 and @common-leaf-name="common-leaf value"]' || ['/parent-200/child-202'] + 'attributes reversed in order' | '//child-202[@common-leaf-name="common-leaf value" and @common-leaf-name-int=5]' || ['/parent-200/child-202'] + } + + @Unroll + @Sql([CLEAR_DATA, SET_DATA]) + def 'Cps Path query error scenario using descendant anywhere ends with yang list containing %scenario '() { + 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, OMIT_DESCENDANTS) + then: 'exception is thrown' + thrown(CpsPathException) + where: 'the following data is used' + scenario | cpsPath + 'one of the attributes without value' | '//child-202[@common-leaf-name-int=5 and @another-attribute"]' + 'more than one attribute separated by or' | '//child-202[@common-leaf-name-int=5 or @common-leaf-name="common-leaf value"]' + } +} 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 24aa3d41fa..a47bd65d02 100644 --- 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 @@ -24,7 +24,6 @@ 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 @@ -61,10 +60,10 @@ class CpsDataPersistenceServiceSpec extends CpsPersistenceSpecBase { 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 { @@ -326,65 +325,4 @@ class CpsDataPersistenceServiceSpec extends CpsPersistenceSpecBase { static Map<String, Object> getLeavesMap(FragmentEntity fragmentEntity) { 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 - } - - @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]' - '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]' - } - - @Unroll - @Sql([CLEAR_DATA, SET_DATA]) - def 'Cps Path query using descendant anywhere and #type (further) descendants.'() { - when: 'a query is executed to get a data node by the given cps path' - def cpsPath = '//child-202' - def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, cpsPath, includeDescendantsOption) - then: 'the data node has the correct number of children' - DataNode dataNode = result.stream().findFirst().get() - dataNode.getChildDataNodes().size() == expectedNumberOfChildNodes - where: 'the following data is used' - type | includeDescendantsOption || expectedNumberOfChildNodes - 'omit' | OMIT_DESCENDANTS || 0 - 'include' | INCLUDE_ALL_DESCENDANTS || 1 - } - - @Unroll - @Sql([CLEAR_DATA, SET_DATA]) - def 'Cps Path query using descendant anywhere with %scenario '() { - 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, OMIT_DESCENDANTS) - then: 'Only one data node is returned' - result.size() == 1 - and: - result.stream().findFirst().get().xpath == expectedXPath - where: 'the following data is used' - scenario | cpsPath || expectedXPath - 'fully unique descendant name' | '//grand-child-202' || '/parent-200/child-202/grand-child-202' - 'descendant name and parent' | '//child-202/grand-child-202' || '/parent-200/child-202/grand-child-202' - 'descendant name match end of other node' | '//child-202' || '/parent-200/child-202' - } } diff --git a/cps-ri/src/test/groovy/org/onap/cps/spi/query/CpsPathQuerySpec.groovy b/cps-ri/src/test/groovy/org/onap/cps/spi/query/CpsPathQuerySpec.groovy index 7f558dd10b..99302a4013 100644 --- a/cps-ri/src/test/groovy/org/onap/cps/spi/query/CpsPathQuerySpec.groovy +++ b/cps-ri/src/test/groovy/org/onap/cps/spi/query/CpsPathQuerySpec.groovy @@ -1,6 +1,7 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2021 Nordix Foundation + * Modifications Copyright (C) 2020-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. @@ -56,21 +57,37 @@ class CpsPathQuerySpec extends Specification { where: 'the following data is used' scenario | cpsPath || expectedEndsWithValue 'yang container' | '//cps-path' || 'cps-path' - 'yang list' | '//cps-path[@key=value]' || 'cps-path[@key=value]' 'parent & child' | '//parent/child' || 'parent/child' } @Unroll + def 'Parse cps path that ends with a yang list containing #scenario.'() { + when: 'the given cps path is parsed' + def result = objectUnderTest.createFrom(cpsPath) + then: 'the query has the right type' + result.cpsPathQueryType == CpsPathQueryType.XPATH_HAS_DESCENDANT_WITH_LEAF_VALUES + and: 'the right ends with parameters are set' + result.descendantName == "child" + result.leavesData.size() == expectedNumberOfLeaves + where: 'the following data is used' + scenario | cpsPath || expectedNumberOfLeaves + 'one attribute' | '//child[@common-leaf-name-int=5]' || 1 + 'more than one attribute' | '//child[@int-leaf=5 and @leaf-name="leaf value"]' || 2 + } + + @Unroll def 'Parse cps path with #scenario.'() { when: 'the given cps path is parsed' objectUnderTest.createFrom(cpsPath) then: 'a CpsPathException is thrown' thrown(CpsPathException) where: 'the following data is used' - scenario | cpsPath - 'no / at the start' | 'invalid-cps-path/child' - 'additional / after descendant option' | '///cps-path' - 'float value' | '/parent-200/child-202[@common-leaf-name-float=5.0]' - 'too many containers' | '/1/2/3/4/5/6/7/8/9/10/11/12/13/14/15/16/17/18/19/20/21/22/23/24/25/26/27/28/29/30/31/32/33/34/35/36/37/38/39/40/41/42/43/44/45/46/47/48/49/50/51/52/53/54/55/56/57/58/59/60/61/62/63/64/65/66/67/68/69/70/71/72/73/74/75/76/77/78/79/80/81/82/83/84/85/86/87/88/89/90/91/92/93/94/95/96/97/98/99/100[@a=1]' + scenario | cpsPath + 'no / at the start' | 'invalid-cps-path/child' + 'additional / after descendant option' | '///cps-path' + 'float value' | '/parent-200/child-202[@common-leaf-name-float=5.0]' + 'too many containers' | '/1/2/3/4/5/6/7/8/9/10/11/12/13/14/15/16/17/18/19/20/21/22/23/24/25/26/27/28/29/30/31/32/33/34/35/36/37/38/39/40/41/42/43/44/45/46/47/48/49/50/51/52/53/54/55/56/57/58/59/60/61/62/63/64/65/66/67/68/69/70/71/72/73/74/75/76/77/78/79/80/81/82/83/84/85/86/87/88/89/90/91/92/93/94/95/96/97/98/99/100[@a=1]' + 'end with descendant and more than one attribute separated by "or"' | '//child[@int-leaf=5 or @leaf-name="leaf value"]' + 'missing attribute value' | '//child[@int-leaf=5 and @name]' } } diff --git a/cps-ri/src/test/resources/data/fragment.sql b/cps-ri/src/test/resources/data/fragment.sql index 3c1f793b93..b6dc2ca307 100644 --- a/cps-ri/src/test/resources/data/fragment.sql +++ b/cps-ri/src/test/resources/data/fragment.sql @@ -17,14 +17,16 @@ INSERT INTO FRAGMENT (ID, XPATH, ANCHOR_ID, PARENT_ID, DATASPACE_ID) VALUES (4006, '/parent-1/child-1/grandchild-1', 3001, 4004, 1001); INSERT INTO FRAGMENT (ID, DATASPACE_ID, ANCHOR_ID, PARENT_ID, XPATH, ATTRIBUTES) VALUES - (4101, 1001, 3003, null, '/parent-100', '{"parent-leaf": "parent-leaf-value"}'), - (4102, 1001, 3003, 4101, '/parent-100/child-001', '{"first-child-leaf": "first-child-leaf-value"}'), - (4103, 1001, 3003, 4101, '/parent-100/child-002', '{"second-child-leaf": "second-child-leaf-value"}'), - (4104, 1001, 3003, 4103, '/parent-100/child-002/grand-child', '{"grand-child-leaf": "grand-child-leaf-value"}'); + (4101, 1001, 3003, null, '/parent-100', '{"parent-leaf": "parent-leaf value"}'), + (4102, 1001, 3003, 4101, '/parent-100/child-001', '{"first-child-leaf": "first-child-leaf value"}'), + (4103, 1001, 3003, 4101, '/parent-100/child-002', '{"second-child-leaf": "second-child-leaf value"}'), + (4104, 1001, 3003, 4103, '/parent-100/child-002/grand-child', '{"grand-child-leaf": "grand-child-leaf value"}'); INSERT INTO FRAGMENT (ID, DATASPACE_ID, ANCHOR_ID, PARENT_ID, XPATH, ATTRIBUTES) VALUES (4201, 1001, 3003, null, '/parent-200', '{"leaf-value": "original"}'), (4202, 1001, 3003, 4201, '/parent-200/child-201', '{"leaf-value": "original"}'), (4203, 1001, 3003, 4202, '/parent-200/child-201/grand-child', '{"leaf-value": "original"}'), - (4204, 1001, 3003, 4201, '/parent-200/child-202', '{"common-leaf-name": "common-leaf-value", "common-leaf-name-int" : 5}'), - (4205, 1001, 3003, 4204, '/parent-200/child-202/grand-child-202', '{"common-leaf-name": "common-leaf-value", "common-leaf-name-int" : 5}');
\ No newline at end of file + (4204, 1001, 3003, 4201, '/parent-200/child-202', '{"common-leaf-name": "common-leaf value", "common-leaf-name-int" : 5}'), + (4205, 1001, 3003, 4204, '/parent-200/child-202/grand-child-202', '{"common-leaf-name": "common-leaf value", "common-leaf-name-int" : 5}'), + (4206, 1001, 3003, null, '/parent-201', '{"leaf-value": "original"}'), + (4207, 1001, 3003, 4201, '/parent-201/child-202', '{"common-leaf-name": "common-leaf other value", "common-leaf-name-int" : 5}');
\ No newline at end of file |