Port yang-parser-rfc7950 to JUnit 5
[yangtools.git] / parser / 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.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertNotNull;
12 import static org.junit.jupiter.api.Assertions.assertSame;
13
14 import org.junit.jupiter.api.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.stmt.LeafEffectiveStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.UnitsEffectiveStatement;
22
23 class Bug6972Test extends AbstractYangTest {
24     @Test
25     void allUnitsShouldBeTheSameInstance() {
26         final var schemaContext = assertEffectiveModelDir("/bugs/bug6972");
27         assertEquals(3, schemaContext.getModules().size());
28
29         final Revision revision = Revision.of("2016-10-20");
30         final var foo = schemaContext.findModule("foo", revision).orElseThrow();
31         final var bar = schemaContext.findModule("bar", revision).orElseThrow();
32         final var baz = schemaContext.findModule("baz", revision).orElseThrow();
33
34         final QName barExportCont = QName.create("bar-ns", "bar-export", revision);
35         final QName barFooCont = QName.create("bar-ns", "bar-foo", revision);
36         final QName barFooLeaf = QName.create("bar-ns", "foo", revision);
37
38         final var unitsBar1 = getEffectiveUnits(bar, barExportCont, barFooLeaf);
39         assertSame(unitsBar1, getEffectiveUnits(bar, barFooCont, barFooLeaf));
40
41         final QName bazExportCont = QName.create("baz-ns", "baz-export", revision);
42         final QName bazFooCont = QName.create("baz-ns", "baz-foo", revision);
43         final QName bazFooLeaf = QName.create("baz-ns", "foo", revision);
44
45         assertSame(unitsBar1, getEffectiveUnits(baz, bazExportCont, bazFooLeaf));
46         assertSame(unitsBar1, getEffectiveUnits(baz, bazFooCont, bazFooLeaf));
47     }
48
49     private static UnitsEffectiveStatement getEffectiveUnits(final Module module, final QName containerQName,
50             final QName leafQName) {
51         final var cont = (ContainerSchemaNode) module.getDataChildByName(containerQName);
52         assertNotNull(cont);
53         final var leaf = (LeafSchemaNode) cont.getDataChildByName(leafQName);
54         assertNotNull(leaf);
55
56         return ((LeafEffectiveStatement) leaf).streamEffectiveSubstatements(UnitsEffectiveStatement.class)
57             .findFirst()
58             .orElse(null);
59     }
60 }