Added more tests for yang parser. Updated current tests.
[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.io.FileNotFoundException;
13 import java.net.URI;
14 import java.text.DateFormat;
15 import java.text.ParseException;
16 import java.text.SimpleDateFormat;
17 import java.util.ArrayList;
18 import java.util.Date;
19 import java.util.List;
20 import java.util.Set;
21
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.controller.yang.common.QName;
25 import org.opendaylight.controller.yang.model.api.AnyXmlSchemaNode;
26 import org.opendaylight.controller.yang.model.api.AugmentationSchema;
27 import org.opendaylight.controller.yang.model.api.ChoiceCaseNode;
28 import org.opendaylight.controller.yang.model.api.ChoiceNode;
29 import org.opendaylight.controller.yang.model.api.ConstraintDefinition;
30 import org.opendaylight.controller.yang.model.api.ContainerSchemaNode;
31 import org.opendaylight.controller.yang.model.api.DataSchemaNode;
32 import org.opendaylight.controller.yang.model.api.Deviation;
33 import org.opendaylight.controller.yang.model.api.Deviation.Deviate;
34 import org.opendaylight.controller.yang.model.api.ExtensionDefinition;
35 import org.opendaylight.controller.yang.model.api.FeatureDefinition;
36 import org.opendaylight.controller.yang.model.api.GroupingDefinition;
37 import org.opendaylight.controller.yang.model.api.LeafSchemaNode;
38 import org.opendaylight.controller.yang.model.api.ListSchemaNode;
39 import org.opendaylight.controller.yang.model.api.Module;
40 import org.opendaylight.controller.yang.model.api.ModuleImport;
41 import org.opendaylight.controller.yang.model.api.NotificationDefinition;
42 import org.opendaylight.controller.yang.model.api.RpcDefinition;
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.type.LengthConstraint;
48 import org.opendaylight.controller.yang.model.api.type.PatternConstraint;
49 import org.opendaylight.controller.yang.model.api.type.RangeConstraint;
50 import org.opendaylight.controller.yang.model.util.Decimal64;
51 import org.opendaylight.controller.yang.model.util.ExtendedType;
52 import org.opendaylight.controller.yang.model.util.Int16;
53 import org.opendaylight.controller.yang.model.util.Int32;
54 import org.opendaylight.controller.yang.model.util.StringType;
55 import org.opendaylight.controller.yang.model.util.Uint32;
56 import org.opendaylight.controller.yang.model.util.UnionType;
57
58 public class YangParserTest {
59
60     private final URI nodesNS = URI.create("urn:simple.nodes.test");
61     private final URI typesNS = URI.create("urn:simple.types.test");
62     private final URI customNS = URI.create("urn:custom.nodes.test");
63     private Date nodesRev;
64     private Date typesRev;
65     private Date customRev;
66
67     private final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
68     private Set<Module> modules;
69
70     @Before
71     public void init() throws FileNotFoundException, ParseException {
72         nodesRev = simpleDateFormat.parse("2013-02-27");
73         typesRev = simpleDateFormat.parse("2013-07-03");
74         customRev = simpleDateFormat.parse("2013-02-27");
75
76         modules = TestUtils.loadModules(getClass().getResource("/model").getPath());
77         assertEquals(3, modules.size());
78     }
79
80     @Test
81     public void testHeaders() throws ParseException {
82         Module test = TestUtils.findModule(modules, "nodes");
83
84         assertEquals("nodes", test.getName());
85         assertEquals("1", test.getYangVersion());
86         assertEquals(nodesNS, test.getNamespace());
87         assertEquals("n", test.getPrefix());
88
89         Set<ModuleImport> imports = test.getImports();
90         assertEquals(2, imports.size());
91
92         ModuleImport import2 = TestUtils.findImport(imports, "t");
93         assertEquals("types", import2.getModuleName());
94         assertEquals(typesRev, import2.getRevision());
95
96         ModuleImport import3 = TestUtils.findImport(imports, "c");
97         assertEquals("custom", import3.getModuleName());
98         assertEquals(customRev, import3.getRevision());
99
100         assertEquals("opendaylight", test.getOrganization());
101         assertEquals("http://www.opendaylight.org/", test.getContact());
102         Date expectedRevision = TestUtils.createDate("2013-02-27");
103         assertEquals(expectedRevision, test.getRevision());
104         assertEquals(" WILL BE DEFINED LATER", test.getReference());
105     }
106
107     @Test
108     public void testOrderingTypedef() {
109         Module test = TestUtils.findModule(modules, "types");
110         Set<TypeDefinition<?>> typedefs = test.getTypeDefinitions();
111         String[] expectedOrder = new String[] { "int32-ext1", "int32-ext2", "my-decimal-type", "my-union",
112                 "my-union-ext", "nested-union2", "string-ext1", "string-ext2", "string-ext3", "string-ext4" };
113         String[] actualOrder = new String[typedefs.size()];
114
115         int i = 0;
116         for (TypeDefinition<?> type : typedefs) {
117             actualOrder[i] = type.getQName().getLocalName();
118             i++;
119         }
120         assertArrayEquals(expectedOrder, actualOrder);
121     }
122
123     @Test
124     public void testOrderingChildNodes() {
125         Module test = TestUtils.findModule(modules, "nodes");
126         AugmentationSchema augment1 = null;
127         for (AugmentationSchema as : test.getAugmentations()) {
128             if ("if:ifType='ds0'".equals(as.getWhenCondition().toString())) {
129                 augment1 = as;
130                 break;
131             }
132         }
133         assertNotNull(augment1);
134
135         String[] expectedOrder = new String[] { "ds0ChannelNumber", "interface-id", "my-type", "odl", "schemas" };
136         String[] actualOrder = new String[expectedOrder.length];
137
138         int i = 0;
139         for (DataSchemaNode augmentChild : augment1.getChildNodes()) {
140             actualOrder[i] = augmentChild.getQName().getLocalName();
141             i++;
142         }
143
144         assertArrayEquals(expectedOrder, actualOrder);
145     }
146
147     @Test
148     public void testOrderingNestedChildNodes() {
149         Module test = TestUtils.findModule(modules, "custom");
150         Set<GroupingDefinition> groupings = test.getGroupings();
151         assertEquals(1, groupings.size());
152         GroupingDefinition target = groupings.iterator().next();
153
154         Set<DataSchemaNode> childNodes = target.getChildNodes();
155         String[] expectedOrder = new String[] { "address", "addresses", "data", "how", "port" };
156         String[] actualOrder = new String[childNodes.size()];
157
158         int i = 0;
159         for (DataSchemaNode child : childNodes) {
160             actualOrder[i] = child.getQName().getLocalName();
161             i++;
162         }
163         assertArrayEquals(expectedOrder, actualOrder);
164     }
165
166     @Test
167     public void testParseContainer() {
168         Module test = TestUtils.findModule(modules, "types");
169         URI expectedNamespace = URI.create("urn:simple.types.test");
170         String expectedPrefix = "t";
171
172         ContainerSchemaNode interfaces = (ContainerSchemaNode) test.getDataChildByName("interfaces");
173         // test SchemaNode args
174         QName expectedQName = new QName(expectedNamespace, typesRev, expectedPrefix, "interfaces");
175         assertEquals(expectedQName, interfaces.getQName());
176         SchemaPath expectedPath = TestUtils.createPath(true, expectedNamespace, typesRev, expectedPrefix, "interfaces");
177         assertEquals(expectedPath, interfaces.getPath());
178         assertNull(interfaces.getDescription());
179         assertNull(interfaces.getReference());
180         assertEquals(Status.CURRENT, interfaces.getStatus());
181         assertEquals(0, interfaces.getUnknownSchemaNodes().size());
182         // test DataSchemaNode args
183         assertFalse(interfaces.isAugmenting());
184         assertTrue(interfaces.isConfiguration());
185         ConstraintDefinition constraints = interfaces.getConstraints();
186         assertNull(constraints.getWhenCondition());
187         assertEquals(0, constraints.getMustConstraints().size());
188         assertFalse(constraints.isMandatory());
189         assertNull(constraints.getMinElements());
190         assertNull(constraints.getMaxElements());
191         // test AugmentationTarget args
192         assertEquals(0, interfaces.getAvailableAugmentations().size());
193         // test ContainerSchemaNode args
194         assertFalse(interfaces.isPresenceContainer());
195         // test DataNodeContainer args
196         assertEquals(0, interfaces.getTypeDefinitions().size());
197         assertEquals(1, interfaces.getChildNodes().size());
198         assertEquals(0, interfaces.getGroupings().size());
199         assertEquals(0, interfaces.getUses().size());
200
201         ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
202         assertNotNull(ifEntry);
203     }
204
205     @Test
206     public void testParseList() {
207         Module test = TestUtils.findModule(modules, "types");
208         URI expectedNamespace = URI.create("urn:simple.types.test");
209         String expectedPrefix = "t";
210
211         ContainerSchemaNode interfaces = (ContainerSchemaNode) test.getDataChildByName("interfaces");
212
213         ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
214         // test SchemaNode args
215         QName expectedQName = new QName(expectedNamespace, typesRev, expectedPrefix, "ifEntry");
216         assertEquals(expectedQName, ifEntry.getQName());
217         SchemaPath expectedPath = TestUtils.createPath(true, expectedNamespace, typesRev, expectedPrefix, "interfaces",
218                 "ifEntry");
219         assertEquals(expectedPath, ifEntry.getPath());
220         assertNull(ifEntry.getDescription());
221         assertNull(ifEntry.getReference());
222         assertEquals(Status.CURRENT, ifEntry.getStatus());
223         assertEquals(0, ifEntry.getUnknownSchemaNodes().size());
224         // test DataSchemaNode args
225         assertFalse(ifEntry.isAugmenting());
226         assertTrue(ifEntry.isConfiguration());
227         ConstraintDefinition constraints = ifEntry.getConstraints();
228         assertNull(constraints.getWhenCondition());
229         assertEquals(0, constraints.getMustConstraints().size());
230         assertFalse(constraints.isMandatory());
231         assertEquals(1, (int) constraints.getMinElements());
232         assertEquals(11, (int) constraints.getMaxElements());
233         // test AugmentationTarget args
234         Set<AugmentationSchema> availableAugmentations = ifEntry.getAvailableAugmentations();
235         assertEquals(2, availableAugmentations.size());
236         // test ListSchemaNode args
237         List<QName> expectedKey = new ArrayList<QName>();
238         expectedKey.add(new QName(expectedNamespace, typesRev, expectedPrefix, "ifIndex"));
239         assertEquals(expectedKey, ifEntry.getKeyDefinition());
240         assertFalse(ifEntry.isUserOrdered());
241         // test DataNodeContainer args
242         assertEquals(0, ifEntry.getTypeDefinitions().size());
243         assertEquals(4, ifEntry.getChildNodes().size());
244         assertEquals(0, ifEntry.getGroupings().size());
245         assertEquals(0, ifEntry.getUses().size());
246
247         LeafSchemaNode ifIndex = (LeafSchemaNode) ifEntry.getDataChildByName("ifIndex");
248         assertTrue(ifIndex.getType() instanceof Uint32);
249         LeafSchemaNode ifMtu = (LeafSchemaNode) ifEntry.getDataChildByName("ifMtu");
250         assertTrue(ifMtu.getType() instanceof Int32);
251     }
252
253     @Test
254     public void testTypedefRangesResolving() throws ParseException {
255         Module testModule = TestUtils.findModule(modules, "nodes");
256         LeafSchemaNode int32Leaf = (LeafSchemaNode) testModule.getDataChildByName("int32-leaf");
257
258         ExtendedType leafType = (ExtendedType) int32Leaf.getType();
259         QName leafTypeQName = leafType.getQName();
260         assertEquals("int32-ext2", leafTypeQName.getLocalName());
261         assertEquals("n", leafTypeQName.getPrefix());
262         assertEquals(nodesNS, leafTypeQName.getNamespace());
263         assertEquals(nodesRev, leafTypeQName.getRevision());
264         assertNull(leafType.getUnits());
265         assertNull(leafType.getDefaultValue());
266         assertTrue(leafType.getLengths().isEmpty());
267         assertTrue(leafType.getPatterns().isEmpty());
268         List<RangeConstraint> ranges = leafType.getRanges();
269         assertEquals(1, ranges.size());
270         RangeConstraint range = ranges.get(0);
271         assertEquals(12L, range.getMin());
272         assertEquals(20L, range.getMax());
273
274         ExtendedType baseType = (ExtendedType) leafType.getBaseType();
275         QName baseTypeQName = baseType.getQName();
276         assertEquals("int32-ext2", baseTypeQName.getLocalName());
277         assertEquals("t", baseTypeQName.getPrefix());
278         assertEquals(typesNS, baseTypeQName.getNamespace());
279         assertEquals(typesRev, baseTypeQName.getRevision());
280         assertEquals("mile", baseType.getUnits());
281         assertEquals("11", baseType.getDefaultValue());
282         assertTrue(leafType.getLengths().isEmpty());
283         assertTrue(leafType.getPatterns().isEmpty());
284         List<RangeConstraint> baseTypeRanges = baseType.getRanges();
285         assertEquals(2, baseTypeRanges.size());
286         RangeConstraint baseTypeRange1 = baseTypeRanges.get(0);
287         assertEquals(3L, baseTypeRange1.getMin());
288         assertEquals(9L, baseTypeRange1.getMax());
289         RangeConstraint baseTypeRange2 = baseTypeRanges.get(1);
290         assertEquals(11L, baseTypeRange2.getMin());
291         assertEquals(20L, baseTypeRange2.getMax());
292
293         ExtendedType base = (ExtendedType) baseType.getBaseType();
294         QName baseQName = base.getQName();
295         assertEquals("int32-ext1", baseQName.getLocalName());
296         assertEquals("t", baseQName.getPrefix());
297         assertEquals(typesNS, baseQName.getNamespace());
298         assertEquals(typesRev, baseQName.getRevision());
299         assertNull(base.getUnits());
300         assertNull(base.getDefaultValue());
301         assertTrue(leafType.getLengths().isEmpty());
302         assertTrue(leafType.getPatterns().isEmpty());
303         List<RangeConstraint> baseRanges = base.getRanges();
304         assertEquals(1, baseRanges.size());
305         RangeConstraint baseRange = baseRanges.get(0);
306         assertEquals(2L, baseRange.getMin());
307         assertEquals(20L, baseRange.getMax());
308
309         assertTrue(base.getBaseType() instanceof Int32);
310     }
311
312     @Test
313     public void testTypedefPatternsResolving() {
314         Module testModule = TestUtils.findModule(modules, "nodes");
315         LeafSchemaNode stringleaf = (LeafSchemaNode) testModule.getDataChildByName("string-leaf");
316
317         ExtendedType type = (ExtendedType) stringleaf.getType();
318         QName typeQName = type.getQName();
319         assertEquals("string-ext4", typeQName.getLocalName());
320         assertEquals("t", typeQName.getPrefix());
321         assertEquals(typesNS, typeQName.getNamespace());
322         assertEquals(typesRev, typeQName.getRevision());
323         assertNull(type.getUnits());
324         assertNull(type.getDefaultValue());
325         List<PatternConstraint> patterns = type.getPatterns();
326         assertEquals(1, patterns.size());
327         PatternConstraint pattern = patterns.iterator().next();
328         assertEquals("[e-z]*", pattern.getRegularExpression());
329         assertTrue(type.getLengths().isEmpty());
330         assertTrue(type.getRanges().isEmpty());
331
332         ExtendedType baseType1 = (ExtendedType) type.getBaseType();
333         QName baseType1QName = baseType1.getQName();
334         assertEquals("string-ext3", baseType1QName.getLocalName());
335         assertEquals("t", baseType1QName.getPrefix());
336         assertEquals(typesNS, baseType1QName.getNamespace());
337         assertEquals(typesRev, baseType1QName.getRevision());
338         assertNull(baseType1.getUnits());
339         assertNull(baseType1.getDefaultValue());
340         patterns = baseType1.getPatterns();
341         assertEquals(1, patterns.size());
342         pattern = patterns.iterator().next();
343         assertEquals("[b-u]*", pattern.getRegularExpression());
344         assertTrue(baseType1.getLengths().isEmpty());
345         assertTrue(baseType1.getRanges().isEmpty());
346
347         ExtendedType baseType2 = (ExtendedType) baseType1.getBaseType();
348         QName baseType2QName = baseType2.getQName();
349         assertEquals("string-ext2", baseType2QName.getLocalName());
350         assertEquals("t", baseType2QName.getPrefix());
351         assertEquals(typesNS, baseType2QName.getNamespace());
352         assertEquals(typesRev, baseType2QName.getRevision());
353         assertNull(baseType2.getUnits());
354         assertNull(baseType2.getDefaultValue());
355         assertTrue(baseType2.getPatterns().isEmpty());
356         List<LengthConstraint> baseType2Lengths = baseType2.getLengths();
357         assertEquals(1, baseType2Lengths.size());
358         LengthConstraint length = baseType2Lengths.get(0);
359         assertEquals(6L, length.getMin());
360         assertEquals(10L, length.getMax());
361         assertTrue(baseType2.getRanges().isEmpty());
362
363         ExtendedType baseType3 = (ExtendedType) baseType2.getBaseType();
364         QName baseType3QName = baseType3.getQName();
365         assertEquals("string-ext1", baseType3QName.getLocalName());
366         assertEquals("t", baseType3QName.getPrefix());
367         assertEquals(typesNS, baseType3QName.getNamespace());
368         assertEquals(typesRev, baseType3QName.getRevision());
369         assertNull(baseType3.getUnits());
370         assertNull(baseType3.getDefaultValue());
371         patterns = baseType3.getPatterns();
372         assertEquals(1, patterns.size());
373         pattern = patterns.iterator().next();
374         assertEquals("[a-k]*", pattern.getRegularExpression());
375         List<LengthConstraint> baseType3Lengths = baseType3.getLengths();
376         assertEquals(1, baseType3Lengths.size());
377         length = baseType3Lengths.get(0);
378         assertEquals(5L, length.getMin());
379         assertEquals(11L, length.getMax());
380         assertTrue(baseType3.getRanges().isEmpty());
381
382         assertTrue(baseType3.getBaseType() instanceof StringType);
383     }
384
385     @Test
386     public void testTypedefLengthsResolving() {
387         Module testModule = TestUtils.findModule(modules, "nodes");
388
389         LeafSchemaNode lengthLeaf = (LeafSchemaNode) testModule.getDataChildByName("length-leaf");
390         ExtendedType type = (ExtendedType) lengthLeaf.getType();
391
392         QName typeQName = type.getQName();
393         assertEquals("string-ext2", typeQName.getLocalName());
394         assertEquals("n", typeQName.getPrefix());
395         assertEquals(nodesNS, typeQName.getNamespace());
396         assertEquals(nodesRev, typeQName.getRevision());
397         assertNull(type.getUnits());
398         assertNull(type.getDefaultValue());
399         assertTrue(type.getPatterns().isEmpty());
400         List<LengthConstraint> typeLengths = type.getLengths();
401         assertEquals(1, typeLengths.size());
402         LengthConstraint length = typeLengths.get(0);
403         assertEquals(7L, length.getMin());
404         assertEquals(10L, length.getMax());
405         assertTrue(type.getRanges().isEmpty());
406
407         ExtendedType baseType1 = (ExtendedType) type.getBaseType();
408         QName baseType1QName = baseType1.getQName();
409         assertEquals("string-ext2", baseType1QName.getLocalName());
410         assertEquals("t", baseType1QName.getPrefix());
411         assertEquals(typesNS, baseType1QName.getNamespace());
412         assertEquals(typesRev, baseType1QName.getRevision());
413         assertNull(baseType1.getUnits());
414         assertNull(baseType1.getDefaultValue());
415         assertTrue(baseType1.getPatterns().isEmpty());
416         List<LengthConstraint> baseType2Lengths = baseType1.getLengths();
417         assertEquals(1, baseType2Lengths.size());
418         length = baseType2Lengths.get(0);
419         assertEquals(6L, length.getMin());
420         assertEquals(10L, length.getMax());
421         assertTrue(baseType1.getRanges().isEmpty());
422
423         ExtendedType baseType2 = (ExtendedType) baseType1.getBaseType();
424         QName baseType2QName = baseType2.getQName();
425         assertEquals("string-ext1", baseType2QName.getLocalName());
426         assertEquals("t", baseType2QName.getPrefix());
427         assertEquals(typesNS, baseType2QName.getNamespace());
428         assertEquals(typesRev, baseType2QName.getRevision());
429         assertNull(baseType2.getUnits());
430         assertNull(baseType2.getDefaultValue());
431         List<PatternConstraint> patterns = baseType2.getPatterns();
432         assertEquals(1, patterns.size());
433         PatternConstraint pattern = patterns.iterator().next();
434         assertEquals("[a-k]*", pattern.getRegularExpression());
435         List<LengthConstraint> baseType3Lengths = baseType2.getLengths();
436         assertEquals(1, baseType3Lengths.size());
437         length = baseType3Lengths.get(0);
438         assertEquals(5L, length.getMin());
439         assertEquals(11L, length.getMax());
440         assertTrue(baseType2.getRanges().isEmpty());
441
442         assertTrue(baseType2.getBaseType() instanceof StringType);
443     }
444
445     @Test
446     public void testTypedefDecimal1() {
447         Module testModule = TestUtils.findModule(modules, "nodes");
448         LeafSchemaNode testleaf = (LeafSchemaNode) testModule.getDataChildByName("decimal-leaf");
449
450         ExtendedType type = (ExtendedType) testleaf.getType();
451         QName typeQName = type.getQName();
452         assertEquals("my-decimal-type", typeQName.getLocalName());
453         assertEquals("n", typeQName.getPrefix());
454         assertEquals(nodesNS, typeQName.getNamespace());
455         assertEquals(nodesRev, typeQName.getRevision());
456         assertNull(type.getUnits());
457         assertNull(type.getDefaultValue());
458         assertEquals(4, (int) type.getFractionDigits());
459         assertTrue(type.getLengths().isEmpty());
460         assertTrue(type.getPatterns().isEmpty());
461         assertTrue(type.getRanges().isEmpty());
462
463         ExtendedType typeBase = (ExtendedType) type.getBaseType();
464         QName typeBaseQName = typeBase.getQName();
465         assertEquals("my-decimal-type", typeBaseQName.getLocalName());
466         assertEquals("t", typeBaseQName.getPrefix());
467         assertEquals(typesNS, typeBaseQName.getNamespace());
468         assertEquals(typesRev, typeBaseQName.getRevision());
469         assertNull(typeBase.getUnits());
470         assertNull(typeBase.getDefaultValue());
471         assertNull(typeBase.getFractionDigits());
472         assertTrue(typeBase.getLengths().isEmpty());
473         assertTrue(typeBase.getPatterns().isEmpty());
474         assertTrue(typeBase.getRanges().isEmpty());
475
476         Decimal64 decimal = (Decimal64) typeBase.getBaseType();
477         assertEquals(6, (int) decimal.getFractionDigits());
478     }
479
480     @Test
481     public void testTypedefDecimal2() {
482         Module testModule = TestUtils.findModule(modules, "nodes");
483         LeafSchemaNode testleaf = (LeafSchemaNode) testModule.getDataChildByName("decimal-leaf2");
484
485         ExtendedType type = (ExtendedType) testleaf.getType();
486         QName typeQName = type.getQName();
487         assertEquals("my-decimal-type", typeQName.getLocalName());
488         assertEquals("t", typeQName.getPrefix());
489         assertEquals(typesNS, typeQName.getNamespace());
490         assertEquals(typesRev, typeQName.getRevision());
491         assertNull(type.getUnits());
492         assertNull(type.getDefaultValue());
493         assertNull(type.getFractionDigits());
494         assertTrue(type.getLengths().isEmpty());
495         assertTrue(type.getPatterns().isEmpty());
496         assertTrue(type.getRanges().isEmpty());
497
498         Decimal64 baseTypeDecimal = (Decimal64) type.getBaseType();
499         assertEquals(6, (int) baseTypeDecimal.getFractionDigits());
500     }
501
502     @Test
503     public void testTypedefUnion() {
504         Module testModule = TestUtils.findModule(modules, "nodes");
505         LeafSchemaNode unionleaf = (LeafSchemaNode) testModule.getDataChildByName("union-leaf");
506
507         ExtendedType type = (ExtendedType) unionleaf.getType();
508         QName typeQName = type.getQName();
509         assertEquals("my-union-ext", typeQName.getLocalName());
510         assertEquals("t", typeQName.getPrefix());
511         assertEquals(typesNS, typeQName.getNamespace());
512         assertEquals(typesRev, typeQName.getRevision());
513         assertNull(type.getUnits());
514         assertNull(type.getDefaultValue());
515         assertNull(type.getFractionDigits());
516         assertTrue(type.getLengths().isEmpty());
517         assertTrue(type.getPatterns().isEmpty());
518         assertTrue(type.getRanges().isEmpty());
519
520         ExtendedType baseType = (ExtendedType) type.getBaseType();
521         QName baseTypeQName = baseType.getQName();
522         assertEquals("my-union", baseTypeQName.getLocalName());
523         assertEquals("t", baseTypeQName.getPrefix());
524         assertEquals(typesNS, baseTypeQName.getNamespace());
525         assertEquals(typesRev, baseTypeQName.getRevision());
526         assertNull(baseType.getUnits());
527         assertNull(baseType.getDefaultValue());
528         assertNull(baseType.getFractionDigits());
529         assertTrue(baseType.getLengths().isEmpty());
530         assertTrue(baseType.getPatterns().isEmpty());
531         assertTrue(baseType.getRanges().isEmpty());
532
533         UnionType unionType = (UnionType) baseType.getBaseType();
534         List<TypeDefinition<?>> unionTypes = unionType.getTypes();
535         assertEquals(2, unionTypes.size());
536
537         ExtendedType unionType1 = (ExtendedType) unionTypes.get(0);
538         QName unionType1QName = baseType.getQName();
539         assertEquals("my-union", unionType1QName.getLocalName());
540         assertEquals("t", unionType1QName.getPrefix());
541         assertEquals(typesNS, unionType1QName.getNamespace());
542         assertEquals(typesRev, unionType1QName.getRevision());
543         assertNull(unionType1.getUnits());
544         assertNull(unionType1.getDefaultValue());
545         assertNull(unionType1.getFractionDigits());
546         assertTrue(unionType1.getLengths().isEmpty());
547         assertTrue(unionType1.getPatterns().isEmpty());
548         List<RangeConstraint> ranges = unionType1.getRanges();
549         assertEquals(1, ranges.size());
550         RangeConstraint range = ranges.get(0);
551         assertEquals(1L, range.getMin());
552         assertEquals(100L, range.getMax());
553         assertTrue(unionType1.getBaseType() instanceof Int16);
554
555         assertTrue(unionTypes.get(1) instanceof Int32);
556     }
557
558     @Test
559     public void testNestedUnionResolving() {
560         Module testModule = TestUtils.findModule(modules, "nodes");
561         LeafSchemaNode testleaf = (LeafSchemaNode) testModule.getDataChildByName("custom-union-leaf");
562
563         ExtendedType type = (ExtendedType) testleaf.getType();
564         QName testleafTypeQName = type.getQName();
565         assertEquals(customNS, testleafTypeQName.getNamespace());
566         assertEquals(customRev, testleafTypeQName.getRevision());
567         assertEquals("c", testleafTypeQName.getPrefix());
568         assertEquals("union1", testleafTypeQName.getLocalName());
569         assertNull(type.getUnits());
570         assertNull(type.getDefaultValue());
571         assertNull(type.getFractionDigits());
572         assertTrue(type.getLengths().isEmpty());
573         assertTrue(type.getPatterns().isEmpty());
574         assertTrue(type.getRanges().isEmpty());
575
576         ExtendedType typeBase = (ExtendedType) type.getBaseType();
577         QName typeBaseQName = typeBase.getQName();
578         assertEquals(customNS, typeBaseQName.getNamespace());
579         assertEquals(customRev, typeBaseQName.getRevision());
580         assertEquals("c", typeBaseQName.getPrefix());
581         assertEquals("union2", typeBaseQName.getLocalName());
582         assertNull(typeBase.getUnits());
583         assertNull(typeBase.getDefaultValue());
584         assertNull(typeBase.getFractionDigits());
585         assertTrue(typeBase.getLengths().isEmpty());
586         assertTrue(typeBase.getPatterns().isEmpty());
587         assertTrue(typeBase.getRanges().isEmpty());
588
589         UnionType union = (UnionType) typeBase.getBaseType();
590         List<TypeDefinition<?>> unionTypes = union.getTypes();
591         assertEquals(2, unionTypes.size());
592         assertTrue(unionTypes.get(0) instanceof Int32);
593         assertTrue(unionTypes.get(1) instanceof ExtendedType);
594
595         ExtendedType unionType1 = (ExtendedType) unionTypes.get(1);
596         QName uniontType1QName = unionType1.getQName();
597         assertEquals(typesNS, uniontType1QName.getNamespace());
598         assertEquals(typesRev, uniontType1QName.getRevision());
599         assertEquals("t", uniontType1QName.getPrefix());
600         assertEquals("nested-union2", uniontType1QName.getLocalName());
601         assertNull(unionType1.getUnits());
602         assertNull(unionType1.getDefaultValue());
603         assertNull(unionType1.getFractionDigits());
604         assertTrue(unionType1.getLengths().isEmpty());
605         assertTrue(unionType1.getPatterns().isEmpty());
606         assertTrue(unionType1.getRanges().isEmpty());
607
608         UnionType nestedUnion = (UnionType) unionType1.getBaseType();
609         List<TypeDefinition<?>> nestedUnion2Types = nestedUnion.getTypes();
610         assertEquals(2, nestedUnion2Types.size());
611         assertTrue(nestedUnion2Types.get(0) instanceof StringType);
612         assertTrue(nestedUnion2Types.get(1) instanceof ExtendedType);
613
614         ExtendedType myUnionExt = (ExtendedType) nestedUnion2Types.get(1);
615         QName myUnionExtQName = myUnionExt.getQName();
616         assertEquals(typesNS, myUnionExtQName.getNamespace());
617         assertEquals(typesRev, myUnionExtQName.getRevision());
618         assertEquals("t", myUnionExtQName.getPrefix());
619         assertEquals("my-union-ext", myUnionExtQName.getLocalName());
620         assertNull(myUnionExt.getUnits());
621         assertNull(myUnionExt.getDefaultValue());
622         assertNull(myUnionExt.getFractionDigits());
623         assertTrue(myUnionExt.getLengths().isEmpty());
624         assertTrue(myUnionExt.getPatterns().isEmpty());
625         assertTrue(myUnionExt.getRanges().isEmpty());
626
627         ExtendedType myUnion = (ExtendedType) myUnionExt.getBaseType();
628         QName myUnionQName = myUnion.getQName();
629         assertEquals(typesNS, myUnionQName.getNamespace());
630         assertEquals(typesRev, myUnionQName.getRevision());
631         assertEquals("t", myUnionQName.getPrefix());
632         assertEquals("my-union", myUnionQName.getLocalName());
633         assertNull(myUnion.getUnits());
634         assertNull(myUnion.getDefaultValue());
635         assertNull(myUnion.getFractionDigits());
636         assertTrue(myUnion.getLengths().isEmpty());
637         assertTrue(myUnion.getPatterns().isEmpty());
638         assertTrue(myUnion.getRanges().isEmpty());
639
640         UnionType myUnionBase = (UnionType) myUnion.getBaseType();
641         List<TypeDefinition<?>> myUnionBaseTypes = myUnionBase.getTypes();
642         assertEquals(2, myUnionBaseTypes.size());
643         assertTrue(myUnionBaseTypes.get(0) instanceof ExtendedType);
644         assertTrue(myUnionBaseTypes.get(1) instanceof Int32);
645
646         ExtendedType int16Ext = (ExtendedType) myUnionBaseTypes.get(0);
647         QName int16ExtQName = int16Ext.getQName();
648         assertEquals(typesNS, int16ExtQName.getNamespace());
649         assertEquals(typesRev, int16ExtQName.getRevision());
650         assertEquals("t", int16ExtQName.getPrefix());
651         assertEquals("int16", int16ExtQName.getLocalName());
652         assertNull(int16Ext.getUnits());
653         assertNull(int16Ext.getDefaultValue());
654         assertNull(int16Ext.getFractionDigits());
655         assertTrue(int16Ext.getLengths().isEmpty());
656         assertTrue(int16Ext.getPatterns().isEmpty());
657         List<RangeConstraint> ranges = int16Ext.getRanges();
658         assertEquals(1, ranges.size());
659         RangeConstraint range = ranges.get(0);
660         assertEquals(1L, range.getMin());
661         assertEquals(100L, range.getMax());
662
663         assertTrue(int16Ext.getBaseType() instanceof Int16);
664     }
665
666     @Test
667     public void testChoice() {
668         Module testModule = TestUtils.findModule(modules, "nodes");
669         ContainerSchemaNode transfer = (ContainerSchemaNode) testModule.getDataChildByName("transfer");
670         ChoiceNode how = (ChoiceNode) transfer.getDataChildByName("how");
671         Set<ChoiceCaseNode> cases = how.getCases();
672         assertEquals(5, cases.size());
673         ChoiceCaseNode input = null;
674         ChoiceCaseNode output = null;
675         for (ChoiceCaseNode caseNode : cases) {
676             if ("input".equals(caseNode.getQName().getLocalName())) {
677                 input = caseNode;
678             } else if ("output".equals(caseNode.getQName().getLocalName())) {
679                 output = caseNode;
680             }
681         }
682         assertNotNull(input);
683         assertNotNull(input.getPath());
684         assertNotNull(output);
685         assertNotNull(output.getPath());
686     }
687
688     @Test
689     public void testAnyXml() {
690         Module testModule = TestUtils.findModule(modules, "nodes");
691         AnyXmlSchemaNode data = (AnyXmlSchemaNode) testModule.getDataChildByName("data");
692         assertNotNull("anyxml data not found", data);
693
694         // test SchemaNode args
695         QName qname = data.getQName();
696         assertEquals("data", qname.getLocalName());
697         assertEquals("n", qname.getPrefix());
698         assertEquals(nodesNS, qname.getNamespace());
699         assertEquals(nodesRev, qname.getRevision());
700         assertTrue(data.getDescription().contains("Copy of the source typesstore subset that matched"));
701         assertNull(data.getReference());
702         assertEquals(Status.OBSOLETE, data.getStatus());
703         assertEquals(0, data.getUnknownSchemaNodes().size());
704         // test DataSchemaNode args
705         assertFalse(data.isAugmenting());
706         assertTrue(data.isConfiguration());
707         ConstraintDefinition constraints = data.getConstraints();
708         assertNull(constraints.getWhenCondition());
709         assertEquals(0, constraints.getMustConstraints().size());
710         assertFalse(constraints.isMandatory());
711         assertNull(constraints.getMinElements());
712         assertNull(constraints.getMaxElements());
713     }
714
715     @Test
716     public void testDeviation() {
717         Module testModule = TestUtils.findModule(modules, "nodes");
718         Set<Deviation> deviations = testModule.getDeviations();
719         assertEquals(1, deviations.size());
720         Deviation dev = deviations.iterator().next();
721
722         assertEquals("system/user ref", dev.getReference());
723
724         List<QName> path = new ArrayList<QName>();
725         path.add(new QName(typesNS, typesRev, "t", "interfaces"));
726         path.add(new QName(typesNS, typesRev, "t", "ifEntry"));
727         SchemaPath expectedPath = new SchemaPath(path, true);
728
729         assertEquals(expectedPath, dev.getTargetPath());
730         assertEquals(Deviate.ADD, dev.getDeviate());
731     }
732
733     @Test
734     public void testUnknownNode() {
735         Module testModule = TestUtils.findModule(modules, "custom");
736         ContainerSchemaNode network = (ContainerSchemaNode) testModule.getDataChildByName("network");
737         List<UnknownSchemaNode> unknownNodes = network.getUnknownSchemaNodes();
738         assertEquals(1, unknownNodes.size());
739         UnknownSchemaNode unknownNode = unknownNodes.get(0);
740         assertNotNull(unknownNode.getNodeType());
741         assertEquals("point", unknownNode.getNodeParameter());
742     }
743
744     @Test
745     public void testFeature() {
746         Module testModule = TestUtils.findModule(modules, "custom");
747         Set<FeatureDefinition> features = testModule.getFeatures();
748         assertEquals(1, features.size());
749     }
750
751     @Test
752     public void testExtension() {
753         Module testModule = TestUtils.findModule(modules, "custom");
754         List<ExtensionDefinition> extensions = testModule.getExtensionSchemaNodes();
755         assertEquals(1, extensions.size());
756         ExtensionDefinition extension = extensions.get(0);
757         assertEquals("name", extension.getArgument());
758         assertTrue(extension.isYinElement());
759     }
760
761     @Test
762     public void testNotification() {
763         Module testModule = TestUtils.findModule(modules, "custom");
764         String expectedPrefix = "c";
765
766         Set<NotificationDefinition> notifications = testModule.getNotifications();
767         assertEquals(1, notifications.size());
768
769         NotificationDefinition notification = notifications.iterator().next();
770         // test SchemaNode args
771         QName expectedQName = new QName(customNS, customRev, expectedPrefix, "event");
772         assertEquals(expectedQName, notification.getQName());
773         SchemaPath expectedPath = TestUtils.createPath(true, customNS, customRev, expectedPrefix, "event");
774         assertEquals(expectedPath, notification.getPath());
775         assertNull(notification.getDescription());
776         assertNull(notification.getReference());
777         assertEquals(Status.CURRENT, notification.getStatus());
778         assertEquals(0, notification.getUnknownSchemaNodes().size());
779         // test DataNodeContainer args
780         assertEquals(0, notification.getTypeDefinitions().size());
781         assertEquals(3, notification.getChildNodes().size());
782         assertEquals(0, notification.getGroupings().size());
783         assertEquals(0, notification.getUses().size());
784
785         LeafSchemaNode eventClass = (LeafSchemaNode) notification.getDataChildByName("event-class");
786         assertTrue(eventClass.getType() instanceof StringType);
787         AnyXmlSchemaNode reportingEntity = (AnyXmlSchemaNode) notification.getDataChildByName("reporting-entity");
788         assertNotNull(reportingEntity);
789         LeafSchemaNode severity = (LeafSchemaNode) notification.getDataChildByName("severity");
790         assertTrue(severity.getType() instanceof StringType);
791     }
792
793     @Test
794     public void testRpc() {
795         Module testModule = TestUtils.findModule(modules, "custom");
796
797         Set<RpcDefinition> rpcs = testModule.getRpcs();
798         assertEquals(1, rpcs.size());
799
800         RpcDefinition rpc = rpcs.iterator().next();
801         assertEquals("Retrieve all or part of a specified configuration.", rpc.getDescription());
802         assertEquals("RFC 6241, Section 7.1", rpc.getReference());
803
804         ContainerSchemaNode input = rpc.getInput();
805         assertNotNull(input.getDataChildByName("source"));
806         assertNotNull(input.getDataChildByName("filter"));
807         ContainerSchemaNode output = rpc.getOutput();
808         assertNotNull(output.getDataChildByName("data"));
809     }
810
811     @Test
812     public void testTypePath() throws ParseException {
813         Module test = TestUtils.findModule(modules, "types");
814         Set<TypeDefinition<?>> types = test.getTypeDefinitions();
815
816         // my-base-int32-type
817         ExtendedType int32Typedef = (ExtendedType) TestUtils.findTypedef(types, "int32-ext1");
818         QName int32TypedefQName = int32Typedef.getQName();
819
820         assertEquals(typesNS, int32TypedefQName.getNamespace());
821         assertEquals(typesRev, int32TypedefQName.getRevision());
822         assertEquals("t", int32TypedefQName.getPrefix());
823         assertEquals("int32-ext1", int32TypedefQName.getLocalName());
824
825         SchemaPath typeSchemaPath = int32Typedef.getPath();
826         List<QName> typePath = typeSchemaPath.getPath();
827         assertEquals(1, typePath.size());
828         assertEquals(int32TypedefQName, typePath.get(0));
829
830         // my-base-int32-type/int32
831         Int32 int32 = (Int32) int32Typedef.getBaseType();
832         QName int32QName = int32.getQName();
833         assertEquals(URI.create("urn:ietf:params:xml:ns:yang:1"), int32QName.getNamespace());
834         assertNull(int32QName.getRevision());
835         assertEquals("", int32QName.getPrefix());
836         assertEquals("int32", int32QName.getLocalName());
837
838         SchemaPath int32SchemaPath = int32.getPath();
839         List<QName> int32Path = int32SchemaPath.getPath();
840         assertEquals(3, int32Path.size());
841         assertEquals(int32TypedefQName, int32Path.get(0));
842         assertEquals(int32QName, int32Path.get(2));
843     }
844
845     @Test
846     public void testTypePath2() throws ParseException {
847         Module test = TestUtils.findModule(modules, "types");
848         Set<TypeDefinition<?>> types = test.getTypeDefinitions();
849
850         // my-base-int32-type
851         ExtendedType myDecType = (ExtendedType) TestUtils.findTypedef(types, "my-decimal-type");
852         QName myDecTypeQName = myDecType.getQName();
853
854         assertEquals(typesNS, myDecTypeQName.getNamespace());
855         assertEquals(typesRev, myDecTypeQName.getRevision());
856         assertEquals("t", myDecTypeQName.getPrefix());
857         assertEquals("my-decimal-type", myDecTypeQName.getLocalName());
858
859         SchemaPath typeSchemaPath = myDecType.getPath();
860         List<QName> typePath = typeSchemaPath.getPath();
861         assertEquals(1, typePath.size());
862         assertEquals(myDecTypeQName, typePath.get(0));
863
864         // my-base-int32-type/int32
865         Decimal64 dec64 = (Decimal64) myDecType.getBaseType();
866         QName dec64QName = dec64.getQName();
867
868         assertEquals(URI.create("urn:ietf:params:xml:ns:yang:1"), dec64QName.getNamespace());
869         assertNull(dec64QName.getRevision());
870         assertEquals("", dec64QName.getPrefix());
871         assertEquals("decimal64", dec64QName.getLocalName());
872
873         SchemaPath dec64SchemaPath = dec64.getPath();
874         List<QName> dec64Path = dec64SchemaPath.getPath();
875         assertEquals(2, dec64Path.size());
876         assertEquals(myDecTypeQName, dec64Path.get(0));
877         assertEquals(dec64QName, dec64Path.get(1));
878     }
879
880 }