2 * Copyright (c) 2015 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.test;
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;
17 import java.io.IOException;
18 import java.net.URISyntaxException;
19 import java.util.List;
21 import org.junit.BeforeClass;
22 import org.junit.Test;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.common.QNameModule;
25 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefContext;
26 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefContextUtils;
27 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefYangSyntaxErrorException;
28 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
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.YangSyntaxErrorException;
33 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
35 public class LeafRefContextTreeBuilderTest {
37 private static SchemaContext context;
38 private static Module impMod;
39 private static Module tstMod;
40 private static QNameModule imp;
41 private static QNameModule tst;
42 private static LeafRefContext rootLeafRefContext;
45 public static void init() throws URISyntaxException, IOException,
46 YangSyntaxErrorException, LeafRefYangSyntaxErrorException {
47 final File resourceFile = new File(
48 LeafRefContextTreeBuilderTest.class
50 "/leafref-context-test/correct-modules/leafref-test.yang")
52 final File resourceDir = resourceFile.getParentFile();
54 final YangParserImpl parser = YangParserImpl.getInstance();
55 context = parser.parseFile(resourceFile, resourceDir);
57 final Set<Module> modules = context.getModules();
58 for (final Module module : modules) {
59 if (module.getName().equals("import-mod")) {
62 if (module.getName().equals("leafref-test")) {
67 imp = impMod.getQNameModule();
68 tst = tstMod.getQNameModule();
70 rootLeafRefContext = LeafRefContext.create(context);
74 public void buildLeafRefContextTreeTest1() {
76 final QName q1 = QName.create(tst, "odl-project");
77 final QName q2 = QName.create(tst, "project");
78 final QName q3 = QName.create(tst, "project-lead");
80 final LeafRefContext leafRefCtx = rootLeafRefContext
81 .getReferencingChildByName(q1).getReferencingChildByName(q2)
82 .getReferencingChildByName(q3);
84 assertTrue(leafRefCtx.isReferencing());
85 assertNotNull(leafRefCtx.getLeafRefTargetPath());
86 assertFalse(leafRefCtx.getLeafRefTargetPath().isAbsolute());
87 assertNotNull(leafRefCtx.getAbsoluteLeafRefTargetPath());
88 assertTrue(leafRefCtx.getAbsoluteLeafRefTargetPath().isAbsolute());
91 System.out.println("******* Test 1 ************");
92 System.out.println("Original definition string:");
93 System.out.println(leafRefCtx.getLeafRefTargetPathString());
94 System.out.println("Parsed leafref path:");
95 System.out.println(leafRefCtx.getLeafRefTargetPath().toString());
96 System.out.println("Absolute leafref path:");
98 .println(leafRefCtx.getAbsoluteLeafRefTargetPath().toString());
102 public void buildLeafRefContextTreeTest2() {
104 final QName q1 = QName.create(tst, "odl-project");
105 final QName q2 = QName.create(tst, "project");
106 final QName q4 = QName.create(tst, "project-lead2");
108 final LeafRefContext leafRefCtx2 = rootLeafRefContext
109 .getReferencingChildByName(q1).getReferencingChildByName(q2)
110 .getReferencingChildByName(q4);
112 assertTrue(leafRefCtx2.isReferencing());
113 assertNotNull(leafRefCtx2.getLeafRefTargetPath());
114 assertTrue(leafRefCtx2.getLeafRefTargetPath().isAbsolute());
115 assertNotNull(leafRefCtx2.getAbsoluteLeafRefTargetPath());
116 assertTrue(leafRefCtx2.getAbsoluteLeafRefTargetPath().isAbsolute());
118 System.out.println();
119 System.out.println("******* Test 2 ************");
120 System.out.println("Original definition string2:");
121 System.out.println(leafRefCtx2.getLeafRefTargetPathString());
122 System.out.println("Parsed leafref path2:");
123 System.out.println(leafRefCtx2.getLeafRefTargetPath().toString());
124 System.out.println("Absolute leafref path2:");
125 System.out.println(leafRefCtx2.getAbsoluteLeafRefTargetPath()
127 System.out.println();
132 public void buildLeafRefContextTreeXPathTest() {
133 final QName q1 = QName.create(tst, "odl-project");
134 final QName q2 = QName.create(tst, "project");
135 final QName q5 = QName.create(tst, "ch1");
136 final QName q6 = QName.create(tst, "c1");
137 final QName q7 = QName.create(tst, "ch2");
138 final QName q8 = QName.create(tst, "l1");
139 final LeafRefContext leafRefCtx3 = rootLeafRefContext
140 .getReferencingChildByName(q1).getReferencingChildByName(q2)
141 .getReferencingChildByName(q5).getReferencingChildByName(q6)
142 .getReferencingChildByName(q7).getReferencingChildByName(q6)
143 .getReferencingChildByName(q8);
145 assertTrue(leafRefCtx3.isReferencing());
146 assertNotNull(leafRefCtx3.getLeafRefTargetPath());
147 assertFalse(leafRefCtx3.getLeafRefTargetPath().isAbsolute());
148 assertNotNull(leafRefCtx3.getAbsoluteLeafRefTargetPath());
149 assertTrue(leafRefCtx3.getAbsoluteLeafRefTargetPath().isAbsolute());
151 System.out.println();
152 System.out.println("******* Test 3 ************");
153 System.out.println("Original definition string2:");
154 System.out.println(leafRefCtx3.getLeafRefTargetPathString());
155 System.out.println("Parsed leafref path2:");
156 System.out.println(leafRefCtx3.getLeafRefTargetPath().toString());
157 System.out.println("Absolute leafref path2:");
158 System.out.println(leafRefCtx3.getAbsoluteLeafRefTargetPath()
160 System.out.println();
164 public void buildLeafRefContextTreeTest4() {
165 final QName q9 = QName.create(tst, "odl-project");
166 final QName q10 = QName.create(tst, "project");
167 final QName q11 = QName.create(tst, "name");
169 final LeafRefContext leafRefCtx4 = rootLeafRefContext
170 .getReferencedChildByName(q9).getReferencedChildByName(q10)
171 .getReferencedChildByName(q11);
173 assertNotNull(leafRefCtx4);
174 assertTrue(leafRefCtx4.isReferenced());
175 assertEquals(6, leafRefCtx4.getAllReferencedByLeafRefCtxs().size());
180 public void leafRefContextUtilsTest() {
181 final QName q1 = QName.create(tst, "odl-contributor");
182 final QName q2 = QName.create(tst, "contributor");
183 final QName q3 = QName.create(tst, "odl-project-name");
185 final LeafRefContext odlContrProjNameCtx = rootLeafRefContext
186 .getReferencingChildByName(q1).getReferencingChildByName(q2)
187 .getReferencingChildByName(q3);
189 final DataSchemaNode odlContrProjNameNode = ((DataNodeContainer) ((DataNodeContainer) tstMod
190 .getDataChildByName(q1)).getDataChildByName(q2))
191 .getDataChildByName(q3);
193 final LeafRefContext foundOdlContrProjNameCtx = LeafRefContextUtils
194 .getLeafRefReferencingContext(odlContrProjNameNode,
197 assertNotNull(foundOdlContrProjNameCtx);
198 assertTrue(foundOdlContrProjNameCtx.isReferencing());
199 assertNotNull(foundOdlContrProjNameCtx.getLeafRefTargetPath());
200 assertEquals(odlContrProjNameCtx, foundOdlContrProjNameCtx);
204 public void leafRefContextUtilsTest2() {
205 final QName q1 = QName.create(tst, "odl-project");
206 final QName q2 = QName.create(tst, "project");
207 final QName q3 = QName.create(tst, "name");
209 final LeafRefContext leafRefCtx = rootLeafRefContext
210 .getReferencedChildByName(q1).getReferencedChildByName(q2)
211 .getReferencedChildByName(q3);
213 final DataSchemaNode odlProjNameNode = ((DataNodeContainer) ((DataNodeContainer) tstMod
214 .getDataChildByName(q1)).getDataChildByName(q2))
215 .getDataChildByName(q3);
217 LeafRefContext foundOdlProjNameCtx = LeafRefContextUtils
218 .getLeafRefReferencingContext(odlProjNameNode,
221 assertNull(foundOdlProjNameCtx);
223 foundOdlProjNameCtx = LeafRefContextUtils
224 .getLeafRefReferencedByContext(odlProjNameNode,
227 assertNotNull(foundOdlProjNameCtx);
228 assertTrue(foundOdlProjNameCtx.isReferenced());
229 assertFalse(foundOdlProjNameCtx.getAllReferencedByLeafRefCtxs()
231 assertEquals(6, foundOdlProjNameCtx.getAllReferencedByLeafRefCtxs()
233 assertEquals(leafRefCtx, foundOdlProjNameCtx);
237 public void leafRefContextUtilsTest3() {
238 final QName q16 = QName.create(tst, "con1");
239 final DataSchemaNode con1 = tstMod.getDataChildByName(q16);
240 final List<LeafRefContext> allLeafRefChilds = LeafRefContextUtils
241 .findAllLeafRefChilds(con1, rootLeafRefContext);
243 assertNotNull(allLeafRefChilds);
244 assertFalse(allLeafRefChilds.isEmpty());
245 assertEquals(4, allLeafRefChilds.size());
247 final QName q17 = QName.create(tst, "odl-contributor");
248 final DataSchemaNode odlContributorNode = tstMod.getDataChildByName(q17);
249 List<LeafRefContext> allChildsReferencedByLeafRef = LeafRefContextUtils
250 .findAllChildsReferencedByLeafRef(odlContributorNode,
253 assertNotNull(allChildsReferencedByLeafRef);
254 assertFalse(allChildsReferencedByLeafRef.isEmpty());
255 assertEquals(1, allChildsReferencedByLeafRef.size());
257 allChildsReferencedByLeafRef = LeafRefContextUtils
258 .findAllChildsReferencedByLeafRef(con1, rootLeafRefContext);
260 assertNotNull(allChildsReferencedByLeafRef);
261 assertTrue(allChildsReferencedByLeafRef.isEmpty());
265 @Test(expected = RuntimeException.class)
266 public void incorrectLeafRefPathTest() throws URISyntaxException,
267 IOException, YangSyntaxErrorException,
268 LeafRefYangSyntaxErrorException {
269 final File resourceFile = new File(getClass().getResource(
270 "/leafref-context-test/incorrect-modules/leafref-test.yang")
272 final File resourceDir = resourceFile.getParentFile();
274 final YangParserImpl parser = YangParserImpl.getInstance();
275 final SchemaContext context = parser.parseFile(resourceFile, resourceDir);
277 LeafRefContext.create(context);