Address FIXME for QueuedNotificationManager
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / leafref / context / test / LeafRefContextTreeBuilderTest.java
1 /**
2  * Copyright (c) 2015 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.test;
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.io.File;
17 import java.io.IOException;
18 import java.net.URISyntaxException;
19 import java.util.List;
20 import java.util.Set;
21 import org.junit.BeforeClass;
22 import org.junit.Test;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.common.QNameModule;
25 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefContext;
26 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefContextUtils;
27 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefYangSyntaxErrorException;
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.model.parser.api.YangSyntaxErrorException;
33 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
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, IOException,
46             YangSyntaxErrorException, LeafRefYangSyntaxErrorException {
47         final File resourceFile = new File(
48                 LeafRefContextTreeBuilderTest.class
49                         .getResource(
50                                 "/leafref-context-test/correct-modules/leafref-test.yang")
51                         .toURI());
52         final File resourceDir = resourceFile.getParentFile();
53
54         final YangParserImpl parser = YangParserImpl.getInstance();
55         context = parser.parseFile(resourceFile, resourceDir);
56
57         final Set<Module> modules = context.getModules();
58         for (final Module module : modules) {
59             if (module.getName().equals("import-mod")) {
60                 impMod = module;
61             }
62             if (module.getName().equals("leafref-test")) {
63                 tstMod = module;
64             }
65         }
66
67         imp = impMod.getQNameModule();
68         tst = tstMod.getQNameModule();
69
70         rootLeafRefContext = LeafRefContext.create(context);
71     }
72
73     @Test
74     public void buildLeafRefContextTreeTest1() {
75
76         final QName q1 = QName.create(tst, "odl-project");
77         final QName q2 = QName.create(tst, "project");
78         final QName q3 = QName.create(tst, "project-lead");
79
80         final LeafRefContext leafRefCtx = rootLeafRefContext
81                 .getReferencingChildByName(q1).getReferencingChildByName(q2)
82                 .getReferencingChildByName(q3);
83
84         assertTrue(leafRefCtx.isReferencing());
85         assertNotNull(leafRefCtx.getLeafRefTargetPath());
86         assertFalse(leafRefCtx.getLeafRefTargetPath().isAbsolute());
87         assertNotNull(leafRefCtx.getAbsoluteLeafRefTargetPath());
88         assertTrue(leafRefCtx.getAbsoluteLeafRefTargetPath().isAbsolute());
89
90         System.out.println();
91         System.out.println("******* Test 1 ************");
92         System.out.println("Original definition string:");
93         System.out.println(leafRefCtx.getLeafRefTargetPathString());
94         System.out.println("Parsed leafref path:");
95         System.out.println(leafRefCtx.getLeafRefTargetPath().toString());
96         System.out.println("Absolute leafref path:");
97         System.out
98                 .println(leafRefCtx.getAbsoluteLeafRefTargetPath().toString());
99     }
100
101     @Test
102     public void buildLeafRefContextTreeTest2() {
103
104         final QName q1 = QName.create(tst, "odl-project");
105         final QName q2 = QName.create(tst, "project");
106         final QName q4 = QName.create(tst, "project-lead2");
107
108         final LeafRefContext leafRefCtx2 = rootLeafRefContext
109                 .getReferencingChildByName(q1).getReferencingChildByName(q2)
110                 .getReferencingChildByName(q4);
111
112         assertTrue(leafRefCtx2.isReferencing());
113         assertNotNull(leafRefCtx2.getLeafRefTargetPath());
114         assertTrue(leafRefCtx2.getLeafRefTargetPath().isAbsolute());
115         assertNotNull(leafRefCtx2.getAbsoluteLeafRefTargetPath());
116         assertTrue(leafRefCtx2.getAbsoluteLeafRefTargetPath().isAbsolute());
117
118         System.out.println();
119         System.out.println("******* Test 2 ************");
120         System.out.println("Original definition string2:");
121         System.out.println(leafRefCtx2.getLeafRefTargetPathString());
122         System.out.println("Parsed leafref path2:");
123         System.out.println(leafRefCtx2.getLeafRefTargetPath().toString());
124         System.out.println("Absolute leafref path2:");
125         System.out.println(leafRefCtx2.getAbsoluteLeafRefTargetPath()
126                 .toString());
127         System.out.println();
128
129     }
130
131     @Test
132     public void buildLeafRefContextTreeXPathTest() {
133         final QName q1 = QName.create(tst, "odl-project");
134         final QName q2 = QName.create(tst, "project");
135         final QName q5 = QName.create(tst, "ch1");
136         final QName q6 = QName.create(tst, "c1");
137         final QName q7 = QName.create(tst, "ch2");
138         final QName q8 = QName.create(tst, "l1");
139         final LeafRefContext leafRefCtx3 = rootLeafRefContext
140                 .getReferencingChildByName(q1).getReferencingChildByName(q2)
141                 .getReferencingChildByName(q5).getReferencingChildByName(q6)
142                 .getReferencingChildByName(q7).getReferencingChildByName(q6)
143                 .getReferencingChildByName(q8);
144
145         assertTrue(leafRefCtx3.isReferencing());
146         assertNotNull(leafRefCtx3.getLeafRefTargetPath());
147         assertFalse(leafRefCtx3.getLeafRefTargetPath().isAbsolute());
148         assertNotNull(leafRefCtx3.getAbsoluteLeafRefTargetPath());
149         assertTrue(leafRefCtx3.getAbsoluteLeafRefTargetPath().isAbsolute());
150
151         System.out.println();
152         System.out.println("******* Test 3 ************");
153         System.out.println("Original definition string2:");
154         System.out.println(leafRefCtx3.getLeafRefTargetPathString());
155         System.out.println("Parsed leafref path2:");
156         System.out.println(leafRefCtx3.getLeafRefTargetPath().toString());
157         System.out.println("Absolute leafref path2:");
158         System.out.println(leafRefCtx3.getAbsoluteLeafRefTargetPath()
159                 .toString());
160         System.out.println();
161     }
162
163     @Test
164     public void buildLeafRefContextTreeTest4() {
165         final QName q9 = QName.create(tst, "odl-project");
166         final QName q10 = QName.create(tst, "project");
167         final QName q11 = QName.create(tst, "name");
168
169         final LeafRefContext leafRefCtx4 = rootLeafRefContext
170                 .getReferencedChildByName(q9).getReferencedChildByName(q10)
171                 .getReferencedChildByName(q11);
172
173         assertNotNull(leafRefCtx4);
174         assertTrue(leafRefCtx4.isReferenced());
175         assertEquals(6, leafRefCtx4.getAllReferencedByLeafRefCtxs().size());
176
177     }
178
179     @Test
180     public void leafRefContextUtilsTest() {
181         final QName q1 = QName.create(tst, "odl-contributor");
182         final QName q2 = QName.create(tst, "contributor");
183         final QName q3 = QName.create(tst, "odl-project-name");
184
185         final LeafRefContext odlContrProjNameCtx = rootLeafRefContext
186                 .getReferencingChildByName(q1).getReferencingChildByName(q2)
187                 .getReferencingChildByName(q3);
188
189         final DataSchemaNode odlContrProjNameNode = ((DataNodeContainer) ((DataNodeContainer) tstMod
190                 .getDataChildByName(q1)).getDataChildByName(q2))
191                 .getDataChildByName(q3);
192
193         final LeafRefContext foundOdlContrProjNameCtx = LeafRefContextUtils
194                 .getLeafRefReferencingContext(odlContrProjNameNode,
195                         rootLeafRefContext);
196
197         assertNotNull(foundOdlContrProjNameCtx);
198         assertTrue(foundOdlContrProjNameCtx.isReferencing());
199         assertNotNull(foundOdlContrProjNameCtx.getLeafRefTargetPath());
200         assertEquals(odlContrProjNameCtx, foundOdlContrProjNameCtx);
201     }
202
203     @Test
204     public void leafRefContextUtilsTest2() {
205         final QName q1 = QName.create(tst, "odl-project");
206         final QName q2 = QName.create(tst, "project");
207         final QName q3 = QName.create(tst, "name");
208
209         final LeafRefContext leafRefCtx = rootLeafRefContext
210                 .getReferencedChildByName(q1).getReferencedChildByName(q2)
211                 .getReferencedChildByName(q3);
212
213         final DataSchemaNode odlProjNameNode = ((DataNodeContainer) ((DataNodeContainer) tstMod
214                 .getDataChildByName(q1)).getDataChildByName(q2))
215                 .getDataChildByName(q3);
216
217         LeafRefContext foundOdlProjNameCtx = LeafRefContextUtils
218                 .getLeafRefReferencingContext(odlProjNameNode,
219                         rootLeafRefContext);
220
221         assertNull(foundOdlProjNameCtx);
222
223         foundOdlProjNameCtx = LeafRefContextUtils
224                 .getLeafRefReferencedByContext(odlProjNameNode,
225                         rootLeafRefContext);
226
227         assertNotNull(foundOdlProjNameCtx);
228         assertTrue(foundOdlProjNameCtx.isReferenced());
229         assertFalse(foundOdlProjNameCtx.getAllReferencedByLeafRefCtxs()
230                 .isEmpty());
231         assertEquals(6, foundOdlProjNameCtx.getAllReferencedByLeafRefCtxs()
232                 .size());
233         assertEquals(leafRefCtx, foundOdlProjNameCtx);
234     }
235
236     @Test
237     public void leafRefContextUtilsTest3() {
238         final QName q16 = QName.create(tst, "con1");
239         final DataSchemaNode con1 = tstMod.getDataChildByName(q16);
240         final List<LeafRefContext> allLeafRefChilds = LeafRefContextUtils
241                 .findAllLeafRefChilds(con1, rootLeafRefContext);
242
243         assertNotNull(allLeafRefChilds);
244         assertFalse(allLeafRefChilds.isEmpty());
245         assertEquals(4, allLeafRefChilds.size());
246
247         final QName q17 = QName.create(tst, "odl-contributor");
248         final DataSchemaNode odlContributorNode = tstMod.getDataChildByName(q17);
249         List<LeafRefContext> allChildsReferencedByLeafRef = LeafRefContextUtils
250                 .findAllChildsReferencedByLeafRef(odlContributorNode,
251                         rootLeafRefContext);
252
253         assertNotNull(allChildsReferencedByLeafRef);
254         assertFalse(allChildsReferencedByLeafRef.isEmpty());
255         assertEquals(1, allChildsReferencedByLeafRef.size());
256
257         allChildsReferencedByLeafRef = LeafRefContextUtils
258                 .findAllChildsReferencedByLeafRef(con1, rootLeafRefContext);
259
260         assertNotNull(allChildsReferencedByLeafRef);
261         assertTrue(allChildsReferencedByLeafRef.isEmpty());
262
263     }
264
265     @Test(expected = RuntimeException.class)
266     public void incorrectLeafRefPathTest() throws URISyntaxException,
267             IOException, YangSyntaxErrorException,
268             LeafRefYangSyntaxErrorException {
269         final File resourceFile = new File(getClass().getResource(
270                 "/leafref-context-test/incorrect-modules/leafref-test.yang")
271                 .toURI());
272         final File resourceDir = resourceFile.getParentFile();
273
274         final YangParserImpl parser = YangParserImpl.getInstance();
275         final SchemaContext context = parser.parseFile(resourceFile, resourceDir);
276
277         LeafRefContext.create(context);
278
279     }
280
281 }