910a360c9be920ecab6efe819656828e5d582b28
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / test / java / org / opendaylight / controller / yang / parser / impl / YangParserTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.yang.parser.impl;
9
10 import static org.junit.Assert.*;
11
12 import java.net.URI;
13 import java.util.ArrayList;
14 import java.util.Date;
15 import java.util.HashSet;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Set;
19
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.opendaylight.controller.yang.common.QName;
23 import org.opendaylight.controller.yang.model.api.AnyXmlSchemaNode;
24 import org.opendaylight.controller.yang.model.api.AugmentationSchema;
25 import org.opendaylight.controller.yang.model.api.ChoiceCaseNode;
26 import org.opendaylight.controller.yang.model.api.ChoiceNode;
27 import org.opendaylight.controller.yang.model.api.ConstraintDefinition;
28 import org.opendaylight.controller.yang.model.api.ContainerSchemaNode;
29 import org.opendaylight.controller.yang.model.api.DataSchemaNode;
30 import org.opendaylight.controller.yang.model.api.Deviation;
31 import org.opendaylight.controller.yang.model.api.Deviation.Deviate;
32 import org.opendaylight.controller.yang.model.api.ExtensionDefinition;
33 import org.opendaylight.controller.yang.model.api.FeatureDefinition;
34 import org.opendaylight.controller.yang.model.api.GroupingDefinition;
35 import org.opendaylight.controller.yang.model.api.LeafSchemaNode;
36 import org.opendaylight.controller.yang.model.api.ListSchemaNode;
37 import org.opendaylight.controller.yang.model.api.Module;
38 import org.opendaylight.controller.yang.model.api.ModuleImport;
39 import org.opendaylight.controller.yang.model.api.MustDefinition;
40 import org.opendaylight.controller.yang.model.api.NotificationDefinition;
41 import org.opendaylight.controller.yang.model.api.RpcDefinition;
42 import org.opendaylight.controller.yang.model.api.SchemaNode;
43 import org.opendaylight.controller.yang.model.api.SchemaPath;
44 import org.opendaylight.controller.yang.model.api.Status;
45 import org.opendaylight.controller.yang.model.api.TypeDefinition;
46 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
47 import org.opendaylight.controller.yang.model.api.UsesNode;
48 import org.opendaylight.controller.yang.model.api.type.LengthConstraint;
49 import org.opendaylight.controller.yang.model.api.type.PatternConstraint;
50 import org.opendaylight.controller.yang.model.api.type.RangeConstraint;
51 import org.opendaylight.controller.yang.model.util.Decimal64;
52 import org.opendaylight.controller.yang.model.util.ExtendedType;
53 import org.opendaylight.controller.yang.model.util.Int16;
54 import org.opendaylight.controller.yang.model.util.Int32;
55 import org.opendaylight.controller.yang.model.util.StringType;
56 import org.opendaylight.controller.yang.model.util.Uint32;
57 import org.opendaylight.controller.yang.model.util.UnionType;
58
59 public class YangParserTest {
60     private Set<Module> modules;
61
62     @Before
63     public void init() {
64         modules = TestUtils.loadModules("src/test/resources/model");
65         assertEquals(3, modules.size());
66     }
67
68     @Test
69     public void testHeaders() {
70         Module test = TestUtils.findModule(modules, "types1");
71
72         assertEquals("types1", test.getName());
73         assertEquals("1", test.getYangVersion());
74         URI expectedNamespace = URI.create("urn:simple.container.demo");
75         assertEquals(expectedNamespace, test.getNamespace());
76         assertEquals("t1", test.getPrefix());
77
78         Set<ModuleImport> imports = test.getImports();
79         assertEquals(2, imports.size());
80
81         ModuleImport import2 = TestUtils.findImport(imports, "data");
82         assertEquals("types2", import2.getModuleName());
83         assertEquals(TestUtils.createDate("2013-02-27"), import2.getRevision());
84
85         ModuleImport import3 = TestUtils.findImport(imports, "t3");
86         assertEquals("types3", import3.getModuleName());
87         assertEquals(TestUtils.createDate("2013-02-27"), import3.getRevision());
88
89         assertEquals("opendaylight", test.getOrganization());
90         assertEquals("http://www.opendaylight.org/", test.getContact());
91         Date expectedRevision = TestUtils.createDate("2013-02-27");
92         assertEquals(expectedRevision, test.getRevision());
93         assertEquals(" WILL BE DEFINED LATER", test.getReference());
94     }
95
96     @Test
97     public void testParseContainer() {
98         Module test = TestUtils.findModule(modules, "types2");
99         URI expectedNamespace = URI.create("urn:simple.types.data.demo");
100         String expectedPrefix = "t2";
101         Date expectedRevision = TestUtils.createDate("2013-02-27");
102
103         ContainerSchemaNode interfaces = (ContainerSchemaNode) test
104                 .getDataChildByName("interfaces");
105         // test SchemaNode args
106         QName expectedQName = new QName(expectedNamespace, expectedRevision,
107                 expectedPrefix, "interfaces");
108         assertEquals(expectedQName, interfaces.getQName());
109         SchemaPath expectedPath = TestUtils.createPath(true, expectedNamespace,
110                 expectedRevision, expectedPrefix, "interfaces");
111         assertEquals(expectedPath, interfaces.getPath());
112         assertNull(interfaces.getDescription());
113         assertNull(interfaces.getReference());
114         assertEquals(Status.CURRENT, interfaces.getStatus());
115         assertEquals(0, interfaces.getUnknownSchemaNodes().size());
116         // test DataSchemaNode args
117         assertFalse(interfaces.isAugmenting());
118         assertFalse(interfaces.isConfiguration());
119         ConstraintDefinition constraints = interfaces.getConstraints();
120         assertNull(constraints.getWhenCondition());
121         assertEquals(0, constraints.getMustConstraints().size());
122         assertFalse(constraints.isMandatory());
123         assertNull(constraints.getMinElements());
124         assertNull(constraints.getMaxElements());
125         // test AugmentationTarget args
126         assertEquals(0, interfaces.getAvailableAugmentations().size());
127         // test ContainerSchemaNode args
128         assertFalse(interfaces.isPresenceContainer());
129         // test DataNodeContainer args
130         assertEquals(0, interfaces.getTypeDefinitions().size());
131         assertEquals(1, interfaces.getChildNodes().size());
132         assertEquals(0, interfaces.getGroupings().size());
133         assertEquals(0, interfaces.getUses().size());
134
135         ListSchemaNode ifEntry = (ListSchemaNode) interfaces
136                 .getDataChildByName("ifEntry");
137         assertNotNull(ifEntry);
138     }
139
140     @Test
141     public void testParseList() {
142         Module test = TestUtils.findModule(modules, "types2");
143         URI expectedNamespace = URI.create("urn:simple.types.data.demo");
144         String expectedPrefix = "t2";
145         Date expectedRevision = TestUtils.createDate("2013-02-27");
146
147         ContainerSchemaNode interfaces = (ContainerSchemaNode) test
148                 .getDataChildByName("interfaces");
149
150         ListSchemaNode ifEntry = (ListSchemaNode) interfaces
151                 .getDataChildByName("ifEntry");
152         // test SchemaNode args
153         QName expectedQName = new QName(expectedNamespace, expectedRevision,
154                 expectedPrefix, "ifEntry");
155         assertEquals(expectedQName, ifEntry.getQName());
156         SchemaPath expectedPath = TestUtils.createPath(true, expectedNamespace,
157                 expectedRevision, expectedPrefix, "interfaces", "ifEntry");
158         assertEquals(expectedPath, ifEntry.getPath());
159         assertNull(ifEntry.getDescription());
160         assertNull(ifEntry.getReference());
161         assertEquals(Status.CURRENT, ifEntry.getStatus());
162         assertEquals(0, ifEntry.getUnknownSchemaNodes().size());
163         // test DataSchemaNode args
164         assertFalse(ifEntry.isAugmenting());
165         assertFalse(ifEntry.isConfiguration());
166         ConstraintDefinition constraints = ifEntry.getConstraints();
167         assertNull(constraints.getWhenCondition());
168         assertEquals(0, constraints.getMustConstraints().size());
169         assertFalse(constraints.isMandatory());
170         assertEquals(1, (int)constraints.getMinElements());
171         assertEquals(11, (int)constraints.getMaxElements());
172         // test AugmentationTarget args
173         Set<AugmentationSchema> availableAugmentations = ifEntry
174                 .getAvailableAugmentations();
175         assertEquals(2, availableAugmentations.size());
176         // test ListSchemaNode args
177         List<QName> expectedKey = new ArrayList<QName>();
178         expectedKey.add(new QName(expectedNamespace, expectedRevision,
179                 expectedPrefix, "ifIndex"));
180         assertEquals(expectedKey, ifEntry.getKeyDefinition());
181         assertFalse(ifEntry.isUserOrdered());
182         // test DataNodeContainer args
183         assertEquals(0, ifEntry.getTypeDefinitions().size());
184         assertEquals(4, ifEntry.getChildNodes().size());
185         assertEquals(0, ifEntry.getGroupings().size());
186         assertEquals(0, ifEntry.getUses().size());
187
188         LeafSchemaNode ifIndex = (LeafSchemaNode) ifEntry
189                 .getDataChildByName("ifIndex");
190         assertTrue(ifIndex.getType() instanceof Uint32);
191         LeafSchemaNode ifMtu = (LeafSchemaNode) ifEntry
192                 .getDataChildByName("ifMtu");
193         assertTrue(ifMtu.getType() instanceof Int32);
194     }
195
196     @Test
197     public void testAugmentResolving() {
198         // testfile1
199         Module module1 = TestUtils.findModule(modules, "types1");
200
201         Set<AugmentationSchema> module1Augmentations = module1
202                 .getAugmentations();
203         AugmentationSchema augment1 = module1Augmentations.iterator().next();
204         LeafSchemaNode augmentedLeafDefinition = (LeafSchemaNode) augment1
205                 .getDataChildByName("ds0ChannelNumber");
206         assertTrue(augmentedLeafDefinition.isAugmenting());
207
208         // testfile2
209         Module module2 = TestUtils.findModule(modules, "types2");
210
211         ContainerSchemaNode interfaces = (ContainerSchemaNode) module2
212                 .getDataChildByName("interfaces");
213         ListSchemaNode ifEntry = (ListSchemaNode) interfaces
214                 .getDataChildByName("ifEntry");
215         ContainerSchemaNode augmentedContainer = (ContainerSchemaNode) ifEntry
216                 .getDataChildByName("augment-holder");
217
218         // augmentation defined in testfile1 and augmentation returned from
219         // augmented container have to be same
220         Set<AugmentationSchema> augmentedContainerAugments = augmentedContainer
221                 .getAvailableAugmentations();
222         AugmentationSchema augmentDefinition = augmentedContainerAugments
223                 .iterator().next();
224         assertEquals(augment1, augmentDefinition);
225
226         LeafSchemaNode augmentedLeaf = (LeafSchemaNode) augmentedContainer
227                 .getDataChildByName("ds0ChannelNumber");
228         assertTrue(augmentedLeaf.isAugmenting());
229         assertEquals(augmentedLeafDefinition, augmentedLeaf);
230
231         Set<AugmentationSchema> ifEntryAugments = ifEntry
232                 .getAvailableAugmentations();
233         assertEquals(2, ifEntryAugments.size());
234
235         // testfile3
236         Module module3 = TestUtils.findModule(modules, "types3");
237
238         Set<AugmentationSchema> module3Augmentations = module3
239                 .getAugmentations();
240         assertEquals(2, module3Augmentations.size());
241         AugmentationSchema augment3 = null;
242         for (AugmentationSchema as : module3Augmentations) {
243             if ("if:ifType='ds0'".equals(as.getWhenCondition().toString())) {
244                 augment3 = as;
245             }
246         }
247         ContainerSchemaNode augmentedContainerDefinition = (ContainerSchemaNode) augment3
248                 .getDataChildByName("augment-holder");
249         assertTrue(augmentedContainerDefinition.isAugmenting());
250
251         // check
252         assertEquals(augmentedContainer, augmentedContainerDefinition);
253         assertEquals(augmentedLeaf, augmentedLeafDefinition);
254     }
255
256     @Test
257     public void testAugmentTarget() {
258         Module test = TestUtils.findModule(modules, "types2");
259
260         ContainerSchemaNode interfaces = (ContainerSchemaNode) test
261                 .getDataChildByName("interfaces");
262         ListSchemaNode ifEntry = (ListSchemaNode) interfaces
263                 .getDataChildByName("ifEntry");
264         Set<AugmentationSchema> augmentations = ifEntry
265                 .getAvailableAugmentations();
266         assertEquals(2, augmentations.size());
267
268         AugmentationSchema augment = null;
269         for (AugmentationSchema as : augmentations) {
270             if ("if:ifType='ds0'".equals(as.getWhenCondition().toString())) {
271                 augment = as;
272             }
273         }
274         ContainerSchemaNode augmentHolder = (ContainerSchemaNode) augment
275                 .getDataChildByName("augment-holder");
276         assertNotNull(augmentHolder);
277         assertTrue(augmentHolder.isAugmenting());
278         QName augmentHolderQName = augmentHolder.getQName();
279         assertEquals("augment-holder", augmentHolderQName.getLocalName());
280         assertEquals("t3", augmentHolderQName.getPrefix());
281         assertEquals("Description for augment holder",
282                 augmentHolder.getDescription());
283     }
284
285     @Test
286     public void testTypedefRangesResolving() {
287         Module testModule = TestUtils.findModule(modules, "types1");
288
289         LeafSchemaNode testleaf = (LeafSchemaNode) testModule
290                 .getDataChildByName("testleaf");
291         ExtendedType leafType = (ExtendedType) testleaf.getType();
292         assertEquals("my-type1", leafType.getQName().getLocalName());
293         assertEquals("t2", leafType.getQName().getPrefix());
294         ExtendedType baseType = (ExtendedType) leafType.getBaseType();
295         assertEquals("my-base-int32-type", baseType.getQName().getLocalName());
296         assertEquals("t2", baseType.getQName().getPrefix());
297
298         List<RangeConstraint> ranges = leafType.getRanges();
299         assertEquals(1, ranges.size());
300         RangeConstraint range = ranges.get(0);
301         assertEquals(3L, range.getMin());
302         assertEquals(20L, range.getMax());
303     }
304
305     @Test
306     public void testTypedefPatternsResolving() {
307         Module testModule = TestUtils.findModule(modules, "types1");
308
309         LeafSchemaNode testleaf = (LeafSchemaNode) testModule
310                 .getDataChildByName("test-string-leaf");
311         ExtendedType testleafType = (ExtendedType) testleaf.getType();
312         QName testleafTypeQName = testleafType.getQName();
313         assertEquals("my-string-type-ext", testleafTypeQName.getLocalName());
314         assertEquals("t2", testleafTypeQName.getPrefix());
315
316         Set<String> expectedRegex = new HashSet<String>();
317         expectedRegex.add("[a-k]*");
318         expectedRegex.add("[b-u]*");
319         expectedRegex.add("[e-z]*");
320
321         Set<String> actualRegex = new HashSet<String>();
322         List<PatternConstraint> patterns = testleafType.getPatterns();
323         assertEquals(3, patterns.size());
324         for (PatternConstraint pc : patterns) {
325             actualRegex.add(pc.getRegularExpression());
326         }
327         assertEquals(expectedRegex, actualRegex);
328
329         TypeDefinition<?> baseType = testleafType.getBaseType();
330         assertEquals("my-string-type2", baseType.getQName().getLocalName());
331
332         List<LengthConstraint> lengths = testleafType.getLengths();
333         assertEquals(1, lengths.size());
334
335         LengthConstraint length = lengths.get(0);
336         assertEquals(5L, length.getMin());
337         assertEquals(10L, length.getMax());
338     }
339
340     @Test
341     public void testTypedefLengthsResolving() {
342         Module testModule = TestUtils.findModule(modules, "types1");
343
344         LeafSchemaNode testleaf = (LeafSchemaNode) testModule
345                 .getDataChildByName("leaf-with-length");
346         ExtendedType testleafType = (ExtendedType) testleaf.getType();
347         assertEquals("my-string-type", testleafType.getQName().getLocalName());
348
349         List<LengthConstraint> lengths = testleafType.getLengths();
350         assertEquals(1, lengths.size());
351
352         LengthConstraint length = lengths.get(0);
353         assertEquals(7L, length.getMin());
354         assertEquals(10L, length.getMax());
355     }
356
357     @Test
358     public void testTypeDef() {
359         Module testModule = TestUtils.findModule(modules, "types2");
360
361         LeafSchemaNode testleaf = (LeafSchemaNode) testModule
362                 .getDataChildByName("nested-type-leaf");
363         ExtendedType testleafType = (ExtendedType) testleaf.getType();
364         assertEquals("my-type1", testleafType.getQName().getLocalName());
365
366         ExtendedType baseType = (ExtendedType) testleafType.getBaseType();
367         assertEquals("my-base-int32-type", baseType.getQName().getLocalName());
368
369         Int32 int32base = (Int32) baseType.getBaseType();
370         List<RangeConstraint> ranges = int32base.getRangeStatements();
371         assertEquals(1, ranges.size());
372         RangeConstraint range = ranges.get(0);
373         assertEquals(2L, range.getMin());
374         assertEquals(20L, range.getMax());
375     }
376
377     @Test
378     public void testTypedefDecimal1() {
379         Module testModule = TestUtils.findModule(modules, "types1");
380
381         LeafSchemaNode testleaf = (LeafSchemaNode) testModule
382                 .getDataChildByName("test-decimal-leaf");
383         ExtendedType type = (ExtendedType) testleaf.getType();
384         assertEquals(4, (int) type.getFractionDigits());
385
386         Decimal64 baseType = (Decimal64) type.getBaseType();
387         assertEquals(6, (int) baseType.getFractionDigits());
388     }
389
390     @Test
391     public void testTypedefDecimal2() {
392         Module testModule = TestUtils.findModule(modules, "types1");
393
394         LeafSchemaNode testleaf = (LeafSchemaNode) testModule
395                 .getDataChildByName("test-decimal-leaf2");
396         TypeDefinition<?> baseType = testleaf.getType().getBaseType();
397         assertTrue(testleaf.getType().getBaseType() instanceof Decimal64);
398         Decimal64 baseTypeCast = (Decimal64) baseType;
399         assertEquals(5, (int) baseTypeCast.getFractionDigits());
400     }
401
402     @Test
403     public void testTypedefUnion() {
404         Module testModule = TestUtils.findModule(modules, "types1");
405
406         LeafSchemaNode testleaf = (LeafSchemaNode) testModule
407                 .getDataChildByName("union-leaf");
408         ExtendedType testleafType = (ExtendedType) testleaf.getType();
409         assertEquals("my-union-ext", testleafType.getQName().getLocalName());
410
411         ExtendedType baseType = (ExtendedType) testleafType.getBaseType();
412         assertEquals("my-union", baseType.getQName().getLocalName());
413
414         UnionType unionBase = (UnionType) baseType.getBaseType();
415
416         List<TypeDefinition<?>> unionTypes = unionBase.getTypes();
417         Int16 unionType1 = (Int16) unionTypes.get(0);
418         List<RangeConstraint> ranges = unionType1.getRangeStatements();
419         assertEquals(1, ranges.size());
420         RangeConstraint range = ranges.get(0);
421         assertEquals(1L, range.getMin());
422         assertEquals(100L, range.getMax());
423
424         assertTrue(unionTypes.get(0) instanceof Int16);
425         assertTrue(unionTypes.get(1) instanceof Int32);
426     }
427
428     @Test
429     public void testNestedUnionResolving1() {
430         Module testModule = TestUtils.findModule(modules, "types1");
431
432         LeafSchemaNode testleaf = (LeafSchemaNode) testModule
433                 .getDataChildByName("nested-union-leaf");
434
435         ExtendedType nestedUnion1 = (ExtendedType) testleaf.getType();
436         assertEquals("nested-union1", nestedUnion1.getQName().getLocalName());
437
438         ExtendedType nestedUnion2 = (ExtendedType) nestedUnion1.getBaseType();
439         assertEquals("nested-union2", nestedUnion2.getQName().getLocalName());
440
441         UnionType unionType1 = (UnionType) nestedUnion2.getBaseType();
442         List<TypeDefinition<?>> unionTypes = unionType1.getTypes();
443         assertEquals(2, unionTypes.size());
444         assertTrue(unionTypes.get(0) instanceof StringType);
445         assertTrue(unionTypes.get(1) instanceof ExtendedType);
446
447         ExtendedType extendedUnion = (ExtendedType) unionTypes.get(1);
448         ExtendedType extendedUnionBase = (ExtendedType) extendedUnion
449                 .getBaseType();
450         assertEquals("my-union", extendedUnionBase.getQName().getLocalName());
451
452         UnionType extendedTargetUnion = (UnionType) extendedUnionBase
453                 .getBaseType();
454         List<TypeDefinition<?>> extendedTargetTypes = extendedTargetUnion
455                 .getTypes();
456         assertTrue(extendedTargetTypes.get(0) instanceof Int16);
457         assertTrue(extendedTargetTypes.get(1) instanceof Int32);
458
459         Int16 int16 = (Int16) extendedTargetTypes.get(0);
460         List<RangeConstraint> ranges = int16.getRangeStatements();
461         assertEquals(1, ranges.size());
462         RangeConstraint range = ranges.get(0);
463         assertEquals(1L, range.getMin());
464         assertEquals(100L, range.getMax());
465     }
466
467     @Test
468     public void testNestedUnionResolving2() {
469         Module testModule = TestUtils.findModule(modules, "types1");
470
471         LeafSchemaNode testleaf = (LeafSchemaNode) testModule
472                 .getDataChildByName("custom-union-leaf");
473
474         ExtendedType testleafType = (ExtendedType) testleaf.getType();
475         QName testleafTypeQName = testleafType.getQName();
476         assertEquals(URI.create("urn:simple.container.demo.test"),
477                 testleafTypeQName.getNamespace());
478         assertEquals(TestUtils.createDate("2013-02-27"),
479                 testleafTypeQName.getRevision());
480         assertEquals("t3", testleafTypeQName.getPrefix());
481         assertEquals("union1", testleafTypeQName.getLocalName());
482
483         ExtendedType union2 = (ExtendedType) testleafType.getBaseType();
484         QName union2QName = union2.getQName();
485         assertEquals(URI.create("urn:simple.container.demo.test"),
486                 union2QName.getNamespace());
487         assertEquals(TestUtils.createDate("2013-02-27"),
488                 union2QName.getRevision());
489         assertEquals("t3", union2QName.getPrefix());
490         assertEquals("union2", union2QName.getLocalName());
491
492         UnionType union2Base = (UnionType) union2.getBaseType();
493         List<TypeDefinition<?>> unionTypes = union2Base.getTypes();
494         assertEquals(2, unionTypes.size());
495         assertTrue(unionTypes.get(0) instanceof Int32);
496         assertTrue(unionTypes.get(1) instanceof ExtendedType);
497
498         ExtendedType nestedUnion2 = (ExtendedType) unionTypes.get(1);
499         QName nestedUnion2QName = nestedUnion2.getQName();
500         assertEquals(URI.create("urn:simple.types.data.demo"),
501                 nestedUnion2QName.getNamespace());
502         assertEquals(TestUtils.createDate("2013-02-27"),
503                 nestedUnion2QName.getRevision());
504         assertEquals("t2", nestedUnion2QName.getPrefix());
505         assertEquals("nested-union2", nestedUnion2QName.getLocalName());
506
507         UnionType nestedUnion2Base = (UnionType) nestedUnion2.getBaseType();
508         List<TypeDefinition<?>> nestedUnion2Types = nestedUnion2Base.getTypes();
509         assertEquals(2, nestedUnion2Types.size());
510         assertTrue(nestedUnion2Types.get(0) instanceof StringType);
511         assertTrue(nestedUnion2Types.get(1) instanceof ExtendedType);
512
513         ExtendedType myUnionExt = (ExtendedType) nestedUnion2Types.get(1);
514         QName myUnionExtQName = myUnionExt.getQName();
515         assertEquals(URI.create("urn:simple.types.data.demo"),
516                 myUnionExtQName.getNamespace());
517         assertEquals(TestUtils.createDate("2013-02-27"),
518                 myUnionExtQName.getRevision());
519         assertEquals("t2", myUnionExtQName.getPrefix());
520         assertEquals("my-union-ext", myUnionExtQName.getLocalName());
521
522         ExtendedType myUnion = (ExtendedType) myUnionExt.getBaseType();
523         QName myUnionQName = myUnion.getQName();
524         assertEquals(URI.create("urn:simple.types.data.demo"),
525                 myUnionQName.getNamespace());
526         assertEquals(TestUtils.createDate("2013-02-27"),
527                 myUnionQName.getRevision());
528         assertEquals("t2", myUnionQName.getPrefix());
529         assertEquals("my-union", myUnionQName.getLocalName());
530
531         UnionType myUnionBase = (UnionType) myUnion.getBaseType();
532         List<TypeDefinition<?>> myUnionBaseTypes = myUnionBase.getTypes();
533         assertEquals(2, myUnionBaseTypes.size());
534         assertTrue(myUnionBaseTypes.get(0) instanceof Int16);
535         assertTrue(myUnionBaseTypes.get(1) instanceof Int32);
536         Int16 int16 = (Int16) myUnionBaseTypes.get(0);
537         List<RangeConstraint> ranges = int16.getRangeStatements();
538         assertEquals(1, ranges.size());
539         RangeConstraint range = ranges.get(0);
540         assertEquals(1L, range.getMin());
541         assertEquals(100L, range.getMax());
542     }
543
544     @Test
545     public void testRefine() {
546         Module testModule = TestUtils.findModule(modules, "types2");
547
548         ContainerSchemaNode peer = (ContainerSchemaNode) testModule
549                 .getDataChildByName("peer");
550         ContainerSchemaNode destination = (ContainerSchemaNode) peer
551                 .getDataChildByName("destination");
552         Set<UsesNode> usesNodes = destination.getUses();
553         assertEquals(1, usesNodes.size());
554         UsesNode usesNode = usesNodes.iterator().next();
555         Map<SchemaPath, SchemaNode> refines = usesNode.getRefines();
556         assertEquals(5, refines.size());
557
558         LeafSchemaNode refineLeaf = null;
559         ContainerSchemaNode refineContainer = null;
560         ListSchemaNode refineList = null;
561         GroupingDefinition refineGrouping = null;
562         TypeDefinition<?> typedef = null;
563         for (Map.Entry<SchemaPath, SchemaNode> entry : refines.entrySet()) {
564             SchemaNode value = entry.getValue();
565             if (value instanceof LeafSchemaNode) {
566                 refineLeaf = (LeafSchemaNode) value;
567             } else if(value instanceof ContainerSchemaNode) {
568                 refineContainer = (ContainerSchemaNode) value;
569             } else if(value instanceof ListSchemaNode) {
570                 refineList = (ListSchemaNode)value;
571             } else if(value instanceof GroupingDefinition) {
572                 refineGrouping = (GroupingDefinition)value;
573             } else if(value instanceof TypeDefinition<?>) {
574                 typedef = (TypeDefinition<?>)value;
575             }
576         }
577
578         // leaf address
579         assertNotNull(refineLeaf);
580         assertEquals("address", refineLeaf.getQName().getLocalName());
581         assertEquals("description of address defined by refine",
582                 refineLeaf.getDescription());
583         assertEquals("address reference added by refine",
584                 refineLeaf.getReference());
585         assertFalse(refineLeaf.isConfiguration());
586         assertTrue(refineLeaf.getConstraints().isMandatory());
587         Set<MustDefinition> leafMustConstraints = refineLeaf.getConstraints()
588                 .getMustConstraints();
589         assertEquals(1, leafMustConstraints.size());
590         MustDefinition leafMust = leafMustConstraints.iterator().next();
591         assertEquals(
592                 "\"ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)\"",
593                 leafMust.toString());
594
595         // container port
596         assertNotNull(refineContainer);
597         Set<MustDefinition> mustConstraints = refineContainer.getConstraints()
598                 .getMustConstraints();
599         assertEquals(1, mustConstraints.size());
600         MustDefinition must = mustConstraints.iterator().next();
601         assertEquals("must-condition", must.toString());
602         assertEquals("An error message test", must.getErrorMessage());
603         assertEquals(("An error app tag test"), must.getErrorAppTag());
604         assertEquals("description of port defined by refine",
605                 refineContainer.getDescription());
606         assertEquals("port reference added by refine",
607                 refineContainer.getReference());
608         assertFalse(refineContainer.isConfiguration());
609         assertTrue(refineContainer.isPresenceContainer());
610
611         // list addresses
612         assertNotNull(refineList);
613         assertEquals("description of addresses defined by refine", refineList.getDescription());
614         assertEquals("addresses reference added by refine", refineList.getReference());
615         assertFalse(refineList.isConfiguration());
616         assertEquals(2, (int)refineList.getConstraints().getMinElements());
617         assertEquals(12, (int)refineList.getConstraints().getMaxElements());
618
619         // grouping target-inner
620         assertNotNull(refineGrouping);
621         Set<DataSchemaNode> refineGroupingChildren = refineGrouping.getChildNodes();
622         assertEquals(1, refineGroupingChildren.size());
623         LeafSchemaNode refineGroupingLeaf = (LeafSchemaNode)refineGroupingChildren.iterator().next();
624         assertEquals("inner-grouping-id", refineGroupingLeaf.getQName().getLocalName());
625         assertEquals("new target-inner grouping description", refineGrouping.getDescription());
626
627         // typedef group-type
628         assertNotNull(typedef);
629         assertEquals("new group-type description", typedef.getDescription());
630         assertEquals("new group-type reference", typedef.getReference());
631         assertTrue(typedef.getBaseType() instanceof ExtendedType);
632     }
633
634     @Test
635     public void testChoice() {
636         Module testModule = TestUtils.findModule(modules, "types1");
637         ContainerSchemaNode peer = (ContainerSchemaNode) testModule
638                 .getDataChildByName("transfer");
639         ChoiceNode how = (ChoiceNode) peer.getDataChildByName("how");
640         Set<ChoiceCaseNode> cases = how.getCases();
641         assertEquals(3, cases.size());
642     }
643
644     @Test
645     public void testAnyXml() {
646         Module testModule = TestUtils.findModule(modules, "types1");
647         AnyXmlSchemaNode data = (AnyXmlSchemaNode) testModule
648                 .getDataChildByName("data");
649         assertNotNull(data);
650     }
651
652     @Test
653     public void testDeviation() {
654         Module testModule = TestUtils.findModule(modules, "types1");
655         Set<Deviation> deviations = testModule.getDeviations();
656         assertEquals(1, deviations.size());
657
658         Deviation dev = deviations.iterator().next();
659         SchemaPath expectedPath = TestUtils.createPath(true, null, null,
660                 "data", "system", "user");
661         assertEquals(expectedPath, dev.getTargetPath());
662         assertEquals(Deviate.ADD, dev.getDeviate());
663     }
664
665     @Test
666     public void testUnknownNode() {
667         Module testModule = TestUtils.findModule(modules, "types3");
668         ContainerSchemaNode network = (ContainerSchemaNode) testModule
669                 .getDataChildByName("network");
670         List<UnknownSchemaNode> unknownNodes = network.getUnknownSchemaNodes();
671         assertEquals(1, unknownNodes.size());
672         UnknownSchemaNode unknownNode = unknownNodes.get(0);
673         assertNotNull(unknownNode.getNodeType());
674         assertEquals("point", unknownNode.getNodeParameter());
675     }
676
677     @Test
678     public void testFeature() {
679         Module testModule = TestUtils.findModule(modules, "types3");
680         Set<FeatureDefinition> features = testModule.getFeatures();
681         assertEquals(1, features.size());
682     }
683
684     @Test
685     public void testExtension() {
686         Module testModule = TestUtils.findModule(modules, "types3");
687         List<ExtensionDefinition> extensions = testModule
688                 .getExtensionSchemaNodes();
689         assertEquals(1, extensions.size());
690         ExtensionDefinition extension = extensions.get(0);
691         assertEquals("name", extension.getArgument());
692         assertFalse(extension.isYinElement());
693     }
694
695     @Test
696     public void testNotification() {
697         Module testModule = TestUtils.findModule(modules, "types3");
698         URI expectedNamespace = URI.create("urn:simple.container.demo.test");
699         String expectedPrefix = "t3";
700         Date expectedRevision = TestUtils.createDate("2013-02-27");
701
702         Set<NotificationDefinition> notifications = testModule
703                 .getNotifications();
704         assertEquals(1, notifications.size());
705
706         NotificationDefinition notification = notifications.iterator().next();
707         // test SchemaNode args
708         QName expectedQName = new QName(expectedNamespace, expectedRevision,
709                 expectedPrefix, "event");
710         assertEquals(expectedQName, notification.getQName());
711         SchemaPath expectedPath = TestUtils.createPath(true, expectedNamespace,
712                 expectedRevision, expectedPrefix, "event");
713         assertEquals(expectedPath, notification.getPath());
714         assertNull(notification.getDescription());
715         assertNull(notification.getReference());
716         assertEquals(Status.CURRENT, notification.getStatus());
717         assertEquals(0, notification.getUnknownSchemaNodes().size());
718         // test DataNodeContainer args
719         assertEquals(0, notification.getTypeDefinitions().size());
720         assertEquals(3, notification.getChildNodes().size());
721         assertEquals(0, notification.getGroupings().size());
722         assertEquals(0, notification.getUses().size());
723
724         LeafSchemaNode eventClass = (LeafSchemaNode) notification
725                 .getDataChildByName("event-class");
726         assertTrue(eventClass.getType() instanceof StringType);
727         AnyXmlSchemaNode reportingEntity = (AnyXmlSchemaNode) notification
728                 .getDataChildByName("reporting-entity");
729         assertNotNull(reportingEntity);
730         LeafSchemaNode severity = (LeafSchemaNode) notification
731                 .getDataChildByName("severity");
732         assertTrue(severity.getType() instanceof StringType);
733     }
734
735     @Test
736     public void testRpc() {
737         Module testModule = TestUtils.findModule(modules, "types3");
738
739         Set<RpcDefinition> rpcs = testModule.getRpcs();
740         assertEquals(1, rpcs.size());
741
742         RpcDefinition rpc = rpcs.iterator().next();
743         assertEquals("Retrieve all or part of a specified configuration.",
744                 rpc.getDescription());
745         assertEquals("RFC 6241, Section 7.1", rpc.getReference());
746
747         ContainerSchemaNode input = rpc.getInput();
748         assertNotNull(input.getDataChildByName("source"));
749         assertNotNull(input.getDataChildByName("filter"));
750         ContainerSchemaNode output = rpc.getOutput();
751         assertNotNull(output.getDataChildByName("data"));
752     }
753
754     @Test
755     public void testGrouping() {
756         Module testModule = TestUtils.findModule(modules, "types2");
757         Set<GroupingDefinition> groupings = testModule.getGroupings();
758         assertEquals(1, groupings.size());
759         GroupingDefinition grouping = groupings.iterator().next();
760         Set<DataSchemaNode> children = grouping.getChildNodes();
761         assertEquals(5, children.size());
762     }
763
764 }