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.tree.leafref;
10 import static org.junit.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertFalse;
12 import static org.junit.jupiter.api.Assertions.assertTrue;
14 import org.junit.jupiter.api.AfterAll;
15 import org.junit.jupiter.api.BeforeAll;
16 import org.junit.jupiter.api.Test;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.common.QNameModule;
19 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
20 import org.opendaylight.yangtools.yang.model.api.Module;
21 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
22 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
24 class LeafRefContextTest {
25 private static EffectiveModelContext context;
26 private static Module rootMod;
27 private static QNameModule root;
28 private static LeafRefContext rootLeafRefContext;
32 context = YangParserTestUtils.parseYangResourceDirectory("/leafref-context-test/correct-modules");
34 for (final var module : context.getModules()) {
35 if (module.getName().equals("leafref-test2")) {
40 root = rootMod.getQNameModule();
41 rootLeafRefContext = LeafRefContext.create(context);
45 static void cleanup() {
49 rootLeafRefContext = null;
54 final var q1 = QName.create(root, "ref1");
55 final var q2 = QName.create(root, "leaf1");
56 final var q3 = QName.create(root, "cont1");
57 final var q4 = QName.create(root, "cont2");
58 final var q5 = QName.create(root, "list1");
59 final var q6 = QName.create(root, "name");
61 final var leafRefNode = Absolute.of(q1);
62 final var targetNode = Absolute.of(q2);
63 final var cont1Node = Absolute.of(q3);
64 final var cont2Node = Absolute.of(q4);
65 final var name1Node = Absolute.of(q3, q5, q6);
67 assertTrue(rootLeafRefContext.isLeafRef(leafRefNode));
68 assertFalse(rootLeafRefContext.isLeafRef(targetNode));
70 assertTrue(rootLeafRefContext.hasLeafRefChild(cont1Node));
71 assertFalse(rootLeafRefContext.hasLeafRefChild(leafRefNode));
73 assertTrue(rootLeafRefContext.isReferencedByLeafRef(targetNode));
74 assertFalse(rootLeafRefContext.isReferencedByLeafRef(leafRefNode));
76 assertTrue(rootLeafRefContext.hasChildReferencedByLeafRef(cont2Node));
77 assertFalse(rootLeafRefContext.hasChildReferencedByLeafRef(leafRefNode));
79 var leafRefs = rootLeafRefContext.getAllLeafRefsReferencingThisNode(name1Node);
80 assertEquals(4, leafRefs.size());
81 leafRefs = rootLeafRefContext.getAllLeafRefsReferencingThisNode(leafRefNode);
82 assertTrue(leafRefs.isEmpty());