CPS-265 - Update cps path query to support 'ends with' 61/118761/5
authorniamhcore <niamh.core@est.tech>
Wed, 3 Mar 2021 12:05:09 +0000 (12:05 +0000)
committerniamhcore <niamh.core@est.tech>
Thu, 4 Mar 2021 16:27:53 +0000 (16:27 +0000)
Issue-ID: CPS-265
Signed-off-by: niamhcore <niamh.core@est.tech>
Change-Id: I604191feaad820983d86e6fd844f543f51096a4e

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 [new file with mode: 0644]
cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java
cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceServiceSpec.groovy
cps-ri/src/test/groovy/org/onap/cps/spi/query/CpsPathQuerySpec.groovy

index 0c61c99..4cfa78b 100644 (file)
@@ -39,6 +39,7 @@ import org.onap.cps.spi.entities.FragmentEntity;
 import org.onap.cps.spi.model.DataNode;
 import org.onap.cps.spi.model.DataNodeBuilder;
 import org.onap.cps.spi.query.CpsPathQuery;
+import org.onap.cps.spi.query.CpsPathQueryType;
 import org.onap.cps.spi.repository.AnchorRepository;
 import org.onap.cps.spi.repository.DataspaceRepository;
 import org.onap.cps.spi.repository.FragmentRepository;
@@ -131,9 +132,15 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService
         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
         final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
         final CpsPathQuery cpsPathQuery = CpsPathQuery.createFrom(cpsPath);
-        final List<FragmentEntity> fragmentEntities = fragmentRepository
-            .getByAnchorAndXpathAndLeafAttributes(anchorEntity.getId(), cpsPathQuery
-                .getXpathPrefix(), cpsPathQuery.getLeafName(), cpsPathQuery.getLeafValue());
+        final List<FragmentEntity> fragmentEntities;
+        if (CpsPathQueryType.XPATH_LEAF_VALUE.equals(cpsPathQuery.getCpsPathQueryType())) {
+            fragmentEntities = fragmentRepository
+                .getByAnchorAndXpathAndLeafAttributes(anchorEntity.getId(), cpsPathQuery.getXpathPrefix(), cpsPathQuery
+                    .getLeafName(), cpsPathQuery.getLeafValue());
+        } else {
+            fragmentEntities = fragmentRepository
+                    .getByAnchorAndEndsWithXpath(anchorEntity.getId(), cpsPathQuery.getEndsWith());
+        }
         return fragmentEntities.stream()
             .map(fragmentEntity -> toDataNode(fragmentEntity, fetchDescendantsOption))
             .collect(Collectors.toUnmodifiableList());
index 4fcf6e4..e85414c 100644 (file)
@@ -31,32 +31,43 @@ import org.onap.cps.spi.exceptions.CpsPathException;
 @Setter(AccessLevel.PRIVATE)
 public class CpsPathQuery {
 
+    private CpsPathQueryType cpsPathQueryType;
     private String xpathPrefix;
     private String leafName;
     private Object leafValue;
+    private String endsWith;
 
     public static final Pattern QUERY_CPS_PATH_WITH_SINGLE_LEAF_PATTERN =
-        Pattern.compile("(.*)\\[\\s*@(.*?)\\s*=\\s*(.*?)\\s*]");
+        Pattern.compile("((?:\\/[^\\/]+)+?)\\[\\s*@(\\S+?)\\s*=\\s*(.*?)\\s*\\]");
 
-    public static final Pattern LEAF_STRING_VALUE_PATTERN = Pattern.compile("['\"](.*)['\"]");
+    public static final Pattern QUERY_CPS_PATH_ENDS_WITH_PATTERN = Pattern.compile("\\/\\/(.+)");
 
     public static final Pattern LEAF_INTEGER_VALUE_PATTERN = Pattern.compile("[-+]?\\d+");
 
+    public static final Pattern LEAF_STRING_VALUE_PATTERN = Pattern.compile("['\"](.*)['\"]");
+
     /**
-     * Returns a xpath prefix, leaf name and leaf value for the given cps path.
+     * Returns a cps path query.
      *
      * @param cpsPath cps path
-     * @return a CpsPath object containing the xpath prefix, leaf name and leaf value.
+     * @return a CpsPath object.
      */
     public static CpsPathQuery createFrom(final String cpsPath) {
-        final Matcher matcher = QUERY_CPS_PATH_WITH_SINGLE_LEAF_PATTERN.matcher(cpsPath);
+        Matcher matcher = QUERY_CPS_PATH_WITH_SINGLE_LEAF_PATTERN.matcher(cpsPath);
+        final CpsPathQuery cpsPathQuery = new CpsPathQuery();
         if (matcher.matches()) {
-            final CpsPathQuery cpsPathQuery = new CpsPathQuery();
+            cpsPathQuery.setCpsPathQueryType(CpsPathQueryType.XPATH_LEAF_VALUE);
             cpsPathQuery.setXpathPrefix(matcher.group(1));
             cpsPathQuery.setLeafName(matcher.group(2));
             cpsPathQuery.setLeafValue(convertLeafValueToCorrectType(matcher.group(3)));
             return cpsPathQuery;
         }
+        matcher = QUERY_CPS_PATH_ENDS_WITH_PATTERN.matcher(cpsPath);
+        if (matcher.matches()) {
+            cpsPathQuery.setCpsPathQueryType(CpsPathQueryType.XPATH_ENDS_WITH);
+            cpsPathQuery.setEndsWith(matcher.group(1));
+            return cpsPathQuery;
+        }
         throw new CpsPathException("Invalid cps path.",
             String.format("Cannot interpret or parse cps path %s.", cpsPath));
     }
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
new file mode 100644 (file)
index 0000000..1a0f8ca
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * ============LICENSE_START=======================================================
+ *  Copyright (C) 2021 Nordix Foundation
+ *  ================================================================================
+ *  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.query;
+
+/**
+ * The enum Cps path query type.
+ */
+public enum CpsPathQueryType {
+    /**
+     * Xpath ends with cps path query type e.g. //cps-path .
+     */
+    XPATH_ENDS_WITH,
+    /**
+     * Xpath leaf value cps path query type e.g. /cps-path[@leafName=leafValue] .
+     */
+    XPATH_LEAF_VALUE
+}
index a40168a..5ff7cfc 100755 (executable)
@@ -55,9 +55,13 @@ public interface FragmentRepository extends JpaRepository<FragmentEntity, Long>
     @Query(value =\r
         "SELECT * FROM FRAGMENT WHERE (anchor_id = :anchor) AND (xpath = (:xpath) OR xpath LIKE "\r
             + "CONCAT(:xpath,'\\[@%]')) AND attributes @> jsonb_build_object(:leafName , :leafValue)",\r
-                nativeQuery = true)\r
-    // Above query will match an xpath with or without the index for a list [@key=value]\r
-    // and match anchor id, leaf name and leaf value\r
+        nativeQuery = true)\r
+    // Above query will match an xpath with or without the index for a list [@key=value] and match anchor id,\r
+    // leaf name and leaf value\r
     List<FragmentEntity> getByAnchorAndXpathAndLeafAttributes(@Param("anchor") int anchorId, @Param("xpath")\r
         String xpathPrefix, @Param("leafName") String leafName, @Param("leafValue") Object leafValue);\r
+\r
+    @Query(value = "SELECT * FROM FRAGMENT WHERE anchor_id = :anchor AND xpath LIKE %:xpath", nativeQuery = true)\r
+    // Above query will match the end of an xpath and anchor id\r
+    List<FragmentEntity> getByAnchorAndEndsWithXpath(@Param("anchor") int anchorId, @Param("xpath") String xpath);\r
 }
\ No newline at end of file
index 8001c65..bb0b471 100644 (file)
@@ -312,14 +312,14 @@ class CpsDataPersistenceServiceSpec extends CpsPersistenceSpecBase {
         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]'
+            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
+            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
@@ -330,10 +330,39 @@ class CpsDataPersistenceServiceSpec extends CpsPersistenceSpecBase {
         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]'
+            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]'
+            'empty cps path of type ends with' | '///'
+    }
+
+    @Unroll
+    @Sql([CLEAR_DATA, SET_DATA])
+    def 'Cps Path query with and without descendants using #type.'() {
+        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
+            'ends with and omit descendants'        | OMIT_DESCENDANTS         | 0
+            'ends with and include all descendants' | INCLUDE_ALL_DESCENDANTS  | 1
+    }
+
+    @Unroll
+    @Sql([CLEAR_DATA, SET_DATA])
+    def 'Cps Path query using ends with variations of #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, OMIT_DESCENDANTS)
+        then: 'the correct number of data nodes is returned'
+            result.size() == expectedNumberOfDataNodes
+        where: 'the following data is used'
+            type                            | cpsPath             | expectedNumberOfDataNodes
+            'single match with / prefix'    | '///child-202'      | 1
+            'single match without / prefix' | '//grand-child-202' | 1
+            'multiple matches'              | '//202'             | 2
     }
 }
index 1e457fb..7087613 100644 (file)
@@ -28,28 +28,45 @@ class CpsPathQuerySpec extends Specification {
     def objectUnderTest = new CpsPathQuery()
 
     @Unroll
-    def 'Parse cps path with valid cps path and a filter for a leaf of type : #type.'()
-    {   when: 'the given cps path is parsed'
-        def result = objectUnderTest.createFrom(cpsPath)
-        then: 'object has the expected attribute'
+    def 'Parse cps path with valid cps path and a filter for a leaf of type : #type.'() {
+        when: 'the given cps path is parsed'
+            def result = objectUnderTest.createFrom(cpsPath)
+        then: 'the query has the right type'
+            result.cpsPathQueryType == CpsPathQueryType.XPATH_LEAF_VALUE
+        and: 'the right query parameters are set'
             result.xpathPrefix == '/parent-200/child-202'
             result.leafName == expectedLeafName
             result.leafValue == expectedLeafValue
         where: 'the following data is used'
-            type      | cpsPath                                                          || expectedLeafName       | expectedLeafValue
-            'String'  | '/parent-200/child-202[@common-leaf-name=\'common-leaf-value\']' || 'common-leaf-name'     | 'common-leaf-value'
-            'Integer' | '/parent-200/child-202[@common-leaf-name-int=5]'                 || 'common-leaf-name-int' | 5
+            type                        | cpsPath                                                          || expectedLeafName       | expectedLeafValue
+            'String'                    | '/parent-200/child-202[@common-leaf-name=\'common-leaf-value\']' || 'common-leaf-name'     | 'common-leaf-value'
+            'Integer'                   | '/parent-200/child-202[@common-leaf-name-int=5]'                 || 'common-leaf-name-int' | 5
+            'Integer value with spaces' | '/parent-200/child-202[@common-leaf-name-int = 5]'               || 'common-leaf-name-int' | 5
     }
 
     @Unroll
-    def 'Parse cps path with : #scenario.'()
-    {   when: 'the given cps path is parsed'
+    def 'Parse cps path of type ends with a #scenario.'() {
+        when: 'the given cps path is parsed'
+            def result = objectUnderTest.createFrom(cpsPath)
+        then: 'the query has the right type'
+            result.cpsPathQueryType == CpsPathQueryType.XPATH_ENDS_WITH
+        and: 'the right ends with parameters are set'
+            result.endsWith == expectedEndsWithValue
+        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]'
+    }
+
+    @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
-            'invalid cps path'          | 'invalid-cps-path'
-            'cps path with float value' | '/parent-200/child-202[@common-leaf-name-float=5.0]'
+            scenario            | cpsPath
+            'no / at the start' | 'invalid-cps-path/child'
+            'float value'       | '/parent-200/child-202[@common-leaf-name-float=5.0]'
     }
 }