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