Allow Errata 5617 expressions in leafref
[yangtools.git] / yang / 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.Matchers.containsString;
11 import static org.hamcrest.Matchers.isA;
12 import static org.hamcrest.Matchers.startsWith;
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertSame;
15 import static org.junit.Assert.assertThat;
16 import static org.junit.Assert.fail;
17 import static org.mockito.Mockito.doReturn;
18
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.mockito.Mock;
23 import org.mockito.junit.MockitoJUnitRunner;
24 import org.opendaylight.yangtools.yang.common.UnqualifiedQName;
25 import org.opendaylight.yangtools.yang.model.api.PathExpression;
26 import org.opendaylight.yangtools.yang.model.api.PathExpression.DerefSteps;
27 import org.opendaylight.yangtools.yang.model.api.PathExpression.Steps;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
29 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
30 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
31 import org.opendaylight.yangtools.yang.xpath.api.YangLocationPath;
32 import org.opendaylight.yangtools.yang.xpath.api.YangXPathAxis;
33
34 @RunWith(MockitoJUnitRunner.StrictStubs.class)
35 public class PathExpressionParserTest {
36     @Mock
37     private StmtContext<?, ?, ?> ctx;
38     @Mock
39     private StatementSourceReference ref;
40     private final PathExpressionParser parser = new PathExpressionParser();
41
42     @Before
43     public void before() {
44         doReturn(ref).when(ctx).getStatementSourceReference();
45     }
46
47     @Test
48     public void testDerefPath() {
49         // deref() is not valid as per RFC7950, but we tolarate it.
50         final PathExpression deref = parser.parseExpression(ctx, "deref(../id)/../type");
51
52         final Steps steps = deref.getSteps();
53         assertThat(steps, isA(DerefSteps.class));
54
55         final DerefSteps derefSteps = (DerefSteps) steps;
56         assertEquals(YangLocationPath.relative(YangXPathAxis.PARENT.asStep(),
57             YangXPathAxis.CHILD.asStep(UnqualifiedQName.of("type"))), derefSteps.getRelativePath());
58         assertEquals(YangLocationPath.relative(YangXPathAxis.PARENT.asStep(),
59             YangXPathAxis.CHILD.asStep(UnqualifiedQName.of("id"))), derefSteps.getDerefArgument());
60     }
61
62     @Test
63     public void testInvalidLeftParent() {
64         try {
65             parser.parseExpression(ctx, "foo(");
66             fail("SourceException should have been thrown");
67         } catch (SourceException e) {
68             assertSame(ref, e.getSourceReference());
69             assertThat(e.getMessage(), startsWith("extraneous input '(' expecting "));
70             assertThat(e.getMessage(), containsString(" at 1:3 [at "));
71         }
72     }
73
74     @Test
75     public void testInvalidRightParent() {
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 testInvalidIdentifier() {
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("token recognition error at: '%' at 1:3 [at "));
94         }
95     }
96 }