Reduce use of getChildByName()
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / leafref / context / LeafRefContextTreeBuilderTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.impl.leafref.context;
9
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;
15
16 import java.util.List;
17 import java.util.Set;
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;
33
34 public class LeafRefContextTreeBuilderTest {
35     private static final Logger LOG = LoggerFactory.getLogger(LeafRefContextTreeBuilderTest.class);
36
37     private static SchemaContext context;
38     private static Module tstMod;
39     private static QNameModule tst;
40     private static LeafRefContext rootLeafRefContext;
41
42     @BeforeClass
43     public static void init() {
44         context = YangParserTestUtils.parseYangResourceDirectory("/leafref-context-test/correct-modules");
45
46         final Set<Module> modules = context.getModules();
47         for (final Module module : modules) {
48             if (module.getName().equals("leafref-test")) {
49                 tstMod = module;
50             }
51         }
52
53         tst = tstMod.getQNameModule();
54
55         rootLeafRefContext = LeafRefContext.create(context);
56     }
57
58     @AfterClass
59     public static void cleanup() {
60         context = null;
61         tst = null;
62         tstMod = null;
63         rootLeafRefContext = null;
64     }
65
66     @Test
67     public void buildLeafRefContextTreeTest1() {
68
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");
72
73         final LeafRefContext leafRefCtx = rootLeafRefContext.getReferencingChildByName(q1)
74                 .getReferencingChildByName(q2).getReferencingChildByName(q3);
75
76         assertTrue(leafRefCtx.isReferencing());
77         assertNotNull(leafRefCtx.getLeafRefTargetPath());
78         assertFalse(leafRefCtx.getLeafRefTargetPath().isAbsolute());
79         assertNotNull(leafRefCtx.getAbsoluteLeafRefTargetPath());
80         assertTrue(leafRefCtx.getAbsoluteLeafRefTargetPath().isAbsolute());
81
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());
86     }
87
88     @Test
89     public void buildLeafRefContextTreeTest2() {
90
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");
94
95         final LeafRefContext leafRefCtx2 = rootLeafRefContext.getReferencingChildByName(q1)
96                 .getReferencingChildByName(q2).getReferencingChildByName(q4);
97
98         assertTrue(leafRefCtx2.isReferencing());
99         assertNotNull(leafRefCtx2.getLeafRefTargetPath());
100         assertTrue(leafRefCtx2.getLeafRefTargetPath().isAbsolute());
101         assertNotNull(leafRefCtx2.getAbsoluteLeafRefTargetPath());
102         assertTrue(leafRefCtx2.getAbsoluteLeafRefTargetPath().isAbsolute());
103
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());
108     }
109
110     @Test
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);
121
122         assertTrue(leafRefCtx3.isReferencing());
123         assertNotNull(leafRefCtx3.getLeafRefTargetPath());
124         assertFalse(leafRefCtx3.getLeafRefTargetPath().isAbsolute());
125         assertNotNull(leafRefCtx3.getAbsoluteLeafRefTargetPath());
126         assertTrue(leafRefCtx3.getAbsoluteLeafRefTargetPath().isAbsolute());
127
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());
132     }
133
134     @Test
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");
139
140         final LeafRefContext leafRefCtx4 = rootLeafRefContext.getReferencedChildByName(q9)
141                 .getReferencedChildByName(q10).getReferencedChildByName(q11);
142
143         assertNotNull(leafRefCtx4);
144         assertTrue(leafRefCtx4.isReferenced());
145         assertEquals(6, leafRefCtx4.getAllReferencedByLeafRefCtxs().size());
146
147     }
148
149     @Test
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");
154
155         final LeafRefContext odlContrProjNameCtx = rootLeafRefContext.getReferencingChildByName(q1)
156                 .getReferencingChildByName(q2).getReferencingChildByName(q3);
157
158         final DataSchemaNode odlContrProjNameNode = ((DataNodeContainer) ((DataNodeContainer) tstMod
159                 .getDataChildByName(q1)).getDataChildByName(q2)).getDataChildByName(q3);
160
161         final LeafRefContext foundOdlContrProjNameCtx = LeafRefContextUtils.getLeafRefReferencingContext(
162                 odlContrProjNameNode, rootLeafRefContext);
163
164         assertNotNull(foundOdlContrProjNameCtx);
165         assertTrue(foundOdlContrProjNameCtx.isReferencing());
166         assertNotNull(foundOdlContrProjNameCtx.getLeafRefTargetPath());
167         assertEquals(odlContrProjNameCtx, foundOdlContrProjNameCtx);
168     }
169
170     @Test
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");
175
176         final LeafRefContext leafRefCtx = rootLeafRefContext.getReferencedChildByName(q1).getReferencedChildByName(q2)
177                 .getReferencedChildByName(q3);
178
179         final DataSchemaNode odlProjNameNode = ((DataNodeContainer) ((DataNodeContainer) tstMod.getDataChildByName(q1))
180                 .getDataChildByName(q2)).getDataChildByName(q3);
181
182         LeafRefContext foundOdlProjNameCtx = LeafRefContextUtils.getLeafRefReferencingContext(odlProjNameNode,
183                 rootLeafRefContext);
184
185         assertNull(foundOdlProjNameCtx);
186
187         foundOdlProjNameCtx = LeafRefContextUtils.getLeafRefReferencedByContext(odlProjNameNode, rootLeafRefContext);
188
189         assertNotNull(foundOdlProjNameCtx);
190         assertTrue(foundOdlProjNameCtx.isReferenced());
191         assertFalse(foundOdlProjNameCtx.getAllReferencedByLeafRefCtxs().isEmpty());
192         assertEquals(6, foundOdlProjNameCtx.getAllReferencedByLeafRefCtxs().size());
193         assertEquals(leafRefCtx, foundOdlProjNameCtx);
194     }
195
196     @Test
197     public void leafRefContextUtilsTest3() {
198         final QName q16 = QName.create(tst, "con1");
199         final DataSchemaNode con1 = tstMod.findDataChildByName(q16).get();
200         final List<LeafRefContext> allLeafRefChilds = LeafRefContextUtils.findAllLeafRefChilds(con1,
201             rootLeafRefContext);
202
203         assertNotNull(allLeafRefChilds);
204         assertFalse(allLeafRefChilds.isEmpty());
205         assertEquals(4, allLeafRefChilds.size());
206
207         final QName q17 = QName.create(tst, "odl-contributor");
208         final DataSchemaNode odlContributorNode = tstMod.findDataChildByName(q17).get();
209         List<LeafRefContext> allChildsReferencedByLeafRef = LeafRefContextUtils.findAllChildsReferencedByLeafRef(
210                 odlContributorNode, rootLeafRefContext);
211
212         assertNotNull(allChildsReferencedByLeafRef);
213         assertFalse(allChildsReferencedByLeafRef.isEmpty());
214         assertEquals(1, allChildsReferencedByLeafRef.size());
215
216         allChildsReferencedByLeafRef = LeafRefContextUtils.findAllChildsReferencedByLeafRef(con1, rootLeafRefContext);
217
218         assertNotNull(allChildsReferencedByLeafRef);
219         assertTrue(allChildsReferencedByLeafRef.isEmpty());
220     }
221
222     @Test(expected = IllegalArgumentException.class)
223     @Ignore
224     public void incorrectLeafRefPathTest() {
225         LeafRefContext.create(
226                 YangParserTestUtils.parseYangResourceDirectory("/leafref-context-test/incorrect-modules"));
227     }
228 }