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.model.api.EffectiveModelContext;
22 import org.opendaylight.yangtools.yang.model.api.Module;
23 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
24 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
26 public class LeafRefContextTest {
27 private static EffectiveModelContext context;
28 private static Module rootMod;
29 private static QNameModule root;
30 private static LeafRefContext rootLeafRefContext;
33 public static void init() {
34 context = YangParserTestUtils.parseYangResourceDirectory("/leafref-context-test/correct-modules");
36 for (final Module module : context.getModules()) {
37 if (module.getName().equals("leafref-test2")) {
42 root = rootMod.getQNameModule();
43 rootLeafRefContext = LeafRefContext.create(context);
47 public static void cleanup() {
51 rootLeafRefContext = null;
56 final QName q1 = QName.create(root, "ref1");
57 final QName q2 = QName.create(root, "leaf1");
58 final QName q3 = QName.create(root, "cont1");
59 final QName q4 = QName.create(root, "cont2");
60 final QName q5 = QName.create(root, "list1");
61 final QName q6 = QName.create(root, "name");
63 final Absolute leafRefNode = Absolute.of(q1);
64 final Absolute targetNode = Absolute.of(q2);
65 final Absolute cont1Node = Absolute.of(q3);
66 final Absolute cont2Node = Absolute.of(q4);
67 final Absolute name1Node = Absolute.of(q3, q5, q6);
69 assertTrue(rootLeafRefContext.isLeafRef(leafRefNode));
70 assertFalse(rootLeafRefContext.isLeafRef(targetNode));
72 assertTrue(rootLeafRefContext.hasLeafRefChild(cont1Node));
73 assertFalse(rootLeafRefContext.hasLeafRefChild(leafRefNode));
75 assertTrue(rootLeafRefContext.isReferencedByLeafRef(targetNode));
76 assertFalse(rootLeafRefContext.isReferencedByLeafRef(leafRefNode));
78 assertTrue(rootLeafRefContext.hasChildReferencedByLeafRef(cont2Node));
79 assertFalse(rootLeafRefContext.hasChildReferencedByLeafRef(leafRefNode));
81 Map<QName, LeafRefContext> leafRefs = rootLeafRefContext.getAllLeafRefsReferencingThisNode(name1Node);
82 assertEquals(4, leafRefs.size());
83 leafRefs = rootLeafRefContext.getAllLeafRefsReferencingThisNode(leafRefNode);
84 assertTrue(leafRefs.isEmpty());