Remove deprecated Yin/YangStatementSourceImpl
[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.BeforeClass;
19 import org.junit.Ignore;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.common.QNameModule;
23 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefContext;
24 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefContextUtils;
25 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
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
31 public class LeafRefContextTreeBuilderTest {
32
33     private static SchemaContext context;
34     private static Module impMod;
35     private static Module tstMod;
36     private static QNameModule imp;
37     private static QNameModule tst;
38     private static LeafRefContext rootLeafRefContext;
39
40     @BeforeClass
41     public static void init() {
42         context = YangParserTestUtils.parseYangResourceDirectory("/leafref-context-test/correct-modules");
43
44         final Set<Module> modules = context.getModules();
45         for (final Module module : modules) {
46             if (module.getName().equals("import-mod")) {
47                 impMod = module;
48             }
49             if (module.getName().equals("leafref-test")) {
50                 tstMod = module;
51             }
52         }
53
54         imp = impMod.getQNameModule();
55         tst = tstMod.getQNameModule();
56
57         rootLeafRefContext = LeafRefContext.create(context);
58     }
59
60     @Test
61     public void buildLeafRefContextTreeTest1() {
62
63         final QName q1 = QName.create(tst, "odl-project");
64         final QName q2 = QName.create(tst, "project");
65         final QName q3 = QName.create(tst, "project-lead");
66
67         final LeafRefContext leafRefCtx = rootLeafRefContext.getReferencingChildByName(q1)
68                 .getReferencingChildByName(q2).getReferencingChildByName(q3);
69
70         assertTrue(leafRefCtx.isReferencing());
71         assertNotNull(leafRefCtx.getLeafRefTargetPath());
72         assertFalse(leafRefCtx.getLeafRefTargetPath().isAbsolute());
73         assertNotNull(leafRefCtx.getAbsoluteLeafRefTargetPath());
74         assertTrue(leafRefCtx.getAbsoluteLeafRefTargetPath().isAbsolute());
75
76         System.out.println();
77         System.out.println("******* Test 1 ************");
78         System.out.println("Original definition string:");
79         System.out.println(leafRefCtx.getLeafRefTargetPathString());
80         System.out.println("Parsed leafref path:");
81         System.out.println(leafRefCtx.getLeafRefTargetPath().toString());
82         System.out.println("Absolute leafref path:");
83         System.out.println(leafRefCtx.getAbsoluteLeafRefTargetPath().toString());
84     }
85
86     @Test
87     public void buildLeafRefContextTreeTest2() {
88
89         final QName q1 = QName.create(tst, "odl-project");
90         final QName q2 = QName.create(tst, "project");
91         final QName q4 = QName.create(tst, "project-lead2");
92
93         final LeafRefContext leafRefCtx2 = rootLeafRefContext.getReferencingChildByName(q1)
94                 .getReferencingChildByName(q2).getReferencingChildByName(q4);
95
96         assertTrue(leafRefCtx2.isReferencing());
97         assertNotNull(leafRefCtx2.getLeafRefTargetPath());
98         assertTrue(leafRefCtx2.getLeafRefTargetPath().isAbsolute());
99         assertNotNull(leafRefCtx2.getAbsoluteLeafRefTargetPath());
100         assertTrue(leafRefCtx2.getAbsoluteLeafRefTargetPath().isAbsolute());
101
102         System.out.println();
103         System.out.println("******* Test 2 ************");
104         System.out.println("Original definition string2:");
105         System.out.println(leafRefCtx2.getLeafRefTargetPathString());
106         System.out.println("Parsed leafref path2:");
107         System.out.println(leafRefCtx2.getLeafRefTargetPath().toString());
108         System.out.println("Absolute leafref path2:");
109         System.out.println(leafRefCtx2.getAbsoluteLeafRefTargetPath().toString());
110         System.out.println();
111
112     }
113
114     @Test
115     public void buildLeafRefContextTreeXPathTest() {
116         final QName q1 = QName.create(tst, "odl-project");
117         final QName q2 = QName.create(tst, "project");
118         final QName q5 = QName.create(tst, "ch1");
119         final QName q6 = QName.create(tst, "c1");
120         final QName q7 = QName.create(tst, "ch2");
121         final QName q8 = QName.create(tst, "l1");
122         final LeafRefContext leafRefCtx3 = rootLeafRefContext.getReferencingChildByName(q1)
123                 .getReferencingChildByName(q2).getReferencingChildByName(q5).getReferencingChildByName(q6)
124                 .getReferencingChildByName(q7).getReferencingChildByName(q6).getReferencingChildByName(q8);
125
126         assertTrue(leafRefCtx3.isReferencing());
127         assertNotNull(leafRefCtx3.getLeafRefTargetPath());
128         assertFalse(leafRefCtx3.getLeafRefTargetPath().isAbsolute());
129         assertNotNull(leafRefCtx3.getAbsoluteLeafRefTargetPath());
130         assertTrue(leafRefCtx3.getAbsoluteLeafRefTargetPath().isAbsolute());
131
132         System.out.println();
133         System.out.println("******* Test 3 ************");
134         System.out.println("Original definition string2:");
135         System.out.println(leafRefCtx3.getLeafRefTargetPathString());
136         System.out.println("Parsed leafref path2:");
137         System.out.println(leafRefCtx3.getLeafRefTargetPath().toString());
138         System.out.println("Absolute leafref path2:");
139         System.out.println(leafRefCtx3.getAbsoluteLeafRefTargetPath().toString());
140         System.out.println();
141     }
142
143     @Test
144     public void buildLeafRefContextTreeTest4() {
145         final QName q9 = QName.create(tst, "odl-project");
146         final QName q10 = QName.create(tst, "project");
147         final QName q11 = QName.create(tst, "name");
148
149         final LeafRefContext leafRefCtx4 = rootLeafRefContext.getReferencedChildByName(q9)
150                 .getReferencedChildByName(q10).getReferencedChildByName(q11);
151
152         assertNotNull(leafRefCtx4);
153         assertTrue(leafRefCtx4.isReferenced());
154         assertEquals(6, leafRefCtx4.getAllReferencedByLeafRefCtxs().size());
155
156     }
157
158     @Test
159     public void leafRefContextUtilsTest() {
160         final QName q1 = QName.create(tst, "odl-contributor");
161         final QName q2 = QName.create(tst, "contributor");
162         final QName q3 = QName.create(tst, "odl-project-name");
163
164         final LeafRefContext odlContrProjNameCtx = rootLeafRefContext.getReferencingChildByName(q1)
165                 .getReferencingChildByName(q2).getReferencingChildByName(q3);
166
167         final DataSchemaNode odlContrProjNameNode = ((DataNodeContainer) ((DataNodeContainer) tstMod
168                 .getDataChildByName(q1)).getDataChildByName(q2)).getDataChildByName(q3);
169
170         final LeafRefContext foundOdlContrProjNameCtx = LeafRefContextUtils.getLeafRefReferencingContext(
171                 odlContrProjNameNode, rootLeafRefContext);
172
173         assertNotNull(foundOdlContrProjNameCtx);
174         assertTrue(foundOdlContrProjNameCtx.isReferencing());
175         assertNotNull(foundOdlContrProjNameCtx.getLeafRefTargetPath());
176         assertEquals(odlContrProjNameCtx, foundOdlContrProjNameCtx);
177     }
178
179     @Test
180     public void leafRefContextUtilsTest2() {
181         final QName q1 = QName.create(tst, "odl-project");
182         final QName q2 = QName.create(tst, "project");
183         final QName q3 = QName.create(tst, "name");
184
185         final LeafRefContext leafRefCtx = rootLeafRefContext.getReferencedChildByName(q1).getReferencedChildByName(q2)
186                 .getReferencedChildByName(q3);
187
188         final DataSchemaNode odlProjNameNode = ((DataNodeContainer) ((DataNodeContainer) tstMod.getDataChildByName(q1))
189                 .getDataChildByName(q2)).getDataChildByName(q3);
190
191         LeafRefContext foundOdlProjNameCtx = LeafRefContextUtils.getLeafRefReferencingContext(odlProjNameNode,
192                 rootLeafRefContext);
193
194         assertNull(foundOdlProjNameCtx);
195
196         foundOdlProjNameCtx = LeafRefContextUtils.getLeafRefReferencedByContext(odlProjNameNode, rootLeafRefContext);
197
198         assertNotNull(foundOdlProjNameCtx);
199         assertTrue(foundOdlProjNameCtx.isReferenced());
200         assertFalse(foundOdlProjNameCtx.getAllReferencedByLeafRefCtxs().isEmpty());
201         assertEquals(6, foundOdlProjNameCtx.getAllReferencedByLeafRefCtxs().size());
202         assertEquals(leafRefCtx, foundOdlProjNameCtx);
203     }
204
205     @Test
206     public void leafRefContextUtilsTest3() {
207         final QName q16 = QName.create(tst, "con1");
208         final DataSchemaNode con1 = tstMod.getDataChildByName(q16);
209         final List<LeafRefContext> allLeafRefChilds = LeafRefContextUtils
210                 .findAllLeafRefChilds(con1, rootLeafRefContext);
211
212         assertNotNull(allLeafRefChilds);
213         assertFalse(allLeafRefChilds.isEmpty());
214         assertEquals(4, allLeafRefChilds.size());
215
216         final QName q17 = QName.create(tst, "odl-contributor");
217         final DataSchemaNode odlContributorNode = tstMod.getDataChildByName(q17);
218         List<LeafRefContext> allChildsReferencedByLeafRef = LeafRefContextUtils.findAllChildsReferencedByLeafRef(
219                 odlContributorNode, rootLeafRefContext);
220
221         assertNotNull(allChildsReferencedByLeafRef);
222         assertFalse(allChildsReferencedByLeafRef.isEmpty());
223         assertEquals(1, allChildsReferencedByLeafRef.size());
224
225         allChildsReferencedByLeafRef = LeafRefContextUtils.findAllChildsReferencedByLeafRef(con1, rootLeafRefContext);
226
227         assertNotNull(allChildsReferencedByLeafRef);
228         assertTrue(allChildsReferencedByLeafRef.isEmpty());
229
230     }
231
232     @Test(expected = IllegalArgumentException.class)
233     @Ignore
234     public void incorrectLeafRefPathTest() {
235         final SchemaContext context = YangParserTestUtils.parseYangResourceDirectory(
236             "/leafref-context-test/incorrect-modules");
237         LeafRefContext.create(context);
238     }
239
240 }