1736e27360cdef86a0795da8e211ab43f34d3b02
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / leafref / Bug7844Test.java
1 /*
2  * Copyright (c) 2017 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.impl.leafref;
9
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;
14
15 import com.google.common.collect.ImmutableList;
16 import java.net.URI;
17 import java.util.Map;
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;
24
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";
30
31     @Test
32     public void test() throws Exception {
33         final SchemaContext context = YangParserTestUtils.parseYangSources("/bug7844");
34         assertNotNull(context);
35
36         final LeafRefContext leafRefContext = LeafRefContext.create(context);
37         assertNotNull(leafRefContext);
38
39         final Map<QName, LeafRefContext> referencingChilds = leafRefContext.getReferencingChilds();
40         assertEquals(7, referencingChilds.size());
41
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);
54
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);
60
61         final Map<QName, LeafRefContext> referencedByChilds = leafRefContext.getReferencedByChilds();
62         assertEquals(2, referencedByChilds.size());
63
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());
69
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());
75
76         final Map<QName, LeafRefContext> allReferencedByLeafRefCtxs = targetCtx.getAllReferencedByLeafRefCtxs();
77         assertEquals(6, allReferencedByLeafRefCtxs.size());
78         assertTrue(myLeafCtx == targetCtx.getReferencedByLeafRefCtxByName(foo("my-leaf")));
79     }
80
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());
89     }
90
91     private static QName foo(final String localName) {
92         return QName.create(FOO_NS, REV, localName);
93     }
94
95     private static QName bar(final String localName) {
96         return QName.create(BAR_NS, REV, localName);
97     }
98
99     private static QName baz(final String localName) {
100         return QName.create(BAZ_NS, REV, localName);
101     }
102 }