Add yang-xpath-impl
[yangtools.git] / yang / yang-xpath-impl / src / test / java / org / opendaylight / yangtools / yang / xpath / impl / XPathParserTest.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, 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.xpath.impl;
9
10 import static org.junit.Assert.assertEquals;
11
12 import com.google.common.collect.ImmutableMap;
13 import java.net.URI;
14 import java.util.Map;
15 import javax.xml.xpath.XPathExpressionException;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.opendaylight.yangtools.yang.common.QNameModule;
20 import org.opendaylight.yangtools.yang.xpath.api.YangBooleanConstantExpr;
21 import org.opendaylight.yangtools.yang.xpath.api.YangExpr;
22
23 @SuppressWarnings("null")
24 public class XPathParserTest {
25     private static final QNameModule DEF_NS = QNameModule.create(URI.create("defaultns"));
26     private static final Map<String, QNameModule> NAMESPACES = ImmutableMap.of(
27         "foo", QNameModule.create(URI.create("foo")),
28         "bar", QNameModule.create(URI.create("bar")));
29
30     private @Nullable XPathParser<?> parser;
31
32     @Before
33     public void before() {
34         parser = new BigDecimalXPathParser(DEF_NS, NAMESPACES::get);
35     }
36
37     @Test
38     public void testSmoke() throws XPathExpressionException {
39         parseExpr("3 + 5");
40         parseExpr("/a/b");
41         parseExpr("a/b");
42         parseExpr("./a/b");
43         parseExpr("../a/b");
44         parseExpr("foo:foo");
45         parseExpr("@foo");
46         parseExpr("@foo:foo");
47         parseExpr("current()");
48         parseExpr("foo:self()");
49         parseExpr("foo:comment()");
50         parseExpr("/a[foo = 2 and bar = 3]");
51         parseExpr("/a[foo = \"2\" and bar = '3']");
52         parseExpr("/foo:a[foo = 2 and bar = 3]");
53         parseExpr("//a[foo = 2 and bar = 3]");
54         parseExpr("//foo:a[foo=2 and bar:bar=3]");
55         parseExpr("a//b[foo = 2]");
56         parseExpr("foo:a//b[foo = 2]");
57         parseExpr("$foo:comment");
58         parseExpr("$comment");
59         parseExpr("$self");
60     }
61
62     @Test
63     public void testUnionSquashing() throws XPathExpressionException {
64         final YangExpr a = parseExpr("a");
65         assertEquals(a, parseExpr("a|a"));
66         assertEquals(a, parseExpr("a|a|a"));
67         assertEquals(a, parseExpr("a|a|a|a"));
68
69         final YangExpr ab = parseExpr("a|b");
70         assertEquals(ab, parseExpr("a|b|a"));
71         assertEquals(ab, parseExpr("a|b|a|b"));
72         assertEquals(ab, parseExpr("a|a|b|a|b"));
73         assertEquals(ab, parseExpr("a|b|b|a|b"));
74         assertEquals(ab, parseExpr("a|b|a|a|b"));
75     }
76
77     @Test
78     public void testNumberSquashing() throws XPathExpressionException {
79         final YangExpr two = parseExpr("2");
80         assertEquals(two, parseExpr("1 + 1"));
81         assertEquals(two, parseExpr("3 - 1"));
82         assertEquals(two, parseExpr("2 * 1"));
83         assertEquals(two, parseExpr("4 div 2"));
84         assertEquals(two, parseExpr("6 mod 4"));
85     }
86
87     @Test
88     public void testSameExprSquashing() throws XPathExpressionException {
89         // Expressions
90         assertEquals(YangBooleanConstantExpr.FALSE, parseExpr("/a != /a"));
91         assertEquals(YangBooleanConstantExpr.TRUE, parseExpr("/a = /a"));
92
93         // Numbers
94         assertEquals(YangBooleanConstantExpr.FALSE, parseExpr("2 != 2"));
95         assertEquals(YangBooleanConstantExpr.FALSE, parseExpr("2 != (1 + 1)"));
96         assertEquals(YangBooleanConstantExpr.TRUE, parseExpr("2 = 2"));
97         assertEquals(YangBooleanConstantExpr.TRUE, parseExpr("2 = (1 + 1)"));
98     }
99
100     private YangExpr parseExpr(final String xpath) throws XPathExpressionException {
101         return parser.parseExpression(xpath).getRootExpr();
102     }
103 }