Implement ends with cps path query to support multiple attributes with 'and' condition 04/120604/9
authorputhuparambil.aditya <aditya.puthuparambil@bell.ca>
Fri, 16 Apr 2021 12:47:52 +0000 (13:47 +0100)
committerputhuparambil.aditya <aditya.puthuparambil@bell.ca>
Thu, 22 Apr 2021 15:09:49 +0000 (16:09 +0100)
Issue-ID: CPS-309
Signed-off-by: puthuparambil.aditya <aditya.puthuparambil@bell.ca>
Change-Id: I80bf2650e2cd979b806fc29302fc5cb295f65241
Signed-off-by: puthuparambil.aditya <aditya.puthuparambil@bell.ca>
cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java
cps-ri/src/main/java/org/onap/cps/spi/query/CpsPathQuery.java
cps-ri/src/main/java/org/onap/cps/spi/query/CpsPathQueryType.java
cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java
cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceQueryDataNodeSpec.groovy [new file with mode: 0644]
cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceServiceSpec.groovy
cps-ri/src/test/groovy/org/onap/cps/spi/query/CpsPathQuerySpec.groovy
cps-ri/src/test/resources/data/fragment.sql

index ca279f3..fa13c7d 100644 (file)
@@ -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());
index ce78c06..c5861bd 100644 (file)
@@ -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()) {
index abdbfb9..3ae54ba 100644 (file)
@@ -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.
@@ -27,6 +28,10 @@ public enum CpsPathQueryType {
      * Xpath descendant anywhere type e.g. //nodeName .
      */
     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] .
      */
index eb2e82b..b896fe8 100755 (executable)
@@ -1,7 +1,7 @@
 /*-\r
  * ============LICENSE_START=======================================================\r
  *  Copyright (C) 2020-201 Nordix Foundation. All rights reserved.\r
- *  Modifications Copyright (C) 2020 Bell Canada. All rights reserved.\r
+ *  Modifications Copyright (C) 2020-2021 Bell Canada. All rights reserved.\r
  * ================================================================================\r
  * Licensed under the Apache License, Version 2.0 (the "License");\r
  * you may not use this file except in compliance with the License.\r
@@ -66,4 +66,11 @@ public interface FragmentRepository extends JpaRepository<FragmentEntity, Long>
     // Above query will match the anchor id and last descendant name\r
     List<FragmentEntity> getByAnchorAndXpathEndsInDescendantName(@Param("anchor") int anchorId,\r
                                                                  @Param("descendantName") String descendantName);\r
+\r
+    @Query(value = "SELECT * FROM FRAGMENT WHERE anchor_id = :anchor AND xpath LIKE CONCAT('%/',:descendantName) "\r
+        + "AND attributes @>  :leafDataAsJson\\:\\:jsonb", nativeQuery = true)\r
+    // Above query will match the anchor id, last descendant name and all parameters passed into leafDataASJson with the\r
+    // attribute values of the requested data node eg: {"leaf_name":"value", "another_leaf_name":"another value"}​​​​​​\r
+    List<FragmentEntity> getByAnchorAndDescendentNameAndLeafValues(@Param("anchor") int anchorId,\r
+        @Param("descendantName") String descendantName, @Param("leafDataAsJson") String leafDataAsJson);\r
 }\r
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 (file)
index 0000000..6a0a6f4
--- /dev/null
@@ -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"]'
+    }
+}
index 24aa3d4..a47bd65 100644 (file)
@@ -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'
-    }
 }
index 7f558dd..99302a4 100644 (file)
@@ -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,10 +57,24 @@ 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'
@@ -67,10 +82,12 @@ class CpsPathQuerySpec extends Specification {
         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]'
     }
 }
index 3c1f793..b6dc2ca 100644 (file)
@@ -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