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.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertFalse;
12 import static org.junit.jupiter.api.Assertions.assertNotNull;
13 import static org.junit.jupiter.api.Assertions.assertTrue;
15 import com.google.common.collect.ImmutableList;
16 import org.junit.jupiter.api.Test;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.common.QNameModule;
19 import org.opendaylight.yangtools.yang.common.XMLNamespace;
20 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
23 private static final String FOO_NS = "foo";
24 private static final String BAR_NS = "bar";
25 private static final String BAZ_NS = "baz";
29 final var context = YangParserTestUtils.parseYang("""
34 import baz { prefix baz-imp; }
37 type baz-imp:my-leafref;
38 description "bar-leafref";
41 typedef bar-base-leafref {
43 path "/baz-imp:root/baz-imp:target";
47 leaf my-leafref-in-bar {
48 type bar-base-leafref;
51 leaf my-leafref-in-bar-2 {
52 type bar-base-leafref;
53 description "bar-base-leafref-2";
66 path "/baz-mod:root/baz-mod:target";
68 description "baz-leafref";
81 import bar { prefix bar-imp; }
88 type bar-imp:bar-leafref;
89 description "foo-leafref";
96 typedef foo-leafref-2 {
97 type bar-imp:bar-base-leafref;
98 description "foo-leaf-ref-2";
101 leaf bar-base-leafref {
102 type bar-imp:bar-base-leafref;
105 leaf bar-base-leafref-2 {
106 type bar-imp:bar-base-leafref;
107 description "bar-base-leafref-2";
110 leaf direct-leafref {
112 path "/bar-imp:bar-target";
116 assertNotNull(context);
118 final var leafRefContext = LeafRefContext.create(context);
119 assertNotNull(leafRefContext);
121 final var referencingChilds = leafRefContext.getReferencingChilds();
122 assertEquals(7, referencingChilds.size());
124 final var bazQNameModule = QNameModule.create(XMLNamespace.of(BAZ_NS));
125 final var expectedPathToBazTarget = LeafRefPath.create(true,
126 new QNameWithPredicateImpl(bazQNameModule, "root", ImmutableList.of()),
127 new QNameWithPredicateImpl(bazQNameModule, "target", ImmutableList.of()));
128 final var myLeafCtx = referencingChilds.get(foo("my-leaf"));
129 assertLeafRef(myLeafCtx, expectedPathToBazTarget);
130 assertLeafRef(referencingChilds.get(foo("my-leaf-2")), expectedPathToBazTarget);
131 assertLeafRef(referencingChilds.get(foo("bar-base-leafref")), expectedPathToBazTarget);
132 assertLeafRef(referencingChilds.get(foo("bar-base-leafref-2")), expectedPathToBazTarget);
133 assertLeafRef(referencingChilds.get(bar("my-leafref-in-bar")), expectedPathToBazTarget);
134 assertLeafRef(referencingChilds.get(bar("my-leafref-in-bar-2")), expectedPathToBazTarget);
136 final var barQNameModule = QNameModule.create(XMLNamespace.of(BAR_NS));
137 final var expectedPathToBarTarget = LeafRefPath.create(true,
138 new QNameWithPredicateImpl(barQNameModule, "bar-target", ImmutableList.of()));
139 assertLeafRef(referencingChilds.get(foo("direct-leafref")), expectedPathToBarTarget);
141 final var referencedByChilds = leafRefContext.getReferencedByChilds();
142 assertEquals(2, referencedByChilds.size());
144 final var rootCtx = referencedByChilds.get(baz("root"));
145 assertEquals(1, rootCtx.getReferencedByChilds().size());
146 assertTrue(rootCtx.getReferencingChilds().isEmpty());
147 assertFalse(rootCtx.isReferencing());
148 assertFalse(rootCtx.isReferenced());
150 final var targetCtx = rootCtx.getReferencedChildByName(baz("target"));
151 assertTrue(targetCtx.getReferencedByChilds().isEmpty());
152 assertTrue(targetCtx.getReferencingChilds().isEmpty());
153 assertTrue(targetCtx.isReferenced());
154 assertFalse(targetCtx.isReferencing());
156 final var allReferencedByLeafRefCtxs = targetCtx.getAllReferencedByLeafRefCtxs();
157 assertEquals(6, allReferencedByLeafRefCtxs.size());
158 assertTrue(myLeafCtx == targetCtx.getReferencedByLeafRefCtxByName(foo("my-leaf")));
161 private static void assertLeafRef(final LeafRefContext leafRefToTest, final LeafRefPath expectedLeafRefPath) {
162 assertNotNull(leafRefToTest);
163 assertNotNull(expectedLeafRefPath);
164 assertTrue(leafRefToTest.getReferencedByChilds().isEmpty());
165 assertTrue(leafRefToTest.getReferencingChilds().isEmpty());
166 assertFalse(leafRefToTest.isReferenced());
167 assertTrue(leafRefToTest.isReferencing());
168 assertEquals(expectedLeafRefPath, leafRefToTest.getAbsoluteLeafRefTargetPath());
171 private static QName foo(final String localName) {
172 return QName.create(FOO_NS, localName);
175 private static QName bar(final String localName) {
176 return QName.create(BAR_NS, localName);
179 private static QName baz(final String localName) {
180 return QName.create(BAZ_NS, localName);