2 * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others. 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.model.util;
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;
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;
29 public class LeafrefStaticAnalysisTest {
30 private static final QName FOO = QName.create("leafrefs", "foo");
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;
39 public static void beforeClass() {
40 context = YangParserTestUtils.parseYangResource("/leafrefs.yang");
41 module = context.getModules().iterator().next();
43 foo = (ListSchemaNode) module.findDataChildByName(FOO).get();
44 bar = (ContainerSchemaNode) foo.findDataChildByName(QName.create(FOO, "bar")).get();
45 grp = module.getGroupings().iterator().next();
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()));
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());
62 assertThat(found, isA(LeafSchemaNode.class));
63 assertEquals(SchemaPath.create(true, FOO, QName.create(FOO, "id")), found.getPath());
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()));
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());
82 assertThat(found, isA(LeafSchemaNode.class));
83 assertEquals(QName.create(FOO, "prop"), found.getQName());
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());
92 assertThat(found, isA(LeafSchemaNode.class));
93 assertEquals(QName.create(FOO, "prop"), found.getQName());
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());
102 assertThat(found, isA(LeafSchemaNode.class));
103 assertEquals(QName.create(FOO, "prop"), found.getQName());
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()));
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()));
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()));
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()));
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()));