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