Adjust test suite parser update to conform with API changes
[yangtools.git] / yang / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / AugmentProcessTest.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.assertFalse;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.assertTrue;
16 import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource;
17
18 import java.net.URI;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.common.QNameModule;
22 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
24 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.Module;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
33 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
34
35 public class AugmentProcessTest {
36
37     private static final StatementStreamSource AUGMENTED = sourceForResource("/stmt-test/augments/augmented.yang");
38     private static final StatementStreamSource ROOT = sourceForResource("/stmt-test/augments/aug-root.yang");
39
40     private static final QNameModule ROOT_QNAME_MODULE = QNameModule.create(URI.create("root"));
41     private static final QNameModule AUGMENTED_QNAME_MODULE = QNameModule.create(URI.create("aug"));
42
43     private final QName augParent1 = QName.create(AUGMENTED_QNAME_MODULE,
44             "aug-parent1");
45     private final QName augParent2 = QName.create(AUGMENTED_QNAME_MODULE,
46             "aug-parent2");
47     private final QName contTarget = QName.create(AUGMENTED_QNAME_MODULE,
48             "cont-target");
49
50     private final QName contAdded1 = QName.create(ROOT_QNAME_MODULE,
51             "cont-added1");
52     private final QName contAdded2 = QName.create(ROOT_QNAME_MODULE,
53             "cont-added2");
54
55     private final QName list1 = QName.create(ROOT_QNAME_MODULE, "list1");
56     private final QName axml = QName.create(ROOT_QNAME_MODULE, "axml");
57
58     private final QName contGrp = QName.create(ROOT_QNAME_MODULE,
59             "cont-grp");
60     private final QName axmlGrp = QName.create(ROOT_QNAME_MODULE,
61             "axml-grp");
62
63     private final QName augCont1 = QName.create(ROOT_QNAME_MODULE, "aug-cont1");
64     private final QName augCont2 = QName.create(ROOT_QNAME_MODULE, "aug-cont2");
65
66     private final QName grpCont2 = QName.create(ROOT_QNAME_MODULE, "grp-cont2");
67     private final QName grpCont22 = QName.create(ROOT_QNAME_MODULE,
68             "grp-cont22");
69     private final QName grpAdd = QName.create(ROOT_QNAME_MODULE, "grp-add");
70
71     private static final StatementStreamSource MULTIPLE_AUGMENT = sourceForResource(
72             "/stmt-test/augments/multiple-augment-test.yang");
73
74     private static final StatementStreamSource MULTIPLE_AUGMENT_ROOT = sourceForResource(
75             "/stmt-test/augments/multiple-augment-root.yang");
76     private static final StatementStreamSource MULTIPLE_AUGMENT_IMPORTED = sourceForResource(
77             "/stmt-test/augments/multiple-augment-imported.yang");
78     private static final StatementStreamSource MULTIPLE_AUGMENT_SUBMODULE = sourceForResource(
79             "/stmt-test/augments/multiple-augment-submodule.yang");
80
81     private static final StatementStreamSource MULTIPLE_AUGMENT_INCORRECT = sourceForResource(
82             "/stmt-test/augments/multiple-augment-incorrect.yang");
83
84     private static final StatementStreamSource MULTIPLE_AUGMENT_INCORRECT2 = sourceForResource(
85             "/stmt-test/augments/multiple-augment-incorrect2.yang");
86
87     @Test
88     public void multipleAugmentsAndMultipleModulesTest() throws ReactorException {
89         SchemaContext result = RFC7950Reactors.defaultReactor().newBuild()
90                 .addSources(MULTIPLE_AUGMENT_ROOT, MULTIPLE_AUGMENT_IMPORTED, MULTIPLE_AUGMENT_SUBMODULE)
91                 .buildEffective();
92         assertNotNull(result);
93     }
94
95     @Test
96     public void multipleAugmentTest() throws ReactorException {
97         SchemaContext result = RFC7950Reactors.defaultReactor().newBuild()
98                 .addSource(MULTIPLE_AUGMENT)
99                 .buildEffective();
100         assertNotNull(result);
101     }
102
103     @Test(expected = SomeModifiersUnresolvedException.class)
104     public void multipleAugmentIncorrectPathTest() throws  ReactorException {
105         SchemaContext result = RFC7950Reactors.defaultReactor().newBuild()
106                 .addSource(MULTIPLE_AUGMENT_INCORRECT)
107                 .buildEffective();
108         assertNull(result);
109     }
110
111     @Test(expected = SomeModifiersUnresolvedException.class)
112     public void multipleAugmentIncorrectPathAndGrpTest() throws  ReactorException {
113         SchemaContext result = RFC7950Reactors.defaultReactor().newBuild()
114                 .addSource(MULTIPLE_AUGMENT_INCORRECT2)
115                 .buildEffective();
116         assertNull(result);
117     }
118
119     @Test
120     public void readAndParseYangFileTest() throws ReactorException {
121         final SchemaContext root = RFC7950Reactors.defaultReactor().newBuild()
122                 .addSources(AUGMENTED, ROOT)
123                 .buildEffective();
124         assertNotNull(root);
125
126         final Module augmentedModule = root.findModules("augmented").iterator().next();
127         assertNotNull(augmentedModule);
128
129         final ContainerSchemaNode augParent1Node = (ContainerSchemaNode) root.getDataChildByName(augParent1);
130         final ContainerSchemaNode augParent2Node = (ContainerSchemaNode) augParent1Node.getDataChildByName(augParent2);
131         final ContainerSchemaNode targetContNode = (ContainerSchemaNode) augParent2Node.getDataChildByName(contTarget);
132         assertNotNull(targetContNode);
133
134         assertNotNull(targetContNode.getChildNodes());
135         assertEquals(3, targetContNode.getChildNodes().size());
136
137         final ContainerSchemaNode contAdded1Node = (ContainerSchemaNode) targetContNode.getDataChildByName(contAdded1);
138         assertNotNull(contAdded1Node);
139         final ListSchemaNode list1Node = (ListSchemaNode) contAdded1Node.getDataChildByName(list1);
140         assertNotNull(list1Node);
141
142         final ContainerSchemaNode contAdded2Node = (ContainerSchemaNode) targetContNode.getDataChildByName(contAdded2);
143         assertNotNull(contAdded2Node);
144         final AnyXmlSchemaNode axmlNode = (AnyXmlSchemaNode) contAdded2Node.getDataChildByName(axml);
145         assertNotNull(axmlNode);
146
147         final ContainerSchemaNode contGrpNode = (ContainerSchemaNode) targetContNode.getDataChildByName(contGrp);
148         assertNotNull(contGrpNode);
149         final AnyXmlSchemaNode axmlGrpNode = (AnyXmlSchemaNode) contGrpNode.getDataChildByName(axmlGrp);
150         assertNotNull(axmlGrpNode);
151
152         final ContainerSchemaNode augCont1Node = (ContainerSchemaNode) root.getDataChildByName(augCont1);
153         final ContainerSchemaNode augCont2Node = (ContainerSchemaNode) augCont1Node.getDataChildByName(augCont2);
154         assertNotNull(augCont2Node);
155
156         final ContainerSchemaNode grpCont2Node = (ContainerSchemaNode) augCont2Node.getDataChildByName(grpCont2);
157         final ContainerSchemaNode grpCont22Node = (ContainerSchemaNode) grpCont2Node.getDataChildByName(grpCont22);
158         assertNotNull(grpCont22Node);
159
160         final ContainerSchemaNode grpAddNode = (ContainerSchemaNode) grpCont22Node.getDataChildByName(grpAdd);
161         assertNotNull(grpAddNode);
162     }
163
164     @Test
165     public void caseShortHandAugmentingTest() throws Exception {
166         final SchemaContext context = StmtTestUtils.parseYangSources("/choice-case-type-test-models");
167
168         assertNotNull(context);
169
170         final String rev = "2013-07-01";
171         final String ns = "urn:ietf:params:xml:ns:yang:choice-monitoring";
172         final String nsAug = "urn:ietf:params:xml:ns:yang:augment-monitoring";
173
174         final ContainerSchemaNode netconf = (ContainerSchemaNode) context.getDataChildByName(QName.create(ns, rev,
175                 "netconf-state"));
176         final ContainerSchemaNode datastores = (ContainerSchemaNode) netconf.getDataChildByName(QName.create(ns, rev,
177                 "datastores"));
178         final ListSchemaNode datastore = (ListSchemaNode) datastores.getDataChildByName(QName.create(ns, rev,
179                 "datastore"));
180         final ContainerSchemaNode locks = (ContainerSchemaNode) datastore.getDataChildByName(QName.create(ns, rev,
181                 "locks"));
182         final ChoiceSchemaNode lockType = (ChoiceSchemaNode) locks.getDataChildByName(QName
183                 .create(ns, rev, "lock-type"));
184
185         final ChoiceCaseNode leafAugCase = lockType.findCaseNodes("leaf-aug-case").iterator().next();
186         assertTrue(leafAugCase.isAugmenting());
187         final DataSchemaNode leafAug = leafAugCase.getDataChildByName(QName.create(nsAug, rev, "leaf-aug-case"));
188         assertFalse(leafAug.isAugmenting());
189     }
190
191 }