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.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.DataNodeContainer;
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 {
30 private static SchemaContext context;
31 private static Module rootMod;
32 private static QNameModule root;
33 private static LeafRefContext rootLeafRefContext;
36 public static void init() {
37 context = YangParserTestUtils.parseYangResourceDirectory("/leafref-context-test/correct-modules");
39 final Set<Module> modules = context.getModules();
40 for (final Module module : modules) {
41 if (module.getName().equals("leafref-test2")) {
46 root = rootMod.getQNameModule();
47 rootLeafRefContext = LeafRefContext.create(context);
53 final QName q1 = QName.create(root, "ref1");
54 final QName q2 = QName.create(root, "leaf1");
55 final QName q3 = QName.create(root, "cont1");
56 final QName q4 = QName.create(root, "cont2");
57 final QName q5 = QName.create(root, "list1");
58 final QName q6 = QName.create(root, "name");
60 final DataSchemaNode leafRefNode = rootMod.getDataChildByName(q1);
61 final DataSchemaNode targetNode = rootMod.getDataChildByName(q2);
62 final DataSchemaNode cont1Node = rootMod.getDataChildByName(q3);
63 final DataSchemaNode cont2Node = rootMod.getDataChildByName(q4);
64 final DataSchemaNode name1Node = ((DataNodeContainer) ((DataNodeContainer) rootMod.getDataChildByName(q3))
65 .getDataChildByName(q5)).getDataChildByName(q6);
67 assertTrue(LeafRefContextUtils.isLeafRef(leafRefNode, rootLeafRefContext));
68 assertFalse(LeafRefContextUtils.isLeafRef(targetNode, rootLeafRefContext));
70 assertTrue(LeafRefContextUtils.hasLeafRefChild(cont1Node, rootLeafRefContext));
71 assertFalse(LeafRefContextUtils.hasLeafRefChild(leafRefNode, rootLeafRefContext));
73 assertTrue(LeafRefContextUtils.isReferencedByLeafRef(targetNode, rootLeafRefContext));
74 assertFalse(LeafRefContextUtils.isReferencedByLeafRef(leafRefNode, rootLeafRefContext));
76 assertTrue(LeafRefContextUtils.hasChildReferencedByLeafRef(cont2Node, rootLeafRefContext));
77 assertFalse(LeafRefContextUtils.hasChildReferencedByLeafRef(leafRefNode, rootLeafRefContext));
79 Map<QName, LeafRefContext> leafRefs = LeafRefContextUtils.getAllLeafRefsReferencingThisNode(name1Node,
81 assertEquals(4, leafRefs.size());
82 leafRefs = LeafRefContextUtils.getAllLeafRefsReferencingThisNode(leafRefNode, rootLeafRefContext);
83 assertTrue(leafRefs.isEmpty());