Cleanup resources allocated in @BeforeClass
[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.DataNodeContainer;
24 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.Module;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
28
29 public class LeafRefContextTest {
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     @AfterClass
51     public static void cleanup() {
52         context = null;
53         root = null;
54         rootMod = null;
55         rootLeafRefContext = null;
56     }
57
58     @Test
59     public void test() {
60
61         final QName q1 = QName.create(root, "ref1");
62         final QName q2 = QName.create(root, "leaf1");
63         final QName q3 = QName.create(root, "cont1");
64         final QName q4 = QName.create(root, "cont2");
65         final QName q5 = QName.create(root, "list1");
66         final QName q6 = QName.create(root, "name");
67
68         final DataSchemaNode leafRefNode = rootMod.getDataChildByName(q1);
69         final DataSchemaNode targetNode = rootMod.getDataChildByName(q2);
70         final DataSchemaNode cont1Node = rootMod.getDataChildByName(q3);
71         final DataSchemaNode cont2Node = rootMod.getDataChildByName(q4);
72         final DataSchemaNode name1Node = ((DataNodeContainer) ((DataNodeContainer) rootMod.getDataChildByName(q3))
73                 .getDataChildByName(q5)).getDataChildByName(q6);
74
75         assertTrue(LeafRefContextUtils.isLeafRef(leafRefNode, rootLeafRefContext));
76         assertFalse(LeafRefContextUtils.isLeafRef(targetNode, rootLeafRefContext));
77
78         assertTrue(LeafRefContextUtils.hasLeafRefChild(cont1Node, rootLeafRefContext));
79         assertFalse(LeafRefContextUtils.hasLeafRefChild(leafRefNode, rootLeafRefContext));
80
81         assertTrue(LeafRefContextUtils.isReferencedByLeafRef(targetNode, rootLeafRefContext));
82         assertFalse(LeafRefContextUtils.isReferencedByLeafRef(leafRefNode, rootLeafRefContext));
83
84         assertTrue(LeafRefContextUtils.hasChildReferencedByLeafRef(cont2Node, rootLeafRefContext));
85         assertFalse(LeafRefContextUtils.hasChildReferencedByLeafRef(leafRefNode, rootLeafRefContext));
86
87         Map<QName, LeafRefContext> leafRefs = LeafRefContextUtils.getAllLeafRefsReferencingThisNode(name1Node,
88                 rootLeafRefContext);
89         assertEquals(4, leafRefs.size());
90         leafRefs = LeafRefContextUtils.getAllLeafRefsReferencingThisNode(leafRefNode, rootLeafRefContext);
91         assertTrue(leafRefs.isEmpty());
92     }
93 }