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