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