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