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.data.impl.leafref.LeafRefContextUtils;
28 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
30 import org.opendaylight.yangtools.yang.model.api.Module;
31 import org.opendaylight.yangtools.yang.parser.api.YangParserException;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
33 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
34 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 public class LeafRefContextTreeBuilderTest {
39 private static final Logger LOG = LoggerFactory.getLogger(LeafRefContextTreeBuilderTest.class);
41 private static EffectiveModelContext context;
42 private static Module tstMod;
43 private static QNameModule tst;
44 private static LeafRefContext rootLeafRefContext;
47 public static void init() {
48 context = YangParserTestUtils.parseYangResourceDirectory("/leafref-context-test/correct-modules");
50 for (final Module module : context.getModules()) {
51 if (module.getName().equals("leafref-test")) {
56 tst = tstMod.getQNameModule();
58 rootLeafRefContext = LeafRefContext.create(context);
62 public static void cleanup() {
66 rootLeafRefContext = null;
70 public void buildLeafRefContextTreeTest1() {
72 final QName q1 = QName.create(tst, "odl-project");
73 final QName q2 = QName.create(tst, "project");
74 final QName q3 = QName.create(tst, "project-lead");
76 final LeafRefContext leafRefCtx = rootLeafRefContext.getReferencingChildByName(q1)
77 .getReferencingChildByName(q2).getReferencingChildByName(q3);
79 assertTrue(leafRefCtx.isReferencing());
80 assertNotNull(leafRefCtx.getLeafRefTargetPath());
81 assertFalse(leafRefCtx.getLeafRefTargetPath().isAbsolute());
82 assertNotNull(leafRefCtx.getAbsoluteLeafRefTargetPath());
83 assertTrue(leafRefCtx.getAbsoluteLeafRefTargetPath().isAbsolute());
85 LOG.debug("******* Test 1 ************");
86 LOG.debug("Original definition string: {}", leafRefCtx.getLeafRefTargetPathString());
87 LOG.debug("Parsed leafref path: {}", leafRefCtx.getLeafRefTargetPath());
88 LOG.debug("Absolute leafref path: {}", leafRefCtx.getAbsoluteLeafRefTargetPath());
92 public void buildLeafRefContextTreeTest2() {
94 final QName q1 = QName.create(tst, "odl-project");
95 final QName q2 = QName.create(tst, "project");
96 final QName q4 = QName.create(tst, "project-lead2");
98 final LeafRefContext leafRefCtx2 = rootLeafRefContext.getReferencingChildByName(q1)
99 .getReferencingChildByName(q2).getReferencingChildByName(q4);
101 assertTrue(leafRefCtx2.isReferencing());
102 assertNotNull(leafRefCtx2.getLeafRefTargetPath());
103 assertTrue(leafRefCtx2.getLeafRefTargetPath().isAbsolute());
104 assertNotNull(leafRefCtx2.getAbsoluteLeafRefTargetPath());
105 assertTrue(leafRefCtx2.getAbsoluteLeafRefTargetPath().isAbsolute());
107 LOG.debug("******* Test 2 ************");
108 LOG.debug("Original definition string2: {}", leafRefCtx2.getLeafRefTargetPathString());
109 LOG.debug("Parsed leafref path2: {}", leafRefCtx2.getLeafRefTargetPath());
110 LOG.debug("Absolute leafref path2: {}", leafRefCtx2.getAbsoluteLeafRefTargetPath());
114 public void buildLeafRefContextTreeXPathTest() {
115 final QName q1 = QName.create(tst, "odl-project");
116 final QName q2 = QName.create(tst, "project");
117 final QName q5 = QName.create(tst, "ch1");
118 final QName q6 = QName.create(tst, "c1");
119 final QName q7 = QName.create(tst, "ch2");
120 final QName q8 = QName.create(tst, "l1");
121 final LeafRefContext leafRefCtx3 = rootLeafRefContext.getReferencingChildByName(q1)
122 .getReferencingChildByName(q2).getReferencingChildByName(q5).getReferencingChildByName(q6)
123 .getReferencingChildByName(q7).getReferencingChildByName(q6).getReferencingChildByName(q8);
125 assertTrue(leafRefCtx3.isReferencing());
126 assertNotNull(leafRefCtx3.getLeafRefTargetPath());
127 assertFalse(leafRefCtx3.getLeafRefTargetPath().isAbsolute());
128 assertNotNull(leafRefCtx3.getAbsoluteLeafRefTargetPath());
129 assertTrue(leafRefCtx3.getAbsoluteLeafRefTargetPath().isAbsolute());
131 LOG.debug("******* Test 3 ************");
132 LOG.debug("Original definition string2: {}", leafRefCtx3.getLeafRefTargetPathString());
133 LOG.debug("Parsed leafref path2: {}", leafRefCtx3.getLeafRefTargetPath());
134 LOG.debug("Absolute leafref path2: {}", leafRefCtx3.getAbsoluteLeafRefTargetPath());
138 public void buildLeafRefContextTreeTest4() {
139 final QName q9 = QName.create(tst, "odl-project");
140 final QName q10 = QName.create(tst, "project");
141 final QName q11 = QName.create(tst, "name");
143 final LeafRefContext leafRefCtx4 = rootLeafRefContext.getReferencedChildByName(q9)
144 .getReferencedChildByName(q10).getReferencedChildByName(q11);
146 assertNotNull(leafRefCtx4);
147 assertTrue(leafRefCtx4.isReferenced());
148 assertEquals(6, leafRefCtx4.getAllReferencedByLeafRefCtxs().size());
153 public void leafRefContextUtilsTest() {
154 final QName q1 = QName.create(tst, "odl-contributor");
155 final QName q2 = QName.create(tst, "contributor");
156 final QName q3 = QName.create(tst, "odl-project-name");
158 final LeafRefContext odlContrProjNameCtx = rootLeafRefContext.getReferencingChildByName(q1)
159 .getReferencingChildByName(q2).getReferencingChildByName(q3);
161 final DataSchemaNode odlContrProjNameNode = tstMod.findDataChildByName(q1, q2, q3).get();
163 final LeafRefContext foundOdlContrProjNameCtx = LeafRefContextUtils.getLeafRefReferencingContext(
164 odlContrProjNameNode, rootLeafRefContext);
166 assertNotNull(foundOdlContrProjNameCtx);
167 assertTrue(foundOdlContrProjNameCtx.isReferencing());
168 assertNotNull(foundOdlContrProjNameCtx.getLeafRefTargetPath());
169 assertEquals(odlContrProjNameCtx, foundOdlContrProjNameCtx);
173 public void leafRefContextUtilsTest2() {
174 final QName q1 = QName.create(tst, "odl-project");
175 final QName q2 = QName.create(tst, "project");
176 final QName q3 = QName.create(tst, "name");
178 final LeafRefContext leafRefCtx = rootLeafRefContext.getReferencedChildByName(q1).getReferencedChildByName(q2)
179 .getReferencedChildByName(q3);
181 final DataSchemaNode odlProjNameNode = tstMod.findDataChildByName(q1, q2, q3).get();
183 LeafRefContext foundOdlProjNameCtx = LeafRefContextUtils.getLeafRefReferencingContext(odlProjNameNode,
186 assertNull(foundOdlProjNameCtx);
188 foundOdlProjNameCtx = LeafRefContextUtils.getLeafRefReferencedByContext(odlProjNameNode, rootLeafRefContext);
190 assertNotNull(foundOdlProjNameCtx);
191 assertTrue(foundOdlProjNameCtx.isReferenced());
192 assertFalse(foundOdlProjNameCtx.getAllReferencedByLeafRefCtxs().isEmpty());
193 assertEquals(6, foundOdlProjNameCtx.getAllReferencedByLeafRefCtxs().size());
194 assertEquals(leafRefCtx, foundOdlProjNameCtx);
198 public void leafRefContextUtilsTest3() {
199 final QName q16 = QName.create(tst, "con1");
200 final DataSchemaNode con1 = tstMod.findDataChildByName(q16).get();
201 final List<LeafRefContext> allLeafRefChilds = LeafRefContextUtils.findAllLeafRefChilds(con1,
204 assertNotNull(allLeafRefChilds);
205 assertFalse(allLeafRefChilds.isEmpty());
206 assertEquals(4, allLeafRefChilds.size());
208 final QName q17 = QName.create(tst, "odl-contributor");
209 final DataSchemaNode odlContributorNode = tstMod.findDataChildByName(q17).get();
210 List<LeafRefContext> allChildsReferencedByLeafRef = LeafRefContextUtils.findAllChildsReferencedByLeafRef(
211 odlContributorNode, rootLeafRefContext);
213 assertNotNull(allChildsReferencedByLeafRef);
214 assertFalse(allChildsReferencedByLeafRef.isEmpty());
215 assertEquals(1, allChildsReferencedByLeafRef.size());
217 allChildsReferencedByLeafRef = LeafRefContextUtils.findAllChildsReferencedByLeafRef(con1, rootLeafRefContext);
219 assertNotNull(allChildsReferencedByLeafRef);
220 assertTrue(allChildsReferencedByLeafRef.isEmpty());
224 public void incorrectLeafRefPathTest() {
225 final IllegalStateException ise = assertThrows(IllegalStateException.class,
226 () -> YangParserTestUtils.parseYangResourceDirectory("/leafref-context-test/incorrect-modules"));
227 final Throwable ype = ise.getCause();
228 assertThat(ype, instanceOf(YangParserException.class));
229 final Throwable reactor = ype.getCause();
230 assertThat(reactor, instanceOf(ReactorException.class));
231 final Throwable source = reactor.getCause();
232 assertThat(source, instanceOf(SourceException.class));
233 assertThat(source.getMessage(), startsWith("token recognition error at: './' at 1:2"));