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;
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;
28 public class LeafRefContextTest {
29 private static SchemaContext context;
30 private static Module rootMod;
31 private static QNameModule root;
32 private static LeafRefContext rootLeafRefContext;
35 public static void init() {
36 context = YangParserTestUtils.parseYangResourceDirectory("/leafref-context-test/correct-modules");
38 final Set<Module> modules = context.getModules();
39 for (final Module module : modules) {
40 if (module.getName().equals("leafref-test2")) {
45 root = rootMod.getQNameModule();
46 rootLeafRefContext = LeafRefContext.create(context);
50 public static void cleanup() {
54 rootLeafRefContext = null;
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");
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();
73 assertTrue(LeafRefContextUtils.isLeafRef(leafRefNode, rootLeafRefContext));
74 assertFalse(LeafRefContextUtils.isLeafRef(targetNode, rootLeafRefContext));
76 assertTrue(LeafRefContextUtils.hasLeafRefChild(cont1Node, rootLeafRefContext));
77 assertFalse(LeafRefContextUtils.hasLeafRefChild(leafRefNode, rootLeafRefContext));
79 assertTrue(LeafRefContextUtils.isReferencedByLeafRef(targetNode, rootLeafRefContext));
80 assertFalse(LeafRefContextUtils.isReferencedByLeafRef(leafRefNode, rootLeafRefContext));
82 assertTrue(LeafRefContextUtils.hasChildReferencedByLeafRef(cont2Node, rootLeafRefContext));
83 assertFalse(LeafRefContextUtils.hasChildReferencedByLeafRef(leafRefNode, rootLeafRefContext));
85 Map<QName, LeafRefContext> leafRefs = LeafRefContextUtils.getAllLeafRefsReferencingThisNode(name1Node,
87 assertEquals(4, leafRefs.size());
88 leafRefs = LeafRefContextUtils.getAllLeafRefsReferencingThisNode(leafRefNode, rootLeafRefContext);
89 assertTrue(leafRefs.isEmpty());