Bug 7159: Add yang-test-util artifact
[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 import java.io.File;
16 import java.io.FileNotFoundException;
17 import java.net.URISyntaxException;
18 import java.util.Arrays;
19 import java.util.List;
20 import java.util.Set;
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;
34
35 public class LeafRefContextTreeBuilderTest {
36
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;
43
44     @BeforeClass
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();
49
50         context = YangParserTestUtils.parseYangSources(Arrays.asList(resourceDir.listFiles()));
51
52         final Set<Module> modules = context.getModules();
53         for (final Module module : modules) {
54             if (module.getName().equals("import-mod")) {
55                 impMod = module;
56             }
57             if (module.getName().equals("leafref-test")) {
58                 tstMod = module;
59             }
60         }
61
62         imp = impMod.getQNameModule();
63         tst = tstMod.getQNameModule();
64
65         rootLeafRefContext = LeafRefContext.create(context);
66     }
67
68     @Test
69     public void buildLeafRefContextTreeTest1() {
70
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");
74
75         final LeafRefContext leafRefCtx = rootLeafRefContext.getReferencingChildByName(q1)
76                 .getReferencingChildByName(q2).getReferencingChildByName(q3);
77
78         assertTrue(leafRefCtx.isReferencing());
79         assertNotNull(leafRefCtx.getLeafRefTargetPath());
80         assertFalse(leafRefCtx.getLeafRefTargetPath().isAbsolute());
81         assertNotNull(leafRefCtx.getAbsoluteLeafRefTargetPath());
82         assertTrue(leafRefCtx.getAbsoluteLeafRefTargetPath().isAbsolute());
83
84         System.out.println();
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());
92     }
93
94     @Test
95     public void buildLeafRefContextTreeTest2() {
96
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");
100
101         final LeafRefContext leafRefCtx2 = rootLeafRefContext.getReferencingChildByName(q1)
102                 .getReferencingChildByName(q2).getReferencingChildByName(q4);
103
104         assertTrue(leafRefCtx2.isReferencing());
105         assertNotNull(leafRefCtx2.getLeafRefTargetPath());
106         assertTrue(leafRefCtx2.getLeafRefTargetPath().isAbsolute());
107         assertNotNull(leafRefCtx2.getAbsoluteLeafRefTargetPath());
108         assertTrue(leafRefCtx2.getAbsoluteLeafRefTargetPath().isAbsolute());
109
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();
119
120     }
121
122     @Test
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);
133
134         assertTrue(leafRefCtx3.isReferencing());
135         assertNotNull(leafRefCtx3.getLeafRefTargetPath());
136         assertFalse(leafRefCtx3.getLeafRefTargetPath().isAbsolute());
137         assertNotNull(leafRefCtx3.getAbsoluteLeafRefTargetPath());
138         assertTrue(leafRefCtx3.getAbsoluteLeafRefTargetPath().isAbsolute());
139
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();
149     }
150
151     @Test
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");
156
157         final LeafRefContext leafRefCtx4 = rootLeafRefContext.getReferencedChildByName(q9)
158                 .getReferencedChildByName(q10).getReferencedChildByName(q11);
159
160         assertNotNull(leafRefCtx4);
161         assertTrue(leafRefCtx4.isReferenced());
162         assertEquals(6, leafRefCtx4.getAllReferencedByLeafRefCtxs().size());
163
164     }
165
166     @Test
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");
171
172         final LeafRefContext odlContrProjNameCtx = rootLeafRefContext.getReferencingChildByName(q1)
173                 .getReferencingChildByName(q2).getReferencingChildByName(q3);
174
175         final DataSchemaNode odlContrProjNameNode = ((DataNodeContainer) ((DataNodeContainer) tstMod
176                 .getDataChildByName(q1)).getDataChildByName(q2)).getDataChildByName(q3);
177
178         final LeafRefContext foundOdlContrProjNameCtx = LeafRefContextUtils.getLeafRefReferencingContext(
179                 odlContrProjNameNode, rootLeafRefContext);
180
181         assertNotNull(foundOdlContrProjNameCtx);
182         assertTrue(foundOdlContrProjNameCtx.isReferencing());
183         assertNotNull(foundOdlContrProjNameCtx.getLeafRefTargetPath());
184         assertEquals(odlContrProjNameCtx, foundOdlContrProjNameCtx);
185     }
186
187     @Test
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");
192
193         final LeafRefContext leafRefCtx = rootLeafRefContext.getReferencedChildByName(q1).getReferencedChildByName(q2)
194                 .getReferencedChildByName(q3);
195
196         final DataSchemaNode odlProjNameNode = ((DataNodeContainer) ((DataNodeContainer) tstMod.getDataChildByName(q1))
197                 .getDataChildByName(q2)).getDataChildByName(q3);
198
199         LeafRefContext foundOdlProjNameCtx = LeafRefContextUtils.getLeafRefReferencingContext(odlProjNameNode,
200                 rootLeafRefContext);
201
202         assertNull(foundOdlProjNameCtx);
203
204         foundOdlProjNameCtx = LeafRefContextUtils.getLeafRefReferencedByContext(odlProjNameNode, rootLeafRefContext);
205
206         assertNotNull(foundOdlProjNameCtx);
207         assertTrue(foundOdlProjNameCtx.isReferenced());
208         assertFalse(foundOdlProjNameCtx.getAllReferencedByLeafRefCtxs().isEmpty());
209         assertEquals(6, foundOdlProjNameCtx.getAllReferencedByLeafRefCtxs().size());
210         assertEquals(leafRefCtx, foundOdlProjNameCtx);
211     }
212
213     @Test
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);
219
220         assertNotNull(allLeafRefChilds);
221         assertFalse(allLeafRefChilds.isEmpty());
222         assertEquals(4, allLeafRefChilds.size());
223
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);
228
229         assertNotNull(allChildsReferencedByLeafRef);
230         assertFalse(allChildsReferencedByLeafRef.isEmpty());
231         assertEquals(1, allChildsReferencedByLeafRef.size());
232
233         allChildsReferencedByLeafRef = LeafRefContextUtils.findAllChildsReferencedByLeafRef(con1, rootLeafRefContext);
234
235         assertNotNull(allChildsReferencedByLeafRef);
236         assertTrue(allChildsReferencedByLeafRef.isEmpty());
237
238     }
239
240     @Test(expected = IllegalArgumentException.class)
241     @Ignore
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();
246
247         final SchemaContext context = YangParserTestUtils.parseYangSources(Arrays.asList(resourceDir.listFiles()));
248
249         LeafRefContext.create(context);
250
251     }
252
253 }