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;
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.DataNodeContainer;
26 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.Module;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
31 public class LeafRefContextTreeBuilderTest {
33 private static SchemaContext context;
34 private static Module impMod;
35 private static Module tstMod;
36 private static QNameModule imp;
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 final Set<Module> modules = context.getModules();
45 for (final Module module : modules) {
46 if (module.getName().equals("import-mod")) {
49 if (module.getName().equals("leafref-test")) {
54 imp = impMod.getQNameModule();
55 tst = tstMod.getQNameModule();
57 rootLeafRefContext = LeafRefContext.create(context);
61 public void buildLeafRefContextTreeTest1() {
63 final QName q1 = QName.create(tst, "odl-project");
64 final QName q2 = QName.create(tst, "project");
65 final QName q3 = QName.create(tst, "project-lead");
67 final LeafRefContext leafRefCtx = rootLeafRefContext.getReferencingChildByName(q1)
68 .getReferencingChildByName(q2).getReferencingChildByName(q3);
70 assertTrue(leafRefCtx.isReferencing());
71 assertNotNull(leafRefCtx.getLeafRefTargetPath());
72 assertFalse(leafRefCtx.getLeafRefTargetPath().isAbsolute());
73 assertNotNull(leafRefCtx.getAbsoluteLeafRefTargetPath());
74 assertTrue(leafRefCtx.getAbsoluteLeafRefTargetPath().isAbsolute());
77 System.out.println("******* Test 1 ************");
78 System.out.println("Original definition string:");
79 System.out.println(leafRefCtx.getLeafRefTargetPathString());
80 System.out.println("Parsed leafref path:");
81 System.out.println(leafRefCtx.getLeafRefTargetPath().toString());
82 System.out.println("Absolute leafref path:");
83 System.out.println(leafRefCtx.getAbsoluteLeafRefTargetPath().toString());
87 public void buildLeafRefContextTreeTest2() {
89 final QName q1 = QName.create(tst, "odl-project");
90 final QName q2 = QName.create(tst, "project");
91 final QName q4 = QName.create(tst, "project-lead2");
93 final LeafRefContext leafRefCtx2 = rootLeafRefContext.getReferencingChildByName(q1)
94 .getReferencingChildByName(q2).getReferencingChildByName(q4);
96 assertTrue(leafRefCtx2.isReferencing());
97 assertNotNull(leafRefCtx2.getLeafRefTargetPath());
98 assertTrue(leafRefCtx2.getLeafRefTargetPath().isAbsolute());
99 assertNotNull(leafRefCtx2.getAbsoluteLeafRefTargetPath());
100 assertTrue(leafRefCtx2.getAbsoluteLeafRefTargetPath().isAbsolute());
102 System.out.println();
103 System.out.println("******* Test 2 ************");
104 System.out.println("Original definition string2:");
105 System.out.println(leafRefCtx2.getLeafRefTargetPathString());
106 System.out.println("Parsed leafref path2:");
107 System.out.println(leafRefCtx2.getLeafRefTargetPath().toString());
108 System.out.println("Absolute leafref path2:");
109 System.out.println(leafRefCtx2.getAbsoluteLeafRefTargetPath().toString());
110 System.out.println();
115 public void buildLeafRefContextTreeXPathTest() {
116 final QName q1 = QName.create(tst, "odl-project");
117 final QName q2 = QName.create(tst, "project");
118 final QName q5 = QName.create(tst, "ch1");
119 final QName q6 = QName.create(tst, "c1");
120 final QName q7 = QName.create(tst, "ch2");
121 final QName q8 = QName.create(tst, "l1");
122 final LeafRefContext leafRefCtx3 = rootLeafRefContext.getReferencingChildByName(q1)
123 .getReferencingChildByName(q2).getReferencingChildByName(q5).getReferencingChildByName(q6)
124 .getReferencingChildByName(q7).getReferencingChildByName(q6).getReferencingChildByName(q8);
126 assertTrue(leafRefCtx3.isReferencing());
127 assertNotNull(leafRefCtx3.getLeafRefTargetPath());
128 assertFalse(leafRefCtx3.getLeafRefTargetPath().isAbsolute());
129 assertNotNull(leafRefCtx3.getAbsoluteLeafRefTargetPath());
130 assertTrue(leafRefCtx3.getAbsoluteLeafRefTargetPath().isAbsolute());
132 System.out.println();
133 System.out.println("******* Test 3 ************");
134 System.out.println("Original definition string2:");
135 System.out.println(leafRefCtx3.getLeafRefTargetPathString());
136 System.out.println("Parsed leafref path2:");
137 System.out.println(leafRefCtx3.getLeafRefTargetPath().toString());
138 System.out.println("Absolute leafref path2:");
139 System.out.println(leafRefCtx3.getAbsoluteLeafRefTargetPath().toString());
140 System.out.println();
144 public void buildLeafRefContextTreeTest4() {
145 final QName q9 = QName.create(tst, "odl-project");
146 final QName q10 = QName.create(tst, "project");
147 final QName q11 = QName.create(tst, "name");
149 final LeafRefContext leafRefCtx4 = rootLeafRefContext.getReferencedChildByName(q9)
150 .getReferencedChildByName(q10).getReferencedChildByName(q11);
152 assertNotNull(leafRefCtx4);
153 assertTrue(leafRefCtx4.isReferenced());
154 assertEquals(6, leafRefCtx4.getAllReferencedByLeafRefCtxs().size());
159 public void leafRefContextUtilsTest() {
160 final QName q1 = QName.create(tst, "odl-contributor");
161 final QName q2 = QName.create(tst, "contributor");
162 final QName q3 = QName.create(tst, "odl-project-name");
164 final LeafRefContext odlContrProjNameCtx = rootLeafRefContext.getReferencingChildByName(q1)
165 .getReferencingChildByName(q2).getReferencingChildByName(q3);
167 final DataSchemaNode odlContrProjNameNode = ((DataNodeContainer) ((DataNodeContainer) tstMod
168 .getDataChildByName(q1)).getDataChildByName(q2)).getDataChildByName(q3);
170 final LeafRefContext foundOdlContrProjNameCtx = LeafRefContextUtils.getLeafRefReferencingContext(
171 odlContrProjNameNode, rootLeafRefContext);
173 assertNotNull(foundOdlContrProjNameCtx);
174 assertTrue(foundOdlContrProjNameCtx.isReferencing());
175 assertNotNull(foundOdlContrProjNameCtx.getLeafRefTargetPath());
176 assertEquals(odlContrProjNameCtx, foundOdlContrProjNameCtx);
180 public void leafRefContextUtilsTest2() {
181 final QName q1 = QName.create(tst, "odl-project");
182 final QName q2 = QName.create(tst, "project");
183 final QName q3 = QName.create(tst, "name");
185 final LeafRefContext leafRefCtx = rootLeafRefContext.getReferencedChildByName(q1).getReferencedChildByName(q2)
186 .getReferencedChildByName(q3);
188 final DataSchemaNode odlProjNameNode = ((DataNodeContainer) ((DataNodeContainer) tstMod.getDataChildByName(q1))
189 .getDataChildByName(q2)).getDataChildByName(q3);
191 LeafRefContext foundOdlProjNameCtx = LeafRefContextUtils.getLeafRefReferencingContext(odlProjNameNode,
194 assertNull(foundOdlProjNameCtx);
196 foundOdlProjNameCtx = LeafRefContextUtils.getLeafRefReferencedByContext(odlProjNameNode, rootLeafRefContext);
198 assertNotNull(foundOdlProjNameCtx);
199 assertTrue(foundOdlProjNameCtx.isReferenced());
200 assertFalse(foundOdlProjNameCtx.getAllReferencedByLeafRefCtxs().isEmpty());
201 assertEquals(6, foundOdlProjNameCtx.getAllReferencedByLeafRefCtxs().size());
202 assertEquals(leafRefCtx, foundOdlProjNameCtx);
206 public void leafRefContextUtilsTest3() {
207 final QName q16 = QName.create(tst, "con1");
208 final DataSchemaNode con1 = tstMod.getDataChildByName(q16);
209 final List<LeafRefContext> allLeafRefChilds = LeafRefContextUtils
210 .findAllLeafRefChilds(con1, rootLeafRefContext);
212 assertNotNull(allLeafRefChilds);
213 assertFalse(allLeafRefChilds.isEmpty());
214 assertEquals(4, allLeafRefChilds.size());
216 final QName q17 = QName.create(tst, "odl-contributor");
217 final DataSchemaNode odlContributorNode = tstMod.getDataChildByName(q17);
218 List<LeafRefContext> allChildsReferencedByLeafRef = LeafRefContextUtils.findAllChildsReferencedByLeafRef(
219 odlContributorNode, rootLeafRefContext);
221 assertNotNull(allChildsReferencedByLeafRef);
222 assertFalse(allChildsReferencedByLeafRef.isEmpty());
223 assertEquals(1, allChildsReferencedByLeafRef.size());
225 allChildsReferencedByLeafRef = LeafRefContextUtils.findAllChildsReferencedByLeafRef(con1, rootLeafRefContext);
227 assertNotNull(allChildsReferencedByLeafRef);
228 assertTrue(allChildsReferencedByLeafRef.isEmpty());
232 @Test(expected = IllegalArgumentException.class)
234 public void incorrectLeafRefPathTest() {
235 final SchemaContext context = YangParserTestUtils.parseYangResourceDirectory(
236 "/leafref-context-test/incorrect-modules");
237 LeafRefContext.create(context);