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