X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=data%2Fyang-data-impl%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fdata%2Fimpl%2Fleafref%2Fcontext%2FLeafRefContextTest.java;fp=data%2Fyang-data-impl%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fdata%2Fimpl%2Fleafref%2Fcontext%2FLeafRefContextTest.java;h=e9c650fb05f9b22caed7b999f1cf56f1ca04352a;hb=9728fe497bcb7349f7e6ef9d3d984202d7ac07e7;hp=0000000000000000000000000000000000000000;hpb=6fbe90e71812236b6d8dee506c421b37605e5797;p=yangtools.git diff --git a/data/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/leafref/context/LeafRefContextTest.java b/data/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/leafref/context/LeafRefContextTest.java new file mode 100644 index 0000000000..e9c650fb05 --- /dev/null +++ b/data/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/leafref/context/LeafRefContextTest.java @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.yangtools.yang.data.impl.leafref.context; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.util.Map; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.common.QNameModule; +import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefContext; +import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefContextUtils; +import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; +import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext; +import org.opendaylight.yangtools.yang.model.api.Module; +import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils; + +public class LeafRefContextTest { + private static EffectiveModelContext context; + private static Module rootMod; + private static QNameModule root; + private static LeafRefContext rootLeafRefContext; + + @BeforeClass + public static void init() { + context = YangParserTestUtils.parseYangResourceDirectory("/leafref-context-test/correct-modules"); + + for (final Module module : context.getModules()) { + if (module.getName().equals("leafref-test2")) { + rootMod = module; + } + } + + root = rootMod.getQNameModule(); + rootLeafRefContext = LeafRefContext.create(context); + } + + @AfterClass + public static void cleanup() { + context = null; + root = null; + rootMod = null; + rootLeafRefContext = null; + } + + @Test + public void test() { + + final QName q1 = QName.create(root, "ref1"); + final QName q2 = QName.create(root, "leaf1"); + final QName q3 = QName.create(root, "cont1"); + final QName q4 = QName.create(root, "cont2"); + final QName q5 = QName.create(root, "list1"); + final QName q6 = QName.create(root, "name"); + + final DataSchemaNode leafRefNode = rootMod.findDataChildByName(q1).get(); + final DataSchemaNode targetNode = rootMod.findDataChildByName(q2).get(); + final DataSchemaNode cont1Node = rootMod.findDataChildByName(q3).get(); + final DataSchemaNode cont2Node = rootMod.findDataChildByName(q4).get(); + final DataSchemaNode name1Node = rootMod.findDataChildByName(q3, q5, q6).get(); + + assertTrue(LeafRefContextUtils.isLeafRef(leafRefNode, rootLeafRefContext)); + assertFalse(LeafRefContextUtils.isLeafRef(targetNode, rootLeafRefContext)); + + assertTrue(LeafRefContextUtils.hasLeafRefChild(cont1Node, rootLeafRefContext)); + assertFalse(LeafRefContextUtils.hasLeafRefChild(leafRefNode, rootLeafRefContext)); + + assertTrue(LeafRefContextUtils.isReferencedByLeafRef(targetNode, rootLeafRefContext)); + assertFalse(LeafRefContextUtils.isReferencedByLeafRef(leafRefNode, rootLeafRefContext)); + + assertTrue(LeafRefContextUtils.hasChildReferencedByLeafRef(cont2Node, rootLeafRefContext)); + assertFalse(LeafRefContextUtils.hasChildReferencedByLeafRef(leafRefNode, rootLeafRefContext)); + + Map leafRefs = LeafRefContextUtils.getAllLeafRefsReferencingThisNode(name1Node, + rootLeafRefContext); + assertEquals(4, leafRefs.size()); + leafRefs = LeafRefContextUtils.getAllLeafRefsReferencingThisNode(leafRefNode, rootLeafRefContext); + assertTrue(leafRefs.isEmpty()); + } +}