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