BUG-7052: extract SimpleSchemaContext
[yangtools.git] / yang / yang-parser-impl / 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
9 package org.opendaylight.yangtools.yang.stmt;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14
15 import java.util.Date;
16 import org.junit.Ignore;
17 import org.junit.Test;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
20 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.Module;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
25 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.LeafEffectiveStatementImpl;
26 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.UnitsEffectiveStatementImpl;
27
28 public class Bug6972Test {
29
30     @Ignore
31     @Test
32     public void allUnitsShouldBeTheSameInstance() throws Exception {
33         final SchemaContext schemaContext = StmtTestUtils.parseYangSources("/bugs/bug6972");
34         assertNotNull(schemaContext);
35         assertEquals(3, schemaContext.getModules().size());
36
37         final Date revision = SimpleDateFormatUtil.getRevisionFormat().parse("2016-10-20");
38
39         final Module foo = schemaContext.findModuleByName("foo", revision);
40         assertNotNull(foo);
41         final Module bar = schemaContext.findModuleByName("bar", revision);
42         assertNotNull(bar);
43         final Module baz = schemaContext.findModuleByName("baz", revision);
44         assertNotNull(baz);
45
46         final QName barExportCont = QName.create("bar-ns", "bar-export", revision);
47         final QName barFooCont = QName.create("bar-ns", "bar-foo", revision);
48         final QName barFooLeaf= QName.create("bar-ns", "foo", revision);
49
50         final UnitsEffectiveStatementImpl unitsBar1 = getEffectiveUnits(bar, barExportCont, barFooLeaf);
51         final UnitsEffectiveStatementImpl unitsBar2 = getEffectiveUnits(bar, barFooCont, barFooLeaf);
52
53         final QName bazExportCont = QName.create("baz-ns", "baz-export", revision);
54         final QName bazFooCont = QName.create("baz-ns", "baz-foo", revision);
55         final QName bazFooLeaf= QName.create("baz-ns", "foo", revision);
56
57         final UnitsEffectiveStatementImpl unitsBaz1 = getEffectiveUnits(baz, bazExportCont, bazFooLeaf);
58         final UnitsEffectiveStatementImpl unitsBaz2 = getEffectiveUnits(baz, bazFooCont, bazFooLeaf);
59
60         assertTrue(unitsBar1 == unitsBar2 && unitsBar1 == unitsBaz1 && unitsBar1 == unitsBaz2);
61     }
62
63     private static UnitsEffectiveStatementImpl getEffectiveUnits(final Module module, final QName containerQName,
64             final QName leafQName) {
65         UnitsEffectiveStatementImpl units = null;
66
67         final ContainerSchemaNode cont = (ContainerSchemaNode) module.getDataChildByName(containerQName);
68         assertNotNull(cont);
69         final LeafSchemaNode leaf = (LeafSchemaNode) cont.getDataChildByName(leafQName);
70         assertNotNull(leaf);
71
72         for (EffectiveStatement<?, ?> effStmt : ((LeafEffectiveStatementImpl) leaf).effectiveSubstatements()) {
73             if (effStmt instanceof UnitsEffectiveStatementImpl) {
74                 units = (UnitsEffectiveStatementImpl) effStmt;
75                 break;
76             }
77         }
78
79         return units;
80     }
81 }