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