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.api.YangParserException;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
31 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
32 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 public class LeafRefContextTreeBuilderTest {
37 private static final Logger LOG = LoggerFactory.getLogger(LeafRefContextTreeBuilderTest.class);
39 private static EffectiveModelContext context;
40 private static Module tstMod;
41 private static QNameModule tst;
42 private static LeafRefContext rootLeafRefContext;
45 public static void init() {
46 context = YangParserTestUtils.parseYangResourceDirectory("/leafref-context-test/correct-modules");
48 for (final Module module : context.getModules()) {
49 if (module.getName().equals("leafref-test")) {
54 tst = tstMod.getQNameModule();
56 rootLeafRefContext = LeafRefContext.create(context);
60 public static void cleanup() {
64 rootLeafRefContext = null;
68 public void buildLeafRefContextTreeTest1() {
70 final QName q1 = QName.create(tst, "odl-project");
71 final QName q2 = QName.create(tst, "project");
72 final QName q3 = QName.create(tst, "project-lead");
74 final LeafRefContext leafRefCtx = rootLeafRefContext.getReferencingChildByName(q1)
75 .getReferencingChildByName(q2).getReferencingChildByName(q3);
77 assertTrue(leafRefCtx.isReferencing());
78 assertNotNull(leafRefCtx.getLeafRefTargetPath());
79 assertFalse(leafRefCtx.getLeafRefTargetPath().isAbsolute());
80 assertNotNull(leafRefCtx.getAbsoluteLeafRefTargetPath());
81 assertTrue(leafRefCtx.getAbsoluteLeafRefTargetPath().isAbsolute());
83 LOG.debug("******* Test 1 ************");
84 LOG.debug("Original definition string: {}", leafRefCtx.getLeafRefTargetPathString());
85 LOG.debug("Parsed leafref path: {}", leafRefCtx.getLeafRefTargetPath());
86 LOG.debug("Absolute leafref path: {}", leafRefCtx.getAbsoluteLeafRefTargetPath());
90 public void buildLeafRefContextTreeTest2() {
92 final QName q1 = QName.create(tst, "odl-project");
93 final QName q2 = QName.create(tst, "project");
94 final QName q4 = QName.create(tst, "project-lead2");
96 final LeafRefContext leafRefCtx2 = rootLeafRefContext.getReferencingChildByName(q1)
97 .getReferencingChildByName(q2).getReferencingChildByName(q4);
99 assertTrue(leafRefCtx2.isReferencing());
100 assertNotNull(leafRefCtx2.getLeafRefTargetPath());
101 assertTrue(leafRefCtx2.getLeafRefTargetPath().isAbsolute());
102 assertNotNull(leafRefCtx2.getAbsoluteLeafRefTargetPath());
103 assertTrue(leafRefCtx2.getAbsoluteLeafRefTargetPath().isAbsolute());
105 LOG.debug("******* Test 2 ************");
106 LOG.debug("Original definition string2: {}", leafRefCtx2.getLeafRefTargetPathString());
107 LOG.debug("Parsed leafref path2: {}", leafRefCtx2.getLeafRefTargetPath());
108 LOG.debug("Absolute leafref path2: {}", leafRefCtx2.getAbsoluteLeafRefTargetPath());
112 public void buildLeafRefContextTreeXPathTest() {
113 final QName q1 = QName.create(tst, "odl-project");
114 final QName q2 = QName.create(tst, "project");
115 final QName q5 = QName.create(tst, "ch1");
116 final QName q6 = QName.create(tst, "c1");
117 final QName q7 = QName.create(tst, "ch2");
118 final QName q8 = QName.create(tst, "l1");
119 final LeafRefContext leafRefCtx3 = rootLeafRefContext.getReferencingChildByName(q1)
120 .getReferencingChildByName(q2).getReferencingChildByName(q5).getReferencingChildByName(q6)
121 .getReferencingChildByName(q7).getReferencingChildByName(q6).getReferencingChildByName(q8);
123 assertTrue(leafRefCtx3.isReferencing());
124 assertNotNull(leafRefCtx3.getLeafRefTargetPath());
125 assertFalse(leafRefCtx3.getLeafRefTargetPath().isAbsolute());
126 assertNotNull(leafRefCtx3.getAbsoluteLeafRefTargetPath());
127 assertTrue(leafRefCtx3.getAbsoluteLeafRefTargetPath().isAbsolute());
129 LOG.debug("******* Test 3 ************");
130 LOG.debug("Original definition string2: {}", leafRefCtx3.getLeafRefTargetPathString());
131 LOG.debug("Parsed leafref path2: {}", leafRefCtx3.getLeafRefTargetPath());
132 LOG.debug("Absolute leafref path2: {}", leafRefCtx3.getAbsoluteLeafRefTargetPath());
136 public void buildLeafRefContextTreeTest4() {
137 final QName q9 = QName.create(tst, "odl-project");
138 final QName q10 = QName.create(tst, "project");
139 final QName q11 = QName.create(tst, "name");
141 final LeafRefContext leafRefCtx4 = rootLeafRefContext.getReferencedChildByName(q9)
142 .getReferencedChildByName(q10).getReferencedChildByName(q11);
144 assertNotNull(leafRefCtx4);
145 assertTrue(leafRefCtx4.isReferenced());
146 assertEquals(6, leafRefCtx4.getAllReferencedByLeafRefCtxs().size());
151 public void leafRefContextUtilsTest() {
152 final QName q1 = QName.create(tst, "odl-contributor");
153 final QName q2 = QName.create(tst, "contributor");
154 final QName q3 = QName.create(tst, "odl-project-name");
156 final LeafRefContext found = rootLeafRefContext.getLeafRefReferencingContext(Absolute.of(q1, q2, q3));
157 assertNotNull(found);
158 assertTrue(found.isReferencing());
159 assertNotNull(found.getLeafRefTargetPath());
160 assertEquals(rootLeafRefContext
161 .getReferencingChildByName(q1).getReferencingChildByName(q2).getReferencingChildByName(q3), found);
165 public void leafRefContextUtilsTest2() {
166 final QName q1 = QName.create(tst, "odl-project");
167 final QName q2 = QName.create(tst, "project");
168 final QName q3 = QName.create(tst, "name");
170 final Absolute node = Absolute.of(q1, q2, q3);
171 LeafRefContext found = rootLeafRefContext.getLeafRefReferencingContext(node);
174 found = rootLeafRefContext.getLeafRefReferencedByContext(node);
176 assertNotNull(found);
177 assertTrue(found.isReferenced());
178 assertFalse(found.getAllReferencedByLeafRefCtxs().isEmpty());
179 assertEquals(6, found.getAllReferencedByLeafRefCtxs().size());
180 assertEquals(rootLeafRefContext
181 .getReferencedChildByName(q1).getReferencedChildByName(q2).getReferencedChildByName(q3), found);
185 public void leafRefContextUtilsTest3() {
186 final QName q16 = QName.create(tst, "con1");
187 final Absolute con1 = Absolute.of(q16);
189 final List<LeafRefContext> allLeafRefChilds = rootLeafRefContext.findAllLeafRefChilds(con1);
191 assertNotNull(allLeafRefChilds);
192 assertFalse(allLeafRefChilds.isEmpty());
193 assertEquals(4, allLeafRefChilds.size());
195 List<LeafRefContext> allChildsReferencedByLeafRef = rootLeafRefContext.findAllChildsReferencedByLeafRef(
196 Absolute.of(QName.create(tst, "odl-contributor")));
198 assertNotNull(allChildsReferencedByLeafRef);
199 assertFalse(allChildsReferencedByLeafRef.isEmpty());
200 assertEquals(1, allChildsReferencedByLeafRef.size());
202 allChildsReferencedByLeafRef = rootLeafRefContext.findAllChildsReferencedByLeafRef(con1);
204 assertNotNull(allChildsReferencedByLeafRef);
205 assertTrue(allChildsReferencedByLeafRef.isEmpty());
209 public void incorrectLeafRefPathTest() {
210 final IllegalStateException ise = assertThrows(IllegalStateException.class,
211 () -> YangParserTestUtils.parseYangResourceDirectory("/leafref-context-test/incorrect-modules"));
212 final Throwable ype = ise.getCause();
213 assertThat(ype, instanceOf(YangParserException.class));
214 final Throwable reactor = ype.getCause();
215 assertThat(reactor, instanceOf(ReactorException.class));
216 final Throwable source = reactor.getCause();
217 assertThat(source, instanceOf(SourceException.class));
218 assertThat(source.getMessage(), startsWith("token recognition error at: './' at 1:2"));