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