Fix PathExpressionParser predicate path handling 73/86573/1
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 25 Dec 2019 20:58:24 +0000 (21:58 +0100)
committerRobert Varga <nite@hq.sk>
Wed, 25 Dec 2019 21:58:38 +0000 (21:58 +0000)
Handling of a predicate was wrong in that the right-handside
involving a current() call did not correctly predicates.

This turns out to be a simple typo in two places, fix it and add
an explicit test.

JIRA: YANGTOOLS-1053
Change-Id: I54bac10cdd508503ad896edd628ffa436173ca6b
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit 90457a20b9d0f6a7652563781d04c19002d29a4e)

yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/path/PathExpressionParser.java
yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/path/PathExpressionParserTest.java

index 83d3d122ee56a04c94db6a3c058f144c805454ae..91d1da54a713033e8ba8431c8457ae53abd6a39b 100644 (file)
@@ -193,7 +193,7 @@ class PathExpressionParser {
         final List<Step> steps = new ArrayList<>();
         int offset = 0;
         while (offset < children - 1) {
-            final ParseTree child = expr.getChild(offset);
+            final ParseTree child = relPath.getChild(offset);
             if (child instanceof Node_identifierContext) {
                 break;
             }
@@ -207,7 +207,7 @@ class PathExpressionParser {
 
         // Process node identifiers
         while (offset < children) {
-            final ParseTree child = expr.getChild(offset);
+            final ParseTree child = relPath.getChild(offset);
             if (child instanceof Node_identifierContext) {
                 steps.add(createChildStep(ctx, (Node_identifierContext) child, ImmutableList.of()));
             }
index b1d4d6471cc3f1eb051b7a2d79202b157c3c0422..527ff2243e7c8838c35fe380d62938d8bf730b71 100644 (file)
@@ -13,9 +13,12 @@ import static org.hamcrest.Matchers.startsWith;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 import static org.mockito.Mockito.doReturn;
 
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -24,11 +27,18 @@ import org.mockito.junit.MockitoJUnitRunner;
 import org.opendaylight.yangtools.yang.common.UnqualifiedQName;
 import org.opendaylight.yangtools.yang.model.api.PathExpression;
 import org.opendaylight.yangtools.yang.model.api.PathExpression.DerefSteps;
+import org.opendaylight.yangtools.yang.model.api.PathExpression.LocationPathSteps;
 import org.opendaylight.yangtools.yang.model.api.PathExpression.Steps;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
+import org.opendaylight.yangtools.yang.xpath.api.YangBinaryOperator;
+import org.opendaylight.yangtools.yang.xpath.api.YangFunction;
+import org.opendaylight.yangtools.yang.xpath.api.YangFunctionCallExpr;
 import org.opendaylight.yangtools.yang.xpath.api.YangLocationPath;
+import org.opendaylight.yangtools.yang.xpath.api.YangLocationPath.Relative;
+import org.opendaylight.yangtools.yang.xpath.api.YangPathExpr;
+import org.opendaylight.yangtools.yang.xpath.api.YangQNameExpr;
 import org.opendaylight.yangtools.yang.xpath.api.YangXPathAxis;
 
 @RunWith(MockitoJUnitRunner.StrictStubs.class)
@@ -93,4 +103,22 @@ public class PathExpressionParserTest {
             assertThat(e.getMessage(), startsWith("token recognition error at: '%' at 1:3 [at "));
         }
     }
+
+    @Test
+    public void testCurrentPredicateParsing() {
+        final YangLocationPath path = ((LocationPathSteps) parser.parseExpression(ctx,
+            "/device_types/device_type[type = current()/../type_text]/desc").getSteps()).getLocationPath();
+        assertTrue(path.isAbsolute());
+
+        path.getSteps();
+        assertEquals(ImmutableList.of(
+            YangXPathAxis.CHILD.asStep(UnqualifiedQName.of("device_types")),
+            YangXPathAxis.CHILD.asStep(UnqualifiedQName.of("device_type"),
+                ImmutableSet.of(YangBinaryOperator.EQUALS.exprWith(
+                    YangQNameExpr.of(UnqualifiedQName.of("type")),
+                    YangPathExpr.of(YangFunctionCallExpr.of(YangFunction.CURRENT.getIdentifier()), Relative.relative(
+                        YangXPathAxis.PARENT.asStep(),
+                        YangXPathAxis.CHILD.asStep(UnqualifiedQName.of("type_text"))))))),
+            YangXPathAxis.CHILD.asStep(UnqualifiedQName.of("desc"))), path.getSteps());
+    }
 }