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.DataNodeContainer;
24 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.Module;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
29 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);
51 public static void cleanup() {
55 rootLeafRefContext = null;
61 final QName q1 = QName.create(root, "ref1");
62 final QName q2 = QName.create(root, "leaf1");
63 final QName q3 = QName.create(root, "cont1");
64 final QName q4 = QName.create(root, "cont2");
65 final QName q5 = QName.create(root, "list1");
66 final QName q6 = QName.create(root, "name");
68 final DataSchemaNode leafRefNode = rootMod.getDataChildByName(q1);
69 final DataSchemaNode targetNode = rootMod.getDataChildByName(q2);
70 final DataSchemaNode cont1Node = rootMod.getDataChildByName(q3);
71 final DataSchemaNode cont2Node = rootMod.getDataChildByName(q4);
72 final DataSchemaNode name1Node = ((DataNodeContainer) ((DataNodeContainer) rootMod.getDataChildByName(q3))
73 .getDataChildByName(q5)).getDataChildByName(q6);
75 assertTrue(LeafRefContextUtils.isLeafRef(leafRefNode, rootLeafRefContext));
76 assertFalse(LeafRefContextUtils.isLeafRef(targetNode, rootLeafRefContext));
78 assertTrue(LeafRefContextUtils.hasLeafRefChild(cont1Node, rootLeafRefContext));
79 assertFalse(LeafRefContextUtils.hasLeafRefChild(leafRefNode, rootLeafRefContext));
81 assertTrue(LeafRefContextUtils.isReferencedByLeafRef(targetNode, rootLeafRefContext));
82 assertFalse(LeafRefContextUtils.isReferencedByLeafRef(leafRefNode, rootLeafRefContext));
84 assertTrue(LeafRefContextUtils.hasChildReferencedByLeafRef(cont2Node, rootLeafRefContext));
85 assertFalse(LeafRefContextUtils.hasChildReferencedByLeafRef(leafRefNode, rootLeafRefContext));
87 Map<QName, LeafRefContext> leafRefs = LeafRefContextUtils.getAllLeafRefsReferencingThisNode(name1Node,
89 assertEquals(4, leafRefs.size());
90 leafRefs = LeafRefContextUtils.getAllLeafRefsReferencingThisNode(leafRefNode, rootLeafRefContext);
91 assertTrue(leafRefs.isEmpty());