Fixup yang-parser-api module a bit
[yangtools.git] / parser / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / path / PathExpressionParserTest.java
1 /*
2  * Copyright (c) 2019 PANTHEON.tech, s.r.o.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.path;
9
10 import static org.hamcrest.MatcherAssert.assertThat;
11 import static org.hamcrest.Matchers.containsString;
12 import static org.hamcrest.Matchers.isA;
13 import static org.hamcrest.Matchers.startsWith;
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertSame;
16 import static org.junit.Assert.assertTrue;
17 import static org.junit.Assert.fail;
18 import static org.mockito.Mockito.doReturn;
19
20 import com.google.common.collect.ImmutableList;
21 import com.google.common.collect.ImmutableSet;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.mockito.Mock;
26 import org.mockito.junit.MockitoJUnitRunner;
27 import org.opendaylight.yangtools.yang.common.UnresolvedQName;
28 import org.opendaylight.yangtools.yang.model.api.PathExpression;
29 import org.opendaylight.yangtools.yang.model.api.PathExpression.DerefSteps;
30 import org.opendaylight.yangtools.yang.model.api.PathExpression.LocationPathSteps;
31 import org.opendaylight.yangtools.yang.model.api.PathExpression.Steps;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
33 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
34 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
35 import org.opendaylight.yangtools.yang.xpath.api.YangBinaryOperator;
36 import org.opendaylight.yangtools.yang.xpath.api.YangFunction;
37 import org.opendaylight.yangtools.yang.xpath.api.YangFunctionCallExpr;
38 import org.opendaylight.yangtools.yang.xpath.api.YangLocationPath;
39 import org.opendaylight.yangtools.yang.xpath.api.YangLocationPath.Relative;
40 import org.opendaylight.yangtools.yang.xpath.api.YangPathExpr;
41 import org.opendaylight.yangtools.yang.xpath.api.YangQNameExpr;
42 import org.opendaylight.yangtools.yang.xpath.api.YangXPathAxis;
43
44 @RunWith(MockitoJUnitRunner.StrictStubs.class)
45 public class PathExpressionParserTest {
46     @Mock
47     public StmtContext<?, ?, ?> ctx;
48     @Mock
49     public StatementSourceReference ref;
50
51     @SuppressWarnings("exports")
52     public final PathExpressionParser parser = new PathExpressionParser();
53
54     @Before
55     public void before() {
56         doReturn(ref).when(ctx).sourceReference();
57     }
58
59     @Test
60     public void testDerefPath() {
61         // deref() is not valid as per RFC7950, but we tolarate it.
62         final PathExpression deref = parser.parseExpression(ctx, "deref(../id)/../type");
63
64         final Steps steps = deref.getSteps();
65         assertThat(steps, isA(DerefSteps.class));
66
67         final DerefSteps derefSteps = (DerefSteps) steps;
68         assertEquals(YangLocationPath.relative(YangXPathAxis.PARENT.asStep(),
69             YangXPathAxis.CHILD.asStep(UnresolvedQName.unqualified("type"))), derefSteps.getRelativePath());
70         assertEquals(YangLocationPath.relative(YangXPathAxis.PARENT.asStep(),
71             YangXPathAxis.CHILD.asStep(UnresolvedQName.unqualified("id"))), derefSteps.getDerefArgument());
72     }
73
74     @Test
75     public void testInvalidLeftParent() {
76         try {
77             parser.parseExpression(ctx, "foo(");
78             fail("SourceException should have been thrown");
79         } catch (SourceException e) {
80             assertSame(ref, e.getSourceReference());
81             assertThat(e.getMessage(), startsWith("extraneous input '(' expecting "));
82             assertThat(e.getMessage(), containsString(" at 1:3 [at "));
83         }
84     }
85
86     @Test
87     public void testInvalidRightParent() {
88         try {
89             parser.parseExpression(ctx, "foo)");
90             fail("SourceException should have been thrown");
91         } catch (SourceException e) {
92             assertSame(ref, e.getSourceReference());
93             assertThat(e.getMessage(), startsWith("extraneous input ')' expecting "));
94             assertThat(e.getMessage(), containsString(" at 1:3 [at "));
95         }
96     }
97
98     @Test
99     public void testInvalidIdentifier() {
100         try {
101             parser.parseExpression(ctx, "foo%");
102             fail("SourceException should have been thrown");
103         } catch (SourceException e) {
104             assertSame(ref, e.getSourceReference());
105             assertThat(e.getMessage(), startsWith("token recognition error at: '%' at 1:3 [at "));
106         }
107     }
108
109     @Test
110     public void testCurrentPredicateParsing() {
111         final YangLocationPath path = ((LocationPathSteps) parser.parseExpression(ctx,
112             "/device_types/device_type[type = current()/../type_text]/desc").getSteps()).getLocationPath();
113         assertTrue(path.isAbsolute());
114
115         path.getSteps();
116         assertEquals(ImmutableList.of(
117             YangXPathAxis.CHILD.asStep(UnresolvedQName.unqualified("device_types")),
118             YangXPathAxis.CHILD.asStep(UnresolvedQName.unqualified("device_type"),
119                 ImmutableSet.of(YangBinaryOperator.EQUALS.exprWith(
120                     YangQNameExpr.of(UnresolvedQName.unqualified("type")),
121                     YangPathExpr.of(YangFunctionCallExpr.of(YangFunction.CURRENT.getIdentifier()), Relative.relative(
122                         YangXPathAxis.PARENT.asStep(),
123                         YangXPathAxis.CHILD.asStep(UnresolvedQName.unqualified("type_text"))))))),
124             YangXPathAxis.CHILD.asStep(UnresolvedQName.unqualified("desc"))), path.getSteps());
125     }
126 }