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