2 * Copyright (c) 2017 Cisco Systems, Inc. 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.data.tree.leafref;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
15 import com.google.common.collect.ImmutableList;
17 import org.junit.Test;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.common.QNameModule;
20 import org.opendaylight.yangtools.yang.common.XMLNamespace;
21 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
22 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
24 public class Bug7844Test {
25 private static final String FOO_NS = "foo";
26 private static final String BAR_NS = "bar";
27 private static final String BAZ_NS = "baz";
31 final EffectiveModelContext context = YangParserTestUtils.parseYang("""
36 import baz { prefix baz-imp; }
39 type baz-imp:my-leafref;
40 description "bar-leafref";
43 typedef bar-base-leafref {
45 path "/baz-imp:root/baz-imp:target";
49 leaf my-leafref-in-bar {
50 type bar-base-leafref;
53 leaf my-leafref-in-bar-2 {
54 type bar-base-leafref;
55 description "bar-base-leafref-2";
68 path "/baz-mod:root/baz-mod:target";
70 description "baz-leafref";
83 import bar { prefix bar-imp; }
90 type bar-imp:bar-leafref;
91 description "foo-leafref";
98 typedef foo-leafref-2 {
99 type bar-imp:bar-base-leafref;
100 description "foo-leaf-ref-2";
103 leaf bar-base-leafref {
104 type bar-imp:bar-base-leafref;
107 leaf bar-base-leafref-2 {
108 type bar-imp:bar-base-leafref;
109 description "bar-base-leafref-2";
112 leaf direct-leafref {
114 path "/bar-imp:bar-target";
118 assertNotNull(context);
120 final LeafRefContext leafRefContext = LeafRefContext.create(context);
121 assertNotNull(leafRefContext);
123 final Map<QName, LeafRefContext> referencingChilds = leafRefContext.getReferencingChilds();
124 assertEquals(7, referencingChilds.size());
126 final QNameModule bazQNameModule = QNameModule.create(XMLNamespace.of(BAZ_NS));
127 final LeafRefPath expectedPathToBazTarget = LeafRefPath.create(true,
128 new QNameWithPredicateImpl(bazQNameModule, "root", ImmutableList.of()),
129 new QNameWithPredicateImpl(bazQNameModule, "target", ImmutableList.of()));
130 final LeafRefContext myLeafCtx = referencingChilds.get(foo("my-leaf"));
131 assertLeafRef(myLeafCtx, expectedPathToBazTarget);
132 assertLeafRef(referencingChilds.get(foo("my-leaf-2")), expectedPathToBazTarget);
133 assertLeafRef(referencingChilds.get(foo("bar-base-leafref")), expectedPathToBazTarget);
134 assertLeafRef(referencingChilds.get(foo("bar-base-leafref-2")), expectedPathToBazTarget);
135 assertLeafRef(referencingChilds.get(bar("my-leafref-in-bar")), expectedPathToBazTarget);
136 assertLeafRef(referencingChilds.get(bar("my-leafref-in-bar-2")), expectedPathToBazTarget);
138 final QNameModule barQNameModule = QNameModule.create(XMLNamespace.of(BAR_NS));
139 final LeafRefPath expectedPathToBarTarget = LeafRefPath.create(true,
140 new QNameWithPredicateImpl(barQNameModule, "bar-target", ImmutableList.of()));
141 assertLeafRef(referencingChilds.get(foo("direct-leafref")), expectedPathToBarTarget);
143 final Map<QName, LeafRefContext> referencedByChilds = leafRefContext.getReferencedByChilds();
144 assertEquals(2, referencedByChilds.size());
146 final LeafRefContext rootCtx = referencedByChilds.get(baz("root"));
147 assertEquals(1, rootCtx.getReferencedByChilds().size());
148 assertTrue(rootCtx.getReferencingChilds().isEmpty());
149 assertFalse(rootCtx.isReferencing());
150 assertFalse(rootCtx.isReferenced());
152 final LeafRefContext targetCtx = rootCtx.getReferencedChildByName(baz("target"));
153 assertTrue(targetCtx.getReferencedByChilds().isEmpty());
154 assertTrue(targetCtx.getReferencingChilds().isEmpty());
155 assertTrue(targetCtx.isReferenced());
156 assertFalse(targetCtx.isReferencing());
158 final Map<QName, LeafRefContext> allReferencedByLeafRefCtxs = targetCtx.getAllReferencedByLeafRefCtxs();
159 assertEquals(6, allReferencedByLeafRefCtxs.size());
160 assertTrue(myLeafCtx == targetCtx.getReferencedByLeafRefCtxByName(foo("my-leaf")));
163 private static void assertLeafRef(final LeafRefContext leafRefToTest, final LeafRefPath expectedLeafRefPath) {
164 assertNotNull(leafRefToTest);
165 assertNotNull(expectedLeafRefPath);
166 assertTrue(leafRefToTest.getReferencedByChilds().isEmpty());
167 assertTrue(leafRefToTest.getReferencingChilds().isEmpty());
168 assertFalse(leafRefToTest.isReferenced());
169 assertTrue(leafRefToTest.isReferencing());
170 assertEquals(expectedLeafRefPath, leafRefToTest.getAbsoluteLeafRefTargetPath());
173 private static QName foo(final String localName) {
174 return QName.create(FOO_NS, localName);
177 private static QName bar(final String localName) {
178 return QName.create(BAR_NS, localName);
181 private static QName baz(final String localName) {
182 return QName.create(BAZ_NS, localName);