Migrate yang-data-tree-ri to JUnit5
[yangtools.git] / data / yang-data-tree-ri / src / test / java / org / opendaylight / yangtools / yang / data / tree / leafref / LeafRefContextTest.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.assertTrue;
13
14 import org.junit.jupiter.api.AfterAll;
15 import org.junit.jupiter.api.BeforeAll;
16 import org.junit.jupiter.api.Test;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.common.QNameModule;
19 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
20 import org.opendaylight.yangtools.yang.model.api.Module;
21 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
22 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
23
24 class LeafRefContextTest {
25     private static EffectiveModelContext context;
26     private static Module rootMod;
27     private static QNameModule root;
28     private static LeafRefContext rootLeafRefContext;
29
30     @BeforeAll
31     static void init() {
32         context = YangParserTestUtils.parseYangResourceDirectory("/leafref-context-test/correct-modules");
33
34         for (final var module : context.getModules()) {
35             if (module.getName().equals("leafref-test2")) {
36                 rootMod = module;
37             }
38         }
39
40         root = rootMod.getQNameModule();
41         rootLeafRefContext = LeafRefContext.create(context);
42     }
43
44     @AfterAll
45     static void cleanup() {
46         context = null;
47         root = null;
48         rootMod = null;
49         rootLeafRefContext = null;
50     }
51
52     @Test
53     void test() {
54         final var q1 = QName.create(root, "ref1");
55         final var q2 = QName.create(root, "leaf1");
56         final var q3 = QName.create(root, "cont1");
57         final var q4 = QName.create(root, "cont2");
58         final var q5 = QName.create(root, "list1");
59         final var q6 = QName.create(root, "name");
60
61         final var leafRefNode = Absolute.of(q1);
62         final var targetNode = Absolute.of(q2);
63         final var cont1Node = Absolute.of(q3);
64         final var cont2Node = Absolute.of(q4);
65         final var name1Node = Absolute.of(q3, q5, q6);
66
67         assertTrue(rootLeafRefContext.isLeafRef(leafRefNode));
68         assertFalse(rootLeafRefContext.isLeafRef(targetNode));
69
70         assertTrue(rootLeafRefContext.hasLeafRefChild(cont1Node));
71         assertFalse(rootLeafRefContext.hasLeafRefChild(leafRefNode));
72
73         assertTrue(rootLeafRefContext.isReferencedByLeafRef(targetNode));
74         assertFalse(rootLeafRefContext.isReferencedByLeafRef(leafRefNode));
75
76         assertTrue(rootLeafRefContext.hasChildReferencedByLeafRef(cont2Node));
77         assertFalse(rootLeafRefContext.hasChildReferencedByLeafRef(leafRefNode));
78
79         var leafRefs = rootLeafRefContext.getAllLeafRefsReferencingThisNode(name1Node);
80         assertEquals(4, leafRefs.size());
81         leafRefs = rootLeafRefContext.getAllLeafRefsReferencingThisNode(leafRefNode);
82         assertTrue(leafRefs.isEmpty());
83     }
84 }