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.impl.leafref.context;
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.data.impl.leafref.LeafRefContext;
27 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
28 import org.opendaylight.yangtools.yang.model.api.Module;
29 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
30 import org.opendaylight.yangtools.yang.parser.api.YangParserException;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
32 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
33 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 public class LeafRefContextTreeBuilderTest {
38 private static final Logger LOG = LoggerFactory.getLogger(LeafRefContextTreeBuilderTest.class);
40 private static EffectiveModelContext context;
41 private static Module tstMod;
42 private static QNameModule tst;
43 private static LeafRefContext rootLeafRefContext;
46 public static void init() {
47 context = YangParserTestUtils.parseYangResourceDirectory("/leafref-context-test/correct-modules");
49 for (final Module module : context.getModules()) {
50 if (module.getName().equals("leafref-test")) {
55 tst = tstMod.getQNameModule();
57 rootLeafRefContext = LeafRefContext.create(context);
61 public static void cleanup() {
65 rootLeafRefContext = null;
69 public void buildLeafRefContextTreeTest1() {
71 final QName q1 = QName.create(tst, "odl-project");
72 final QName q2 = QName.create(tst, "project");
73 final QName q3 = QName.create(tst, "project-lead");
75 final LeafRefContext leafRefCtx = rootLeafRefContext.getReferencingChildByName(q1)
76 .getReferencingChildByName(q2).getReferencingChildByName(q3);
78 assertTrue(leafRefCtx.isReferencing());
79 assertNotNull(leafRefCtx.getLeafRefTargetPath());
80 assertFalse(leafRefCtx.getLeafRefTargetPath().isAbsolute());
81 assertNotNull(leafRefCtx.getAbsoluteLeafRefTargetPath());
82 assertTrue(leafRefCtx.getAbsoluteLeafRefTargetPath().isAbsolute());
84 LOG.debug("******* Test 1 ************");
85 LOG.debug("Original definition string: {}", leafRefCtx.getLeafRefTargetPathString());
86 LOG.debug("Parsed leafref path: {}", leafRefCtx.getLeafRefTargetPath());
87 LOG.debug("Absolute leafref path: {}", leafRefCtx.getAbsoluteLeafRefTargetPath());
91 public void buildLeafRefContextTreeTest2() {
93 final QName q1 = QName.create(tst, "odl-project");
94 final QName q2 = QName.create(tst, "project");
95 final QName q4 = QName.create(tst, "project-lead2");
97 final LeafRefContext leafRefCtx2 = rootLeafRefContext.getReferencingChildByName(q1)
98 .getReferencingChildByName(q2).getReferencingChildByName(q4);
100 assertTrue(leafRefCtx2.isReferencing());
101 assertNotNull(leafRefCtx2.getLeafRefTargetPath());
102 assertTrue(leafRefCtx2.getLeafRefTargetPath().isAbsolute());
103 assertNotNull(leafRefCtx2.getAbsoluteLeafRefTargetPath());
104 assertTrue(leafRefCtx2.getAbsoluteLeafRefTargetPath().isAbsolute());
106 LOG.debug("******* Test 2 ************");
107 LOG.debug("Original definition string2: {}", leafRefCtx2.getLeafRefTargetPathString());
108 LOG.debug("Parsed leafref path2: {}", leafRefCtx2.getLeafRefTargetPath());
109 LOG.debug("Absolute leafref path2: {}", leafRefCtx2.getAbsoluteLeafRefTargetPath());
113 public void buildLeafRefContextTreeXPathTest() {
114 final QName q1 = QName.create(tst, "odl-project");
115 final QName q2 = QName.create(tst, "project");
116 final QName q5 = QName.create(tst, "ch1");
117 final QName q6 = QName.create(tst, "c1");
118 final QName q7 = QName.create(tst, "ch2");
119 final QName q8 = QName.create(tst, "l1");
120 final LeafRefContext leafRefCtx3 = rootLeafRefContext.getReferencingChildByName(q1)
121 .getReferencingChildByName(q2).getReferencingChildByName(q5).getReferencingChildByName(q6)
122 .getReferencingChildByName(q7).getReferencingChildByName(q6).getReferencingChildByName(q8);
124 assertTrue(leafRefCtx3.isReferencing());
125 assertNotNull(leafRefCtx3.getLeafRefTargetPath());
126 assertFalse(leafRefCtx3.getLeafRefTargetPath().isAbsolute());
127 assertNotNull(leafRefCtx3.getAbsoluteLeafRefTargetPath());
128 assertTrue(leafRefCtx3.getAbsoluteLeafRefTargetPath().isAbsolute());
130 LOG.debug("******* Test 3 ************");
131 LOG.debug("Original definition string2: {}", leafRefCtx3.getLeafRefTargetPathString());
132 LOG.debug("Parsed leafref path2: {}", leafRefCtx3.getLeafRefTargetPath());
133 LOG.debug("Absolute leafref path2: {}", leafRefCtx3.getAbsoluteLeafRefTargetPath());
137 public void buildLeafRefContextTreeTest4() {
138 final QName q9 = QName.create(tst, "odl-project");
139 final QName q10 = QName.create(tst, "project");
140 final QName q11 = QName.create(tst, "name");
142 final LeafRefContext leafRefCtx4 = rootLeafRefContext.getReferencedChildByName(q9)
143 .getReferencedChildByName(q10).getReferencedChildByName(q11);
145 assertNotNull(leafRefCtx4);
146 assertTrue(leafRefCtx4.isReferenced());
147 assertEquals(6, leafRefCtx4.getAllReferencedByLeafRefCtxs().size());
152 public void leafRefContextUtilsTest() {
153 final QName q1 = QName.create(tst, "odl-contributor");
154 final QName q2 = QName.create(tst, "contributor");
155 final QName q3 = QName.create(tst, "odl-project-name");
157 final LeafRefContext found = rootLeafRefContext.getLeafRefReferencingContext(Absolute.of(q1, q2, q3));
158 assertNotNull(found);
159 assertTrue(found.isReferencing());
160 assertNotNull(found.getLeafRefTargetPath());
161 assertEquals(rootLeafRefContext
162 .getReferencingChildByName(q1).getReferencingChildByName(q2).getReferencingChildByName(q3), found);
166 public void leafRefContextUtilsTest2() {
167 final QName q1 = QName.create(tst, "odl-project");
168 final QName q2 = QName.create(tst, "project");
169 final QName q3 = QName.create(tst, "name");
171 final Absolute node = Absolute.of(q1, q2, q3);
172 LeafRefContext found = rootLeafRefContext.getLeafRefReferencingContext(node);
175 found = rootLeafRefContext.getLeafRefReferencedByContext(node);
177 assertNotNull(found);
178 assertTrue(found.isReferenced());
179 assertFalse(found.getAllReferencedByLeafRefCtxs().isEmpty());
180 assertEquals(6, found.getAllReferencedByLeafRefCtxs().size());
181 assertEquals(rootLeafRefContext
182 .getReferencedChildByName(q1).getReferencedChildByName(q2).getReferencedChildByName(q3), found);
186 public void leafRefContextUtilsTest3() {
187 final QName q16 = QName.create(tst, "con1");
188 final Absolute con1 = Absolute.of(q16);
190 final List<LeafRefContext> allLeafRefChilds = rootLeafRefContext.findAllLeafRefChilds(con1);
192 assertNotNull(allLeafRefChilds);
193 assertFalse(allLeafRefChilds.isEmpty());
194 assertEquals(4, allLeafRefChilds.size());
196 List<LeafRefContext> allChildsReferencedByLeafRef = rootLeafRefContext.findAllChildsReferencedByLeafRef(
197 Absolute.of(QName.create(tst, "odl-contributor")));
199 assertNotNull(allChildsReferencedByLeafRef);
200 assertFalse(allChildsReferencedByLeafRef.isEmpty());
201 assertEquals(1, allChildsReferencedByLeafRef.size());
203 allChildsReferencedByLeafRef = rootLeafRefContext.findAllChildsReferencedByLeafRef(con1);
205 assertNotNull(allChildsReferencedByLeafRef);
206 assertTrue(allChildsReferencedByLeafRef.isEmpty());
210 public void incorrectLeafRefPathTest() {
211 final IllegalStateException ise = assertThrows(IllegalStateException.class,
212 () -> YangParserTestUtils.parseYangResourceDirectory("/leafref-context-test/incorrect-modules"));
213 final Throwable ype = ise.getCause();
214 assertThat(ype, instanceOf(YangParserException.class));
215 final Throwable reactor = ype.getCause();
216 assertThat(reactor, instanceOf(ReactorException.class));
217 final Throwable source = reactor.getCause();
218 assertThat(source, instanceOf(SourceException.class));
219 assertThat(source.getMessage(), startsWith("token recognition error at: './' at 1:2"));