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 public final PathExpressionParser parser = new PathExpressionParser();
54 public void before() {
55 doReturn(ref).when(ctx).sourceReference();
59 public void testDerefPath() {
60 // deref() is not valid as per RFC7950, but we tolarate it.
61 final PathExpression deref = parser.parseExpression(ctx, "deref(../id)/../type");
63 final Steps steps = deref.getSteps();
64 assertThat(steps, isA(DerefSteps.class));
66 final DerefSteps derefSteps = (DerefSteps) steps;
67 assertEquals(YangLocationPath.relative(YangXPathAxis.PARENT.asStep(),
68 YangXPathAxis.CHILD.asStep(UnqualifiedQName.of("type"))), derefSteps.getRelativePath());
69 assertEquals(YangLocationPath.relative(YangXPathAxis.PARENT.asStep(),
70 YangXPathAxis.CHILD.asStep(UnqualifiedQName.of("id"))), derefSteps.getDerefArgument());
74 public void testInvalidLeftParent() {
76 parser.parseExpression(ctx, "foo(");
77 fail("SourceException should have been thrown");
78 } catch (SourceException e) {
79 assertSame(ref, e.getSourceReference());
80 assertThat(e.getMessage(), startsWith("extraneous input '(' expecting "));
81 assertThat(e.getMessage(), containsString(" at 1:3 [at "));
86 public void testInvalidRightParent() {
88 parser.parseExpression(ctx, "foo)");
89 fail("SourceException should have been thrown");
90 } catch (SourceException e) {
91 assertSame(ref, e.getSourceReference());
92 assertThat(e.getMessage(), startsWith("extraneous input ')' expecting "));
93 assertThat(e.getMessage(), containsString(" at 1:3 [at "));
98 public void testInvalidIdentifier() {
100 parser.parseExpression(ctx, "foo%");
101 fail("SourceException should have been thrown");
102 } catch (SourceException e) {
103 assertSame(ref, e.getSourceReference());
104 assertThat(e.getMessage(), startsWith("token recognition error at: '%' at 1:3 [at "));
109 public void testCurrentPredicateParsing() {
110 final YangLocationPath path = ((LocationPathSteps) parser.parseExpression(ctx,
111 "/device_types/device_type[type = current()/../type_text]/desc").getSteps()).getLocationPath();
112 assertTrue(path.isAbsolute());
115 assertEquals(ImmutableList.of(
116 YangXPathAxis.CHILD.asStep(UnqualifiedQName.of("device_types")),
117 YangXPathAxis.CHILD.asStep(UnqualifiedQName.of("device_type"),
118 ImmutableSet.of(YangBinaryOperator.EQUALS.exprWith(
119 YangQNameExpr.of(UnqualifiedQName.of("type")),
120 YangPathExpr.of(YangFunctionCallExpr.of(YangFunction.CURRENT.getIdentifier()), Relative.relative(
121 YangXPathAxis.PARENT.asStep(),
122 YangXPathAxis.CHILD.asStep(UnqualifiedQName.of("type_text"))))))),
123 YangXPathAxis.CHILD.asStep(UnqualifiedQName.of("desc"))), path.getSteps());