Enable Bug6972Test
[yangtools.git] / yang / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / Bug6972Test.java
1 /*
2  * Copyright (c) 2016 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.stmt;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertSame;
13
14 import org.junit.Test;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.common.Revision;
17 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.Module;
20 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
21 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.LeafEffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.UnitsEffectiveStatement;
24
25 public class Bug6972Test {
26     @Test
27     public void allUnitsShouldBeTheSameInstance() throws Exception {
28         final SchemaContext schemaContext = StmtTestUtils.parseYangSources("/bugs/bug6972");
29         assertNotNull(schemaContext);
30         assertEquals(3, schemaContext.getModules().size());
31
32         final Revision revision = Revision.of("2016-10-20");
33         final Module foo = schemaContext.findModule("foo", revision).get();
34         final Module bar = schemaContext.findModule("bar", revision).get();
35         final Module baz = schemaContext.findModule("baz", revision).get();
36
37         final QName barExportCont = QName.create("bar-ns", "bar-export", revision);
38         final QName barFooCont = QName.create("bar-ns", "bar-foo", revision);
39         final QName barFooLeaf = QName.create("bar-ns", "foo", revision);
40
41         final UnitsEffectiveStatement unitsBar1 = getEffectiveUnits(bar, barExportCont, barFooLeaf);
42         assertSame(unitsBar1, getEffectiveUnits(bar, barFooCont, barFooLeaf));
43
44         final QName bazExportCont = QName.create("baz-ns", "baz-export", revision);
45         final QName bazFooCont = QName.create("baz-ns", "baz-foo", revision);
46         final QName bazFooLeaf = QName.create("baz-ns", "foo", revision);
47
48         assertSame(unitsBar1, getEffectiveUnits(baz, bazExportCont, bazFooLeaf));
49         assertSame(unitsBar1, getEffectiveUnits(baz, bazFooCont, bazFooLeaf));
50     }
51
52     private static UnitsEffectiveStatement getEffectiveUnits(final Module module, final QName containerQName,
53             final QName leafQName) {
54         UnitsEffectiveStatement units = null;
55
56         final ContainerSchemaNode cont = (ContainerSchemaNode) module.getDataChildByName(containerQName);
57         assertNotNull(cont);
58         final LeafSchemaNode leaf = (LeafSchemaNode) cont.getDataChildByName(leafQName);
59         assertNotNull(leaf);
60
61         for (EffectiveStatement<?, ?> effStmt : ((LeafEffectiveStatement) leaf).effectiveSubstatements()) {
62             if (effStmt instanceof UnitsEffectiveStatement) {
63                 units = (UnitsEffectiveStatement) effStmt;
64                 break;
65             }
66         }
67
68         return units;
69     }
70 }