9bf6bc26846ee14fcc0ec8a175ecc7c7417b8eb4
[yangtools.git] / yang / yang-parser-impl / 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.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14
15 import org.junit.Test;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
18 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
21 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24
25 public class Bug6183Test {
26     private static final String FOO_NS = "foo";
27
28     @Test
29     public void testYang10() throws Exception {
30         assertSchemaContext(StmtTestUtils.parseYangSources("/bugs/bug6183/yang10"));
31     }
32
33     @Test
34     public void testYang11() throws Exception {
35         assertSchemaContext(StmtTestUtils.parseYangSources("/bugs/bug6183/yang11"));
36     }
37
38     public void assertSchemaContext(final SchemaContext context) throws Exception {
39         assertNotNull(context);
40         assertEquals(3, context.getChildNodes().size());
41         assertEquals(1, context.getModules().size());
42         assertEquals(4, context.getModules().iterator().next().getAugmentations().size());
43
44         assertTrue(context.getDataChildByName(foo("before")) instanceof ContainerSchemaNode);
45         assertTrue(context.getDataChildByName(foo("after")) instanceof ContainerSchemaNode);
46
47         final DataSchemaNode dataChildByName = context.getDataChildByName(foo("my-choice"));
48         assertTrue(dataChildByName instanceof ChoiceSchemaNode);
49         final ChoiceSchemaNode myChoice = (ChoiceSchemaNode) dataChildByName;
50
51         assertEquals(4, myChoice.getCases().size());
52
53         final ChoiceCaseNode implCase = myChoice.getCaseNodeByName(foo("implicit-case-container"));
54         assertNotNull(implCase);
55         final ChoiceCaseNode declCaseOne = myChoice.getCaseNodeByName(foo("declared-case-one"));
56         assertNotNull(declCaseOne);
57         final ChoiceCaseNode secondImplCase = myChoice.getCaseNodeByName(foo("second-implicit-case-container"));
58         assertNotNull(secondImplCase);
59         final ChoiceCaseNode declCaseTwo = myChoice.getCaseNodeByName(foo("declared-case-two"));
60         assertNotNull(declCaseTwo);
61
62         assertEquals(1, declCaseOne.getChildNodes().size());
63         assertFalse(getLeafSchemaNode(declCaseOne, "leaf-in-declare-case-one").isAugmenting());
64         assertEquals(1, declCaseTwo.getChildNodes().size());
65         assertFalse(getLeafSchemaNode(declCaseTwo, "leaf-in-declare-case-two").isAugmenting());
66
67         assertEquals(2, implCase.getChildNodes().size());
68         assertTrue(getLeafSchemaNode(implCase, "leaf-after-container").isAugmenting());
69         final ContainerSchemaNode implCaseContainer = getContainerSchemaNode(implCase, "implicit-case-container");
70
71         assertEquals(3, implCaseContainer.getChildNodes().size());
72         assertTrue(getLeafSchemaNode(implCaseContainer, "leaf-inside-container").isAugmenting());
73         assertFalse(getLeafSchemaNode(implCaseContainer, "declared-leaf-in-case-container").isAugmenting());
74         final ContainerSchemaNode declContInCaseCont = getContainerSchemaNode(implCaseContainer,
75                 "declared-container-in-case-container");
76
77         assertEquals(1, declContInCaseCont.getChildNodes().size());
78         assertFalse(getLeafSchemaNode(declContInCaseCont, "declared-leaf").isAugmenting());
79
80         assertEquals(2, secondImplCase.getChildNodes().size());
81         assertTrue(getLeafSchemaNode(secondImplCase, "leaf-after-second-container").isAugmenting());
82         final ContainerSchemaNode secondImplCaseContainer = getContainerSchemaNode(secondImplCase,
83                 "second-implicit-case-container");
84
85         assertEquals(2, secondImplCaseContainer.getChildNodes().size());
86         assertTrue(getLeafSchemaNode(secondImplCaseContainer, "leaf-inside-second-container").isAugmenting());
87         assertFalse(getLeafSchemaNode(secondImplCaseContainer, "declared-leaf-in-second-case-container")
88             .isAugmenting());
89     }
90
91     private static ContainerSchemaNode getContainerSchemaNode(final DataNodeContainer parent,
92             final String containerName) {
93         final DataSchemaNode dataChildByName = parent.getDataChildByName(foo(containerName));
94         assertTrue(dataChildByName instanceof ContainerSchemaNode);
95         return (ContainerSchemaNode) dataChildByName;
96     }
97
98     private static LeafSchemaNode getLeafSchemaNode(final DataNodeContainer parent, final String leafName) {
99         final DataSchemaNode dataChildByName = parent.getDataChildByName(foo(leafName));
100         assertTrue(dataChildByName instanceof LeafSchemaNode);
101         return (LeafSchemaNode) dataChildByName;
102     }
103
104     private static QName foo(final String localName) {
105         return QName.create(FOO_NS, localName);
106     }
107 }