Fold yang-model-util-ut
[yangtools.git] / yang / yang-model-util / src / test / java / org / opendaylight / yangtools / yang / model / util / LeafrefStaticAnalysisTest.java
1 /*
2  * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others.  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.model.util;
9
10 import static org.hamcrest.MatcherAssert.assertThat;
11 import static org.hamcrest.Matchers.isA;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNull;
14
15 import org.junit.BeforeClass;
16 import org.junit.Test;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
20 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
21 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.Module;
24 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
26 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
27 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
28
29 public class LeafrefStaticAnalysisTest {
30     private static final QName FOO = QName.create("leafrefs", "foo");
31
32     private static EffectiveModelContext context;
33     private static GroupingDefinition grp;
34     private static ListSchemaNode foo;
35     private static ContainerSchemaNode bar;
36     private static Module module;
37
38     @BeforeClass
39     public static void beforeClass() {
40         context = YangParserTestUtils.parseYangResource("/leafrefs.yang");
41         module = context.getModules().iterator().next();
42
43         foo = (ListSchemaNode) module.findDataChildByName(FOO).get();
44         bar = (ContainerSchemaNode) foo.findDataChildByName(QName.create(FOO, "bar")).get();
45         grp = module.getGroupings().iterator().next();
46     }
47
48     @Test
49     public void testGrpOuterId() {
50         final LeafSchemaNode leaf = (LeafSchemaNode) grp.findDataChildByName(QName.create(FOO, "outer-id")).get();
51         // Cannot be found as the reference goes outside of the grouping
52         assertNull(SchemaContextUtil.findDataSchemaNodeForRelativeXPath(context, module, leaf,
53             ((LeafrefTypeDefinition) leaf.getType()).getPathStatement()));
54     }
55
56     @Test
57     public void testFooOuterId() {
58         final LeafSchemaNode leaf = (LeafSchemaNode) bar.findDataChildByName(QName.create(FOO, "outer-id")).get();
59         final SchemaNode found = SchemaContextUtil.findDataSchemaNodeForRelativeXPath(context, module, leaf,
60             ((LeafrefTypeDefinition) leaf.getType()).getPathStatement());
61
62         assertThat(found, isA(LeafSchemaNode.class));
63         assertEquals(SchemaPath.create(true, FOO, QName.create(FOO, "id")), found.getPath());
64     }
65
66     @Test
67     public void testGrpOuterIndirectProp() {
68         final LeafSchemaNode leaf = (LeafSchemaNode) grp.findDataChildByName(
69             QName.create(FOO, "outer-indirect-prop")).get();
70         // Cannot resolve deref outer-id
71         assertNull(SchemaContextUtil.findDataSchemaNodeForRelativeXPath(context, module, leaf,
72             ((LeafrefTypeDefinition) leaf.getType()).getPathStatement()));
73     }
74
75     @Test
76     public void testFooOuterIndirectProp() {
77         final LeafSchemaNode leaf = (LeafSchemaNode) bar.findDataChildByName(
78             QName.create(FOO, "outer-indirect-prop")).get();
79         final SchemaNode found = SchemaContextUtil.findDataSchemaNodeForRelativeXPath(context, module, leaf,
80             ((LeafrefTypeDefinition) leaf.getType()).getPathStatement());
81
82         assertThat(found, isA(LeafSchemaNode.class));
83         assertEquals(QName.create(FOO, "prop"), found.getQName());
84     }
85
86     @Test
87     public void testGrpIndirect() {
88         final LeafSchemaNode leaf = (LeafSchemaNode) grp.findDataChildByName(QName.create(FOO, "indirect")).get();
89         final SchemaNode found = SchemaContextUtil.findDataSchemaNodeForRelativeXPath(context, module, leaf,
90             ((LeafrefTypeDefinition) leaf.getType()).getPathStatement());
91
92         assertThat(found, isA(LeafSchemaNode.class));
93         assertEquals(QName.create(FOO, "prop"), found.getQName());
94     }
95
96     @Test
97     public void testFooIndirect() {
98         final LeafSchemaNode leaf = (LeafSchemaNode) bar.findDataChildByName(QName.create(FOO, "indirect")).get();
99         final SchemaNode found = SchemaContextUtil.findDataSchemaNodeForRelativeXPath(context, module, leaf,
100             ((LeafrefTypeDefinition) leaf.getType()).getPathStatement());
101
102         assertThat(found, isA(LeafSchemaNode.class));
103         assertEquals(QName.create(FOO, "prop"), found.getQName());
104     }
105
106     @Test
107     public void testGrpDerefNonExistent() {
108         final LeafSchemaNode leaf = (LeafSchemaNode) grp.findDataChildByName(
109             QName.create(FOO, "deref-non-existent")).get();
110         assertNull(SchemaContextUtil.findDataSchemaNodeForRelativeXPath(context, module, leaf,
111             ((LeafrefTypeDefinition) leaf.getType()).getPathStatement()));
112     }
113
114     @Test
115     public void testFooDerefNonExistent() {
116         final LeafSchemaNode leaf = (LeafSchemaNode) bar.findDataChildByName(
117             QName.create(FOO, "deref-non-existent")).get();
118         assertNull(SchemaContextUtil.findDataSchemaNodeForRelativeXPath(context, module, leaf,
119             ((LeafrefTypeDefinition) leaf.getType()).getPathStatement()));
120     }
121
122     @Test
123     public void testGrpNonExistentDeref() {
124         final LeafSchemaNode leaf = (LeafSchemaNode) grp.findDataChildByName(
125             QName.create(FOO, "non-existent-deref")).get();
126         assertNull(SchemaContextUtil.findDataSchemaNodeForRelativeXPath(context, module, leaf,
127             ((LeafrefTypeDefinition) leaf.getType()).getPathStatement()));
128     }
129
130     @Test
131     public void testFooNonExistentDeref() {
132         final LeafSchemaNode leaf = (LeafSchemaNode) bar.findDataChildByName(
133             QName.create(FOO, "non-existent-deref")).get();
134         assertNull(SchemaContextUtil.findDataSchemaNodeForRelativeXPath(context, module, leaf,
135             ((LeafrefTypeDefinition) leaf.getType()).getPathStatement()));
136     }
137
138     @Test
139     public void testNonExistentRelativeXpath() {
140         final LeafSchemaNode leaf = (LeafSchemaNode) bar.findDataChildByName(
141                 QName.create(FOO, "indirect-with-current")).get();
142         assertNull(SchemaContextUtil.findDataSchemaNodeForRelativeXPath(context, module, leaf,
143                 ((LeafrefTypeDefinition) leaf.getType()).getPathStatement()));
144     }
145 }