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.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;
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.UnqualifiedQName;
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;
44 @RunWith(MockitoJUnitRunner.StrictStubs.class)
45 public class PathExpressionParserTest {
47 public StmtContext<?, ?, ?> ctx;
49 public StatementSourceReference ref;
51 @SuppressWarnings("exports")
52 public final PathExpressionParser parser = new PathExpressionParser();
55 public void before() {
56 doReturn(ref).when(ctx).sourceReference();
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");
64 final Steps steps = deref.getSteps();
65 assertThat(steps, isA(DerefSteps.class));
67 final DerefSteps derefSteps = (DerefSteps) steps;
68 assertEquals(YangLocationPath.relative(YangXPathAxis.PARENT.asStep(),
69 YangXPathAxis.CHILD.asStep(UnqualifiedQName.of("type"))), derefSteps.getRelativePath());
70 assertEquals(YangLocationPath.relative(YangXPathAxis.PARENT.asStep(),
71 YangXPathAxis.CHILD.asStep(UnqualifiedQName.of("id"))), derefSteps.getDerefArgument());
75 public void testInvalidLeftParent() {
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 testInvalidRightParent() {
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 "));
99 public void testInvalidIdentifier() {
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 "));
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());
116 assertEquals(ImmutableList.of(
117 YangXPathAxis.CHILD.asStep(UnqualifiedQName.of("device_types")),
118 YangXPathAxis.CHILD.asStep(UnqualifiedQName.of("device_type"),
119 ImmutableSet.of(YangBinaryOperator.EQUALS.exprWith(
120 YangQNameExpr.of(UnqualifiedQName.of("type")),
121 YangPathExpr.of(YangFunctionCallExpr.of(YangFunction.CURRENT.getIdentifier()), Relative.relative(
122 YangXPathAxis.PARENT.asStep(),
123 YangXPathAxis.CHILD.asStep(UnqualifiedQName.of("type_text"))))))),
124 YangXPathAxis.CHILD.asStep(UnqualifiedQName.of("desc"))), path.getSteps());