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.io.FileNotFoundException;
17 import java.net.URISyntaxException;
18 import java.util.Arrays;
19 import java.util.List;
21 import org.junit.BeforeClass;
22 import org.junit.Ignore;
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.data.impl.leafref.LeafRefContext;
27 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefContextUtils;
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.parser.spi.meta.ReactorException;
33 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
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, FileNotFoundException, ReactorException {
46 final File resourceFile = new File(LeafRefContextTreeBuilderTest.class.getResource(
47 "/leafref-context-test/correct-modules/leafref-test.yang").toURI());
48 final File resourceDir = resourceFile.getParentFile();
50 context = YangParserTestUtils.parseYangSources(Arrays.asList(resourceDir.listFiles()));
52 final Set<Module> modules = context.getModules();
53 for (final Module module : modules) {
54 if (module.getName().equals("import-mod")) {
57 if (module.getName().equals("leafref-test")) {
62 imp = impMod.getQNameModule();
63 tst = tstMod.getQNameModule();
65 rootLeafRefContext = LeafRefContext.create(context);
69 public void buildLeafRefContextTreeTest1() {
71 final QName q1 = QName.create(tst, "odl-project");
72 final QName q2 = QName.create(tst, "project");
73 final QName q3 = QName.create(tst, "project-lead");
75 final LeafRefContext leafRefCtx = rootLeafRefContext.getReferencingChildByName(q1)
76 .getReferencingChildByName(q2).getReferencingChildByName(q3);
78 assertTrue(leafRefCtx.isReferencing());
79 assertNotNull(leafRefCtx.getLeafRefTargetPath());
80 assertFalse(leafRefCtx.getLeafRefTargetPath().isAbsolute());
81 assertNotNull(leafRefCtx.getAbsoluteLeafRefTargetPath());
82 assertTrue(leafRefCtx.getAbsoluteLeafRefTargetPath().isAbsolute());
85 System.out.println("******* Test 1 ************");
86 System.out.println("Original definition string:");
87 System.out.println(leafRefCtx.getLeafRefTargetPathString());
88 System.out.println("Parsed leafref path:");
89 System.out.println(leafRefCtx.getLeafRefTargetPath().toString());
90 System.out.println("Absolute leafref path:");
91 System.out.println(leafRefCtx.getAbsoluteLeafRefTargetPath().toString());
95 public void buildLeafRefContextTreeTest2() {
97 final QName q1 = QName.create(tst, "odl-project");
98 final QName q2 = QName.create(tst, "project");
99 final QName q4 = QName.create(tst, "project-lead2");
101 final LeafRefContext leafRefCtx2 = rootLeafRefContext.getReferencingChildByName(q1)
102 .getReferencingChildByName(q2).getReferencingChildByName(q4);
104 assertTrue(leafRefCtx2.isReferencing());
105 assertNotNull(leafRefCtx2.getLeafRefTargetPath());
106 assertTrue(leafRefCtx2.getLeafRefTargetPath().isAbsolute());
107 assertNotNull(leafRefCtx2.getAbsoluteLeafRefTargetPath());
108 assertTrue(leafRefCtx2.getAbsoluteLeafRefTargetPath().isAbsolute());
110 System.out.println();
111 System.out.println("******* Test 2 ************");
112 System.out.println("Original definition string2:");
113 System.out.println(leafRefCtx2.getLeafRefTargetPathString());
114 System.out.println("Parsed leafref path2:");
115 System.out.println(leafRefCtx2.getLeafRefTargetPath().toString());
116 System.out.println("Absolute leafref path2:");
117 System.out.println(leafRefCtx2.getAbsoluteLeafRefTargetPath().toString());
118 System.out.println();
123 public void buildLeafRefContextTreeXPathTest() {
124 final QName q1 = QName.create(tst, "odl-project");
125 final QName q2 = QName.create(tst, "project");
126 final QName q5 = QName.create(tst, "ch1");
127 final QName q6 = QName.create(tst, "c1");
128 final QName q7 = QName.create(tst, "ch2");
129 final QName q8 = QName.create(tst, "l1");
130 final LeafRefContext leafRefCtx3 = rootLeafRefContext.getReferencingChildByName(q1)
131 .getReferencingChildByName(q2).getReferencingChildByName(q5).getReferencingChildByName(q6)
132 .getReferencingChildByName(q7).getReferencingChildByName(q6).getReferencingChildByName(q8);
134 assertTrue(leafRefCtx3.isReferencing());
135 assertNotNull(leafRefCtx3.getLeafRefTargetPath());
136 assertFalse(leafRefCtx3.getLeafRefTargetPath().isAbsolute());
137 assertNotNull(leafRefCtx3.getAbsoluteLeafRefTargetPath());
138 assertTrue(leafRefCtx3.getAbsoluteLeafRefTargetPath().isAbsolute());
140 System.out.println();
141 System.out.println("******* Test 3 ************");
142 System.out.println("Original definition string2:");
143 System.out.println(leafRefCtx3.getLeafRefTargetPathString());
144 System.out.println("Parsed leafref path2:");
145 System.out.println(leafRefCtx3.getLeafRefTargetPath().toString());
146 System.out.println("Absolute leafref path2:");
147 System.out.println(leafRefCtx3.getAbsoluteLeafRefTargetPath().toString());
148 System.out.println();
152 public void buildLeafRefContextTreeTest4() {
153 final QName q9 = QName.create(tst, "odl-project");
154 final QName q10 = QName.create(tst, "project");
155 final QName q11 = QName.create(tst, "name");
157 final LeafRefContext leafRefCtx4 = rootLeafRefContext.getReferencedChildByName(q9)
158 .getReferencedChildByName(q10).getReferencedChildByName(q11);
160 assertNotNull(leafRefCtx4);
161 assertTrue(leafRefCtx4.isReferenced());
162 assertEquals(6, leafRefCtx4.getAllReferencedByLeafRefCtxs().size());
167 public void leafRefContextUtilsTest() {
168 final QName q1 = QName.create(tst, "odl-contributor");
169 final QName q2 = QName.create(tst, "contributor");
170 final QName q3 = QName.create(tst, "odl-project-name");
172 final LeafRefContext odlContrProjNameCtx = rootLeafRefContext.getReferencingChildByName(q1)
173 .getReferencingChildByName(q2).getReferencingChildByName(q3);
175 final DataSchemaNode odlContrProjNameNode = ((DataNodeContainer) ((DataNodeContainer) tstMod
176 .getDataChildByName(q1)).getDataChildByName(q2)).getDataChildByName(q3);
178 final LeafRefContext foundOdlContrProjNameCtx = LeafRefContextUtils.getLeafRefReferencingContext(
179 odlContrProjNameNode, rootLeafRefContext);
181 assertNotNull(foundOdlContrProjNameCtx);
182 assertTrue(foundOdlContrProjNameCtx.isReferencing());
183 assertNotNull(foundOdlContrProjNameCtx.getLeafRefTargetPath());
184 assertEquals(odlContrProjNameCtx, foundOdlContrProjNameCtx);
188 public void leafRefContextUtilsTest2() {
189 final QName q1 = QName.create(tst, "odl-project");
190 final QName q2 = QName.create(tst, "project");
191 final QName q3 = QName.create(tst, "name");
193 final LeafRefContext leafRefCtx = rootLeafRefContext.getReferencedChildByName(q1).getReferencedChildByName(q2)
194 .getReferencedChildByName(q3);
196 final DataSchemaNode odlProjNameNode = ((DataNodeContainer) ((DataNodeContainer) tstMod.getDataChildByName(q1))
197 .getDataChildByName(q2)).getDataChildByName(q3);
199 LeafRefContext foundOdlProjNameCtx = LeafRefContextUtils.getLeafRefReferencingContext(odlProjNameNode,
202 assertNull(foundOdlProjNameCtx);
204 foundOdlProjNameCtx = LeafRefContextUtils.getLeafRefReferencedByContext(odlProjNameNode, rootLeafRefContext);
206 assertNotNull(foundOdlProjNameCtx);
207 assertTrue(foundOdlProjNameCtx.isReferenced());
208 assertFalse(foundOdlProjNameCtx.getAllReferencedByLeafRefCtxs().isEmpty());
209 assertEquals(6, foundOdlProjNameCtx.getAllReferencedByLeafRefCtxs().size());
210 assertEquals(leafRefCtx, foundOdlProjNameCtx);
214 public void leafRefContextUtilsTest3() {
215 final QName q16 = QName.create(tst, "con1");
216 final DataSchemaNode con1 = tstMod.getDataChildByName(q16);
217 final List<LeafRefContext> allLeafRefChilds = LeafRefContextUtils
218 .findAllLeafRefChilds(con1, rootLeafRefContext);
220 assertNotNull(allLeafRefChilds);
221 assertFalse(allLeafRefChilds.isEmpty());
222 assertEquals(4, allLeafRefChilds.size());
224 final QName q17 = QName.create(tst, "odl-contributor");
225 final DataSchemaNode odlContributorNode = tstMod.getDataChildByName(q17);
226 List<LeafRefContext> allChildsReferencedByLeafRef = LeafRefContextUtils.findAllChildsReferencedByLeafRef(
227 odlContributorNode, rootLeafRefContext);
229 assertNotNull(allChildsReferencedByLeafRef);
230 assertFalse(allChildsReferencedByLeafRef.isEmpty());
231 assertEquals(1, allChildsReferencedByLeafRef.size());
233 allChildsReferencedByLeafRef = LeafRefContextUtils.findAllChildsReferencedByLeafRef(con1, rootLeafRefContext);
235 assertNotNull(allChildsReferencedByLeafRef);
236 assertTrue(allChildsReferencedByLeafRef.isEmpty());
240 @Test(expected = IllegalArgumentException.class)
242 public void incorrectLeafRefPathTest() throws URISyntaxException, FileNotFoundException, ReactorException {
243 final File resourceFile = new File(getClass().getResource(
244 "/leafref-context-test/incorrect-modules/leafref-test.yang").toURI());
245 final File resourceDir = resourceFile.getParentFile();
247 final SchemaContext context = YangParserTestUtils.parseYangSources(Arrays.asList(resourceDir.listFiles()));
249 LeafRefContext.create(context);