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