c1b3473d415fe21e8c802dc0d38c24e0aaa14546
[yangtools.git] / data / yang-data-tree-ri / src / test / java / org / opendaylight / yangtools / yang / data / tree / leafref / Bug7844Test.java
1 /*
2  * Copyright (c) 2017 Cisco Systems, Inc. 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.data.tree.leafref;
9
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;
14
15 import com.google.common.collect.ImmutableList;
16 import java.util.Map;
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;
23
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";
28
29     @Test
30     public void test() {
31         final EffectiveModelContext context = YangParserTestUtils.parseYang("""
32             module bar {
33               namespace bar;
34               prefix bar-mod;
35
36               import baz { prefix baz-imp; }
37
38               typedef bar-leafref {
39                 type baz-imp:my-leafref;
40                 description "bar-leafref";
41               }
42
43               typedef bar-base-leafref {
44                 type leafref {
45                   path "/baz-imp:root/baz-imp:target";
46                 }
47               }
48
49               leaf my-leafref-in-bar {
50                 type bar-base-leafref;
51               }
52
53               leaf my-leafref-in-bar-2 {
54                 type bar-base-leafref;
55                 description "bar-base-leafref-2";
56               }
57
58               leaf bar-target {
59                 type string;
60               }
61             }""", """
62             module baz {
63               namespace baz;
64               prefix baz-mod;
65
66               typedef my-leafref {
67                 type leafref {
68                   path "/baz-mod:root/baz-mod:target";
69                 }
70                 description "baz-leafref";
71               }
72
73               container root {
74                 leaf target {
75                   type string;
76                 }
77               }
78             }""", """
79             module foo {
80               namespace foo;
81               prefix foo-mod;
82
83               import bar { prefix bar-imp; }
84
85               leaf my-leaf {
86                 type foo-leafref;
87               }
88
89               typedef foo-leafref {
90                 type bar-imp:bar-leafref;
91                 description "foo-leafref";
92               }
93
94               leaf my-leaf-2 {
95                 type foo-leafref-2;
96               }
97
98               typedef foo-leafref-2 {
99                 type bar-imp:bar-base-leafref;
100                 description "foo-leaf-ref-2";
101               }
102
103               leaf bar-base-leafref {
104                 type bar-imp:bar-base-leafref;
105               }
106
107               leaf bar-base-leafref-2 {
108                 type bar-imp:bar-base-leafref;
109                 description "bar-base-leafref-2";
110               }
111
112               leaf direct-leafref {
113                 type leafref {
114                   path "/bar-imp:bar-target";
115                 }
116               }
117             }""");
118         assertNotNull(context);
119
120         final LeafRefContext leafRefContext = LeafRefContext.create(context);
121         assertNotNull(leafRefContext);
122
123         final Map<QName, LeafRefContext> referencingChilds = leafRefContext.getReferencingChilds();
124         assertEquals(7, referencingChilds.size());
125
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);
137
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);
142
143         final Map<QName, LeafRefContext> referencedByChilds = leafRefContext.getReferencedByChilds();
144         assertEquals(2, referencedByChilds.size());
145
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());
151
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());
157
158         final Map<QName, LeafRefContext> allReferencedByLeafRefCtxs = targetCtx.getAllReferencedByLeafRefCtxs();
159         assertEquals(6, allReferencedByLeafRefCtxs.size());
160         assertTrue(myLeafCtx == targetCtx.getReferencedByLeafRefCtxByName(foo("my-leaf")));
161     }
162
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());
171     }
172
173     private static QName foo(final String localName) {
174         return QName.create(FOO_NS, localName);
175     }
176
177     private static QName bar(final String localName) {
178         return QName.create(BAR_NS, localName);
179     }
180
181     private static QName baz(final String localName) {
182         return QName.create(BAZ_NS, localName);
183     }
184 }