2 * Copyright (c) 2016 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.assertInstanceOf;
13 import static org.junit.jupiter.api.Assertions.assertNotNull;
14 import static org.junit.jupiter.api.Assertions.assertNull;
15 import static org.junit.jupiter.api.Assertions.assertThrows;
16 import static org.junit.jupiter.api.Assertions.assertTrue;
18 import org.junit.jupiter.api.AfterAll;
19 import org.junit.jupiter.api.BeforeAll;
20 import org.junit.jupiter.api.Test;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.common.QNameModule;
23 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
24 import org.opendaylight.yangtools.yang.model.api.Module;
25 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
27 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
28 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 class LeafRefContextTreeBuilderTest {
33 private static final Logger LOG = LoggerFactory.getLogger(LeafRefContextTreeBuilderTest.class);
35 private static EffectiveModelContext context;
36 private static Module tstMod;
37 private static QNameModule tst;
38 private static LeafRefContext rootLeafRefContext;
42 context = YangParserTestUtils.parseYangResourceDirectory("/leafref-context-test/correct-modules");
44 for (final var module : context.getModules()) {
45 if (module.getName().equals("leafref-test")) {
50 tst = tstMod.getQNameModule();
52 rootLeafRefContext = LeafRefContext.create(context);
56 static void cleanup() {
60 rootLeafRefContext = null;
64 void buildLeafRefContextTreeTest1() {
66 final var q1 = QName.create(tst, "odl-project");
67 final var q2 = QName.create(tst, "project");
68 final var q3 = QName.create(tst, "project-lead");
70 final var leafRefCtx = rootLeafRefContext.getReferencingChildByName(q1)
71 .getReferencingChildByName(q2).getReferencingChildByName(q3);
73 assertTrue(leafRefCtx.isReferencing());
74 assertNotNull(leafRefCtx.getLeafRefTargetPath());
75 assertFalse(leafRefCtx.getLeafRefTargetPath().isAbsolute());
76 assertNotNull(leafRefCtx.getAbsoluteLeafRefTargetPath());
77 assertTrue(leafRefCtx.getAbsoluteLeafRefTargetPath().isAbsolute());
79 LOG.debug("******* Test 1 ************");
80 LOG.debug("Original definition string: {}", leafRefCtx.getLeafRefTargetPathString());
81 LOG.debug("Parsed leafref path: {}", leafRefCtx.getLeafRefTargetPath());
82 LOG.debug("Absolute leafref path: {}", leafRefCtx.getAbsoluteLeafRefTargetPath());
86 void buildLeafRefContextTreeTest2() {
88 final var q1 = QName.create(tst, "odl-project");
89 final var q2 = QName.create(tst, "project");
90 final var q4 = QName.create(tst, "project-lead2");
92 final var leafRefCtx2 = rootLeafRefContext.getReferencingChildByName(q1)
93 .getReferencingChildByName(q2).getReferencingChildByName(q4);
95 assertTrue(leafRefCtx2.isReferencing());
96 assertNotNull(leafRefCtx2.getLeafRefTargetPath());
97 assertTrue(leafRefCtx2.getLeafRefTargetPath().isAbsolute());
98 assertNotNull(leafRefCtx2.getAbsoluteLeafRefTargetPath());
99 assertTrue(leafRefCtx2.getAbsoluteLeafRefTargetPath().isAbsolute());
101 LOG.debug("******* Test 2 ************");
102 LOG.debug("Original definition string2: {}", leafRefCtx2.getLeafRefTargetPathString());
103 LOG.debug("Parsed leafref path2: {}", leafRefCtx2.getLeafRefTargetPath());
104 LOG.debug("Absolute leafref path2: {}", leafRefCtx2.getAbsoluteLeafRefTargetPath());
108 void buildLeafRefContextTreeXPathTest() {
109 final var q1 = QName.create(tst, "odl-project");
110 final var q2 = QName.create(tst, "project");
111 final var q5 = QName.create(tst, "ch1");
112 final var q6 = QName.create(tst, "c1");
113 final var q7 = QName.create(tst, "ch2");
114 final var q8 = QName.create(tst, "l1");
115 final var leafRefCtx3 = rootLeafRefContext.getReferencingChildByName(q1)
116 .getReferencingChildByName(q2).getReferencingChildByName(q5).getReferencingChildByName(q6)
117 .getReferencingChildByName(q7).getReferencingChildByName(q6).getReferencingChildByName(q8);
119 assertTrue(leafRefCtx3.isReferencing());
120 assertNotNull(leafRefCtx3.getLeafRefTargetPath());
121 assertFalse(leafRefCtx3.getLeafRefTargetPath().isAbsolute());
122 assertNotNull(leafRefCtx3.getAbsoluteLeafRefTargetPath());
123 assertTrue(leafRefCtx3.getAbsoluteLeafRefTargetPath().isAbsolute());
125 LOG.debug("******* Test 3 ************");
126 LOG.debug("Original definition string2: {}", leafRefCtx3.getLeafRefTargetPathString());
127 LOG.debug("Parsed leafref path2: {}", leafRefCtx3.getLeafRefTargetPath());
128 LOG.debug("Absolute leafref path2: {}", leafRefCtx3.getAbsoluteLeafRefTargetPath());
132 void buildLeafRefContextTreeTest4() {
133 final var q9 = QName.create(tst, "odl-project");
134 final var q10 = QName.create(tst, "project");
135 final var q11 = QName.create(tst, "name");
137 final var leafRefCtx4 = rootLeafRefContext.getReferencedChildByName(q9)
138 .getReferencedChildByName(q10).getReferencedChildByName(q11);
140 assertNotNull(leafRefCtx4);
141 assertTrue(leafRefCtx4.isReferenced());
142 assertEquals(6, leafRefCtx4.getAllReferencedByLeafRefCtxs().size());
147 void leafRefContextUtilsTest() {
148 final var q1 = QName.create(tst, "odl-contributor");
149 final var q2 = QName.create(tst, "contributor");
150 final var q3 = QName.create(tst, "odl-project-name");
152 final var found = rootLeafRefContext.getLeafRefReferencingContext(Absolute.of(q1, q2, q3));
153 assertNotNull(found);
154 assertTrue(found.isReferencing());
155 assertNotNull(found.getLeafRefTargetPath());
156 assertEquals(rootLeafRefContext
157 .getReferencingChildByName(q1).getReferencingChildByName(q2).getReferencingChildByName(q3), found);
161 void leafRefContextUtilsTest2() {
162 final var q1 = QName.create(tst, "odl-project");
163 final var q2 = QName.create(tst, "project");
164 final var q3 = QName.create(tst, "name");
166 final var node = Absolute.of(q1, q2, q3);
167 LeafRefContext found = rootLeafRefContext.getLeafRefReferencingContext(node);
170 found = rootLeafRefContext.getLeafRefReferencedByContext(node);
172 assertNotNull(found);
173 assertTrue(found.isReferenced());
174 assertFalse(found.getAllReferencedByLeafRefCtxs().isEmpty());
175 assertEquals(6, found.getAllReferencedByLeafRefCtxs().size());
176 assertEquals(rootLeafRefContext
177 .getReferencedChildByName(q1).getReferencedChildByName(q2).getReferencedChildByName(q3), found);
181 void leafRefContextUtilsTest3() {
182 final var q16 = QName.create(tst, "con1");
183 final var con1 = Absolute.of(q16);
185 final var allLeafRefChilds = rootLeafRefContext.findAllLeafRefChilds(con1);
187 assertNotNull(allLeafRefChilds);
188 assertFalse(allLeafRefChilds.isEmpty());
189 assertEquals(4, allLeafRefChilds.size());
191 var allChildsReferencedByLeafRef = rootLeafRefContext.findAllChildsReferencedByLeafRef(
192 Absolute.of(QName.create(tst, "odl-contributor")));
194 assertNotNull(allChildsReferencedByLeafRef);
195 assertFalse(allChildsReferencedByLeafRef.isEmpty());
196 assertEquals(1, allChildsReferencedByLeafRef.size());
198 allChildsReferencedByLeafRef = rootLeafRefContext.findAllChildsReferencedByLeafRef(con1);
200 assertNotNull(allChildsReferencedByLeafRef);
201 assertTrue(allChildsReferencedByLeafRef.isEmpty());
205 void incorrectLeafRefPathTest() {
206 final var ise = assertThrows(IllegalStateException.class,
207 () -> YangParserTestUtils.parseYangResourceDirectory("/leafref-context-test/incorrect-modules"));
208 final var ype = ise.getCause();
209 final var reactor = ype.getCause();
210 assertInstanceOf(ReactorException.class, reactor);
211 final var source = reactor.getCause();
212 assertInstanceOf(SourceException.class, source);
213 assertTrue(source.getMessage().startsWith("token recognition error at: './' at 1:2"));