2 * Copyright (c) 2017 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;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
15 import com.google.common.collect.ImmutableList;
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.common.XMLNamespace;
21 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
22 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
24 public class Bug7844Test {
25 private static final String FOO_NS = "foo";
26 private static final String BAR_NS = "bar";
27 private static final String BAZ_NS = "baz";
31 final EffectiveModelContext context = YangParserTestUtils.parseYangResourceDirectory("/bug7844");
32 assertNotNull(context);
34 final LeafRefContext leafRefContext = LeafRefContext.create(context);
35 assertNotNull(leafRefContext);
37 final Map<QName, LeafRefContext> referencingChilds = leafRefContext.getReferencingChilds();
38 assertEquals(7, referencingChilds.size());
40 final QNameModule bazQNameModule = QNameModule.create(XMLNamespace.of(BAZ_NS));
41 final LeafRefPath expectedPathToBazTarget = LeafRefPath.create(true,
42 new QNameWithPredicateImpl(bazQNameModule, "root", ImmutableList.of()),
43 new QNameWithPredicateImpl(bazQNameModule, "target", ImmutableList.of()));
44 final LeafRefContext myLeafCtx = referencingChilds.get(foo("my-leaf"));
45 assertLeafRef(myLeafCtx, expectedPathToBazTarget);
46 assertLeafRef(referencingChilds.get(foo("my-leaf-2")), expectedPathToBazTarget);
47 assertLeafRef(referencingChilds.get(foo("bar-base-leafref")), expectedPathToBazTarget);
48 assertLeafRef(referencingChilds.get(foo("bar-base-leafref-2")), expectedPathToBazTarget);
49 assertLeafRef(referencingChilds.get(bar("my-leafref-in-bar")), expectedPathToBazTarget);
50 assertLeafRef(referencingChilds.get(bar("my-leafref-in-bar-2")), expectedPathToBazTarget);
52 final QNameModule barQNameModule = QNameModule.create(XMLNamespace.of(BAR_NS));
53 final LeafRefPath expectedPathToBarTarget = LeafRefPath.create(true,
54 new QNameWithPredicateImpl(barQNameModule, "bar-target", ImmutableList.of()));
55 assertLeafRef(referencingChilds.get(foo("direct-leafref")), expectedPathToBarTarget);
57 final Map<QName, LeafRefContext> referencedByChilds = leafRefContext.getReferencedByChilds();
58 assertEquals(2, referencedByChilds.size());
60 final LeafRefContext rootCtx = referencedByChilds.get(baz("root"));
61 assertEquals(1, rootCtx.getReferencedByChilds().size());
62 assertTrue(rootCtx.getReferencingChilds().isEmpty());
63 assertFalse(rootCtx.isReferencing());
64 assertFalse(rootCtx.isReferenced());
66 final LeafRefContext targetCtx = rootCtx.getReferencedChildByName(baz("target"));
67 assertTrue(targetCtx.getReferencedByChilds().isEmpty());
68 assertTrue(targetCtx.getReferencingChilds().isEmpty());
69 assertTrue(targetCtx.isReferenced());
70 assertFalse(targetCtx.isReferencing());
72 final Map<QName, LeafRefContext> allReferencedByLeafRefCtxs = targetCtx.getAllReferencedByLeafRefCtxs();
73 assertEquals(6, allReferencedByLeafRefCtxs.size());
74 assertTrue(myLeafCtx == targetCtx.getReferencedByLeafRefCtxByName(foo("my-leaf")));
77 private static void assertLeafRef(final LeafRefContext leafRefToTest, final LeafRefPath expectedLeafRefPath) {
78 assertNotNull(leafRefToTest);
79 assertNotNull(expectedLeafRefPath);
80 assertTrue(leafRefToTest.getReferencedByChilds().isEmpty());
81 assertTrue(leafRefToTest.getReferencingChilds().isEmpty());
82 assertFalse(leafRefToTest.isReferenced());
83 assertTrue(leafRefToTest.isReferencing());
84 assertEquals(expectedLeafRefPath, leafRefToTest.getAbsoluteLeafRefTargetPath());
87 private static QName foo(final String localName) {
88 return QName.create(FOO_NS, localName);
91 private static QName bar(final String localName) {
92 return QName.create(BAR_NS, localName);
95 private static QName baz(final String localName) {
96 return QName.create(BAZ_NS, localName);