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