2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
3 * This program and the accompanying materials are made available under the
4 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
5 * and is available at http://www.eclipse.org/legal/epl-v10.html
7 package org.opendaylight.yangtools.yang.parser.builder.impl;
9 import static org.junit.Assert.assertEquals;
10 import static org.junit.Assert.assertFalse;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertTrue;
15 import java.text.SimpleDateFormat;
16 import java.util.Date;
17 import java.util.List;
18 import org.junit.Before;
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.SchemaPath;
23 import org.opendaylight.yangtools.yang.model.util.Uint16;
24 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
25 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
26 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
27 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
28 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
31 * Test suite for increasing of test coverage of ModuleBuilder implementation.
33 * @see org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder
35 * @author Lukas Sedlak <lsedlak@cisco.com>
37 * @deprecated Pre-Beryllium implementation, scheduled for removal.
40 public class ModuleBuilderTest {
42 private final static String TEST_MODULE_NAMESPACE = "urn:opendaylight.foo";
43 private final static String TEST_MODULE_REVISION = "2014-09-22";
44 private final static String MODULE_NAME = "TestModule";
45 private static final String TEST_MODULE_PATH = "/test/module/path";
47 private ModuleBuilder moduleBuilder;
50 public void setUp() throws Exception {
51 moduleBuilder = new ModuleBuilder(MODULE_NAME , TEST_MODULE_PATH);
55 public void testGetSetModuleBuilderMethods() throws Exception {
56 assertEquals("module", moduleBuilder.getStatementName());
57 assertEquals(TEST_MODULE_PATH, moduleBuilder.getModuleSourcePath());
58 assertNotNull(moduleBuilder.getAllLists());
59 final URI namespace = URI.create(TEST_MODULE_NAMESPACE + "?" + TEST_MODULE_REVISION);
60 moduleBuilder.setNamespace(namespace);
61 assertEquals(namespace, moduleBuilder.getNamespace());
62 moduleBuilder.setBelongsTo("TEST_SUBMODULE");
63 assertEquals("TEST_SUBMODULE", moduleBuilder.getBelongsTo());
64 final Date revision = new SimpleDateFormat("yyyy-dd-mm").parse(TEST_MODULE_REVISION);
65 moduleBuilder.setRevision(revision);
66 assertEquals(revision, moduleBuilder.getRevision());
69 @Test(expected = IllegalArgumentException.class)
70 public void testCheckPrefixFailOnPrefixNull() {
71 moduleBuilder.addModuleImport("TEST_MODULE", null, null);
74 @Test(expected = IllegalArgumentException.class)
75 public void testCheckPrefixFailOnPrefixStringIsEmpty() {
76 moduleBuilder.addModuleImport("TEST_MODULE", null, "");
79 @Test(expected = IllegalArgumentException.class)
80 public void testCheckPrefixFailOnPrefixEqualsModuleBuilderPrefix() {
81 moduleBuilder.setPrefix("prefix");
82 moduleBuilder.addModuleImport("TEST_MODULE", null, "prefix");
86 public void testGetActualNodeNullResult() {
87 moduleBuilder.exitNode();
88 assertNull(moduleBuilder.getActualNode());
91 @Test(expected = YangParseException.class)
92 public void testSetParent() throws Exception {
93 moduleBuilder.setParent(new Builder() {
95 public String getModuleName() {
100 public int getLine() {
105 public Builder getParent() {
110 public void setParent(final Builder parent) {
115 public void addUnknownNodeBuilder(final UnknownSchemaNodeBuilder unknownNode) {
120 public List<UnknownSchemaNodeBuilder> getUnknownNodes() {
125 public Object build() {
131 @Test(expected = YangParseException.class)
132 public void testAddExtensionWhereParentNodeIsNotModuleBuilder() throws Exception {
133 final QName containerQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "top-level-container");
134 SchemaPath containerPath = SchemaPath.create(true, containerQName);
135 moduleBuilder.enterNode(new ContainerSchemaNodeBuilder(MODULE_NAME, 10, containerQName, containerPath));
136 final QName extQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-ext");
137 SchemaPath extPath = SchemaPath.create(true, containerQName, extQName);
138 moduleBuilder.addExtension(extQName, 12, extPath);
141 @Test(expected = YangParseException.class)
142 public void testAddExtensionWhereNameOfExtensionCollides() throws Exception {
143 final QName extQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-ext");
144 SchemaPath extPath = SchemaPath.create(true, extQName);
145 moduleBuilder.addExtension(extQName, 12, extPath);
147 final QName extQName2 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-ext");
148 moduleBuilder.addExtension(extQName2, 22, extPath);
151 @Test(expected = YangParseException.class)
152 public void testAddDuplicateGroupingIntoModule() {
153 final QName testGroup1 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-group");
154 final SchemaPath groupPath1 = SchemaPath.create(true, testGroup1);
156 final QName testGroup2 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-group");
157 final SchemaPath groupPath2 = SchemaPath.create(true, testGroup2);
159 moduleBuilder.addGrouping(12, testGroup1, groupPath1);
160 moduleBuilder.addGrouping(12, testGroup2, groupPath2);
163 @Test(expected = YangParseException.class)
164 public void testAddDuplicateGroupingIntoDataNodeContainer() {
165 final QName containerQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "top-level-container");
166 SchemaPath containerPath = SchemaPath.create(true, containerQName);
167 moduleBuilder.enterNode(new ContainerSchemaNodeBuilder(MODULE_NAME, 10, containerQName, containerPath));
168 final QName testGroup1 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-group");
169 final SchemaPath groupPath1 = SchemaPath.create(true, containerQName, testGroup1);
171 final QName testGroup2 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-group");
172 final SchemaPath groupPath2 = SchemaPath.create(true, containerQName, testGroup2);
174 moduleBuilder.addGrouping(12, testGroup1, groupPath1);
175 moduleBuilder.addGrouping(22, testGroup2, groupPath2);
179 public void testAddTwoGroupingIntoDataNodeContainer() {
180 final QName containerQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "top-level-container");
181 SchemaPath containerPath = SchemaPath.create(true, containerQName);
182 moduleBuilder.enterNode(new ContainerSchemaNodeBuilder(MODULE_NAME, 10, containerQName, containerPath));
183 final QName testGroup1 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-group");
184 final SchemaPath groupPath1 = SchemaPath.create(true, containerQName, testGroup1);
186 final QName testGroup2 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-group2");
187 final SchemaPath groupPath2 = SchemaPath.create(true, containerQName, testGroup2);
189 GroupingBuilder grouping1 = moduleBuilder.addGrouping(12, testGroup1, groupPath1);
190 GroupingBuilder grouping2 = moduleBuilder.addGrouping(22, testGroup2, groupPath2);
192 assertNotNull(grouping1);
193 assertNotNull(grouping2);
196 @Test(expected = YangParseException.class)
197 public void testAddDuplicateGroupingIntoRpcDefinition() {
198 final QName rpcDefName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "rpc-definition");
199 SchemaPath rpcPath = SchemaPath.create(true, rpcDefName);
200 moduleBuilder.enterNode(new RpcDefinitionBuilder(MODULE_NAME, 10, rpcDefName, rpcPath));
201 final QName testGroup1 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-group");
202 final SchemaPath groupPath1 = SchemaPath.create(true, rpcDefName, testGroup1);
204 final QName testGroup2 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-group");
205 final SchemaPath groupPath2 = SchemaPath.create(true, rpcDefName, testGroup2);
207 moduleBuilder.addGrouping(12, testGroup1, groupPath1);
208 moduleBuilder.addGrouping(22, testGroup2, groupPath2);
212 public void testAddTwoGroupingsIntoRpcDefinition() {
213 final QName rpcDefName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "rpc-definition");
214 SchemaPath rpcPath = SchemaPath.create(true, rpcDefName);
215 moduleBuilder.enterNode(new RpcDefinitionBuilder(MODULE_NAME, 10, rpcDefName, rpcPath));
216 final QName testGroup1 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-group");
217 final SchemaPath groupPath1 = SchemaPath.create(true, rpcDefName, testGroup1);
219 final QName testGroup2 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-group2");
220 final SchemaPath groupPath2 = SchemaPath.create(true, rpcDefName, testGroup2);
222 GroupingBuilder grouping1 = moduleBuilder.addGrouping(12, testGroup1, groupPath1);
223 GroupingBuilder grouping2 = moduleBuilder.addGrouping(22, testGroup2, groupPath2);
225 assertNotNull(grouping1);
226 assertNotNull(grouping2);
229 @Test(expected = YangParseException.class)
230 public void testAddGroupingIntoNonValidStatementDefinition() {
231 final QName leafListDefName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-leaflist");
232 SchemaPath leaflistPath = SchemaPath.create(true, leafListDefName);
233 moduleBuilder.enterNode(new LeafListSchemaNodeBuilder(MODULE_NAME, 10, leafListDefName, leaflistPath));
234 final QName testGroup = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-group");
235 final SchemaPath groupPath = SchemaPath.create(true, leafListDefName, testGroup);
237 moduleBuilder.addGrouping(12, testGroup, groupPath);
240 @Test(expected = YangParseException.class)
241 public void testInvalidAugmentPathTargetString() {
242 final QName targetSchemaName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "fictive-target-container");
243 moduleBuilder.addAugment(12, "something/something", SchemaPath.create(true, targetSchemaName), 0);
246 @Test(expected = YangParseException.class)
247 public void testInvalidAugmentPathTargetStringInUsesNode() {
248 final QName usesNode = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "fictive-uses-node");
249 SchemaPath targetPath = SchemaPath.create(true, usesNode);
250 moduleBuilder.enterNode(new UsesNodeBuilderImpl(MODULE_NAME, 10, targetPath));
251 final QName targetSchemaName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "fictive-target-container");
252 moduleBuilder.addAugment(12, "/something/something", SchemaPath.create(true, targetSchemaName), 0);
255 @Test(expected = YangParseException.class)
256 public void testInvalidAugmentDefinition() {
257 final QName containerQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "top-level-container");
258 SchemaPath containerPath = SchemaPath.create(true, containerQName);
259 moduleBuilder.enterNode(new ContainerSchemaNodeBuilder(MODULE_NAME, 10, containerQName, containerPath));
261 final QName targetSchemaName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "fictive-target-container");
262 moduleBuilder.addAugment(12, "/something/something", SchemaPath.create(true, targetSchemaName), 0);
265 @Test(expected = YangParseException.class)
266 public void testInvalidUsesNodeUse() {
267 final QName leafListDefName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-leaflist");
268 SchemaPath leaflistPath = SchemaPath.create(true, leafListDefName);
269 moduleBuilder.enterNode(new LeafListSchemaNodeBuilder(MODULE_NAME, 10, leafListDefName, leaflistPath));
271 final QName testGroup = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-group");
272 final SchemaPath groupPath = SchemaPath.create(true, testGroup);
274 moduleBuilder.addUsesNode(17, groupPath);
277 @Test(expected = YangParseException.class)
278 public void testAddRefineToNonUsesNodeBuilder() throws Exception {
279 final QName leafListDefName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-leaflist");
280 SchemaPath leaflistPath = SchemaPath.create(true, leafListDefName);
281 moduleBuilder.enterNode(new LeafListSchemaNodeBuilder(MODULE_NAME, 10, leafListDefName, leaflistPath));
283 moduleBuilder.addRefine(new RefineHolderImpl(MODULE_NAME, 12, "testRefineName"));
286 @Test(expected = YangParseException.class)
287 public void testAddRpcToNonModuleBuilder() throws Exception {
288 final QName containerQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "top-level-container");
289 SchemaPath containerPath = SchemaPath.create(true, containerQName);
290 moduleBuilder.enterNode(new ContainerSchemaNodeBuilder(MODULE_NAME, 10, containerQName, containerPath));
292 final QName rpcDefName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "rpc-definition");
293 SchemaPath rpcPath = SchemaPath.create(true, rpcDefName);
295 moduleBuilder.addRpc(17, rpcDefName, rpcPath);
298 @Test(expected = YangParseException.class)
299 public void testAddRpcWithSameNameAsAlreadyExistingDataNodeContainer() throws Exception {
300 final QName containerQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "element-foo");
301 SchemaPath containerPath = SchemaPath.create(true, containerQName);
302 moduleBuilder.addContainerNode(10, containerQName, containerPath);
304 final QName rpcDefName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "element-foo");
305 SchemaPath rpcPath = SchemaPath.create(true, rpcDefName);
306 moduleBuilder.addRpc(17, rpcDefName, rpcPath);
309 @Test(expected = YangParseException.class)
310 public void testAddDuplicateRpc() throws Exception {
311 final QName rpcDefName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "rpc-definition");
312 SchemaPath rpcPath = SchemaPath.create(true, rpcDefName);
313 moduleBuilder.addRpc(17, rpcDefName, rpcPath);
315 final QName rpcDefName2 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "rpc-definition");
316 SchemaPath rpcPath2 = SchemaPath.create(true, rpcDefName2);
317 moduleBuilder.addRpc(23, rpcDefName2, rpcPath2);
320 @Test(expected = YangParseException.class)
321 public void testAddRpcWithSameNameAsAlreadyDefinedNotification() throws Exception {
322 final QName rpcDefName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "rpc-definition");
323 SchemaPath rpcPath = SchemaPath.create(true, rpcDefName);
324 moduleBuilder.addNotification(17, rpcDefName, rpcPath);
326 final QName rpcDefName2 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "rpc-definition");
327 SchemaPath rpcPath2 = SchemaPath.create(true, rpcDefName2);
328 moduleBuilder.addRpc(23, rpcDefName2, rpcPath2);
331 @Test(expected = YangParseException.class)
332 public void testAddRpcInputForInvalidParent() throws Exception {
333 final QName rpcDefName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "rpc-definition");
334 final QName rpcInput = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "input");
335 SchemaPath rpcInputPath = SchemaPath.create(true, rpcDefName, rpcInput);
337 moduleBuilder.addRpcInput(23, rpcInput, rpcInputPath);
340 @Test(expected = YangParseException.class)
341 public void testAddRpcOutputForInvalidParent() throws Exception {
342 final QName rpcDefName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "rpc-definition");
343 final QName rpcOutput = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "output");
344 SchemaPath rpcOutputPath = SchemaPath.create(true, rpcDefName, rpcOutput);
346 moduleBuilder.addRpcOutput(rpcOutputPath, rpcOutput, 23);
349 @Test(expected = YangParseException.class)
350 public void testAddNotificationForInvalidParent() throws Exception {
351 final QName containerQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "top-level-container");
352 SchemaPath containerPath = SchemaPath.create(true, containerQName);
353 moduleBuilder.enterNode(new ContainerSchemaNodeBuilder(MODULE_NAME, 10, containerQName, containerPath));
355 final QName notificationName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "notify-this");
356 SchemaPath notificationPath = SchemaPath.create(true, notificationName);
358 moduleBuilder.addNotification(12, notificationName, notificationPath);
361 @Test(expected = YangParseException.class)
362 public void testAddNotificationWithSameNameAsAlreadyExistingDataNodeContainer() throws Exception {
363 final QName containerQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "notify");
364 SchemaPath containerPath = SchemaPath.create(true, containerQName);
365 moduleBuilder.addContainerNode(10, containerQName, containerPath);
367 final QName notificationName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "notify");
368 SchemaPath notificationPath = SchemaPath.create(true, notificationName);
370 moduleBuilder.addNotification(12, notificationName, notificationPath);
373 @Test(expected = YangParseException.class)
374 public void testAddDuplicateNotification() throws Exception {
375 final QName notificationName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "notify-this");
376 SchemaPath notificationPath = SchemaPath.create(true, notificationName);
378 moduleBuilder.addNotification(12, notificationName, notificationPath);
380 final QName notificationName2 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "notify-this");
381 SchemaPath notificationPath2 = SchemaPath.create(true, notificationName2);
383 moduleBuilder.addNotification(17, notificationName2, notificationPath2);
386 @Test(expected = YangParseException.class)
387 public void testAddNotificationWithSameNameAsAlreadyDefinedRpc() throws Exception {
388 final QName rpcDefName2 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "notify-this");
389 SchemaPath rpcPath2 = SchemaPath.create(true, rpcDefName2);
390 moduleBuilder.addRpc(17, rpcDefName2, rpcPath2);
392 final QName rpcDefName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "notify-this");
393 SchemaPath rpcPath = SchemaPath.create(true, rpcDefName);
394 moduleBuilder.addNotification(23, rpcDefName, rpcPath);
397 @Test(expected = YangParseException.class)
398 public void testAddFeatureIntoNonModuleBuilder() throws Exception {
399 final QName containerQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "top-level-container");
400 SchemaPath containerPath = SchemaPath.create(true, containerQName);
401 moduleBuilder.enterNode(new ContainerSchemaNodeBuilder(MODULE_NAME, 10, containerQName, containerPath));
403 final QName testFeature = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-feature");
404 SchemaPath featurePath = SchemaPath.create(true, testFeature);
406 moduleBuilder.addFeature(23, testFeature, featurePath);
409 @Test(expected = YangParseException.class)
410 public void testAddDuplicateFeature() throws Exception {
411 final QName testFeature = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-feature");
412 SchemaPath featurePath = SchemaPath.create(true, testFeature);
414 moduleBuilder.addFeature(23, testFeature, featurePath);
416 final QName testFeature2 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-feature");
417 SchemaPath featurePath2 = SchemaPath.create(true, testFeature2);
419 moduleBuilder.addFeature(23, testFeature2, featurePath2);
423 public void testAddTwoFeatures() throws Exception {
424 final QName testFeature = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-feature");
425 SchemaPath featurePath = SchemaPath.create(true, testFeature);
427 final FeatureBuilder feature1 = moduleBuilder.addFeature(23, testFeature, featurePath);
429 final QName testFeature2 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-feature2");
430 SchemaPath featurePath2 = SchemaPath.create(true, testFeature2);
432 final FeatureBuilder feature2 = moduleBuilder.addFeature(23, testFeature2, featurePath2);
434 assertNotNull(feature1);
435 assertNotNull(feature2);
438 @Test(expected = YangParseException.class)
439 public void testAddCaseWhenParentIsNull() throws Exception {
440 moduleBuilder.exitNode();
444 private void addTestCase() {
445 final QName testCase = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-case");
446 SchemaPath testCasePath = SchemaPath.create(true, testCase);
448 moduleBuilder.addCase(72, testCase, testCasePath);
451 @Test(expected = YangParseException.class)
452 public void testAddCaseWhenParentIsModule() throws Exception {
456 @Test(expected = YangParseException.class)
457 public void testAddCaseWhenParentIsNotChoiceNorAugmentation() throws Exception {
458 final QName containerQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "top-level-container");
459 SchemaPath containerPath = SchemaPath.create(true, containerQName);
460 moduleBuilder.enterNode(new ContainerSchemaNodeBuilder(MODULE_NAME, 10, containerQName, containerPath));
464 @Test(expected = YangParseException.class)
465 public void testAddDuplicateTypedefIntoContainer() throws Exception {
466 final QName containerQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "top-level-container");
467 SchemaPath containerPath = SchemaPath.create(true, containerQName);
468 moduleBuilder.enterNode(new ContainerSchemaNodeBuilder(MODULE_NAME, 10, containerQName, containerPath));
470 tryToAddDuplicateTypedef();
473 private void tryToAddDuplicateTypedef() {
474 final QName testTypedef1 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-type-definition");
475 SchemaPath testTypedefPath1 = SchemaPath.create(true, testTypedef1);
477 final QName testTypedef2 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-type-definition");
478 SchemaPath testTypedefPath2 = SchemaPath.create(true, testTypedef2);
480 moduleBuilder.addTypedef(23, testTypedef1, testTypedefPath1);
481 moduleBuilder.addTypedef(44, testTypedef2, testTypedefPath2);
484 @Test(expected = YangParseException.class)
485 public void testAddDuplicateTypedefIntoRpcDefinition() throws Exception {
486 final QName rpcQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "top-level-rpc");
487 SchemaPath rpcPath = SchemaPath.create(true, rpcQName);
488 moduleBuilder.enterNode(new RpcDefinitionBuilder(MODULE_NAME, 10, rpcQName, rpcPath));
490 tryToAddDuplicateTypedef();
493 @Test(expected = YangParseException.class)
494 public void testAddTypedefIntoNonValidStatement() throws Exception {
495 final QName leafName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-leaf");
496 SchemaPath leafPath = SchemaPath.create(true, leafName);
497 moduleBuilder.enterNode(new LeafSchemaNodeBuilder(MODULE_NAME, 10, leafName, leafPath));
499 final QName testRpc = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-type-definition");
500 SchemaPath testRpcPath = SchemaPath.create(true, testRpc);
502 moduleBuilder.addTypedef(23, testRpc, testRpcPath);
506 public void testAddTwoTypedefsIntoContainer() throws Exception {
507 final QName containerQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "top-level-container");
508 SchemaPath containerPath = SchemaPath.create(true, containerQName);
509 moduleBuilder.enterNode(new ContainerSchemaNodeBuilder(MODULE_NAME, 10, containerQName, containerPath));
511 tryToAddTwoTypedefs();
514 private void tryToAddTwoTypedefs() {
515 final QName testTypedef1 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-type-definition");
516 SchemaPath testTypedefPath1 = SchemaPath.create(true, testTypedef1);
518 final QName testTypedef2 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-type-definition2");
519 SchemaPath testTypedefPath2 = SchemaPath.create(true, testTypedef2);
521 final TypeDefinitionBuilderImpl typedef1 = moduleBuilder.addTypedef(23, testTypedef1, testTypedefPath1);
522 final TypeDefinitionBuilderImpl typedef2 = moduleBuilder.addTypedef(44, testTypedef2, testTypedefPath2);
524 assertNotNull(typedef1);
525 assertNotNull(typedef2);
529 public void testAddTwoTypedefsIntoRpcDefinition() throws Exception {
530 final QName rpcQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "top-level-rpc");
531 SchemaPath rpcPath = SchemaPath.create(true, rpcQName);
532 moduleBuilder.enterNode(new RpcDefinitionBuilder(MODULE_NAME, 10, rpcQName, rpcPath));
534 tryToAddTwoTypedefs();
537 @Test(expected = YangParseException.class)
538 public void testSetType() throws Exception {
539 final Uint16 uint = Uint16.getInstance();
540 moduleBuilder.setType(uint);
543 @Test(expected = YangParseException.class)
544 public void testAddUnionTypeWhenParentIsNull() throws Exception {
545 moduleBuilder.exitNode();
550 private void tryToAddUnionType() {
551 final QName unionQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "union-type-def");
552 moduleBuilder.addUnionType(25, QNameModule.create(unionQName.getNamespace(), unionQName.getRevision()));
555 @Test(expected = YangParseException.class)
556 public void testAddUnionTypeWhenParentIsNotTypeAwareBuilder() throws Exception {
557 final QName containerQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "top-level-container");
558 SchemaPath containerPath = SchemaPath.create(true, containerQName);
559 moduleBuilder.enterNode(new ContainerSchemaNodeBuilder(MODULE_NAME, 10, containerQName, containerPath));
564 @Test(expected = YangParseException.class)
565 public void addIdentityrefTypeWhenParentIsNull() throws Exception {
566 moduleBuilder.exitNode();
567 tryToAddIdentityrefType();
570 private void tryToAddIdentityrefType() {
571 final QName identityrefQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "identityref-type-def");
572 SchemaPath identityrefPath = SchemaPath.create(true, identityrefQName);
573 moduleBuilder.addIdentityrefType(25, identityrefPath, "simplebase");
576 @Test(expected = YangParseException.class)
577 public void addIdentityrefTypeWhenParentIsNotTypeAwareBuilder() throws Exception {
578 final QName containerQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "top-level-container");
579 SchemaPath containerPath = SchemaPath.create(true, containerQName);
580 moduleBuilder.enterNode(new ContainerSchemaNodeBuilder(MODULE_NAME, 10, containerQName, containerPath));
582 tryToAddIdentityrefType();
585 @Test(expected = YangParseException.class)
586 public void testAddDeviationWhereParentIsNotModule() throws Exception {
587 final QName containerQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "top-level-container");
588 SchemaPath containerPath = SchemaPath.create(true, containerQName);
589 moduleBuilder.enterNode(new ContainerSchemaNodeBuilder(MODULE_NAME, 10, containerQName, containerPath));
591 final QName deviationQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-deviation-name");
592 SchemaPath deviationPath = SchemaPath.create(true, deviationQName);
594 moduleBuilder.addDeviation(78, deviationPath);
597 @Test(expected = YangParseException.class)
598 public void testAddIdentityWhereParentIsNotModule() throws Exception {
599 final QName containerQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "top-level-container");
600 SchemaPath containerPath = SchemaPath.create(true, containerQName);
601 moduleBuilder.enterNode(new ContainerSchemaNodeBuilder(MODULE_NAME, 10, containerQName, containerPath));
606 private void tryToAddIdentity() {
607 final QName identityQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-identity-name");
608 SchemaPath identityPath = SchemaPath.create(true, identityQName);
610 moduleBuilder.addIdentity(identityQName, 78, identityPath);
613 @Test(expected = YangParseException.class)
614 public void testAddDuplicateIdentity() throws Exception {
620 public void testAddUnknownSchemaNodeDirectlyToParent() throws Exception {
621 final QName unknownQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "my-extension-use");
622 SchemaPath unknownPath = SchemaPath.create(true, unknownQName);
623 assertTrue(moduleBuilder.getUnknownNodes().isEmpty());
624 moduleBuilder.addUnknownSchemaNode(72, unknownQName, unknownPath);
625 assertFalse(moduleBuilder.getUnknownNodes().isEmpty());
629 public void testAddUnknownSchemaNodeIntoDataNodeContainer() throws Exception {
630 final QName containerQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "top-level-container");
631 SchemaPath containerPath = SchemaPath.create(true, containerQName);
632 moduleBuilder.enterNode(new ContainerSchemaNodeBuilder(MODULE_NAME, 10, containerQName, containerPath));
634 final QName unknownQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "my-extension-use");
635 SchemaPath unknownPath = SchemaPath.create(true, unknownQName);
636 assertTrue(moduleBuilder.getAllUnknownNodes().isEmpty());
637 moduleBuilder.addUnknownSchemaNode(72, unknownQName, unknownPath);
638 assertFalse(moduleBuilder.getAllUnknownNodes().isEmpty());
641 @Test(expected = YangParseException.class)
642 public void testAddUnknownSchemaNodeIntoNonSchemaNode() throws Exception {
643 final QName containerQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "top-level-non-schema-node");
644 SchemaPath containerPath = SchemaPath.create(true, containerQName);
645 moduleBuilder.enterNode(new NonSchemaNodeBuilder(MODULE_NAME, 10, containerQName, containerPath));
647 final QName unknownQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "my-extension-use");
648 SchemaPath unknownPath = SchemaPath.create(true, unknownQName);
649 assertTrue(moduleBuilder.getUnknownNodes().isEmpty());
650 moduleBuilder.addUnknownSchemaNode(72, unknownQName, unknownPath);
651 assertFalse(moduleBuilder.getUnknownNodes().isEmpty());
654 @Test(expected = YangParseException.class)
655 public void testAddContainerWithSameNameAsRpcDefintion() throws Exception {
656 final QName rpcDefName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "message");
657 SchemaPath rpcPath = SchemaPath.create(true, rpcDefName);
658 moduleBuilder.addRpc(70, rpcDefName, rpcPath);
662 private void tryToAddContainer() {
663 final QName containerQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "message");
664 final SchemaPath containerPath = SchemaPath.create(true, containerQName);
666 moduleBuilder.addContainerNode(22, containerQName, containerPath);
669 @Test(expected = YangParseException.class)
670 public void testAddContainerWithSameNameAsNotificationDefintion() throws Exception {
671 final QName notificationName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "message");
672 SchemaPath notificationPath = SchemaPath.create(true, notificationName);
673 moduleBuilder.addNotification(70, notificationName, notificationPath);
678 public void testAddContainerWithAlreadyAddedRpcDefintion() throws Exception {
679 final QName rpcDefName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "send-message");
680 SchemaPath rpcPath = SchemaPath.create(true, rpcDefName);
681 moduleBuilder.addRpc(70, rpcDefName, rpcPath);
684 DataSchemaNodeBuilder addedContainer = moduleBuilder.getDataChildByName("message");
685 assertNotNull(addedContainer);
689 public void testAddContainerWithAlreadyAddedNotificationDefintion() throws Exception {
690 final QName notificationName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "notify-me");
691 SchemaPath notificationPath = SchemaPath.create(true, notificationName);
692 moduleBuilder.addNotification(70, notificationName, notificationPath);
695 DataSchemaNodeBuilder addedContainer = moduleBuilder.getDataChildByName("message");
696 assertNotNull(addedContainer);
699 @Test(expected = YangParseException.class)
700 public void testAddDuplicateSchemaNodeIntoSubnodeOfModule() throws Exception {
701 final QName containerQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "top-level-container");
702 SchemaPath containerPath = SchemaPath.create(true, containerQName);
703 moduleBuilder.enterNode(new ContainerSchemaNodeBuilder(MODULE_NAME, 10, containerQName, containerPath));
705 final QName leafQName1 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "simple-leaf");
706 SchemaPath leafPath1 = SchemaPath.create(true, containerQName, leafQName1);
708 final QName leafQName2 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "simple-leaf");
709 SchemaPath leafPath2 = SchemaPath.create(true, containerQName, leafQName2);
711 moduleBuilder.addLeafNode(12, leafQName1, leafPath1);
712 moduleBuilder.addLeafNode(20, leafQName2, leafPath2);
715 @Test(expected = YangParseException.class)
716 public void testAddDuplicateCaseIntoChoiceCaseNode() throws Exception {
717 //FIXME: move this into ChoiceBuilderTest !!!
718 final QName choiceQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "top-level-container");
719 SchemaPath choicePath = SchemaPath.create(true, choiceQName);
721 moduleBuilder.enterNode(new ChoiceBuilder(MODULE_NAME, 10, choiceQName, choicePath));
723 final QName caseQName1 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "simple-case");
724 SchemaPath casePath1 = SchemaPath.create(true, choiceQName, caseQName1);
726 final QName caseQName2 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "simple-case");
727 SchemaPath casePath2 = SchemaPath.create(true, choiceQName, caseQName2);
729 moduleBuilder.addCase(12, caseQName1, casePath1);
730 moduleBuilder.addCase(20, caseQName2, casePath2);
733 @Test(expected = YangParseException.class)
734 public void testAddChoiceWithSameNameAsCase() throws Exception {
735 final QName choiceQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "my-choice");
736 SchemaPath choicePath = SchemaPath.create(true, choiceQName);
738 final ChoiceBuilder choiceBuilder = moduleBuilder.addChoice(8, choiceQName, choicePath);
739 moduleBuilder.enterNode(choiceBuilder);
741 final QName caseQName1 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "simple-case");
742 SchemaPath casePath1 = SchemaPath.create(true, choiceQName, caseQName1);
744 final QName caseQName2 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "simple-case");
745 SchemaPath casePath2 = SchemaPath.create(true, choiceQName, caseQName2);
747 moduleBuilder.addCase(12, caseQName1, casePath1);
748 moduleBuilder.addChoice(20, caseQName2, casePath2);
751 @Test(expected = YangParseException.class)
752 public void testAddChildToSubnodeForNonDataNodeContainerAndNonChoiceNode() {
753 final QName leafQName1 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "simple-leaf");
754 SchemaPath leafPath1 = SchemaPath.create(true, leafQName1);
756 final LeafSchemaNodeBuilder leafBuilder = moduleBuilder.addLeafNode(10, leafQName1, leafPath1);
757 moduleBuilder.enterNode(leafBuilder);
759 final QName leafQName2 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "simple-leaf");
760 SchemaPath leafPath2 = SchemaPath.create(true, leafQName1, leafQName2);
762 moduleBuilder.addLeafNode(12, leafQName2, leafPath2);
765 private static class NonSchemaNodeBuilder implements Builder {
770 final SchemaPath path;
772 public NonSchemaNodeBuilder(final String moduleName, final int line, final QName name, final SchemaPath path) {
773 this.moduleName = moduleName;
780 public String getModuleName() {
785 public int getLine() {
790 public Builder getParent() {
795 public void setParent(final Builder parent) {
800 public void addUnknownNodeBuilder(final UnknownSchemaNodeBuilder unknownNode) {
805 public List<UnknownSchemaNodeBuilder> getUnknownNodes() {
810 public Object build() {