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