2 * Copyright (c) 2019 PANTHEON.tech, s.r.o. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.path;
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;
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;
34 @RunWith(MockitoJUnitRunner.StrictStubs.class)
35 public class PathExpressionParserTest {
37 private StmtContext<?, ?, ?> ctx;
39 private StatementSourceReference ref;
40 private final PathExpressionParser parser = new PathExpressionParser();
43 public void before() {
44 doReturn(ref).when(ctx).getStatementSourceReference();
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");
52 final Steps steps = deref.getSteps();
53 assertThat(steps, isA(DerefSteps.class));
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());
63 public void testInvalidLeftParent() {
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 "));
75 public void testInvalidRightParent() {
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 "));
87 public void testInvalidIdentifier() {
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 "));