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.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.CoreMatchers.startsWith;
12 import static org.hamcrest.MatcherAssert.assertThat;
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertFalse;
15 import static org.junit.Assert.assertNotNull;
16 import static org.junit.Assert.assertNull;
17 import static org.junit.Assert.assertThrows;
18 import static org.junit.Assert.assertTrue;
20 import java.util.List;
21 import org.junit.AfterClass;
22 import org.junit.BeforeClass;
23 import org.junit.Test;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.common.QNameModule;
26 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
27 import org.opendaylight.yangtools.yang.model.api.Module;
28 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
30 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
31 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 public class LeafRefContextTreeBuilderTest {
36 private static final Logger LOG = LoggerFactory.getLogger(LeafRefContextTreeBuilderTest.class);
38 private static EffectiveModelContext context;
39 private static Module tstMod;
40 private static QNameModule tst;
41 private static LeafRefContext rootLeafRefContext;
44 public static void init() {
45 context = YangParserTestUtils.parseYangResourceDirectory("/leafref-context-test/correct-modules");
47 for (final Module module : context.getModules()) {
48 if (module.getName().equals("leafref-test")) {
53 tst = tstMod.getQNameModule();
55 rootLeafRefContext = LeafRefContext.create(context);
59 public static void cleanup() {
63 rootLeafRefContext = null;
67 public void buildLeafRefContextTreeTest1() {
69 final QName q1 = QName.create(tst, "odl-project");
70 final QName q2 = QName.create(tst, "project");
71 final QName q3 = QName.create(tst, "project-lead");
73 final LeafRefContext leafRefCtx = rootLeafRefContext.getReferencingChildByName(q1)
74 .getReferencingChildByName(q2).getReferencingChildByName(q3);
76 assertTrue(leafRefCtx.isReferencing());
77 assertNotNull(leafRefCtx.getLeafRefTargetPath());
78 assertFalse(leafRefCtx.getLeafRefTargetPath().isAbsolute());
79 assertNotNull(leafRefCtx.getAbsoluteLeafRefTargetPath());
80 assertTrue(leafRefCtx.getAbsoluteLeafRefTargetPath().isAbsolute());
82 LOG.debug("******* Test 1 ************");
83 LOG.debug("Original definition string: {}", leafRefCtx.getLeafRefTargetPathString());
84 LOG.debug("Parsed leafref path: {}", leafRefCtx.getLeafRefTargetPath());
85 LOG.debug("Absolute leafref path: {}", leafRefCtx.getAbsoluteLeafRefTargetPath());
89 public void buildLeafRefContextTreeTest2() {
91 final QName q1 = QName.create(tst, "odl-project");
92 final QName q2 = QName.create(tst, "project");
93 final QName q4 = QName.create(tst, "project-lead2");
95 final LeafRefContext leafRefCtx2 = rootLeafRefContext.getReferencingChildByName(q1)
96 .getReferencingChildByName(q2).getReferencingChildByName(q4);
98 assertTrue(leafRefCtx2.isReferencing());
99 assertNotNull(leafRefCtx2.getLeafRefTargetPath());
100 assertTrue(leafRefCtx2.getLeafRefTargetPath().isAbsolute());
101 assertNotNull(leafRefCtx2.getAbsoluteLeafRefTargetPath());
102 assertTrue(leafRefCtx2.getAbsoluteLeafRefTargetPath().isAbsolute());
104 LOG.debug("******* Test 2 ************");
105 LOG.debug("Original definition string2: {}", leafRefCtx2.getLeafRefTargetPathString());
106 LOG.debug("Parsed leafref path2: {}", leafRefCtx2.getLeafRefTargetPath());
107 LOG.debug("Absolute leafref path2: {}", leafRefCtx2.getAbsoluteLeafRefTargetPath());
111 public void buildLeafRefContextTreeXPathTest() {
112 final QName q1 = QName.create(tst, "odl-project");
113 final QName q2 = QName.create(tst, "project");
114 final QName q5 = QName.create(tst, "ch1");
115 final QName q6 = QName.create(tst, "c1");
116 final QName q7 = QName.create(tst, "ch2");
117 final QName q8 = QName.create(tst, "l1");
118 final LeafRefContext leafRefCtx3 = rootLeafRefContext.getReferencingChildByName(q1)
119 .getReferencingChildByName(q2).getReferencingChildByName(q5).getReferencingChildByName(q6)
120 .getReferencingChildByName(q7).getReferencingChildByName(q6).getReferencingChildByName(q8);
122 assertTrue(leafRefCtx3.isReferencing());
123 assertNotNull(leafRefCtx3.getLeafRefTargetPath());
124 assertFalse(leafRefCtx3.getLeafRefTargetPath().isAbsolute());
125 assertNotNull(leafRefCtx3.getAbsoluteLeafRefTargetPath());
126 assertTrue(leafRefCtx3.getAbsoluteLeafRefTargetPath().isAbsolute());
128 LOG.debug("******* Test 3 ************");
129 LOG.debug("Original definition string2: {}", leafRefCtx3.getLeafRefTargetPathString());
130 LOG.debug("Parsed leafref path2: {}", leafRefCtx3.getLeafRefTargetPath());
131 LOG.debug("Absolute leafref path2: {}", leafRefCtx3.getAbsoluteLeafRefTargetPath());
135 public void buildLeafRefContextTreeTest4() {
136 final QName q9 = QName.create(tst, "odl-project");
137 final QName q10 = QName.create(tst, "project");
138 final QName q11 = QName.create(tst, "name");
140 final LeafRefContext leafRefCtx4 = rootLeafRefContext.getReferencedChildByName(q9)
141 .getReferencedChildByName(q10).getReferencedChildByName(q11);
143 assertNotNull(leafRefCtx4);
144 assertTrue(leafRefCtx4.isReferenced());
145 assertEquals(6, leafRefCtx4.getAllReferencedByLeafRefCtxs().size());
150 public void leafRefContextUtilsTest() {
151 final QName q1 = QName.create(tst, "odl-contributor");
152 final QName q2 = QName.create(tst, "contributor");
153 final QName q3 = QName.create(tst, "odl-project-name");
155 final LeafRefContext found = rootLeafRefContext.getLeafRefReferencingContext(Absolute.of(q1, q2, q3));
156 assertNotNull(found);
157 assertTrue(found.isReferencing());
158 assertNotNull(found.getLeafRefTargetPath());
159 assertEquals(rootLeafRefContext
160 .getReferencingChildByName(q1).getReferencingChildByName(q2).getReferencingChildByName(q3), found);
164 public void leafRefContextUtilsTest2() {
165 final QName q1 = QName.create(tst, "odl-project");
166 final QName q2 = QName.create(tst, "project");
167 final QName q3 = QName.create(tst, "name");
169 final Absolute node = Absolute.of(q1, q2, q3);
170 LeafRefContext found = rootLeafRefContext.getLeafRefReferencingContext(node);
173 found = rootLeafRefContext.getLeafRefReferencedByContext(node);
175 assertNotNull(found);
176 assertTrue(found.isReferenced());
177 assertFalse(found.getAllReferencedByLeafRefCtxs().isEmpty());
178 assertEquals(6, found.getAllReferencedByLeafRefCtxs().size());
179 assertEquals(rootLeafRefContext
180 .getReferencedChildByName(q1).getReferencedChildByName(q2).getReferencedChildByName(q3), found);
184 public void leafRefContextUtilsTest3() {
185 final QName q16 = QName.create(tst, "con1");
186 final Absolute con1 = Absolute.of(q16);
188 final List<LeafRefContext> allLeafRefChilds = rootLeafRefContext.findAllLeafRefChilds(con1);
190 assertNotNull(allLeafRefChilds);
191 assertFalse(allLeafRefChilds.isEmpty());
192 assertEquals(4, allLeafRefChilds.size());
194 List<LeafRefContext> allChildsReferencedByLeafRef = rootLeafRefContext.findAllChildsReferencedByLeafRef(
195 Absolute.of(QName.create(tst, "odl-contributor")));
197 assertNotNull(allChildsReferencedByLeafRef);
198 assertFalse(allChildsReferencedByLeafRef.isEmpty());
199 assertEquals(1, allChildsReferencedByLeafRef.size());
201 allChildsReferencedByLeafRef = rootLeafRefContext.findAllChildsReferencedByLeafRef(con1);
203 assertNotNull(allChildsReferencedByLeafRef);
204 assertTrue(allChildsReferencedByLeafRef.isEmpty());
208 public void incorrectLeafRefPathTest() {
209 final IllegalStateException ise = assertThrows(IllegalStateException.class,
210 () -> YangParserTestUtils.parseYangResourceDirectory("/leafref-context-test/incorrect-modules"));
211 final Throwable ype = ise.getCause();
212 final Throwable reactor = ype.getCause();
213 assertThat(reactor, instanceOf(ReactorException.class));
214 final Throwable source = reactor.getCause();
215 assertThat(source, instanceOf(SourceException.class));
216 assertThat(source.getMessage(), startsWith("token recognition error at: './' at 1:2"));