2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.yang.parser.impl;
10 import static org.junit.Assert.*;
12 import java.io.FileNotFoundException;
14 import java.text.DateFormat;
15 import java.text.SimpleDateFormat;
16 import java.util.ArrayList;
17 import java.util.Date;
18 import java.util.HashSet;
19 import java.util.List;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.opendaylight.controller.yang.common.QName;
26 import org.opendaylight.controller.yang.model.api.AnyXmlSchemaNode;
27 import org.opendaylight.controller.yang.model.api.AugmentationSchema;
28 import org.opendaylight.controller.yang.model.api.ChoiceCaseNode;
29 import org.opendaylight.controller.yang.model.api.ChoiceNode;
30 import org.opendaylight.controller.yang.model.api.ConstraintDefinition;
31 import org.opendaylight.controller.yang.model.api.ContainerSchemaNode;
32 import org.opendaylight.controller.yang.model.api.DataSchemaNode;
33 import org.opendaylight.controller.yang.model.api.Deviation;
34 import org.opendaylight.controller.yang.model.api.Deviation.Deviate;
35 import org.opendaylight.controller.yang.model.api.ExtensionDefinition;
36 import org.opendaylight.controller.yang.model.api.FeatureDefinition;
37 import org.opendaylight.controller.yang.model.api.GroupingDefinition;
38 import org.opendaylight.controller.yang.model.api.LeafListSchemaNode;
39 import org.opendaylight.controller.yang.model.api.LeafSchemaNode;
40 import org.opendaylight.controller.yang.model.api.ListSchemaNode;
41 import org.opendaylight.controller.yang.model.api.Module;
42 import org.opendaylight.controller.yang.model.api.ModuleImport;
43 import org.opendaylight.controller.yang.model.api.MustDefinition;
44 import org.opendaylight.controller.yang.model.api.NotificationDefinition;
45 import org.opendaylight.controller.yang.model.api.RpcDefinition;
46 import org.opendaylight.controller.yang.model.api.SchemaNode;
47 import org.opendaylight.controller.yang.model.api.SchemaPath;
48 import org.opendaylight.controller.yang.model.api.Status;
49 import org.opendaylight.controller.yang.model.api.TypeDefinition;
50 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
51 import org.opendaylight.controller.yang.model.api.UsesNode;
52 import org.opendaylight.controller.yang.model.api.type.LengthConstraint;
53 import org.opendaylight.controller.yang.model.api.type.PatternConstraint;
54 import org.opendaylight.controller.yang.model.api.type.RangeConstraint;
55 import org.opendaylight.controller.yang.model.util.Decimal64;
56 import org.opendaylight.controller.yang.model.util.ExtendedType;
57 import org.opendaylight.controller.yang.model.util.Int16;
58 import org.opendaylight.controller.yang.model.util.Int32;
59 import org.opendaylight.controller.yang.model.util.Leafref;
60 import org.opendaylight.controller.yang.model.util.StringType;
61 import org.opendaylight.controller.yang.model.util.Uint32;
62 import org.opendaylight.controller.yang.model.util.UnionType;
64 public class YangParserTest {
65 private Set<Module> modules;
68 public void init() throws FileNotFoundException {
69 modules = TestUtils.loadModules("src/test/resources/model");
70 assertEquals(3, modules.size());
74 public void testHeaders() {
75 Module test = TestUtils.findModule(modules, "types1");
77 assertEquals("types1", test.getName());
78 assertEquals("1", test.getYangVersion());
79 URI expectedNamespace = URI.create("urn:simple.container.demo");
80 assertEquals(expectedNamespace, test.getNamespace());
81 assertEquals("t1", test.getPrefix());
83 Set<ModuleImport> imports = test.getImports();
84 assertEquals(2, imports.size());
86 ModuleImport import2 = TestUtils.findImport(imports, "data");
87 assertEquals("types2", import2.getModuleName());
88 assertEquals(TestUtils.createDate("2013-02-27"), import2.getRevision());
90 ModuleImport import3 = TestUtils.findImport(imports, "t3");
91 assertEquals("types3", import3.getModuleName());
92 assertEquals(TestUtils.createDate("2013-02-27"), import3.getRevision());
94 assertEquals("opendaylight", test.getOrganization());
95 assertEquals("http://www.opendaylight.org/", test.getContact());
96 Date expectedRevision = TestUtils.createDate("2013-02-27");
97 assertEquals(expectedRevision, test.getRevision());
98 assertEquals(" WILL BE DEFINED LATER", test.getReference());
102 public void testParseContainer() {
103 Module test = TestUtils.findModule(modules, "types2");
104 URI expectedNamespace = URI.create("urn:simple.types.data.demo");
105 String expectedPrefix = "t2";
106 Date expectedRevision = TestUtils.createDate("2013-02-27");
108 ContainerSchemaNode interfaces = (ContainerSchemaNode) test
109 .getDataChildByName("interfaces");
110 // test SchemaNode args
111 QName expectedQName = new QName(expectedNamespace, expectedRevision,
112 expectedPrefix, "interfaces");
113 assertEquals(expectedQName, interfaces.getQName());
114 SchemaPath expectedPath = TestUtils.createPath(true, expectedNamespace,
115 expectedRevision, expectedPrefix, "interfaces");
116 assertEquals(expectedPath, interfaces.getPath());
117 assertNull(interfaces.getDescription());
118 assertNull(interfaces.getReference());
119 assertEquals(Status.CURRENT, interfaces.getStatus());
120 assertEquals(0, interfaces.getUnknownSchemaNodes().size());
121 // test DataSchemaNode args
122 assertFalse(interfaces.isAugmenting());
123 assertFalse(interfaces.isConfiguration());
124 ConstraintDefinition constraints = interfaces.getConstraints();
125 assertNull(constraints.getWhenCondition());
126 assertEquals(0, constraints.getMustConstraints().size());
127 assertFalse(constraints.isMandatory());
128 assertNull(constraints.getMinElements());
129 assertNull(constraints.getMaxElements());
130 // test AugmentationTarget args
131 assertEquals(0, interfaces.getAvailableAugmentations().size());
132 // test ContainerSchemaNode args
133 assertFalse(interfaces.isPresenceContainer());
134 // test DataNodeContainer args
135 assertEquals(0, interfaces.getTypeDefinitions().size());
136 assertEquals(1, interfaces.getChildNodes().size());
137 assertEquals(0, interfaces.getGroupings().size());
138 assertEquals(0, interfaces.getUses().size());
140 ListSchemaNode ifEntry = (ListSchemaNode) interfaces
141 .getDataChildByName("ifEntry");
142 assertNotNull(ifEntry);
146 public void testParseList() {
147 Module test = TestUtils.findModule(modules, "types2");
148 URI expectedNamespace = URI.create("urn:simple.types.data.demo");
149 String expectedPrefix = "t2";
150 Date expectedRevision = TestUtils.createDate("2013-02-27");
152 ContainerSchemaNode interfaces = (ContainerSchemaNode) test
153 .getDataChildByName("interfaces");
155 ListSchemaNode ifEntry = (ListSchemaNode) interfaces
156 .getDataChildByName("ifEntry");
157 // test SchemaNode args
158 QName expectedQName = new QName(expectedNamespace, expectedRevision,
159 expectedPrefix, "ifEntry");
160 assertEquals(expectedQName, ifEntry.getQName());
161 SchemaPath expectedPath = TestUtils.createPath(true, expectedNamespace,
162 expectedRevision, expectedPrefix, "interfaces", "ifEntry");
163 assertEquals(expectedPath, ifEntry.getPath());
164 assertNull(ifEntry.getDescription());
165 assertNull(ifEntry.getReference());
166 assertEquals(Status.CURRENT, ifEntry.getStatus());
167 assertEquals(0, ifEntry.getUnknownSchemaNodes().size());
168 // test DataSchemaNode args
169 assertFalse(ifEntry.isAugmenting());
170 assertFalse(ifEntry.isConfiguration());
171 ConstraintDefinition constraints = ifEntry.getConstraints();
172 assertNull(constraints.getWhenCondition());
173 assertEquals(0, constraints.getMustConstraints().size());
174 assertFalse(constraints.isMandatory());
175 assertEquals(1, (int) constraints.getMinElements());
176 assertEquals(11, (int) constraints.getMaxElements());
177 // test AugmentationTarget args
178 Set<AugmentationSchema> availableAugmentations = ifEntry
179 .getAvailableAugmentations();
180 assertEquals(2, availableAugmentations.size());
181 // test ListSchemaNode args
182 List<QName> expectedKey = new ArrayList<QName>();
183 expectedKey.add(new QName(expectedNamespace, expectedRevision,
184 expectedPrefix, "ifIndex"));
185 assertEquals(expectedKey, ifEntry.getKeyDefinition());
186 assertFalse(ifEntry.isUserOrdered());
187 // test DataNodeContainer args
188 assertEquals(0, ifEntry.getTypeDefinitions().size());
189 assertEquals(4, ifEntry.getChildNodes().size());
190 assertEquals(0, ifEntry.getGroupings().size());
191 assertEquals(0, ifEntry.getUses().size());
193 LeafSchemaNode ifIndex = (LeafSchemaNode) ifEntry
194 .getDataChildByName("ifIndex");
195 assertTrue(ifIndex.getType() instanceof Uint32);
196 LeafSchemaNode ifMtu = (LeafSchemaNode) ifEntry
197 .getDataChildByName("ifMtu");
198 assertTrue(ifMtu.getType() instanceof Int32);
202 public void testAugmentResolving() {
204 Module module1 = TestUtils.findModule(modules, "types1");
206 Set<AugmentationSchema> module1Augmentations = module1
208 AugmentationSchema augment1 = module1Augmentations.iterator().next();
209 LeafSchemaNode augmentedLeafDefinition = (LeafSchemaNode) augment1
210 .getDataChildByName("ds0ChannelNumber");
211 assertTrue(augmentedLeafDefinition.isAugmenting());
214 Module module2 = TestUtils.findModule(modules, "types2");
216 ContainerSchemaNode interfaces = (ContainerSchemaNode) module2
217 .getDataChildByName("interfaces");
218 ListSchemaNode ifEntry = (ListSchemaNode) interfaces
219 .getDataChildByName("ifEntry");
220 ContainerSchemaNode augmentedContainer = (ContainerSchemaNode) ifEntry
221 .getDataChildByName("augment-holder");
223 // augmentation defined in testfile1 and augmentation returned from
224 // augmented container have to be same
225 Set<AugmentationSchema> augmentedContainerAugments = augmentedContainer
226 .getAvailableAugmentations();
227 AugmentationSchema augmentDefinition = augmentedContainerAugments
229 assertEquals(augment1, augmentDefinition);
231 LeafSchemaNode augmentedLeaf = (LeafSchemaNode) augmentedContainer
232 .getDataChildByName("ds0ChannelNumber");
233 assertTrue(augmentedLeaf.isAugmenting());
234 assertEquals(augmentedLeafDefinition, augmentedLeaf);
236 Set<AugmentationSchema> ifEntryAugments = ifEntry
237 .getAvailableAugmentations();
238 assertEquals(2, ifEntryAugments.size());
241 Module module3 = TestUtils.findModule(modules, "types3");
243 Set<AugmentationSchema> module3Augmentations = module3
245 assertEquals(2, module3Augmentations.size());
246 AugmentationSchema augment3 = null;
247 for (AugmentationSchema as : module3Augmentations) {
248 if ("if:ifType='ds0'".equals(as.getWhenCondition().toString())) {
252 ContainerSchemaNode augmentedContainerDefinition = (ContainerSchemaNode) augment3
253 .getDataChildByName("augment-holder");
254 assertTrue(augmentedContainerDefinition.isAugmenting());
257 assertEquals(augmentedContainer, augmentedContainerDefinition);
258 assertEquals(augmentedLeaf, augmentedLeafDefinition);
262 public void testAugmentTarget() {
263 Module test = TestUtils.findModule(modules, "types2");
265 ContainerSchemaNode interfaces = (ContainerSchemaNode) test
266 .getDataChildByName("interfaces");
267 ListSchemaNode ifEntry = (ListSchemaNode) interfaces
268 .getDataChildByName("ifEntry");
269 Set<AugmentationSchema> augmentations = ifEntry
270 .getAvailableAugmentations();
271 assertEquals(2, augmentations.size());
273 AugmentationSchema augment = null;
274 for (AugmentationSchema as : augmentations) {
275 if ("if:ifType='ds0'".equals(as.getWhenCondition().toString())) {
279 ContainerSchemaNode augmentHolder = (ContainerSchemaNode) augment
280 .getDataChildByName("augment-holder");
281 assertNotNull(augmentHolder);
282 assertTrue(augmentHolder.isAugmenting());
283 QName augmentHolderQName = augmentHolder.getQName();
284 assertEquals("augment-holder", augmentHolderQName.getLocalName());
285 assertEquals("t3", augmentHolderQName.getPrefix());
286 assertEquals("Description for augment holder",
287 augmentHolder.getDescription());
291 public void testTypedefRangesResolving() {
292 Module testModule = TestUtils.findModule(modules, "types1");
294 LeafSchemaNode testleaf = (LeafSchemaNode) testModule
295 .getDataChildByName("testleaf");
296 ExtendedType leafType = (ExtendedType) testleaf.getType();
297 assertEquals("my-type1", leafType.getQName().getLocalName());
298 assertEquals("t2", leafType.getQName().getPrefix());
299 ExtendedType baseType = (ExtendedType) leafType.getBaseType();
300 assertEquals("my-base-int32-type", baseType.getQName().getLocalName());
301 assertEquals("t2", baseType.getQName().getPrefix());
303 List<RangeConstraint> ranges = leafType.getRanges();
304 assertEquals(1, ranges.size());
305 RangeConstraint range = ranges.get(0);
306 assertEquals(3L, range.getMin());
307 assertEquals(20L, range.getMax());
311 public void testTypedefPatternsResolving() {
312 Module testModule = TestUtils.findModule(modules, "types1");
314 LeafSchemaNode testleaf = (LeafSchemaNode) testModule
315 .getDataChildByName("test-string-leaf");
316 ExtendedType testleafType = (ExtendedType) testleaf.getType();
317 QName testleafTypeQName = testleafType.getQName();
318 assertEquals("my-string-type-ext", testleafTypeQName.getLocalName());
319 assertEquals("t2", testleafTypeQName.getPrefix());
321 Set<String> expectedRegex = new HashSet<String>();
322 expectedRegex.add("[a-k]*");
323 expectedRegex.add("[b-u]*");
324 expectedRegex.add("[e-z]*");
326 Set<String> actualRegex = new HashSet<String>();
327 List<PatternConstraint> patterns = testleafType.getPatterns();
328 assertEquals(3, patterns.size());
329 for (PatternConstraint pc : patterns) {
330 actualRegex.add(pc.getRegularExpression());
332 assertEquals(expectedRegex, actualRegex);
334 TypeDefinition<?> baseType = testleafType.getBaseType();
335 assertEquals("my-string-type2", baseType.getQName().getLocalName());
337 List<LengthConstraint> lengths = testleafType.getLengths();
338 assertEquals(1, lengths.size());
340 LengthConstraint length = lengths.get(0);
341 assertEquals(5L, length.getMin());
342 assertEquals(10L, length.getMax());
346 public void testTypedefLengthsResolving() {
347 Module testModule = TestUtils.findModule(modules, "types1");
349 LeafSchemaNode testleaf = (LeafSchemaNode) testModule
350 .getDataChildByName("leaf-with-length");
351 ExtendedType testleafType = (ExtendedType) testleaf.getType();
352 assertEquals("my-string-type", testleafType.getQName().getLocalName());
354 List<LengthConstraint> lengths = testleafType.getLengths();
355 assertEquals(1, lengths.size());
357 LengthConstraint length = lengths.get(0);
358 assertEquals(7L, length.getMin());
359 assertEquals(10L, length.getMax());
363 public void testTypeDef() {
364 Module testModule = TestUtils.findModule(modules, "types2");
366 LeafSchemaNode testleaf = (LeafSchemaNode) testModule
367 .getDataChildByName("nested-type-leaf");
368 ExtendedType testleafType = (ExtendedType) testleaf.getType();
369 assertEquals("my-type1", testleafType.getQName().getLocalName());
371 ExtendedType baseType = (ExtendedType) testleafType.getBaseType();
372 assertEquals("my-base-int32-type", baseType.getQName().getLocalName());
374 Int32 int32base = (Int32) baseType.getBaseType();
375 List<RangeConstraint> ranges = int32base.getRangeStatements();
376 assertEquals(1, ranges.size());
377 RangeConstraint range = ranges.get(0);
378 assertEquals(2L, range.getMin());
379 assertEquals(20L, range.getMax());
383 public void testTypedefDecimal1() {
384 Module testModule = TestUtils.findModule(modules, "types1");
386 LeafSchemaNode testleaf = (LeafSchemaNode) testModule
387 .getDataChildByName("test-decimal-leaf");
388 ExtendedType type = (ExtendedType) testleaf.getType();
389 assertEquals(4, (int) type.getFractionDigits());
391 Decimal64 baseType = (Decimal64) type.getBaseType();
392 assertEquals(6, (int) baseType.getFractionDigits());
396 public void testTypedefDecimal2() {
397 Module testModule = TestUtils.findModule(modules, "types1");
399 LeafSchemaNode testleaf = (LeafSchemaNode) testModule
400 .getDataChildByName("test-decimal-leaf2");
401 TypeDefinition<?> baseType = testleaf.getType().getBaseType();
402 assertTrue(testleaf.getType().getBaseType() instanceof Decimal64);
403 Decimal64 baseTypeCast = (Decimal64) baseType;
404 assertEquals(5, (int) baseTypeCast.getFractionDigits());
408 public void testTypedefUnion() {
409 Module testModule = TestUtils.findModule(modules, "types1");
411 LeafSchemaNode testleaf = (LeafSchemaNode) testModule
412 .getDataChildByName("union-leaf");
413 ExtendedType testleafType = (ExtendedType) testleaf.getType();
414 assertEquals("my-union-ext", testleafType.getQName().getLocalName());
416 ExtendedType baseType = (ExtendedType) testleafType.getBaseType();
417 assertEquals("my-union", baseType.getQName().getLocalName());
419 UnionType unionBase = (UnionType) baseType.getBaseType();
421 List<TypeDefinition<?>> unionTypes = unionBase.getTypes();
422 Int16 unionType1 = (Int16) unionTypes.get(0);
423 List<RangeConstraint> ranges = unionType1.getRangeStatements();
424 assertEquals(1, ranges.size());
425 RangeConstraint range = ranges.get(0);
426 assertEquals(1L, range.getMin());
427 assertEquals(100L, range.getMax());
429 assertTrue(unionTypes.get(0) instanceof Int16);
430 assertTrue(unionTypes.get(1) instanceof Int32);
434 public void testNestedUnionResolving1() {
435 Module testModule = TestUtils.findModule(modules, "types1");
437 LeafSchemaNode testleaf = (LeafSchemaNode) testModule
438 .getDataChildByName("nested-union-leaf");
440 ExtendedType nestedUnion1 = (ExtendedType) testleaf.getType();
441 assertEquals("nested-union1", nestedUnion1.getQName().getLocalName());
443 ExtendedType nestedUnion2 = (ExtendedType) nestedUnion1.getBaseType();
444 assertEquals("nested-union2", nestedUnion2.getQName().getLocalName());
446 UnionType unionType1 = (UnionType) nestedUnion2.getBaseType();
447 List<TypeDefinition<?>> unionTypes = unionType1.getTypes();
448 assertEquals(2, unionTypes.size());
449 assertTrue(unionTypes.get(0) instanceof StringType);
450 assertTrue(unionTypes.get(1) instanceof ExtendedType);
452 ExtendedType extendedUnion = (ExtendedType) unionTypes.get(1);
453 ExtendedType extendedUnionBase = (ExtendedType) extendedUnion
455 assertEquals("my-union", extendedUnionBase.getQName().getLocalName());
457 UnionType extendedTargetUnion = (UnionType) extendedUnionBase
459 List<TypeDefinition<?>> extendedTargetTypes = extendedTargetUnion
461 assertTrue(extendedTargetTypes.get(0) instanceof Int16);
462 assertTrue(extendedTargetTypes.get(1) instanceof Int32);
464 Int16 int16 = (Int16) extendedTargetTypes.get(0);
465 List<RangeConstraint> ranges = int16.getRangeStatements();
466 assertEquals(1, ranges.size());
467 RangeConstraint range = ranges.get(0);
468 assertEquals(1L, range.getMin());
469 assertEquals(100L, range.getMax());
473 public void testNestedUnionResolving2() {
474 Module testModule = TestUtils.findModule(modules, "types1");
476 LeafSchemaNode testleaf = (LeafSchemaNode) testModule
477 .getDataChildByName("custom-union-leaf");
479 ExtendedType testleafType = (ExtendedType) testleaf.getType();
480 QName testleafTypeQName = testleafType.getQName();
481 assertEquals(URI.create("urn:simple.container.demo.test"),
482 testleafTypeQName.getNamespace());
483 assertEquals(TestUtils.createDate("2013-02-27"),
484 testleafTypeQName.getRevision());
485 assertEquals("t3", testleafTypeQName.getPrefix());
486 assertEquals("union1", testleafTypeQName.getLocalName());
488 ExtendedType union2 = (ExtendedType) testleafType.getBaseType();
489 QName union2QName = union2.getQName();
490 assertEquals(URI.create("urn:simple.container.demo.test"),
491 union2QName.getNamespace());
492 assertEquals(TestUtils.createDate("2013-02-27"),
493 union2QName.getRevision());
494 assertEquals("t3", union2QName.getPrefix());
495 assertEquals("union2", union2QName.getLocalName());
497 UnionType union2Base = (UnionType) union2.getBaseType();
498 List<TypeDefinition<?>> unionTypes = union2Base.getTypes();
499 assertEquals(2, unionTypes.size());
500 assertTrue(unionTypes.get(0) instanceof Int32);
501 assertTrue(unionTypes.get(1) instanceof ExtendedType);
503 ExtendedType nestedUnion2 = (ExtendedType) unionTypes.get(1);
504 QName nestedUnion2QName = nestedUnion2.getQName();
505 assertEquals(URI.create("urn:simple.types.data.demo"),
506 nestedUnion2QName.getNamespace());
507 assertEquals(TestUtils.createDate("2013-02-27"),
508 nestedUnion2QName.getRevision());
509 assertEquals("t2", nestedUnion2QName.getPrefix());
510 assertEquals("nested-union2", nestedUnion2QName.getLocalName());
512 UnionType nestedUnion2Base = (UnionType) nestedUnion2.getBaseType();
513 List<TypeDefinition<?>> nestedUnion2Types = nestedUnion2Base.getTypes();
514 assertEquals(2, nestedUnion2Types.size());
515 assertTrue(nestedUnion2Types.get(0) instanceof StringType);
516 assertTrue(nestedUnion2Types.get(1) instanceof ExtendedType);
518 ExtendedType myUnionExt = (ExtendedType) nestedUnion2Types.get(1);
519 QName myUnionExtQName = myUnionExt.getQName();
520 assertEquals(URI.create("urn:simple.types.data.demo"),
521 myUnionExtQName.getNamespace());
522 assertEquals(TestUtils.createDate("2013-02-27"),
523 myUnionExtQName.getRevision());
524 assertEquals("t2", myUnionExtQName.getPrefix());
525 assertEquals("my-union-ext", myUnionExtQName.getLocalName());
527 ExtendedType myUnion = (ExtendedType) myUnionExt.getBaseType();
528 QName myUnionQName = myUnion.getQName();
529 assertEquals(URI.create("urn:simple.types.data.demo"),
530 myUnionQName.getNamespace());
531 assertEquals(TestUtils.createDate("2013-02-27"),
532 myUnionQName.getRevision());
533 assertEquals("t2", myUnionQName.getPrefix());
534 assertEquals("my-union", myUnionQName.getLocalName());
536 UnionType myUnionBase = (UnionType) myUnion.getBaseType();
537 List<TypeDefinition<?>> myUnionBaseTypes = myUnionBase.getTypes();
538 assertEquals(2, myUnionBaseTypes.size());
539 assertTrue(myUnionBaseTypes.get(0) instanceof Int16);
540 assertTrue(myUnionBaseTypes.get(1) instanceof Int32);
541 Int16 int16 = (Int16) myUnionBaseTypes.get(0);
542 List<RangeConstraint> ranges = int16.getRangeStatements();
543 assertEquals(1, ranges.size());
544 RangeConstraint range = ranges.get(0);
545 assertEquals(1L, range.getMin());
546 assertEquals(100L, range.getMax());
550 public void testRefine() {
551 Module testModule = TestUtils.findModule(modules, "types2");
553 ContainerSchemaNode peer = (ContainerSchemaNode) testModule
554 .getDataChildByName("peer");
555 ContainerSchemaNode destination = (ContainerSchemaNode) peer
556 .getDataChildByName("destination");
557 Set<UsesNode> usesNodes = destination.getUses();
558 assertEquals(1, usesNodes.size());
559 UsesNode usesNode = usesNodes.iterator().next();
560 Map<SchemaPath, SchemaNode> refines = usesNode.getRefines();
561 assertEquals(5, refines.size());
563 LeafSchemaNode refineLeaf = null;
564 ContainerSchemaNode refineContainer = null;
565 ListSchemaNode refineList = null;
566 GroupingDefinition refineGrouping = null;
567 TypeDefinition<?> typedef = null;
568 for (Map.Entry<SchemaPath, SchemaNode> entry : refines.entrySet()) {
569 SchemaNode value = entry.getValue();
570 if (value instanceof LeafSchemaNode) {
571 refineLeaf = (LeafSchemaNode) value;
572 } else if (value instanceof ContainerSchemaNode) {
573 refineContainer = (ContainerSchemaNode) value;
574 } else if (value instanceof ListSchemaNode) {
575 refineList = (ListSchemaNode) value;
576 } else if (value instanceof GroupingDefinition) {
577 refineGrouping = (GroupingDefinition) value;
578 } else if (value instanceof TypeDefinition<?>) {
579 typedef = (TypeDefinition<?>) value;
584 assertNotNull(refineLeaf);
585 assertEquals("address", refineLeaf.getQName().getLocalName());
586 assertEquals("description of address defined by refine",
587 refineLeaf.getDescription());
588 assertEquals("address reference added by refine",
589 refineLeaf.getReference());
590 assertFalse(refineLeaf.isConfiguration());
591 assertTrue(refineLeaf.getConstraints().isMandatory());
592 Set<MustDefinition> leafMustConstraints = refineLeaf.getConstraints()
593 .getMustConstraints();
594 assertEquals(1, leafMustConstraints.size());
595 MustDefinition leafMust = leafMustConstraints.iterator().next();
597 "\"ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)\"",
598 leafMust.toString());
601 assertNotNull(refineContainer);
602 Set<MustDefinition> mustConstraints = refineContainer.getConstraints()
603 .getMustConstraints();
604 assertEquals(1, mustConstraints.size());
605 MustDefinition must = mustConstraints.iterator().next();
606 assertEquals("must-condition", must.toString());
607 assertEquals("An error message test", must.getErrorMessage());
608 assertEquals(("An error app tag test"), must.getErrorAppTag());
609 assertEquals("description of port defined by refine",
610 refineContainer.getDescription());
611 assertEquals("port reference added by refine",
612 refineContainer.getReference());
613 assertFalse(refineContainer.isConfiguration());
614 assertTrue(refineContainer.isPresenceContainer());
617 assertNotNull(refineList);
618 assertEquals("description of addresses defined by refine",
619 refineList.getDescription());
620 assertEquals("addresses reference added by refine",
621 refineList.getReference());
622 assertFalse(refineList.isConfiguration());
623 assertEquals(2, (int) refineList.getConstraints().getMinElements());
624 assertEquals(12, (int) refineList.getConstraints().getMaxElements());
626 // grouping target-inner
627 assertNotNull(refineGrouping);
628 Set<DataSchemaNode> refineGroupingChildren = refineGrouping
630 assertEquals(1, refineGroupingChildren.size());
631 LeafSchemaNode refineGroupingLeaf = (LeafSchemaNode) refineGroupingChildren
633 assertEquals("inner-grouping-id", refineGroupingLeaf.getQName()
635 assertEquals("new target-inner grouping description",
636 refineGrouping.getDescription());
638 // typedef group-type
639 assertNotNull(typedef);
640 assertEquals("new group-type description", typedef.getDescription());
641 assertEquals("new group-type reference", typedef.getReference());
642 assertTrue(typedef.getBaseType() instanceof ExtendedType);
646 public void testChoice() {
647 Module testModule = TestUtils.findModule(modules, "types1");
648 ContainerSchemaNode peer = (ContainerSchemaNode) testModule
649 .getDataChildByName("transfer");
650 ChoiceNode how = (ChoiceNode) peer.getDataChildByName("how");
651 Set<ChoiceCaseNode> cases = how.getCases();
652 assertEquals(3, cases.size());
656 public void testAnyXml() {
657 Module testModule = TestUtils.findModule(modules, "types1");
658 AnyXmlSchemaNode data = (AnyXmlSchemaNode) testModule
659 .getDataChildByName("data");
664 public void testDeviation() {
665 Module testModule = TestUtils.findModule(modules, "types1");
666 Set<Deviation> deviations = testModule.getDeviations();
667 assertEquals(1, deviations.size());
669 Deviation dev = deviations.iterator().next();
670 SchemaPath expectedPath = TestUtils.createPath(true, null, null,
671 "data", "system", "user");
672 assertEquals(expectedPath, dev.getTargetPath());
673 assertEquals(Deviate.ADD, dev.getDeviate());
677 public void testUnknownNode() {
678 Module testModule = TestUtils.findModule(modules, "types3");
679 ContainerSchemaNode network = (ContainerSchemaNode) testModule
680 .getDataChildByName("network");
681 List<UnknownSchemaNode> unknownNodes = network.getUnknownSchemaNodes();
682 assertEquals(1, unknownNodes.size());
683 UnknownSchemaNode unknownNode = unknownNodes.get(0);
684 assertNotNull(unknownNode.getNodeType());
685 assertEquals("point", unknownNode.getNodeParameter());
689 public void testFeature() {
690 Module testModule = TestUtils.findModule(modules, "types3");
691 Set<FeatureDefinition> features = testModule.getFeatures();
692 assertEquals(1, features.size());
696 public void testExtension() {
697 Module testModule = TestUtils.findModule(modules, "types3");
698 List<ExtensionDefinition> extensions = testModule
699 .getExtensionSchemaNodes();
700 assertEquals(1, extensions.size());
701 ExtensionDefinition extension = extensions.get(0);
702 assertEquals("name", extension.getArgument());
703 assertFalse(extension.isYinElement());
707 public void testNotification() {
708 Module testModule = TestUtils.findModule(modules, "types3");
709 URI expectedNamespace = URI.create("urn:simple.container.demo.test");
710 String expectedPrefix = "t3";
711 Date expectedRevision = TestUtils.createDate("2013-02-27");
713 Set<NotificationDefinition> notifications = testModule
715 assertEquals(1, notifications.size());
717 NotificationDefinition notification = notifications.iterator().next();
718 // test SchemaNode args
719 QName expectedQName = new QName(expectedNamespace, expectedRevision,
720 expectedPrefix, "event");
721 assertEquals(expectedQName, notification.getQName());
722 SchemaPath expectedPath = TestUtils.createPath(true, expectedNamespace,
723 expectedRevision, expectedPrefix, "event");
724 assertEquals(expectedPath, notification.getPath());
725 assertNull(notification.getDescription());
726 assertNull(notification.getReference());
727 assertEquals(Status.CURRENT, notification.getStatus());
728 assertEquals(0, notification.getUnknownSchemaNodes().size());
729 // test DataNodeContainer args
730 assertEquals(0, notification.getTypeDefinitions().size());
731 assertEquals(3, notification.getChildNodes().size());
732 assertEquals(0, notification.getGroupings().size());
733 assertEquals(0, notification.getUses().size());
735 LeafSchemaNode eventClass = (LeafSchemaNode) notification
736 .getDataChildByName("event-class");
737 assertTrue(eventClass.getType() instanceof StringType);
738 AnyXmlSchemaNode reportingEntity = (AnyXmlSchemaNode) notification
739 .getDataChildByName("reporting-entity");
740 assertNotNull(reportingEntity);
741 LeafSchemaNode severity = (LeafSchemaNode) notification
742 .getDataChildByName("severity");
743 assertTrue(severity.getType() instanceof StringType);
747 public void testRpc() {
748 Module testModule = TestUtils.findModule(modules, "types3");
750 Set<RpcDefinition> rpcs = testModule.getRpcs();
751 assertEquals(1, rpcs.size());
753 RpcDefinition rpc = rpcs.iterator().next();
754 assertEquals("Retrieve all or part of a specified configuration.",
755 rpc.getDescription());
756 assertEquals("RFC 6241, Section 7.1", rpc.getReference());
758 ContainerSchemaNode input = rpc.getInput();
759 assertNotNull(input.getDataChildByName("source"));
760 assertNotNull(input.getDataChildByName("filter"));
761 ContainerSchemaNode output = rpc.getOutput();
762 assertNotNull(output.getDataChildByName("data"));
766 public void testGrouping() {
767 Module testModule = TestUtils.findModule(modules, "types2");
768 Set<GroupingDefinition> groupings = testModule.getGroupings();
769 assertEquals(1, groupings.size());
770 GroupingDefinition grouping = groupings.iterator().next();
771 Set<DataSchemaNode> children = grouping.getChildNodes();
772 assertEquals(5, children.size());
776 public void testAugmentNodesTypesSchemaPath() throws Exception {
777 final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
778 Module testModule = TestUtils.findModule(modules, "types1");
779 Set<AugmentationSchema> augments = testModule.getAugmentations();
780 assertEquals(1, augments.size());
781 AugmentationSchema augment = augments.iterator().next();
783 LeafSchemaNode ifcId = (LeafSchemaNode) augment
784 .getDataChildByName("interface-id");
785 Leafref ifcIdType = (Leafref) ifcId.getType();
786 SchemaPath ifcIdTypeSchemaPath = ifcIdType.getPath();
787 List<QName> ifcIdTypePath = ifcIdTypeSchemaPath.getPath();
788 QName q0 = new QName(new URI("urn:simple.types.data.demo"),
789 simpleDateFormat.parse("2013-02-27"), "data", "interfaces");
790 QName q1 = new QName(new URI("urn:simple.types.data.demo"),
791 simpleDateFormat.parse("2013-02-27"), "data", "ifEntry");
792 QName q2 = new QName(new URI("urn:simple.container.demo.test"),
793 simpleDateFormat.parse("2013-02-27"), "data", "augment-holder");
794 QName q3 = new QName(new URI("urn:simple.container.demo"),
795 simpleDateFormat.parse("2013-02-27"), "data", "interface-id");
796 assertEquals(q0, ifcIdTypePath.get(0));
797 assertEquals(q1, ifcIdTypePath.get(1));
798 assertEquals(q2, ifcIdTypePath.get(2));
799 assertEquals(q3, ifcIdTypePath.get(3));
801 LeafListSchemaNode higherLayer = (LeafListSchemaNode) augment
802 .getDataChildByName("higher-layer-if");
803 Leafref higherLayerType = (Leafref) higherLayer.getType();
804 SchemaPath higherLayerTypeSchemaPath = higherLayerType.getPath();
805 List<QName> higherLayerTypePath = higherLayerTypeSchemaPath.getPath();
806 assertEquals(q0, higherLayerTypePath.get(0));
807 assertEquals(q1, higherLayerTypePath.get(1));
808 assertEquals(q2, higherLayerTypePath.get(2));
809 q3 = new QName(new URI("urn:simple.container.demo"),
810 simpleDateFormat.parse("2013-02-27"), "data", "higher-layer-if");
811 assertEquals(q3, higherLayerTypePath.get(3));