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.impl.leafref.context;
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.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;
27 public class LeafRefContextTest {
28 private static SchemaContext context;
29 private static Module rootMod;
30 private static QNameModule root;
31 private static LeafRefContext rootLeafRefContext;
34 public static void init() {
35 context = YangParserTestUtils.parseYangResourceDirectory("/leafref-context-test/correct-modules");
37 for (final Module module : context.getModules()) {
38 if (module.getName().equals("leafref-test2")) {
43 root = rootMod.getQNameModule();
44 rootLeafRefContext = LeafRefContext.create(context);
48 public static void cleanup() {
52 rootLeafRefContext = null;
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");
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();
71 assertTrue(LeafRefContextUtils.isLeafRef(leafRefNode, rootLeafRefContext));
72 assertFalse(LeafRefContextUtils.isLeafRef(targetNode, rootLeafRefContext));
74 assertTrue(LeafRefContextUtils.hasLeafRefChild(cont1Node, rootLeafRefContext));
75 assertFalse(LeafRefContextUtils.hasLeafRefChild(leafRefNode, rootLeafRefContext));
77 assertTrue(LeafRefContextUtils.isReferencedByLeafRef(targetNode, rootLeafRefContext));
78 assertFalse(LeafRefContextUtils.isReferencedByLeafRef(leafRefNode, rootLeafRefContext));
80 assertTrue(LeafRefContextUtils.hasChildReferencedByLeafRef(cont2Node, rootLeafRefContext));
81 assertFalse(LeafRefContextUtils.hasChildReferencedByLeafRef(leafRefNode, rootLeafRefContext));
83 Map<QName, LeafRefContext> leafRefs = LeafRefContextUtils.getAllLeafRefsReferencingThisNode(name1Node,
85 assertEquals(4, leafRefs.size());
86 leafRefs = LeafRefContextUtils.getAllLeafRefsReferencingThisNode(leafRefNode, rootLeafRefContext);
87 assertTrue(leafRefs.isEmpty());