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;
14 import java.io.FileNotFoundException;
15 import java.net.URISyntaxException;
16 import java.util.Arrays;
19 import org.junit.BeforeClass;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.common.QNameModule;
23 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefContext;
24 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefContextUtils;
25 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
26 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.Module;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
30 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
32 public class LeafRefContextTest {
34 private static SchemaContext context;
35 private static Module rootMod;
36 private static QNameModule root;
37 private static LeafRefContext rootLeafRefContext;
40 public static void init() throws URISyntaxException, FileNotFoundException, ReactorException {
42 final File resourceFile = new File(LeafRefContextTreeBuilderTest.class.getResource(
43 "/leafref-context-test/correct-modules/leafref-test2.yang").toURI());
45 final File resourceDir = resourceFile.getParentFile();
47 context = YangParserTestUtils.parseYangSources(Arrays.asList(resourceDir.listFiles()));
49 final Set<Module> modules = context.getModules();
50 for (final Module module : modules) {
51 if (module.getName().equals("leafref-test2")) {
56 root = rootMod.getQNameModule();
57 rootLeafRefContext = LeafRefContext.create(context);
63 final QName q1 = QName.create(root, "ref1");
64 final QName q2 = QName.create(root, "leaf1");
65 final QName q3 = QName.create(root, "cont1");
66 final QName q4 = QName.create(root, "cont2");
67 final QName q5 = QName.create(root, "list1");
68 final QName q6 = QName.create(root, "name");
70 final DataSchemaNode leafRefNode = rootMod.getDataChildByName(q1);
71 final DataSchemaNode targetNode = rootMod.getDataChildByName(q2);
72 final DataSchemaNode cont1Node = rootMod.getDataChildByName(q3);
73 final DataSchemaNode cont2Node = rootMod.getDataChildByName(q4);
74 final DataSchemaNode name1Node = ((DataNodeContainer) ((DataNodeContainer) rootMod.getDataChildByName(q3))
75 .getDataChildByName(q5)).getDataChildByName(q6);
77 assertTrue(LeafRefContextUtils.isLeafRef(leafRefNode, rootLeafRefContext));
78 assertFalse(LeafRefContextUtils.isLeafRef(targetNode, rootLeafRefContext));
80 assertTrue(LeafRefContextUtils.hasLeafRefChild(cont1Node, rootLeafRefContext));
81 assertFalse(LeafRefContextUtils.hasLeafRefChild(leafRefNode, rootLeafRefContext));
83 assertTrue(LeafRefContextUtils.isReferencedByLeafRef(targetNode, rootLeafRefContext));
84 assertFalse(LeafRefContextUtils.isReferencedByLeafRef(leafRefNode, rootLeafRefContext));
86 assertTrue(LeafRefContextUtils.hasChildReferencedByLeafRef(cont2Node, rootLeafRefContext));
87 assertFalse(LeafRefContextUtils.hasChildReferencedByLeafRef(leafRefNode, rootLeafRefContext));
89 Map<QName, LeafRefContext> leafRefs = LeafRefContextUtils.getAllLeafRefsReferencingThisNode(name1Node,
91 assertEquals(4, leafRefs.size());
92 leafRefs = LeafRefContextUtils.getAllLeafRefsReferencingThisNode(leafRefNode, rootLeafRefContext);
93 assertTrue(leafRefs.isEmpty());