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.DataNodeContainer;
27 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.Module;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 public class LeafRefContextTreeBuilderTest {
35 private static final Logger LOG = LoggerFactory.getLogger(LeafRefContextTreeBuilderTest.class);
37 private static SchemaContext context;
38 private static Module tstMod;
39 private static QNameModule tst;
40 private static LeafRefContext rootLeafRefContext;
43 public static void init() {
44 context = YangParserTestUtils.parseYangResourceDirectory("/leafref-context-test/correct-modules");
46 final Set<Module> modules = context.getModules();
47 for (final Module module : modules) {
48 if (module.getName().equals("leafref-test")) {
53 tst = tstMod.getQNameModule();
55 rootLeafRefContext = LeafRefContext.create(context);
59 public static void cleanup() {
63 rootLeafRefContext = null;
67 public void buildLeafRefContextTreeTest1() {
69 final QName q1 = QName.create(tst, "odl-project");
70 final QName q2 = QName.create(tst, "project");
71 final QName q3 = QName.create(tst, "project-lead");
73 final LeafRefContext leafRefCtx = rootLeafRefContext.getReferencingChildByName(q1)
74 .getReferencingChildByName(q2).getReferencingChildByName(q3);
76 assertTrue(leafRefCtx.isReferencing());
77 assertNotNull(leafRefCtx.getLeafRefTargetPath());
78 assertFalse(leafRefCtx.getLeafRefTargetPath().isAbsolute());
79 assertNotNull(leafRefCtx.getAbsoluteLeafRefTargetPath());
80 assertTrue(leafRefCtx.getAbsoluteLeafRefTargetPath().isAbsolute());
82 LOG.debug("******* Test 1 ************");
83 LOG.debug("Original definition string: {}", leafRefCtx.getLeafRefTargetPathString());
84 LOG.debug("Parsed leafref path: {}", leafRefCtx.getLeafRefTargetPath());
85 LOG.debug("Absolute leafref path: {}", leafRefCtx.getAbsoluteLeafRefTargetPath());
89 public void buildLeafRefContextTreeTest2() {
91 final QName q1 = QName.create(tst, "odl-project");
92 final QName q2 = QName.create(tst, "project");
93 final QName q4 = QName.create(tst, "project-lead2");
95 final LeafRefContext leafRefCtx2 = rootLeafRefContext.getReferencingChildByName(q1)
96 .getReferencingChildByName(q2).getReferencingChildByName(q4);
98 assertTrue(leafRefCtx2.isReferencing());
99 assertNotNull(leafRefCtx2.getLeafRefTargetPath());
100 assertTrue(leafRefCtx2.getLeafRefTargetPath().isAbsolute());
101 assertNotNull(leafRefCtx2.getAbsoluteLeafRefTargetPath());
102 assertTrue(leafRefCtx2.getAbsoluteLeafRefTargetPath().isAbsolute());
104 LOG.debug("******* Test 2 ************");
105 LOG.debug("Original definition string2: {}", leafRefCtx2.getLeafRefTargetPathString());
106 LOG.debug("Parsed leafref path2: {}", leafRefCtx2.getLeafRefTargetPath());
107 LOG.debug("Absolute leafref path2: {}", leafRefCtx2.getAbsoluteLeafRefTargetPath());
111 public void buildLeafRefContextTreeXPathTest() {
112 final QName q1 = QName.create(tst, "odl-project");
113 final QName q2 = QName.create(tst, "project");
114 final QName q5 = QName.create(tst, "ch1");
115 final QName q6 = QName.create(tst, "c1");
116 final QName q7 = QName.create(tst, "ch2");
117 final QName q8 = QName.create(tst, "l1");
118 final LeafRefContext leafRefCtx3 = rootLeafRefContext.getReferencingChildByName(q1)
119 .getReferencingChildByName(q2).getReferencingChildByName(q5).getReferencingChildByName(q6)
120 .getReferencingChildByName(q7).getReferencingChildByName(q6).getReferencingChildByName(q8);
122 assertTrue(leafRefCtx3.isReferencing());
123 assertNotNull(leafRefCtx3.getLeafRefTargetPath());
124 assertFalse(leafRefCtx3.getLeafRefTargetPath().isAbsolute());
125 assertNotNull(leafRefCtx3.getAbsoluteLeafRefTargetPath());
126 assertTrue(leafRefCtx3.getAbsoluteLeafRefTargetPath().isAbsolute());
128 LOG.debug("******* Test 3 ************");
129 LOG.debug("Original definition string2: {}", leafRefCtx3.getLeafRefTargetPathString());
130 LOG.debug("Parsed leafref path2: {}", leafRefCtx3.getLeafRefTargetPath());
131 LOG.debug("Absolute leafref path2: {}", leafRefCtx3.getAbsoluteLeafRefTargetPath());
135 public void buildLeafRefContextTreeTest4() {
136 final QName q9 = QName.create(tst, "odl-project");
137 final QName q10 = QName.create(tst, "project");
138 final QName q11 = QName.create(tst, "name");
140 final LeafRefContext leafRefCtx4 = rootLeafRefContext.getReferencedChildByName(q9)
141 .getReferencedChildByName(q10).getReferencedChildByName(q11);
143 assertNotNull(leafRefCtx4);
144 assertTrue(leafRefCtx4.isReferenced());
145 assertEquals(6, leafRefCtx4.getAllReferencedByLeafRefCtxs().size());
150 public void leafRefContextUtilsTest() {
151 final QName q1 = QName.create(tst, "odl-contributor");
152 final QName q2 = QName.create(tst, "contributor");
153 final QName q3 = QName.create(tst, "odl-project-name");
155 final LeafRefContext odlContrProjNameCtx = rootLeafRefContext.getReferencingChildByName(q1)
156 .getReferencingChildByName(q2).getReferencingChildByName(q3);
158 final DataSchemaNode odlContrProjNameNode = ((DataNodeContainer) ((DataNodeContainer) tstMod
159 .getDataChildByName(q1)).getDataChildByName(q2)).getDataChildByName(q3);
161 final LeafRefContext foundOdlContrProjNameCtx = LeafRefContextUtils.getLeafRefReferencingContext(
162 odlContrProjNameNode, rootLeafRefContext);
164 assertNotNull(foundOdlContrProjNameCtx);
165 assertTrue(foundOdlContrProjNameCtx.isReferencing());
166 assertNotNull(foundOdlContrProjNameCtx.getLeafRefTargetPath());
167 assertEquals(odlContrProjNameCtx, foundOdlContrProjNameCtx);
171 public void leafRefContextUtilsTest2() {
172 final QName q1 = QName.create(tst, "odl-project");
173 final QName q2 = QName.create(tst, "project");
174 final QName q3 = QName.create(tst, "name");
176 final LeafRefContext leafRefCtx = rootLeafRefContext.getReferencedChildByName(q1).getReferencedChildByName(q2)
177 .getReferencedChildByName(q3);
179 final DataSchemaNode odlProjNameNode = ((DataNodeContainer) ((DataNodeContainer) tstMod.getDataChildByName(q1))
180 .getDataChildByName(q2)).getDataChildByName(q3);
182 LeafRefContext foundOdlProjNameCtx = LeafRefContextUtils.getLeafRefReferencingContext(odlProjNameNode,
185 assertNull(foundOdlProjNameCtx);
187 foundOdlProjNameCtx = LeafRefContextUtils.getLeafRefReferencedByContext(odlProjNameNode, rootLeafRefContext);
189 assertNotNull(foundOdlProjNameCtx);
190 assertTrue(foundOdlProjNameCtx.isReferenced());
191 assertFalse(foundOdlProjNameCtx.getAllReferencedByLeafRefCtxs().isEmpty());
192 assertEquals(6, foundOdlProjNameCtx.getAllReferencedByLeafRefCtxs().size());
193 assertEquals(leafRefCtx, foundOdlProjNameCtx);
197 public void leafRefContextUtilsTest3() {
198 final QName q16 = QName.create(tst, "con1");
199 final DataSchemaNode con1 = tstMod.getDataChildByName(q16);
200 final List<LeafRefContext> allLeafRefChilds = LeafRefContextUtils
201 .findAllLeafRefChilds(con1, rootLeafRefContext);
203 assertNotNull(allLeafRefChilds);
204 assertFalse(allLeafRefChilds.isEmpty());
205 assertEquals(4, allLeafRefChilds.size());
207 final QName q17 = QName.create(tst, "odl-contributor");
208 final DataSchemaNode odlContributorNode = tstMod.getDataChildByName(q17);
209 List<LeafRefContext> allChildsReferencedByLeafRef = LeafRefContextUtils.findAllChildsReferencedByLeafRef(
210 odlContributorNode, rootLeafRefContext);
212 assertNotNull(allChildsReferencedByLeafRef);
213 assertFalse(allChildsReferencedByLeafRef.isEmpty());
214 assertEquals(1, allChildsReferencedByLeafRef.size());
216 allChildsReferencedByLeafRef = LeafRefContextUtils.findAllChildsReferencedByLeafRef(con1, rootLeafRefContext);
218 assertNotNull(allChildsReferencedByLeafRef);
219 assertTrue(allChildsReferencedByLeafRef.isEmpty());
222 @Test(expected = IllegalArgumentException.class)
224 public void incorrectLeafRefPathTest() {
225 LeafRefContext.create(
226 YangParserTestUtils.parseYangResourceDirectory("/leafref-context-test/incorrect-modules"));