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