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 public class ModuleBuilderTest {
39 private final static String TEST_MODULE_NAMESPACE = "urn:opendaylight.foo";
40 private final static String TEST_MODULE_REVISION = "2014-09-22";
41 private final static String MODULE_NAME = "TestModule";
42 private static final String TEST_MODULE_PATH = "/test/module/path";
44 private ModuleBuilder moduleBuilder;
47 public void setUp() throws Exception {
48 moduleBuilder = new ModuleBuilder(MODULE_NAME , TEST_MODULE_PATH);
52 public void testGetSetModuleBuilderMethods() throws Exception {
53 assertEquals("module", moduleBuilder.getStatementName());
54 assertEquals(TEST_MODULE_PATH, moduleBuilder.getModuleSourcePath());
55 assertNotNull(moduleBuilder.getAllLists());
56 final URI namespace = URI.create(TEST_MODULE_NAMESPACE + "?" + TEST_MODULE_REVISION);
57 moduleBuilder.setNamespace(namespace);
58 assertEquals(namespace, moduleBuilder.getNamespace());
59 moduleBuilder.setBelongsTo("TEST_SUBMODULE");
60 assertEquals("TEST_SUBMODULE", moduleBuilder.getBelongsTo());
61 final Date revision = new SimpleDateFormat("yyyy-dd-mm").parse(TEST_MODULE_REVISION);
62 moduleBuilder.setRevision(revision);
63 assertEquals(revision, moduleBuilder.getRevision());
66 @Test(expected = IllegalArgumentException.class)
67 public void testCheckPrefixFailOnPrefixNull() {
68 moduleBuilder.addModuleImport("TEST_MODULE", null, null);
71 @Test(expected = IllegalArgumentException.class)
72 public void testCheckPrefixFailOnPrefixStringIsEmpty() {
73 moduleBuilder.addModuleImport("TEST_MODULE", null, "");
76 @Test(expected = IllegalArgumentException.class)
77 public void testCheckPrefixFailOnPrefixEqualsModuleBuilderPrefix() {
78 moduleBuilder.setPrefix("prefix");
79 moduleBuilder.addModuleImport("TEST_MODULE", null, "prefix");
83 public void testGetActualNodeNullResult() {
84 moduleBuilder.exitNode();
85 assertNull(moduleBuilder.getActualNode());
88 @Test(expected = YangParseException.class)
89 public void testSetParent() throws Exception {
90 moduleBuilder.setParent(new Builder() {
92 public String getModuleName() {
97 public int getLine() {
102 public Builder getParent() {
107 public void setParent(final Builder parent) {
112 public void addUnknownNodeBuilder(final UnknownSchemaNodeBuilder unknownNode) {
117 public List<UnknownSchemaNodeBuilder> getUnknownNodes() {
122 public Object build() {
128 @Test(expected = YangParseException.class)
129 public void testAddExtensionWhereParentNodeIsNotModuleBuilder() throws Exception {
130 final QName containerQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "top-level-container");
131 SchemaPath containerPath = SchemaPath.create(true, containerQName);
132 moduleBuilder.enterNode(new ContainerSchemaNodeBuilder(MODULE_NAME, 10, containerQName, containerPath));
133 final QName extQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-ext");
134 SchemaPath extPath = SchemaPath.create(true, containerQName, extQName);
135 moduleBuilder.addExtension(extQName, 12, extPath);
138 @Test(expected = YangParseException.class)
139 public void testAddExtensionWhereNameOfExtensionCollides() throws Exception {
140 final QName extQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-ext");
141 SchemaPath extPath = SchemaPath.create(true, extQName);
142 moduleBuilder.addExtension(extQName, 12, extPath);
144 final QName extQName2 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-ext");
145 moduleBuilder.addExtension(extQName2, 22, extPath);
148 @Test(expected = YangParseException.class)
149 public void testAddDuplicateGroupingIntoModule() {
150 final QName testGroup1 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-group");
151 final SchemaPath groupPath1 = SchemaPath.create(true, testGroup1);
153 final QName testGroup2 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-group");
154 final SchemaPath groupPath2 = SchemaPath.create(true, testGroup2);
156 moduleBuilder.addGrouping(12, testGroup1, groupPath1);
157 moduleBuilder.addGrouping(12, testGroup2, groupPath2);
160 @Test(expected = YangParseException.class)
161 public void testAddDuplicateGroupingIntoDataNodeContainer() {
162 final QName containerQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "top-level-container");
163 SchemaPath containerPath = SchemaPath.create(true, containerQName);
164 moduleBuilder.enterNode(new ContainerSchemaNodeBuilder(MODULE_NAME, 10, containerQName, containerPath));
165 final QName testGroup1 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-group");
166 final SchemaPath groupPath1 = SchemaPath.create(true, containerQName, testGroup1);
168 final QName testGroup2 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-group");
169 final SchemaPath groupPath2 = SchemaPath.create(true, containerQName, testGroup2);
171 moduleBuilder.addGrouping(12, testGroup1, groupPath1);
172 moduleBuilder.addGrouping(22, testGroup2, groupPath2);
176 public void testAddTwoGroupingIntoDataNodeContainer() {
177 final QName containerQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "top-level-container");
178 SchemaPath containerPath = SchemaPath.create(true, containerQName);
179 moduleBuilder.enterNode(new ContainerSchemaNodeBuilder(MODULE_NAME, 10, containerQName, containerPath));
180 final QName testGroup1 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-group");
181 final SchemaPath groupPath1 = SchemaPath.create(true, containerQName, testGroup1);
183 final QName testGroup2 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-group2");
184 final SchemaPath groupPath2 = SchemaPath.create(true, containerQName, testGroup2);
186 GroupingBuilder grouping1 = moduleBuilder.addGrouping(12, testGroup1, groupPath1);
187 GroupingBuilder grouping2 = moduleBuilder.addGrouping(22, testGroup2, groupPath2);
189 assertNotNull(grouping1);
190 assertNotNull(grouping2);
193 @Test(expected = YangParseException.class)
194 public void testAddDuplicateGroupingIntoRpcDefinition() {
195 final QName rpcDefName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "rpc-definition");
196 SchemaPath rpcPath = SchemaPath.create(true, rpcDefName);
197 moduleBuilder.enterNode(new RpcDefinitionBuilder(MODULE_NAME, 10, rpcDefName, rpcPath));
198 final QName testGroup1 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-group");
199 final SchemaPath groupPath1 = SchemaPath.create(true, rpcDefName, testGroup1);
201 final QName testGroup2 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-group");
202 final SchemaPath groupPath2 = SchemaPath.create(true, rpcDefName, testGroup2);
204 moduleBuilder.addGrouping(12, testGroup1, groupPath1);
205 moduleBuilder.addGrouping(22, testGroup2, groupPath2);
209 public void testAddTwoGroupingsIntoRpcDefinition() {
210 final QName rpcDefName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "rpc-definition");
211 SchemaPath rpcPath = SchemaPath.create(true, rpcDefName);
212 moduleBuilder.enterNode(new RpcDefinitionBuilder(MODULE_NAME, 10, rpcDefName, rpcPath));
213 final QName testGroup1 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-group");
214 final SchemaPath groupPath1 = SchemaPath.create(true, rpcDefName, testGroup1);
216 final QName testGroup2 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-group2");
217 final SchemaPath groupPath2 = SchemaPath.create(true, rpcDefName, testGroup2);
219 GroupingBuilder grouping1 = moduleBuilder.addGrouping(12, testGroup1, groupPath1);
220 GroupingBuilder grouping2 = moduleBuilder.addGrouping(22, testGroup2, groupPath2);
222 assertNotNull(grouping1);
223 assertNotNull(grouping2);
226 @Test(expected = YangParseException.class)
227 public void testAddGroupingIntoNonValidStatementDefinition() {
228 final QName leafListDefName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-leaflist");
229 SchemaPath leaflistPath = SchemaPath.create(true, leafListDefName);
230 moduleBuilder.enterNode(new LeafListSchemaNodeBuilder(MODULE_NAME, 10, leafListDefName, leaflistPath));
231 final QName testGroup = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-group");
232 final SchemaPath groupPath = SchemaPath.create(true, leafListDefName, testGroup);
234 moduleBuilder.addGrouping(12, testGroup, groupPath);
237 @Test(expected = YangParseException.class)
238 public void testInvalidAugmentPathTargetString() {
239 final QName targetSchemaName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "fictive-target-container");
240 moduleBuilder.addAugment(12, "something/something", SchemaPath.create(true, targetSchemaName), 0);
243 @Test(expected = YangParseException.class)
244 public void testInvalidAugmentPathTargetStringInUsesNode() {
245 final QName usesNode = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "fictive-uses-node");
246 SchemaPath targetPath = SchemaPath.create(true, usesNode);
247 moduleBuilder.enterNode(new UsesNodeBuilderImpl(MODULE_NAME, 10, targetPath));
248 final QName targetSchemaName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "fictive-target-container");
249 moduleBuilder.addAugment(12, "/something/something", SchemaPath.create(true, targetSchemaName), 0);
252 @Test(expected = YangParseException.class)
253 public void testInvalidAugmentDefinition() {
254 final QName containerQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "top-level-container");
255 SchemaPath containerPath = SchemaPath.create(true, containerQName);
256 moduleBuilder.enterNode(new ContainerSchemaNodeBuilder(MODULE_NAME, 10, containerQName, containerPath));
258 final QName targetSchemaName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "fictive-target-container");
259 moduleBuilder.addAugment(12, "/something/something", SchemaPath.create(true, targetSchemaName), 0);
262 @Test(expected = YangParseException.class)
263 public void testInvalidUsesNodeUse() {
264 final QName leafListDefName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-leaflist");
265 SchemaPath leaflistPath = SchemaPath.create(true, leafListDefName);
266 moduleBuilder.enterNode(new LeafListSchemaNodeBuilder(MODULE_NAME, 10, leafListDefName, leaflistPath));
268 final QName testGroup = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-group");
269 final SchemaPath groupPath = SchemaPath.create(true, testGroup);
271 moduleBuilder.addUsesNode(17, groupPath);
274 @Test(expected = YangParseException.class)
275 public void testAddRefineToNonUsesNodeBuilder() throws Exception {
276 final QName leafListDefName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-leaflist");
277 SchemaPath leaflistPath = SchemaPath.create(true, leafListDefName);
278 moduleBuilder.enterNode(new LeafListSchemaNodeBuilder(MODULE_NAME, 10, leafListDefName, leaflistPath));
280 moduleBuilder.addRefine(new RefineHolderImpl(MODULE_NAME, 12, "testRefineName"));
283 @Test(expected = YangParseException.class)
284 public void testAddRpcToNonModuleBuilder() throws Exception {
285 final QName containerQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "top-level-container");
286 SchemaPath containerPath = SchemaPath.create(true, containerQName);
287 moduleBuilder.enterNode(new ContainerSchemaNodeBuilder(MODULE_NAME, 10, containerQName, containerPath));
289 final QName rpcDefName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "rpc-definition");
290 SchemaPath rpcPath = SchemaPath.create(true, rpcDefName);
292 moduleBuilder.addRpc(17, rpcDefName, rpcPath);
295 @Test(expected = YangParseException.class)
296 public void testAddRpcWithSameNameAsAlreadyExistingDataNodeContainer() throws Exception {
297 final QName containerQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "element-foo");
298 SchemaPath containerPath = SchemaPath.create(true, containerQName);
299 moduleBuilder.addContainerNode(10, containerQName, containerPath);
301 final QName rpcDefName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "element-foo");
302 SchemaPath rpcPath = SchemaPath.create(true, rpcDefName);
303 moduleBuilder.addRpc(17, rpcDefName, rpcPath);
306 @Test(expected = YangParseException.class)
307 public void testAddDuplicateRpc() throws Exception {
308 final QName rpcDefName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "rpc-definition");
309 SchemaPath rpcPath = SchemaPath.create(true, rpcDefName);
310 moduleBuilder.addRpc(17, rpcDefName, rpcPath);
312 final QName rpcDefName2 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "rpc-definition");
313 SchemaPath rpcPath2 = SchemaPath.create(true, rpcDefName2);
314 moduleBuilder.addRpc(23, rpcDefName2, rpcPath2);
317 @Test(expected = YangParseException.class)
318 public void testAddRpcWithSameNameAsAlreadyDefinedNotification() throws Exception {
319 final QName rpcDefName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "rpc-definition");
320 SchemaPath rpcPath = SchemaPath.create(true, rpcDefName);
321 moduleBuilder.addNotification(17, rpcDefName, rpcPath);
323 final QName rpcDefName2 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "rpc-definition");
324 SchemaPath rpcPath2 = SchemaPath.create(true, rpcDefName2);
325 moduleBuilder.addRpc(23, rpcDefName2, rpcPath2);
328 @Test(expected = YangParseException.class)
329 public void testAddRpcInputForInvalidParent() throws Exception {
330 final QName rpcDefName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "rpc-definition");
331 final QName rpcInput = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "input");
332 SchemaPath rpcInputPath = SchemaPath.create(true, rpcDefName, rpcInput);
334 moduleBuilder.addRpcInput(23, rpcInput, rpcInputPath);
337 @Test(expected = YangParseException.class)
338 public void testAddRpcOutputForInvalidParent() throws Exception {
339 final QName rpcDefName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "rpc-definition");
340 final QName rpcOutput = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "output");
341 SchemaPath rpcOutputPath = SchemaPath.create(true, rpcDefName, rpcOutput);
343 moduleBuilder.addRpcOutput(rpcOutputPath, rpcOutput, 23);
346 @Test(expected = YangParseException.class)
347 public void testAddNotificationForInvalidParent() throws Exception {
348 final QName containerQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "top-level-container");
349 SchemaPath containerPath = SchemaPath.create(true, containerQName);
350 moduleBuilder.enterNode(new ContainerSchemaNodeBuilder(MODULE_NAME, 10, containerQName, containerPath));
352 final QName notificationName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "notify-this");
353 SchemaPath notificationPath = SchemaPath.create(true, notificationName);
355 moduleBuilder.addNotification(12, notificationName, notificationPath);
358 @Test(expected = YangParseException.class)
359 public void testAddNotificationWithSameNameAsAlreadyExistingDataNodeContainer() throws Exception {
360 final QName containerQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "notify");
361 SchemaPath containerPath = SchemaPath.create(true, containerQName);
362 moduleBuilder.addContainerNode(10, containerQName, containerPath);
364 final QName notificationName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "notify");
365 SchemaPath notificationPath = SchemaPath.create(true, notificationName);
367 moduleBuilder.addNotification(12, notificationName, notificationPath);
370 @Test(expected = YangParseException.class)
371 public void testAddDuplicateNotification() throws Exception {
372 final QName notificationName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "notify-this");
373 SchemaPath notificationPath = SchemaPath.create(true, notificationName);
375 moduleBuilder.addNotification(12, notificationName, notificationPath);
377 final QName notificationName2 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "notify-this");
378 SchemaPath notificationPath2 = SchemaPath.create(true, notificationName2);
380 moduleBuilder.addNotification(17, notificationName2, notificationPath2);
383 @Test(expected = YangParseException.class)
384 public void testAddNotificationWithSameNameAsAlreadyDefinedRpc() throws Exception {
385 final QName rpcDefName2 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "notify-this");
386 SchemaPath rpcPath2 = SchemaPath.create(true, rpcDefName2);
387 moduleBuilder.addRpc(17, rpcDefName2, rpcPath2);
389 final QName rpcDefName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "notify-this");
390 SchemaPath rpcPath = SchemaPath.create(true, rpcDefName);
391 moduleBuilder.addNotification(23, rpcDefName, rpcPath);
394 @Test(expected = YangParseException.class)
395 public void testAddFeatureIntoNonModuleBuilder() throws Exception {
396 final QName containerQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "top-level-container");
397 SchemaPath containerPath = SchemaPath.create(true, containerQName);
398 moduleBuilder.enterNode(new ContainerSchemaNodeBuilder(MODULE_NAME, 10, containerQName, containerPath));
400 final QName testFeature = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-feature");
401 SchemaPath featurePath = SchemaPath.create(true, testFeature);
403 moduleBuilder.addFeature(23, testFeature, featurePath);
406 @Test(expected = YangParseException.class)
407 public void testAddDuplicateFeature() throws Exception {
408 final QName testFeature = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-feature");
409 SchemaPath featurePath = SchemaPath.create(true, testFeature);
411 moduleBuilder.addFeature(23, testFeature, featurePath);
413 final QName testFeature2 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-feature");
414 SchemaPath featurePath2 = SchemaPath.create(true, testFeature2);
416 moduleBuilder.addFeature(23, testFeature2, featurePath2);
420 public void testAddTwoFeatures() throws Exception {
421 final QName testFeature = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-feature");
422 SchemaPath featurePath = SchemaPath.create(true, testFeature);
424 final FeatureBuilder feature1 = moduleBuilder.addFeature(23, testFeature, featurePath);
426 final QName testFeature2 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-feature2");
427 SchemaPath featurePath2 = SchemaPath.create(true, testFeature2);
429 final FeatureBuilder feature2 = moduleBuilder.addFeature(23, testFeature2, featurePath2);
431 assertNotNull(feature1);
432 assertNotNull(feature2);
435 @Test(expected = YangParseException.class)
436 public void testAddCaseWhenParentIsNull() throws Exception {
437 moduleBuilder.exitNode();
441 private void addTestCase() {
442 final QName testCase = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-case");
443 SchemaPath testCasePath = SchemaPath.create(true, testCase);
445 moduleBuilder.addCase(72, testCase, testCasePath);
448 @Test(expected = YangParseException.class)
449 public void testAddCaseWhenParentIsModule() throws Exception {
453 @Test(expected = YangParseException.class)
454 public void testAddCaseWhenParentIsNotChoiceNorAugmentation() throws Exception {
455 final QName containerQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "top-level-container");
456 SchemaPath containerPath = SchemaPath.create(true, containerQName);
457 moduleBuilder.enterNode(new ContainerSchemaNodeBuilder(MODULE_NAME, 10, containerQName, containerPath));
461 @Test(expected = YangParseException.class)
462 public void testAddDuplicateTypedefIntoContainer() throws Exception {
463 final QName containerQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "top-level-container");
464 SchemaPath containerPath = SchemaPath.create(true, containerQName);
465 moduleBuilder.enterNode(new ContainerSchemaNodeBuilder(MODULE_NAME, 10, containerQName, containerPath));
467 tryToAddDuplicateTypedef();
470 private void tryToAddDuplicateTypedef() {
471 final QName testTypedef1 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-type-definition");
472 SchemaPath testTypedefPath1 = SchemaPath.create(true, testTypedef1);
474 final QName testTypedef2 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-type-definition");
475 SchemaPath testTypedefPath2 = SchemaPath.create(true, testTypedef2);
477 moduleBuilder.addTypedef(23, testTypedef1, testTypedefPath1);
478 moduleBuilder.addTypedef(44, testTypedef2, testTypedefPath2);
481 @Test(expected = YangParseException.class)
482 public void testAddDuplicateTypedefIntoRpcDefinition() throws Exception {
483 final QName rpcQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "top-level-rpc");
484 SchemaPath rpcPath = SchemaPath.create(true, rpcQName);
485 moduleBuilder.enterNode(new RpcDefinitionBuilder(MODULE_NAME, 10, rpcQName, rpcPath));
487 tryToAddDuplicateTypedef();
490 @Test(expected = YangParseException.class)
491 public void testAddTypedefIntoNonValidStatement() throws Exception {
492 final QName leafName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-leaf");
493 SchemaPath leafPath = SchemaPath.create(true, leafName);
494 moduleBuilder.enterNode(new LeafSchemaNodeBuilder(MODULE_NAME, 10, leafName, leafPath));
496 final QName testRpc = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-type-definition");
497 SchemaPath testRpcPath = SchemaPath.create(true, testRpc);
499 moduleBuilder.addTypedef(23, testRpc, testRpcPath);
503 public void testAddTwoTypedefsIntoContainer() throws Exception {
504 final QName containerQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "top-level-container");
505 SchemaPath containerPath = SchemaPath.create(true, containerQName);
506 moduleBuilder.enterNode(new ContainerSchemaNodeBuilder(MODULE_NAME, 10, containerQName, containerPath));
508 tryToAddTwoTypedefs();
511 private void tryToAddTwoTypedefs() {
512 final QName testTypedef1 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-type-definition");
513 SchemaPath testTypedefPath1 = SchemaPath.create(true, testTypedef1);
515 final QName testTypedef2 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-type-definition2");
516 SchemaPath testTypedefPath2 = SchemaPath.create(true, testTypedef2);
518 final TypeDefinitionBuilderImpl typedef1 = moduleBuilder.addTypedef(23, testTypedef1, testTypedefPath1);
519 final TypeDefinitionBuilderImpl typedef2 = moduleBuilder.addTypedef(44, testTypedef2, testTypedefPath2);
521 assertNotNull(typedef1);
522 assertNotNull(typedef2);
526 public void testAddTwoTypedefsIntoRpcDefinition() throws Exception {
527 final QName rpcQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "top-level-rpc");
528 SchemaPath rpcPath = SchemaPath.create(true, rpcQName);
529 moduleBuilder.enterNode(new RpcDefinitionBuilder(MODULE_NAME, 10, rpcQName, rpcPath));
531 tryToAddTwoTypedefs();
534 @Test(expected = YangParseException.class)
535 public void testSetType() throws Exception {
536 final Uint16 uint = Uint16.getInstance();
537 moduleBuilder.setType(uint);
540 @Test(expected = YangParseException.class)
541 public void testAddUnionTypeWhenParentIsNull() throws Exception {
542 moduleBuilder.exitNode();
547 private void tryToAddUnionType() {
548 final QName unionQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "union-type-def");
549 moduleBuilder.addUnionType(25, QNameModule.create(unionQName.getNamespace(), unionQName.getRevision()));
552 @Test(expected = YangParseException.class)
553 public void testAddUnionTypeWhenParentIsNotTypeAwareBuilder() throws Exception {
554 final QName containerQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "top-level-container");
555 SchemaPath containerPath = SchemaPath.create(true, containerQName);
556 moduleBuilder.enterNode(new ContainerSchemaNodeBuilder(MODULE_NAME, 10, containerQName, containerPath));
561 @Test(expected = YangParseException.class)
562 public void addIdentityrefTypeWhenParentIsNull() throws Exception {
563 moduleBuilder.exitNode();
564 tryToAddIdentityrefType();
567 private void tryToAddIdentityrefType() {
568 final QName identityrefQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "identityref-type-def");
569 SchemaPath identityrefPath = SchemaPath.create(true, identityrefQName);
570 moduleBuilder.addIdentityrefType(25, identityrefPath, "simplebase");
573 @Test(expected = YangParseException.class)
574 public void addIdentityrefTypeWhenParentIsNotTypeAwareBuilder() throws Exception {
575 final QName containerQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "top-level-container");
576 SchemaPath containerPath = SchemaPath.create(true, containerQName);
577 moduleBuilder.enterNode(new ContainerSchemaNodeBuilder(MODULE_NAME, 10, containerQName, containerPath));
579 tryToAddIdentityrefType();
582 @Test(expected = YangParseException.class)
583 public void testAddDeviationWhereParentIsNotModule() throws Exception {
584 final QName containerQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "top-level-container");
585 SchemaPath containerPath = SchemaPath.create(true, containerQName);
586 moduleBuilder.enterNode(new ContainerSchemaNodeBuilder(MODULE_NAME, 10, containerQName, containerPath));
588 final QName deviationQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-deviation-name");
589 SchemaPath deviationPath = SchemaPath.create(true, deviationQName);
591 moduleBuilder.addDeviation(78, deviationPath);
594 @Test(expected = YangParseException.class)
595 public void testAddIdentityWhereParentIsNotModule() throws Exception {
596 final QName containerQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "top-level-container");
597 SchemaPath containerPath = SchemaPath.create(true, containerQName);
598 moduleBuilder.enterNode(new ContainerSchemaNodeBuilder(MODULE_NAME, 10, containerQName, containerPath));
603 private void tryToAddIdentity() {
604 final QName identityQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "test-identity-name");
605 SchemaPath identityPath = SchemaPath.create(true, identityQName);
607 moduleBuilder.addIdentity(identityQName, 78, identityPath);
610 @Test(expected = YangParseException.class)
611 public void testAddDuplicateIdentity() throws Exception {
617 public void testAddUnknownSchemaNodeDirectlyToParent() throws Exception {
618 final QName unknownQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "my-extension-use");
619 SchemaPath unknownPath = SchemaPath.create(true, unknownQName);
620 assertTrue(moduleBuilder.getUnknownNodes().isEmpty());
621 moduleBuilder.addUnknownSchemaNode(72, unknownQName, unknownPath);
622 assertFalse(moduleBuilder.getUnknownNodes().isEmpty());
626 public void testAddUnknownSchemaNodeIntoDataNodeContainer() throws Exception {
627 final QName containerQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "top-level-container");
628 SchemaPath containerPath = SchemaPath.create(true, containerQName);
629 moduleBuilder.enterNode(new ContainerSchemaNodeBuilder(MODULE_NAME, 10, containerQName, containerPath));
631 final QName unknownQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "my-extension-use");
632 SchemaPath unknownPath = SchemaPath.create(true, unknownQName);
633 assertTrue(moduleBuilder.getAllUnknownNodes().isEmpty());
634 moduleBuilder.addUnknownSchemaNode(72, unknownQName, unknownPath);
635 assertFalse(moduleBuilder.getAllUnknownNodes().isEmpty());
638 @Test(expected = YangParseException.class)
639 public void testAddUnknownSchemaNodeIntoNonSchemaNode() throws Exception {
640 final QName containerQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "top-level-non-schema-node");
641 SchemaPath containerPath = SchemaPath.create(true, containerQName);
642 moduleBuilder.enterNode(new NonSchemaNodeBuilder(MODULE_NAME, 10, containerQName, containerPath));
644 final QName unknownQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "my-extension-use");
645 SchemaPath unknownPath = SchemaPath.create(true, unknownQName);
646 assertTrue(moduleBuilder.getUnknownNodes().isEmpty());
647 moduleBuilder.addUnknownSchemaNode(72, unknownQName, unknownPath);
648 assertFalse(moduleBuilder.getUnknownNodes().isEmpty());
651 @Test(expected = YangParseException.class)
652 public void testAddContainerWithSameNameAsRpcDefintion() throws Exception {
653 final QName rpcDefName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "message");
654 SchemaPath rpcPath = SchemaPath.create(true, rpcDefName);
655 moduleBuilder.addRpc(70, rpcDefName, rpcPath);
659 private void tryToAddContainer() {
660 final QName containerQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "message");
661 final SchemaPath containerPath = SchemaPath.create(true, containerQName);
663 moduleBuilder.addContainerNode(22, containerQName, containerPath);
666 @Test(expected = YangParseException.class)
667 public void testAddContainerWithSameNameAsNotificationDefintion() throws Exception {
668 final QName notificationName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "message");
669 SchemaPath notificationPath = SchemaPath.create(true, notificationName);
670 moduleBuilder.addNotification(70, notificationName, notificationPath);
675 public void testAddContainerWithAlreadyAddedRpcDefintion() throws Exception {
676 final QName rpcDefName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "send-message");
677 SchemaPath rpcPath = SchemaPath.create(true, rpcDefName);
678 moduleBuilder.addRpc(70, rpcDefName, rpcPath);
681 DataSchemaNodeBuilder addedContainer = moduleBuilder.getDataChildByName("message");
682 assertNotNull(addedContainer);
686 public void testAddContainerWithAlreadyAddedNotificationDefintion() throws Exception {
687 final QName notificationName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "notify-me");
688 SchemaPath notificationPath = SchemaPath.create(true, notificationName);
689 moduleBuilder.addNotification(70, notificationName, notificationPath);
692 DataSchemaNodeBuilder addedContainer = moduleBuilder.getDataChildByName("message");
693 assertNotNull(addedContainer);
696 @Test(expected = YangParseException.class)
697 public void testAddDuplicateSchemaNodeIntoSubnodeOfModule() throws Exception {
698 final QName containerQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "top-level-container");
699 SchemaPath containerPath = SchemaPath.create(true, containerQName);
700 moduleBuilder.enterNode(new ContainerSchemaNodeBuilder(MODULE_NAME, 10, containerQName, containerPath));
702 final QName leafQName1 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "simple-leaf");
703 SchemaPath leafPath1 = SchemaPath.create(true, containerQName, leafQName1);
705 final QName leafQName2 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "simple-leaf");
706 SchemaPath leafPath2 = SchemaPath.create(true, containerQName, leafQName2);
708 moduleBuilder.addLeafNode(12, leafQName1, leafPath1);
709 moduleBuilder.addLeafNode(20, leafQName2, leafPath2);
712 @Test(expected = YangParseException.class)
713 public void testAddDuplicateCaseIntoChoiceCaseNode() throws Exception {
714 //FIXME: move this into ChoiceBuilderTest !!!
715 final QName choiceQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "top-level-container");
716 SchemaPath choicePath = SchemaPath.create(true, choiceQName);
718 moduleBuilder.enterNode(new ChoiceBuilder(MODULE_NAME, 10, choiceQName, choicePath));
720 final QName caseQName1 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "simple-case");
721 SchemaPath casePath1 = SchemaPath.create(true, choiceQName, caseQName1);
723 final QName caseQName2 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "simple-case");
724 SchemaPath casePath2 = SchemaPath.create(true, choiceQName, caseQName2);
726 moduleBuilder.addCase(12, caseQName1, casePath1);
727 moduleBuilder.addCase(20, caseQName2, casePath2);
730 @Test(expected = YangParseException.class)
731 public void testAddChoiceWithSameNameAsCase() throws Exception {
732 final QName choiceQName = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "my-choice");
733 SchemaPath choicePath = SchemaPath.create(true, choiceQName);
735 final ChoiceBuilder choiceBuilder = moduleBuilder.addChoice(8, choiceQName, choicePath);
736 moduleBuilder.enterNode(choiceBuilder);
738 final QName caseQName1 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "simple-case");
739 SchemaPath casePath1 = SchemaPath.create(true, choiceQName, caseQName1);
741 final QName caseQName2 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "simple-case");
742 SchemaPath casePath2 = SchemaPath.create(true, choiceQName, caseQName2);
744 moduleBuilder.addCase(12, caseQName1, casePath1);
745 moduleBuilder.addChoice(20, caseQName2, casePath2);
748 @Test(expected = YangParseException.class)
749 public void testAddChildToSubnodeForNonDataNodeContainerAndNonChoiceNode() {
750 final QName leafQName1 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "simple-leaf");
751 SchemaPath leafPath1 = SchemaPath.create(true, leafQName1);
753 final LeafSchemaNodeBuilder leafBuilder = moduleBuilder.addLeafNode(10, leafQName1, leafPath1);
754 moduleBuilder.enterNode(leafBuilder);
756 final QName leafQName2 = QName.create(TEST_MODULE_NAMESPACE, TEST_MODULE_REVISION, "simple-leaf");
757 SchemaPath leafPath2 = SchemaPath.create(true, leafQName1, leafQName2);
759 moduleBuilder.addLeafNode(12, leafQName2, leafPath2);
762 private static class NonSchemaNodeBuilder implements Builder {
767 final SchemaPath path;
769 public NonSchemaNodeBuilder(final String moduleName, final int line, final QName name, final SchemaPath path) {
770 this.moduleName = moduleName;
777 public String getModuleName() {
782 public int getLine() {
787 public Builder getParent() {
792 public void setParent(final Builder parent) {
797 public void addUnknownNodeBuilder(final UnknownSchemaNodeBuilder unknownNode) {
802 public List<UnknownSchemaNodeBuilder> getUnknownNodes() {
807 public Object build() {