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