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