BUG-8043: correct LengthConstraint definition
[yangtools.git] / yang / yang-parser-impl / 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.common.SimpleDateFormatUtil;
23 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
25 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.Module;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
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.SourceException;
34 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
35 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
36 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
37
38 public class AugmentProcessTest {
39
40     private static final StatementStreamSource AUGMENTED = sourceForResource("/stmt-test/augments/augmented.yang");
41     private static final StatementStreamSource ROOT = sourceForResource("/stmt-test/augments/aug-root.yang");
42
43     private static final QNameModule ROOT_QNAME_MODULE = QNameModule.create(
44             URI.create("root"), SimpleDateFormatUtil.DEFAULT_DATE_REV);
45     private static final QNameModule AUGMENTED_QNAME_MODULE = QNameModule
46             .create(URI.create("aug"), SimpleDateFormatUtil.DEFAULT_DATE_REV);
47
48     private final QName augParent1 = QName.create(AUGMENTED_QNAME_MODULE,
49             "aug-parent1");
50     private final QName augParent2 = QName.create(AUGMENTED_QNAME_MODULE,
51             "aug-parent2");
52     private final QName contTarget = QName.create(AUGMENTED_QNAME_MODULE,
53             "cont-target");
54
55     private final QName contAdded1 = QName.create(ROOT_QNAME_MODULE,
56             "cont-added1");
57     private final QName contAdded2 = QName.create(ROOT_QNAME_MODULE,
58             "cont-added2");
59
60     private final QName list1 = QName.create(ROOT_QNAME_MODULE, "list1");
61     private final QName axml = QName.create(ROOT_QNAME_MODULE, "axml");
62
63     private final QName contGrp = QName.create(ROOT_QNAME_MODULE,
64             "cont-grp");
65     private final QName axmlGrp = QName.create(ROOT_QNAME_MODULE,
66             "axml-grp");
67
68     private final QName augCont1 = QName.create(ROOT_QNAME_MODULE, "aug-cont1");
69     private final QName augCont2 = QName.create(ROOT_QNAME_MODULE, "aug-cont2");
70
71     private final QName grpCont2 = QName.create(ROOT_QNAME_MODULE, "grp-cont2");
72     private final QName grpCont22 = QName.create(ROOT_QNAME_MODULE,
73             "grp-cont22");
74     private final QName grpAdd = QName.create(ROOT_QNAME_MODULE, "grp-add");
75
76     private static final StatementStreamSource MULTIPLE_AUGMENT = sourceForResource(
77             "/stmt-test/augments/multiple-augment-test.yang");
78
79     private static final StatementStreamSource MULTIPLE_AUGMENT_ROOT = sourceForResource(
80             "/stmt-test/augments/multiple-augment-root.yang");
81     private static final StatementStreamSource MULTIPLE_AUGMENT_IMPORTED = sourceForResource(
82             "/stmt-test/augments/multiple-augment-imported.yang");
83     private static final StatementStreamSource MULTIPLE_AUGMENT_SUBMODULE = sourceForResource(
84             "/stmt-test/augments/multiple-augment-submodule.yang");
85
86     private static final StatementStreamSource MULTIPLE_AUGMENT_INCORRECT = sourceForResource(
87             "/stmt-test/augments/multiple-augment-incorrect.yang");
88
89     private static final StatementStreamSource MULTIPLE_AUGMENT_INCORRECT2 = sourceForResource(
90             "/stmt-test/augments/multiple-augment-incorrect2.yang");
91
92     @Test
93     public void multipleAugmentsAndMultipleModulesTest() throws SourceException, ReactorException {
94         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
95                 .newBuild();
96         addSources(reactor, MULTIPLE_AUGMENT_ROOT, MULTIPLE_AUGMENT_IMPORTED,
97                 MULTIPLE_AUGMENT_SUBMODULE);
98
99         SchemaContext result = reactor.buildEffective();
100         assertNotNull(result);
101     }
102
103     @Test
104     public void multipleAugmentTest() throws SourceException, ReactorException {
105         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
106                 .newBuild();
107         addSources(reactor, MULTIPLE_AUGMENT);
108
109         SchemaContext result = reactor.buildEffective();
110         assertNotNull(result);
111     }
112
113     @Test(expected = SomeModifiersUnresolvedException.class)
114     public void multipleAugmentIncorrectPathTest() throws SourceException, ReactorException {
115         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
116                 .newBuild();
117         addSources(reactor, MULTIPLE_AUGMENT_INCORRECT);
118
119         SchemaContext result = reactor.buildEffective();
120         assertNull(result);
121     }
122
123     @Test(expected = SomeModifiersUnresolvedException.class)
124     public void multipleAugmentIncorrectPathAndGrpTest() throws SourceException, ReactorException {
125         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
126                 .newBuild();
127         addSources(reactor, MULTIPLE_AUGMENT_INCORRECT2);
128         SchemaContext result = reactor.buildEffective();
129         assertNull(result);
130     }
131
132     @Test
133     public void readAndParseYangFileTest() throws SourceException, ReactorException {
134         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
135                 .newBuild();
136         addSources(reactor, AUGMENTED, ROOT);
137
138         final SchemaContext root = reactor.buildEffective();
139         assertNotNull(root);
140
141         final Module augmentedModule = root.findModuleByName("augmented", null);
142         assertNotNull(augmentedModule);
143
144         final ContainerSchemaNode augParent1Node = (ContainerSchemaNode) root
145                 .getDataChildByName(augParent1);
146         final ContainerSchemaNode augParent2Node = (ContainerSchemaNode) augParent1Node
147                 .getDataChildByName(augParent2);
148         final ContainerSchemaNode targetContNode = (ContainerSchemaNode) augParent2Node
149                 .getDataChildByName(contTarget);
150         assertNotNull(targetContNode);
151
152         assertNotNull(targetContNode.getChildNodes());
153         assertEquals(3, targetContNode.getChildNodes().size());
154
155         final ContainerSchemaNode contAdded1Node = (ContainerSchemaNode) targetContNode
156                 .getDataChildByName(contAdded1);
157         assertNotNull(contAdded1Node);
158         final ListSchemaNode list1Node = (ListSchemaNode) contAdded1Node
159                 .getDataChildByName(list1);
160         assertNotNull(list1Node);
161
162         final ContainerSchemaNode contAdded2Node = (ContainerSchemaNode) targetContNode
163                 .getDataChildByName(contAdded2);
164         assertNotNull(contAdded2Node);
165         final AnyXmlSchemaNode axmlNode = (AnyXmlSchemaNode) contAdded2Node
166                 .getDataChildByName(axml);
167         assertNotNull(axmlNode);
168
169         final ContainerSchemaNode contGrpNode = (ContainerSchemaNode) targetContNode
170                 .getDataChildByName(contGrp);
171         assertNotNull(contGrpNode);
172         final AnyXmlSchemaNode axmlGrpNode = (AnyXmlSchemaNode) contGrpNode
173                 .getDataChildByName(axmlGrp);
174         assertNotNull(axmlGrpNode);
175
176         final ContainerSchemaNode augCont1Node = (ContainerSchemaNode) root
177                 .getDataChildByName(augCont1);
178         final ContainerSchemaNode augCont2Node = (ContainerSchemaNode) augCont1Node
179                 .getDataChildByName(augCont2);
180         assertNotNull(augCont2Node);
181
182         final ContainerSchemaNode grpCont2Node = (ContainerSchemaNode) augCont2Node
183                 .getDataChildByName(grpCont2);
184         final ContainerSchemaNode grpCont22Node = (ContainerSchemaNode) grpCont2Node
185                 .getDataChildByName(grpCont22);
186         assertNotNull(grpCont22Node);
187
188         final ContainerSchemaNode grpAddNode = (ContainerSchemaNode) grpCont22Node
189                 .getDataChildByName(grpAdd);
190         assertNotNull(grpAddNode);
191     }
192
193     private static void addSources(final CrossSourceStatementReactor.BuildAction reactor,
194             final StatementStreamSource... sources) {
195         for (final StatementStreamSource source : sources) {
196             reactor.addSource(source);
197         }
198     }
199
200     @Test
201     public void caseShortHandAugmentingTest() throws Exception {
202         final SchemaContext context = StmtTestUtils.parseYangSources("/choice-case-type-test-models");
203
204         assertNotNull(context);
205
206         final String rev = "2013-07-01";
207         final String ns = "urn:ietf:params:xml:ns:yang:choice-monitoring";
208         final String nsAug = "urn:ietf:params:xml:ns:yang:augment-monitoring";
209
210         final ContainerSchemaNode netconf = (ContainerSchemaNode) context.getDataChildByName(QName.create(ns, rev,
211                 "netconf-state"));
212         final ContainerSchemaNode datastores = (ContainerSchemaNode) netconf.getDataChildByName(QName.create(ns, rev,
213                 "datastores"));
214         final ListSchemaNode datastore = (ListSchemaNode) datastores.getDataChildByName(QName.create(ns, rev,
215                 "datastore"));
216         final ContainerSchemaNode locks = (ContainerSchemaNode) datastore.getDataChildByName(QName.create(ns, rev,
217                 "locks"));
218         final ChoiceSchemaNode lockType = (ChoiceSchemaNode) locks.getDataChildByName(QName
219                 .create(ns, rev, "lock-type"));
220
221         final ChoiceCaseNode leafAugCase = lockType.getCaseNodeByName("leaf-aug-case");
222         assertTrue(leafAugCase.isAugmenting());
223         final DataSchemaNode leafAug = leafAugCase.getDataChildByName(QName.create(nsAug, rev, "leaf-aug-case"));
224         assertFalse(leafAug.isAugmenting());
225     }
226
227 }