Revert "Unify ORv1 and IIv5"
[yangtools.git] / parser / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / Bug6183Test.java
1 /*
2  * Copyright (c) 2017 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.assertFalse;
12 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
13 import static org.junit.jupiter.api.Assertions.assertTrue;
14
15 import org.junit.jupiter.api.Test;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
20 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
21 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
22
23 public class Bug6183Test extends AbstractYangTest {
24     private static final String FOO_NS = "foo";
25
26     @Test
27     void testYang10() {
28         assertSchemaContext(assertEffectiveModelDir("/bugs/bug6183/yang10"));
29     }
30
31     @Test
32     void testYang11() {
33         assertSchemaContext(assertEffectiveModelDir("/bugs/bug6183/yang11"));
34     }
35
36     public void assertSchemaContext(final EffectiveModelContext context) {
37         assertEquals(3, context.getChildNodes().size());
38         assertEquals(1, context.getModules().size());
39         assertEquals(4, context.getModules().iterator().next().getAugmentations().size());
40
41         assertInstanceOf(ContainerSchemaNode.class, context.getDataChildByName(foo("before")));
42         assertInstanceOf(ContainerSchemaNode.class, context.getDataChildByName(foo("after")));
43
44         final var dataChildByName = context.getDataChildByName(foo("my-choice"));
45         final var myChoice = assertInstanceOf(ChoiceSchemaNode.class, dataChildByName);
46
47         assertEquals(4, myChoice.getCases().size());
48
49         final var implCase = myChoice.findCaseNode(foo("implicit-case-container")).orElseThrow();
50         final var declCaseOne = myChoice.findCaseNode(foo("declared-case-one")).orElseThrow();
51         final var secondImplCase = myChoice.findCaseNode(foo("second-implicit-case-container")).orElseThrow();
52         final var declCaseTwo = myChoice.findCaseNode(foo("declared-case-two")).orElseThrow();
53
54         assertEquals(1, declCaseOne.getChildNodes().size());
55         assertFalse(getLeafSchemaNode(declCaseOne, "leaf-in-declare-case-one").isAugmenting());
56         assertEquals(1, declCaseTwo.getChildNodes().size());
57         assertFalse(getLeafSchemaNode(declCaseTwo, "leaf-in-declare-case-two").isAugmenting());
58
59         assertEquals(2, implCase.getChildNodes().size());
60         assertTrue(getLeafSchemaNode(implCase, "leaf-after-container").isAugmenting());
61         final var implCaseContainer = getContainerSchemaNode(implCase, "implicit-case-container");
62
63         assertEquals(3, implCaseContainer.getChildNodes().size());
64         assertTrue(getLeafSchemaNode(implCaseContainer, "leaf-inside-container").isAugmenting());
65         assertFalse(getLeafSchemaNode(implCaseContainer, "declared-leaf-in-case-container").isAugmenting());
66         final var declContInCaseCont = getContainerSchemaNode(implCaseContainer,
67                 "declared-container-in-case-container");
68
69         assertEquals(1, declContInCaseCont.getChildNodes().size());
70         assertFalse(getLeafSchemaNode(declContInCaseCont, "declared-leaf").isAugmenting());
71
72         assertEquals(2, secondImplCase.getChildNodes().size());
73         assertTrue(getLeafSchemaNode(secondImplCase, "leaf-after-second-container").isAugmenting());
74         final var secondImplCaseContainer = getContainerSchemaNode(secondImplCase, "second-implicit-case-container");
75
76         assertEquals(2, secondImplCaseContainer.getChildNodes().size());
77         assertTrue(getLeafSchemaNode(secondImplCaseContainer, "leaf-inside-second-container").isAugmenting());
78         assertFalse(getLeafSchemaNode(secondImplCaseContainer, "declared-leaf-in-second-case-container")
79             .isAugmenting());
80     }
81
82     private static ContainerSchemaNode getContainerSchemaNode(final DataNodeContainer parent,
83             final String containerName) {
84         return assertInstanceOf(ContainerSchemaNode.class, parent.getDataChildByName(foo(containerName)));
85     }
86
87     private static LeafSchemaNode getLeafSchemaNode(final DataNodeContainer parent, final String leafName) {
88         return assertInstanceOf(LeafSchemaNode.class, parent.getDataChildByName(foo(leafName)));
89     }
90
91     private static QName foo(final String localName) {
92         return QName.create(FOO_NS, localName);
93     }
94 }