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;
18 import org.junit.Test;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.common.QNameModule;
21 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
22 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
23 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
25 public class Bug7844Test {
26 private static final String FOO_NS = "foo";
27 private static final String BAR_NS = "bar";
28 private static final String BAZ_NS = "baz";
29 private static final String REV = "1970-01-01";
32 public void test() throws Exception {
33 final SchemaContext context = YangParserTestUtils.parseYangResourceDirectory("/bug7844");
34 assertNotNull(context);
36 final LeafRefContext leafRefContext = LeafRefContext.create(context);
37 assertNotNull(leafRefContext);
39 final Map<QName, LeafRefContext> referencingChilds = leafRefContext.getReferencingChilds();
40 assertEquals(7, referencingChilds.size());
42 final QNameModule bazQNameModule = QNameModule.create(new URI(BAZ_NS),
43 SimpleDateFormatUtil.getRevisionFormat().parse(REV));
44 final LeafRefPath expectedPathToBazTarget = LeafRefPath.create(true,
45 new QNameWithPredicateImpl(bazQNameModule, "root", ImmutableList.of()),
46 new QNameWithPredicateImpl(bazQNameModule, "target", ImmutableList.of()));
47 final LeafRefContext myLeafCtx = referencingChilds.get(foo("my-leaf"));
48 assertLeafRef(myLeafCtx, expectedPathToBazTarget);
49 assertLeafRef(referencingChilds.get(foo("my-leaf-2")), expectedPathToBazTarget);
50 assertLeafRef(referencingChilds.get(foo("bar-base-leafref")), expectedPathToBazTarget);
51 assertLeafRef(referencingChilds.get(foo("bar-base-leafref-2")), expectedPathToBazTarget);
52 assertLeafRef(referencingChilds.get(bar("my-leafref-in-bar")), expectedPathToBazTarget);
53 assertLeafRef(referencingChilds.get(bar("my-leafref-in-bar-2")), expectedPathToBazTarget);
55 final QNameModule barQNameModule = QNameModule.create(new URI(BAR_NS),
56 SimpleDateFormatUtil.getRevisionFormat().parse(REV));
57 final LeafRefPath expectedPathToBarTarget = LeafRefPath.create(true,
58 new QNameWithPredicateImpl(barQNameModule, "bar-target", ImmutableList.of()));
59 assertLeafRef(referencingChilds.get(foo("direct-leafref")), expectedPathToBarTarget);
61 final Map<QName, LeafRefContext> referencedByChilds = leafRefContext.getReferencedByChilds();
62 assertEquals(2, referencedByChilds.size());
64 final LeafRefContext rootCtx = referencedByChilds.get(baz("root"));
65 assertEquals(1, rootCtx.getReferencedByChilds().size());
66 assertTrue(rootCtx.getReferencingChilds().isEmpty());
67 assertFalse(rootCtx.isReferencing());
68 assertFalse(rootCtx.isReferenced());
70 final LeafRefContext targetCtx = rootCtx.getReferencedChildByName(baz("target"));
71 assertTrue(targetCtx.getReferencedByChilds().isEmpty());
72 assertTrue(targetCtx.getReferencingChilds().isEmpty());
73 assertTrue(targetCtx.isReferenced());
74 assertFalse(targetCtx.isReferencing());
76 final Map<QName, LeafRefContext> allReferencedByLeafRefCtxs = targetCtx.getAllReferencedByLeafRefCtxs();
77 assertEquals(6, allReferencedByLeafRefCtxs.size());
78 assertTrue(myLeafCtx == targetCtx.getReferencedByLeafRefCtxByName(foo("my-leaf")));
81 private static void assertLeafRef(final LeafRefContext leafRefToTest, final LeafRefPath expectedLeafRefPath) {
82 assertNotNull(leafRefToTest);
83 assertNotNull(expectedLeafRefPath);
84 assertTrue(leafRefToTest.getReferencedByChilds().isEmpty());
85 assertTrue(leafRefToTest.getReferencingChilds().isEmpty());
86 assertFalse(leafRefToTest.isReferenced());
87 assertTrue(leafRefToTest.isReferencing());
88 assertEquals(expectedLeafRefPath, leafRefToTest.getAbsoluteLeafRefTargetPath());
91 private static QName foo(final String localName) {
92 return QName.create(FOO_NS, REV, localName);
95 private static QName bar(final String localName) {
96 return QName.create(BAR_NS, REV, localName);
99 private static QName baz(final String localName) {
100 return QName.create(BAZ_NS, REV, localName);