Xpath to NodeId invalid 53/130653/8
authorGandhapu, Yashwanth <gandhapu.yashwanth@wipro.com>
Fri, 2 Sep 2022 05:31:31 +0000 (11:01 +0530)
committerGandhapu, Yashwanth <gandhapu.yashwanth@wipro.com>
Tue, 6 Sep 2022 06:15:41 +0000 (11:45 +0530)
Issue-ID: CPS-1244
Signed-off-by: Gandhapu, Yashwanth <gandhapu.yashwanth@wipro.com>
Change-Id: Ib091de1247cb0bb48d3157bf2c5becfc9189b773

cps-service/src/main/java/org/onap/cps/utils/YangUtils.java
cps-service/src/test/groovy/org/onap/cps/utils/YangUtilsSpec.groovy

index 8c32010..5e1b486 100644 (file)
@@ -52,7 +52,8 @@ import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 public class YangUtils {
 
     private static final String XPATH_DELIMITER_REGEX = "\\/";
-    private static final String XPATH_NODE_KEY_ATTRIBUTES_REGEX = "\\[.+";
+    private static final String XPATH_NODE_KEY_ATTRIBUTES_REGEX = "\\[.*?\\]";
+    //Might cause an issue with [] inside [] in key-values
 
     /**
      * Parses jsonData into NormalizedNode according to given schema context.
@@ -149,10 +150,11 @@ public class YangUtils {
     }
 
     private static String[] xpathToNodeIdSequence(final String xpath) {
-        final String[] xpathNodeIdSequence = Arrays.stream(xpath.split(XPATH_DELIMITER_REGEX))
-            .map(identifier -> identifier.replaceFirst(XPATH_NODE_KEY_ATTRIBUTES_REGEX, ""))
-            .filter(identifier -> !identifier.isEmpty())
-            .toArray(String[]::new);
+        final String[] xpathNodeIdSequence = Arrays.stream(xpath
+                        .replaceAll(XPATH_NODE_KEY_ATTRIBUTES_REGEX, "")
+                        .split(XPATH_DELIMITER_REGEX))
+                .filter(identifier -> !identifier.isEmpty())
+                .toArray(String[]::new);
         if (xpathNodeIdSequence.length < 1) {
             throw new DataValidationException("Invalid xpath.", "Xpath contains no node identifiers.");
         }
index 25b90d7..3f19091 100644 (file)
@@ -115,4 +115,16 @@ class YangUtilsSpec extends Specification {
             noExceptionThrown()
     }
 
+    def 'Parsing xPath to nodeId for #scenario.'() {
+        when: 'xPath is parsed'
+            def result = YangUtils.xpathToNodeIdSequence(xPath)
+        then: 'result represents an array of expected identifiers'
+            assert result == expectedNodeIdentifier
+        where: 'the following parameters are used'
+            scenario                                       | xPath                                                               || expectedNodeIdentifier
+            'container xpath'                              | '/test-tree'                                                        || ['test-tree']
+            'xpath contains list attribute'                | '/test-tree/branch[@name=\'Branch\']'                               || ['test-tree','branch']
+            'xpath contains list attributes with /'        | '/test-tree/branch[@name=\'/Branch\']/categories[@id=\'/broken\']'  || ['test-tree','branch','categories']
+    }
+
 }