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;
22 import org.junit.AfterClass;
23 import org.junit.BeforeClass;
24 import org.junit.Test;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.common.QNameModule;
27 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefContext;
28 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefContextUtils;
29 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.Module;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32 import org.opendaylight.yangtools.yang.model.parser.api.YangParserException;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
34 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
35 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 public class LeafRefContextTreeBuilderTest {
40 private static final Logger LOG = LoggerFactory.getLogger(LeafRefContextTreeBuilderTest.class);
42 private static SchemaContext context;
43 private static Module tstMod;
44 private static QNameModule tst;
45 private static LeafRefContext rootLeafRefContext;
48 public static void init() {
49 context = YangParserTestUtils.parseYangResourceDirectory("/leafref-context-test/correct-modules");
51 final Set<Module> modules = context.getModules();
52 for (final Module module : modules) {
53 if (module.getName().equals("leafref-test")) {
58 tst = tstMod.getQNameModule();
60 rootLeafRefContext = LeafRefContext.create(context);
64 public static void cleanup() {
68 rootLeafRefContext = null;
72 public void buildLeafRefContextTreeTest1() {
74 final QName q1 = QName.create(tst, "odl-project");
75 final QName q2 = QName.create(tst, "project");
76 final QName q3 = QName.create(tst, "project-lead");
78 final LeafRefContext leafRefCtx = rootLeafRefContext.getReferencingChildByName(q1)
79 .getReferencingChildByName(q2).getReferencingChildByName(q3);
81 assertTrue(leafRefCtx.isReferencing());
82 assertNotNull(leafRefCtx.getLeafRefTargetPath());
83 assertFalse(leafRefCtx.getLeafRefTargetPath().isAbsolute());
84 assertNotNull(leafRefCtx.getAbsoluteLeafRefTargetPath());
85 assertTrue(leafRefCtx.getAbsoluteLeafRefTargetPath().isAbsolute());
87 LOG.debug("******* Test 1 ************");
88 LOG.debug("Original definition string: {}", leafRefCtx.getLeafRefTargetPathString());
89 LOG.debug("Parsed leafref path: {}", leafRefCtx.getLeafRefTargetPath());
90 LOG.debug("Absolute leafref path: {}", leafRefCtx.getAbsoluteLeafRefTargetPath());
94 public void buildLeafRefContextTreeTest2() {
96 final QName q1 = QName.create(tst, "odl-project");
97 final QName q2 = QName.create(tst, "project");
98 final QName q4 = QName.create(tst, "project-lead2");
100 final LeafRefContext leafRefCtx2 = rootLeafRefContext.getReferencingChildByName(q1)
101 .getReferencingChildByName(q2).getReferencingChildByName(q4);
103 assertTrue(leafRefCtx2.isReferencing());
104 assertNotNull(leafRefCtx2.getLeafRefTargetPath());
105 assertTrue(leafRefCtx2.getLeafRefTargetPath().isAbsolute());
106 assertNotNull(leafRefCtx2.getAbsoluteLeafRefTargetPath());
107 assertTrue(leafRefCtx2.getAbsoluteLeafRefTargetPath().isAbsolute());
109 LOG.debug("******* Test 2 ************");
110 LOG.debug("Original definition string2: {}", leafRefCtx2.getLeafRefTargetPathString());
111 LOG.debug("Parsed leafref path2: {}", leafRefCtx2.getLeafRefTargetPath());
112 LOG.debug("Absolute leafref path2: {}", leafRefCtx2.getAbsoluteLeafRefTargetPath());
116 public void buildLeafRefContextTreeXPathTest() {
117 final QName q1 = QName.create(tst, "odl-project");
118 final QName q2 = QName.create(tst, "project");
119 final QName q5 = QName.create(tst, "ch1");
120 final QName q6 = QName.create(tst, "c1");
121 final QName q7 = QName.create(tst, "ch2");
122 final QName q8 = QName.create(tst, "l1");
123 final LeafRefContext leafRefCtx3 = rootLeafRefContext.getReferencingChildByName(q1)
124 .getReferencingChildByName(q2).getReferencingChildByName(q5).getReferencingChildByName(q6)
125 .getReferencingChildByName(q7).getReferencingChildByName(q6).getReferencingChildByName(q8);
127 assertTrue(leafRefCtx3.isReferencing());
128 assertNotNull(leafRefCtx3.getLeafRefTargetPath());
129 assertFalse(leafRefCtx3.getLeafRefTargetPath().isAbsolute());
130 assertNotNull(leafRefCtx3.getAbsoluteLeafRefTargetPath());
131 assertTrue(leafRefCtx3.getAbsoluteLeafRefTargetPath().isAbsolute());
133 LOG.debug("******* Test 3 ************");
134 LOG.debug("Original definition string2: {}", leafRefCtx3.getLeafRefTargetPathString());
135 LOG.debug("Parsed leafref path2: {}", leafRefCtx3.getLeafRefTargetPath());
136 LOG.debug("Absolute leafref path2: {}", leafRefCtx3.getAbsoluteLeafRefTargetPath());
140 public void buildLeafRefContextTreeTest4() {
141 final QName q9 = QName.create(tst, "odl-project");
142 final QName q10 = QName.create(tst, "project");
143 final QName q11 = QName.create(tst, "name");
145 final LeafRefContext leafRefCtx4 = rootLeafRefContext.getReferencedChildByName(q9)
146 .getReferencedChildByName(q10).getReferencedChildByName(q11);
148 assertNotNull(leafRefCtx4);
149 assertTrue(leafRefCtx4.isReferenced());
150 assertEquals(6, leafRefCtx4.getAllReferencedByLeafRefCtxs().size());
155 public void leafRefContextUtilsTest() {
156 final QName q1 = QName.create(tst, "odl-contributor");
157 final QName q2 = QName.create(tst, "contributor");
158 final QName q3 = QName.create(tst, "odl-project-name");
160 final LeafRefContext odlContrProjNameCtx = rootLeafRefContext.getReferencingChildByName(q1)
161 .getReferencingChildByName(q2).getReferencingChildByName(q3);
163 final DataSchemaNode odlContrProjNameNode = tstMod.findDataChildByName(q1, q2, q3).get();
165 final LeafRefContext foundOdlContrProjNameCtx = LeafRefContextUtils.getLeafRefReferencingContext(
166 odlContrProjNameNode, rootLeafRefContext);
168 assertNotNull(foundOdlContrProjNameCtx);
169 assertTrue(foundOdlContrProjNameCtx.isReferencing());
170 assertNotNull(foundOdlContrProjNameCtx.getLeafRefTargetPath());
171 assertEquals(odlContrProjNameCtx, foundOdlContrProjNameCtx);
175 public void leafRefContextUtilsTest2() {
176 final QName q1 = QName.create(tst, "odl-project");
177 final QName q2 = QName.create(tst, "project");
178 final QName q3 = QName.create(tst, "name");
180 final LeafRefContext leafRefCtx = rootLeafRefContext.getReferencedChildByName(q1).getReferencedChildByName(q2)
181 .getReferencedChildByName(q3);
183 final DataSchemaNode odlProjNameNode = tstMod.findDataChildByName(q1, q2, q3).get();
185 LeafRefContext foundOdlProjNameCtx = LeafRefContextUtils.getLeafRefReferencingContext(odlProjNameNode,
188 assertNull(foundOdlProjNameCtx);
190 foundOdlProjNameCtx = LeafRefContextUtils.getLeafRefReferencedByContext(odlProjNameNode, rootLeafRefContext);
192 assertNotNull(foundOdlProjNameCtx);
193 assertTrue(foundOdlProjNameCtx.isReferenced());
194 assertFalse(foundOdlProjNameCtx.getAllReferencedByLeafRefCtxs().isEmpty());
195 assertEquals(6, foundOdlProjNameCtx.getAllReferencedByLeafRefCtxs().size());
196 assertEquals(leafRefCtx, foundOdlProjNameCtx);
200 public void leafRefContextUtilsTest3() {
201 final QName q16 = QName.create(tst, "con1");
202 final DataSchemaNode con1 = tstMod.findDataChildByName(q16).get();
203 final List<LeafRefContext> allLeafRefChilds = LeafRefContextUtils.findAllLeafRefChilds(con1,
206 assertNotNull(allLeafRefChilds);
207 assertFalse(allLeafRefChilds.isEmpty());
208 assertEquals(4, allLeafRefChilds.size());
210 final QName q17 = QName.create(tst, "odl-contributor");
211 final DataSchemaNode odlContributorNode = tstMod.findDataChildByName(q17).get();
212 List<LeafRefContext> allChildsReferencedByLeafRef = LeafRefContextUtils.findAllChildsReferencedByLeafRef(
213 odlContributorNode, rootLeafRefContext);
215 assertNotNull(allChildsReferencedByLeafRef);
216 assertFalse(allChildsReferencedByLeafRef.isEmpty());
217 assertEquals(1, allChildsReferencedByLeafRef.size());
219 allChildsReferencedByLeafRef = LeafRefContextUtils.findAllChildsReferencedByLeafRef(con1, rootLeafRefContext);
221 assertNotNull(allChildsReferencedByLeafRef);
222 assertTrue(allChildsReferencedByLeafRef.isEmpty());
226 public void incorrectLeafRefPathTest() {
227 final IllegalStateException ise = assertThrows(IllegalStateException.class,
228 () -> YangParserTestUtils.parseYangResourceDirectory("/leafref-context-test/incorrect-modules"));
229 final Throwable ype = ise.getCause();
230 assertThat(ype, instanceOf(YangParserException.class));
231 final Throwable reactor = ype.getCause();
232 assertThat(reactor, instanceOf(ReactorException.class));
233 final Throwable source = reactor.getCause();
234 assertThat(source, instanceOf(SourceException.class));
235 assertThat(source.getMessage(), startsWith("token recognition error at: './' at 1:2"));