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.AfterClass;
19 import org.junit.BeforeClass;
20 import org.junit.Ignore;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.common.QNameModule;
24 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefContext;
25 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefContextUtils;
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;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 public class LeafRefContextTreeBuilderTest {
34 private static final Logger LOG = LoggerFactory.getLogger(LeafRefContextTreeBuilderTest.class);
36 private static SchemaContext context;
37 private static Module tstMod;
38 private static QNameModule tst;
39 private static LeafRefContext rootLeafRefContext;
42 public static void init() {
43 context = YangParserTestUtils.parseYangResourceDirectory("/leafref-context-test/correct-modules");
45 final Set<Module> modules = context.getModules();
46 for (final Module module : modules) {
47 if (module.getName().equals("leafref-test")) {
52 tst = tstMod.getQNameModule();
54 rootLeafRefContext = LeafRefContext.create(context);
58 public static void cleanup() {
62 rootLeafRefContext = null;
66 public void buildLeafRefContextTreeTest1() {
68 final QName q1 = QName.create(tst, "odl-project");
69 final QName q2 = QName.create(tst, "project");
70 final QName q3 = QName.create(tst, "project-lead");
72 final LeafRefContext leafRefCtx = rootLeafRefContext.getReferencingChildByName(q1)
73 .getReferencingChildByName(q2).getReferencingChildByName(q3);
75 assertTrue(leafRefCtx.isReferencing());
76 assertNotNull(leafRefCtx.getLeafRefTargetPath());
77 assertFalse(leafRefCtx.getLeafRefTargetPath().isAbsolute());
78 assertNotNull(leafRefCtx.getAbsoluteLeafRefTargetPath());
79 assertTrue(leafRefCtx.getAbsoluteLeafRefTargetPath().isAbsolute());
81 LOG.debug("******* Test 1 ************");
82 LOG.debug("Original definition string: {}", leafRefCtx.getLeafRefTargetPathString());
83 LOG.debug("Parsed leafref path: {}", leafRefCtx.getLeafRefTargetPath());
84 LOG.debug("Absolute leafref path: {}", leafRefCtx.getAbsoluteLeafRefTargetPath());
88 public void buildLeafRefContextTreeTest2() {
90 final QName q1 = QName.create(tst, "odl-project");
91 final QName q2 = QName.create(tst, "project");
92 final QName q4 = QName.create(tst, "project-lead2");
94 final LeafRefContext leafRefCtx2 = rootLeafRefContext.getReferencingChildByName(q1)
95 .getReferencingChildByName(q2).getReferencingChildByName(q4);
97 assertTrue(leafRefCtx2.isReferencing());
98 assertNotNull(leafRefCtx2.getLeafRefTargetPath());
99 assertTrue(leafRefCtx2.getLeafRefTargetPath().isAbsolute());
100 assertNotNull(leafRefCtx2.getAbsoluteLeafRefTargetPath());
101 assertTrue(leafRefCtx2.getAbsoluteLeafRefTargetPath().isAbsolute());
103 LOG.debug("******* Test 2 ************");
104 LOG.debug("Original definition string2: {}", leafRefCtx2.getLeafRefTargetPathString());
105 LOG.debug("Parsed leafref path2: {}", leafRefCtx2.getLeafRefTargetPath());
106 LOG.debug("Absolute leafref path2: {}", leafRefCtx2.getAbsoluteLeafRefTargetPath());
110 public void buildLeafRefContextTreeXPathTest() {
111 final QName q1 = QName.create(tst, "odl-project");
112 final QName q2 = QName.create(tst, "project");
113 final QName q5 = QName.create(tst, "ch1");
114 final QName q6 = QName.create(tst, "c1");
115 final QName q7 = QName.create(tst, "ch2");
116 final QName q8 = QName.create(tst, "l1");
117 final LeafRefContext leafRefCtx3 = rootLeafRefContext.getReferencingChildByName(q1)
118 .getReferencingChildByName(q2).getReferencingChildByName(q5).getReferencingChildByName(q6)
119 .getReferencingChildByName(q7).getReferencingChildByName(q6).getReferencingChildByName(q8);
121 assertTrue(leafRefCtx3.isReferencing());
122 assertNotNull(leafRefCtx3.getLeafRefTargetPath());
123 assertFalse(leafRefCtx3.getLeafRefTargetPath().isAbsolute());
124 assertNotNull(leafRefCtx3.getAbsoluteLeafRefTargetPath());
125 assertTrue(leafRefCtx3.getAbsoluteLeafRefTargetPath().isAbsolute());
127 LOG.debug("******* Test 3 ************");
128 LOG.debug("Original definition string2: {}", leafRefCtx3.getLeafRefTargetPathString());
129 LOG.debug("Parsed leafref path2: {}", leafRefCtx3.getLeafRefTargetPath());
130 LOG.debug("Absolute leafref path2: {}", leafRefCtx3.getAbsoluteLeafRefTargetPath());
134 public void buildLeafRefContextTreeTest4() {
135 final QName q9 = QName.create(tst, "odl-project");
136 final QName q10 = QName.create(tst, "project");
137 final QName q11 = QName.create(tst, "name");
139 final LeafRefContext leafRefCtx4 = rootLeafRefContext.getReferencedChildByName(q9)
140 .getReferencedChildByName(q10).getReferencedChildByName(q11);
142 assertNotNull(leafRefCtx4);
143 assertTrue(leafRefCtx4.isReferenced());
144 assertEquals(6, leafRefCtx4.getAllReferencedByLeafRefCtxs().size());
149 public void leafRefContextUtilsTest() {
150 final QName q1 = QName.create(tst, "odl-contributor");
151 final QName q2 = QName.create(tst, "contributor");
152 final QName q3 = QName.create(tst, "odl-project-name");
154 final LeafRefContext odlContrProjNameCtx = rootLeafRefContext.getReferencingChildByName(q1)
155 .getReferencingChildByName(q2).getReferencingChildByName(q3);
157 final DataSchemaNode odlContrProjNameNode = tstMod.findDataChildByName(q1, q2, q3).get();
159 final LeafRefContext foundOdlContrProjNameCtx = LeafRefContextUtils.getLeafRefReferencingContext(
160 odlContrProjNameNode, rootLeafRefContext);
162 assertNotNull(foundOdlContrProjNameCtx);
163 assertTrue(foundOdlContrProjNameCtx.isReferencing());
164 assertNotNull(foundOdlContrProjNameCtx.getLeafRefTargetPath());
165 assertEquals(odlContrProjNameCtx, foundOdlContrProjNameCtx);
169 public void leafRefContextUtilsTest2() {
170 final QName q1 = QName.create(tst, "odl-project");
171 final QName q2 = QName.create(tst, "project");
172 final QName q3 = QName.create(tst, "name");
174 final LeafRefContext leafRefCtx = rootLeafRefContext.getReferencedChildByName(q1).getReferencedChildByName(q2)
175 .getReferencedChildByName(q3);
177 final DataSchemaNode odlProjNameNode = tstMod.findDataChildByName(q1, q2, q3).get();
179 LeafRefContext foundOdlProjNameCtx = LeafRefContextUtils.getLeafRefReferencingContext(odlProjNameNode,
182 assertNull(foundOdlProjNameCtx);
184 foundOdlProjNameCtx = LeafRefContextUtils.getLeafRefReferencedByContext(odlProjNameNode, rootLeafRefContext);
186 assertNotNull(foundOdlProjNameCtx);
187 assertTrue(foundOdlProjNameCtx.isReferenced());
188 assertFalse(foundOdlProjNameCtx.getAllReferencedByLeafRefCtxs().isEmpty());
189 assertEquals(6, foundOdlProjNameCtx.getAllReferencedByLeafRefCtxs().size());
190 assertEquals(leafRefCtx, foundOdlProjNameCtx);
194 public void leafRefContextUtilsTest3() {
195 final QName q16 = QName.create(tst, "con1");
196 final DataSchemaNode con1 = tstMod.findDataChildByName(q16).get();
197 final List<LeafRefContext> allLeafRefChilds = LeafRefContextUtils.findAllLeafRefChilds(con1,
200 assertNotNull(allLeafRefChilds);
201 assertFalse(allLeafRefChilds.isEmpty());
202 assertEquals(4, allLeafRefChilds.size());
204 final QName q17 = QName.create(tst, "odl-contributor");
205 final DataSchemaNode odlContributorNode = tstMod.findDataChildByName(q17).get();
206 List<LeafRefContext> allChildsReferencedByLeafRef = LeafRefContextUtils.findAllChildsReferencedByLeafRef(
207 odlContributorNode, rootLeafRefContext);
209 assertNotNull(allChildsReferencedByLeafRef);
210 assertFalse(allChildsReferencedByLeafRef.isEmpty());
211 assertEquals(1, allChildsReferencedByLeafRef.size());
213 allChildsReferencedByLeafRef = LeafRefContextUtils.findAllChildsReferencedByLeafRef(con1, rootLeafRefContext);
215 assertNotNull(allChildsReferencedByLeafRef);
216 assertTrue(allChildsReferencedByLeafRef.isEmpty());
219 @Test(expected = IllegalArgumentException.class)
221 public void incorrectLeafRefPathTest() {
222 LeafRefContext.create(
223 YangParserTestUtils.parseYangResourceDirectory("/leafref-context-test/incorrect-modules"));