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