2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.tree.leafref;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
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.model.api.EffectiveModelContext;
21 import org.opendaylight.yangtools.yang.model.api.Module;
22 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
23 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
25 public class LeafRefContextTest {
26 private static EffectiveModelContext context;
27 private static Module rootMod;
28 private static QNameModule root;
29 private static LeafRefContext rootLeafRefContext;
32 public static void init() {
33 context = YangParserTestUtils.parseYangResourceDirectory("/leafref-context-test/correct-modules");
35 for (final Module module : context.getModules()) {
36 if (module.getName().equals("leafref-test2")) {
41 root = rootMod.getQNameModule();
42 rootLeafRefContext = LeafRefContext.create(context);
46 public static void cleanup() {
50 rootLeafRefContext = null;
55 final QName q1 = QName.create(root, "ref1");
56 final QName q2 = QName.create(root, "leaf1");
57 final QName q3 = QName.create(root, "cont1");
58 final QName q4 = QName.create(root, "cont2");
59 final QName q5 = QName.create(root, "list1");
60 final QName q6 = QName.create(root, "name");
62 final Absolute leafRefNode = Absolute.of(q1);
63 final Absolute targetNode = Absolute.of(q2);
64 final Absolute cont1Node = Absolute.of(q3);
65 final Absolute cont2Node = Absolute.of(q4);
66 final Absolute name1Node = Absolute.of(q3, q5, q6);
68 assertTrue(rootLeafRefContext.isLeafRef(leafRefNode));
69 assertFalse(rootLeafRefContext.isLeafRef(targetNode));
71 assertTrue(rootLeafRefContext.hasLeafRefChild(cont1Node));
72 assertFalse(rootLeafRefContext.hasLeafRefChild(leafRefNode));
74 assertTrue(rootLeafRefContext.isReferencedByLeafRef(targetNode));
75 assertFalse(rootLeafRefContext.isReferencedByLeafRef(leafRefNode));
77 assertTrue(rootLeafRefContext.hasChildReferencedByLeafRef(cont2Node));
78 assertFalse(rootLeafRefContext.hasChildReferencedByLeafRef(leafRefNode));
80 Map<QName, LeafRefContext> leafRefs = rootLeafRefContext.getAllLeafRefsReferencingThisNode(name1Node);
81 assertEquals(4, leafRefs.size());
82 leafRefs = rootLeafRefContext.getAllLeafRefsReferencingThisNode(leafRefNode);
83 assertTrue(leafRefs.isEmpty());