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.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertTrue;
16 import java.util.List;
17 import org.junit.AfterClass;
18 import org.junit.BeforeClass;
19 import org.junit.Ignore;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.common.QNameModule;
23 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefContext;
24 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefContextUtils;
25 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.Module;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 public class LeafRefContextTreeBuilderTest {
33 private static final Logger LOG = LoggerFactory.getLogger(LeafRefContextTreeBuilderTest.class);
35 private static SchemaContext context;
36 private static Module tstMod;
37 private static QNameModule tst;
38 private static LeafRefContext rootLeafRefContext;
41 public static void init() {
42 context = YangParserTestUtils.parseYangResourceDirectory("/leafref-context-test/correct-modules");
44 for (final Module module : context.getModules()) {
45 if (module.getName().equals("leafref-test")) {
50 tst = tstMod.getQNameModule();
52 rootLeafRefContext = LeafRefContext.create(context);
56 public static void cleanup() {
60 rootLeafRefContext = null;
64 public void buildLeafRefContextTreeTest1() {
66 final QName q1 = QName.create(tst, "odl-project");
67 final QName q2 = QName.create(tst, "project");
68 final QName q3 = QName.create(tst, "project-lead");
70 final LeafRefContext leafRefCtx = rootLeafRefContext.getReferencingChildByName(q1)
71 .getReferencingChildByName(q2).getReferencingChildByName(q3);
73 assertTrue(leafRefCtx.isReferencing());
74 assertNotNull(leafRefCtx.getLeafRefTargetPath());
75 assertFalse(leafRefCtx.getLeafRefTargetPath().isAbsolute());
76 assertNotNull(leafRefCtx.getAbsoluteLeafRefTargetPath());
77 assertTrue(leafRefCtx.getAbsoluteLeafRefTargetPath().isAbsolute());
79 LOG.debug("******* Test 1 ************");
80 LOG.debug("Original definition string: {}", leafRefCtx.getLeafRefTargetPathString());
81 LOG.debug("Parsed leafref path: {}", leafRefCtx.getLeafRefTargetPath());
82 LOG.debug("Absolute leafref path: {}", leafRefCtx.getAbsoluteLeafRefTargetPath());
86 public void buildLeafRefContextTreeTest2() {
88 final QName q1 = QName.create(tst, "odl-project");
89 final QName q2 = QName.create(tst, "project");
90 final QName q4 = QName.create(tst, "project-lead2");
92 final LeafRefContext leafRefCtx2 = rootLeafRefContext.getReferencingChildByName(q1)
93 .getReferencingChildByName(q2).getReferencingChildByName(q4);
95 assertTrue(leafRefCtx2.isReferencing());
96 assertNotNull(leafRefCtx2.getLeafRefTargetPath());
97 assertTrue(leafRefCtx2.getLeafRefTargetPath().isAbsolute());
98 assertNotNull(leafRefCtx2.getAbsoluteLeafRefTargetPath());
99 assertTrue(leafRefCtx2.getAbsoluteLeafRefTargetPath().isAbsolute());
101 LOG.debug("******* Test 2 ************");
102 LOG.debug("Original definition string2: {}", leafRefCtx2.getLeafRefTargetPathString());
103 LOG.debug("Parsed leafref path2: {}", leafRefCtx2.getLeafRefTargetPath());
104 LOG.debug("Absolute leafref path2: {}", leafRefCtx2.getAbsoluteLeafRefTargetPath());
108 public void buildLeafRefContextTreeXPathTest() {
109 final QName q1 = QName.create(tst, "odl-project");
110 final QName q2 = QName.create(tst, "project");
111 final QName q5 = QName.create(tst, "ch1");
112 final QName q6 = QName.create(tst, "c1");
113 final QName q7 = QName.create(tst, "ch2");
114 final QName q8 = QName.create(tst, "l1");
115 final LeafRefContext leafRefCtx3 = rootLeafRefContext.getReferencingChildByName(q1)
116 .getReferencingChildByName(q2).getReferencingChildByName(q5).getReferencingChildByName(q6)
117 .getReferencingChildByName(q7).getReferencingChildByName(q6).getReferencingChildByName(q8);
119 assertTrue(leafRefCtx3.isReferencing());
120 assertNotNull(leafRefCtx3.getLeafRefTargetPath());
121 assertFalse(leafRefCtx3.getLeafRefTargetPath().isAbsolute());
122 assertNotNull(leafRefCtx3.getAbsoluteLeafRefTargetPath());
123 assertTrue(leafRefCtx3.getAbsoluteLeafRefTargetPath().isAbsolute());
125 LOG.debug("******* Test 3 ************");
126 LOG.debug("Original definition string2: {}", leafRefCtx3.getLeafRefTargetPathString());
127 LOG.debug("Parsed leafref path2: {}", leafRefCtx3.getLeafRefTargetPath());
128 LOG.debug("Absolute leafref path2: {}", leafRefCtx3.getAbsoluteLeafRefTargetPath());
132 public void buildLeafRefContextTreeTest4() {
133 final QName q9 = QName.create(tst, "odl-project");
134 final QName q10 = QName.create(tst, "project");
135 final QName q11 = QName.create(tst, "name");
137 final LeafRefContext leafRefCtx4 = rootLeafRefContext.getReferencedChildByName(q9)
138 .getReferencedChildByName(q10).getReferencedChildByName(q11);
140 assertNotNull(leafRefCtx4);
141 assertTrue(leafRefCtx4.isReferenced());
142 assertEquals(6, leafRefCtx4.getAllReferencedByLeafRefCtxs().size());
147 public void leafRefContextUtilsTest() {
148 final QName q1 = QName.create(tst, "odl-contributor");
149 final QName q2 = QName.create(tst, "contributor");
150 final QName q3 = QName.create(tst, "odl-project-name");
152 final LeafRefContext odlContrProjNameCtx = rootLeafRefContext.getReferencingChildByName(q1)
153 .getReferencingChildByName(q2).getReferencingChildByName(q3);
155 final DataSchemaNode odlContrProjNameNode = tstMod.findDataChildByName(q1, q2, q3).get();
157 final LeafRefContext foundOdlContrProjNameCtx = LeafRefContextUtils.getLeafRefReferencingContext(
158 odlContrProjNameNode, rootLeafRefContext);
160 assertNotNull(foundOdlContrProjNameCtx);
161 assertTrue(foundOdlContrProjNameCtx.isReferencing());
162 assertNotNull(foundOdlContrProjNameCtx.getLeafRefTargetPath());
163 assertEquals(odlContrProjNameCtx, foundOdlContrProjNameCtx);
167 public void leafRefContextUtilsTest2() {
168 final QName q1 = QName.create(tst, "odl-project");
169 final QName q2 = QName.create(tst, "project");
170 final QName q3 = QName.create(tst, "name");
172 final LeafRefContext leafRefCtx = rootLeafRefContext.getReferencedChildByName(q1).getReferencedChildByName(q2)
173 .getReferencedChildByName(q3);
175 final DataSchemaNode odlProjNameNode = tstMod.findDataChildByName(q1, q2, q3).get();
177 LeafRefContext foundOdlProjNameCtx = LeafRefContextUtils.getLeafRefReferencingContext(odlProjNameNode,
180 assertNull(foundOdlProjNameCtx);
182 foundOdlProjNameCtx = LeafRefContextUtils.getLeafRefReferencedByContext(odlProjNameNode, rootLeafRefContext);
184 assertNotNull(foundOdlProjNameCtx);
185 assertTrue(foundOdlProjNameCtx.isReferenced());
186 assertFalse(foundOdlProjNameCtx.getAllReferencedByLeafRefCtxs().isEmpty());
187 assertEquals(6, foundOdlProjNameCtx.getAllReferencedByLeafRefCtxs().size());
188 assertEquals(leafRefCtx, foundOdlProjNameCtx);
192 public void leafRefContextUtilsTest3() {
193 final QName q16 = QName.create(tst, "con1");
194 final DataSchemaNode con1 = tstMod.findDataChildByName(q16).get();
195 final List<LeafRefContext> allLeafRefChilds = LeafRefContextUtils.findAllLeafRefChilds(con1,
198 assertNotNull(allLeafRefChilds);
199 assertFalse(allLeafRefChilds.isEmpty());
200 assertEquals(4, allLeafRefChilds.size());
202 final QName q17 = QName.create(tst, "odl-contributor");
203 final DataSchemaNode odlContributorNode = tstMod.findDataChildByName(q17).get();
204 List<LeafRefContext> allChildsReferencedByLeafRef = LeafRefContextUtils.findAllChildsReferencedByLeafRef(
205 odlContributorNode, rootLeafRefContext);
207 assertNotNull(allChildsReferencedByLeafRef);
208 assertFalse(allChildsReferencedByLeafRef.isEmpty());
209 assertEquals(1, allChildsReferencedByLeafRef.size());
211 allChildsReferencedByLeafRef = LeafRefContextUtils.findAllChildsReferencedByLeafRef(con1, rootLeafRefContext);
213 assertNotNull(allChildsReferencedByLeafRef);
214 assertTrue(allChildsReferencedByLeafRef.isEmpty());
217 @Test(expected = IllegalArgumentException.class)
219 public void incorrectLeafRefPathTest() {
220 LeafRefContext.create(
221 YangParserTestUtils.parseYangResourceDirectory("/leafref-context-test/incorrect-modules"));