X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=yang%2Fyang-parser-impl%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fparser%2Fimpl%2FYangParserTest.java;h=760bee4bb05bb2999a51b5f46f7a443252d8a750;hb=f17062feab0af9776ee220c7ea73fdf23a5d2a8e;hp=d946e07356fb521745f742397f80fe93df0808eb;hpb=56301b40c4372ad9380081e95528c9f2fee3e6e5;p=yangtools.git diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/YangParserTest.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/YangParserTest.java index d946e07356..760bee4bb0 100644 --- a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/YangParserTest.java +++ b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/YangParserTest.java @@ -7,20 +7,37 @@ */ package org.opendaylight.yangtools.yang.parser.impl; -import static org.junit.Assert.*; - +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.File; +import java.io.FileInputStream; import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.math.BigInteger; import java.net.URI; +import java.net.URISyntaxException; +import java.text.DateFormat; import java.text.ParseException; +import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; import java.util.Date; +import java.util.Iterator; +import java.util.LinkedHashSet; import java.util.List; +import java.util.Map; import java.util.Set; import org.junit.Before; import org.junit.Test; import org.opendaylight.yangtools.yang.common.QName; -import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode; import org.opendaylight.yangtools.yang.model.api.AugmentationSchema; import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode; import org.opendaylight.yangtools.yang.model.api.ChoiceNode; @@ -31,13 +48,14 @@ import org.opendaylight.yangtools.yang.model.api.Deviation; import org.opendaylight.yangtools.yang.model.api.Deviation.Deviate; import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition; import org.opendaylight.yangtools.yang.model.api.FeatureDefinition; -import org.opendaylight.yangtools.yang.model.api.GroupingDefinition; import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode; import org.opendaylight.yangtools.yang.model.api.ListSchemaNode; import org.opendaylight.yangtools.yang.model.api.Module; +import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier; import org.opendaylight.yangtools.yang.model.api.ModuleImport; import org.opendaylight.yangtools.yang.model.api.NotificationDefinition; import org.opendaylight.yangtools.yang.model.api.RpcDefinition; +import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.model.api.SchemaPath; import org.opendaylight.yangtools.yang.model.api.Status; import org.opendaylight.yangtools.yang.model.api.TypeDefinition; @@ -45,6 +63,8 @@ import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode; import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint; import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint; import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint; +import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser; +import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException; import org.opendaylight.yangtools.yang.model.util.Decimal64; import org.opendaylight.yangtools.yang.model.util.ExtendedType; import org.opendaylight.yangtools.yang.model.util.Int16; @@ -52,144 +72,72 @@ import org.opendaylight.yangtools.yang.model.util.Int32; import org.opendaylight.yangtools.yang.model.util.StringType; import org.opendaylight.yangtools.yang.model.util.Uint32; import org.opendaylight.yangtools.yang.model.util.UnionType; +import org.opendaylight.yangtools.yang.parser.builder.impl.BuilderUtils; +import org.opendaylight.yangtools.yang.parser.util.YangParseException; public class YangParserTest { - private final URI nodesNS = URI.create("urn:simple.nodes.test"); - private final URI typesNS = URI.create("urn:simple.types.test"); - private final URI customNS = URI.create("urn:custom.nodes.test"); - private Date nodesRev; - private Date typesRev; - private Date customRev; + public static final String FS = File.separator; + + private final URI fooNS = URI.create("urn:opendaylight.foo"); + private final URI barNS = URI.create("urn:opendaylight.bar"); + private final URI bazNS = URI.create("urn:opendaylight.baz"); + private Date fooRev; + private Date barRev; + private Date bazRev; private Set modules; @Before - public void init() throws FileNotFoundException, ParseException { - nodesRev = TestUtils.simpleDateFormat.parse("2013-02-27"); - typesRev = TestUtils.simpleDateFormat.parse("2013-07-03"); - customRev = TestUtils.simpleDateFormat.parse("2013-02-27"); + public void init() throws Exception { + DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); + fooRev = simpleDateFormat.parse("2013-02-27"); + barRev = simpleDateFormat.parse("2013-07-03"); + bazRev = simpleDateFormat.parse("2013-02-27"); - modules = TestUtils.loadModules(getClass().getResource("/model").getPath()); + modules = TestUtils.loadModules(getClass().getResource("/model").toURI()); assertEquals(3, modules.size()); } @Test public void testHeaders() throws ParseException { - Module test = TestUtils.findModule(modules, "nodes"); + Module foo = TestUtils.findModule(modules, "foo"); - assertEquals("nodes", test.getName()); - assertEquals("1", test.getYangVersion()); - assertEquals(nodesNS, test.getNamespace()); - assertEquals("n", test.getPrefix()); + assertEquals("foo", foo.getName()); + assertEquals("1", foo.getYangVersion()); + assertEquals(fooNS, foo.getNamespace()); + assertEquals("foo", foo.getPrefix()); - Set imports = test.getImports(); + Set imports = foo.getImports(); assertEquals(2, imports.size()); - ModuleImport import2 = TestUtils.findImport(imports, "t"); - assertEquals("types", import2.getModuleName()); - assertEquals(typesRev, import2.getRevision()); + ModuleImport import2 = TestUtils.findImport(imports, "br"); + assertEquals("bar", import2.getModuleName()); + assertEquals(barRev, import2.getRevision()); - ModuleImport import3 = TestUtils.findImport(imports, "c"); - assertEquals("custom", import3.getModuleName()); - assertEquals(customRev, import3.getRevision()); + ModuleImport import3 = TestUtils.findImport(imports, "bz"); + assertEquals("baz", import3.getModuleName()); + assertEquals(bazRev, import3.getRevision()); - assertEquals("opendaylight", test.getOrganization()); - assertEquals("http://www.opendaylight.org/", test.getContact()); + assertEquals("opendaylight", foo.getOrganization()); + assertEquals("http://www.opendaylight.org/", foo.getContact()); Date expectedRevision = TestUtils.createDate("2013-02-27"); - assertEquals(expectedRevision, test.getRevision()); - assertEquals(" WILL BE DEFINED LATER", test.getReference()); - } - - @Test - public void testOrderingTypedef() { - Module test = TestUtils.findModule(modules, "types"); - Set> typedefs = test.getTypeDefinitions(); - String[] expectedOrder = new String[] { "int32-ext1", "int32-ext2", "my-decimal-type", "my-union", - "my-union-ext", "nested-union2", "string-ext1", "string-ext2", "string-ext3", "string-ext4" }; - String[] actualOrder = new String[typedefs.size()]; - - int i = 0; - for (TypeDefinition type : typedefs) { - actualOrder[i] = type.getQName().getLocalName(); - i++; - } - assertArrayEquals(expectedOrder, actualOrder); - } - - @Test - public void testOrderingChildNodes() { - Module test = TestUtils.findModule(modules, "nodes"); - AugmentationSchema augment1 = null; - for (AugmentationSchema as : test.getAugmentations()) { - if ("if:ifType='ds0'".equals(as.getWhenCondition().toString())) { - augment1 = as; - break; - } - } - assertNotNull(augment1); - - String[] expectedOrder = new String[] { "ds0ChannelNumber", "interface-id", "my-type", "odl", "schemas" }; - String[] actualOrder = new String[expectedOrder.length]; - - int i = 0; - for (DataSchemaNode augmentChild : augment1.getChildNodes()) { - actualOrder[i] = augmentChild.getQName().getLocalName(); - i++; - } - - assertArrayEquals(expectedOrder, actualOrder); - } - - @Test - public void testOrderingNestedChildNodes1() { - Module test = TestUtils.findModule(modules, "nodes"); - - Set childNodes = test.getChildNodes(); - String[] expectedOrder = new String[] { "address", "addresses", "custom-union-leaf", "data", "datas", - "decimal-leaf", "decimal-leaf2", "ext", "how", "int32-leaf", "length-leaf", "mycont", "peer", "port", - "string-leaf", "transfer", "union-leaf" }; - String[] actualOrder = new String[childNodes.size()]; - - int i = 0; - for (DataSchemaNode child : childNodes) { - actualOrder[i] = child.getQName().getLocalName(); - i++; - } - assertArrayEquals(expectedOrder, actualOrder); - } - - @Test - public void testOrderingNestedChildNodes2() { - Module test = TestUtils.findModule(modules, "custom"); - Set groupings = test.getGroupings(); - assertEquals(1, groupings.size()); - GroupingDefinition target = groupings.iterator().next(); - - Set childNodes = target.getChildNodes(); - String[] expectedOrder = new String[] { "address", "addresses", "data", "how", "port" }; - String[] actualOrder = new String[childNodes.size()]; - - int i = 0; - for (DataSchemaNode child : childNodes) { - actualOrder[i] = child.getQName().getLocalName(); - i++; - } - assertArrayEquals(expectedOrder, actualOrder); + assertEquals(expectedRevision, foo.getRevision()); + assertEquals(" WILL BE DEFINED LATER", foo.getReference()); } @Test public void testParseList() { - Module test = TestUtils.findModule(modules, "types"); - URI expectedNamespace = URI.create("urn:simple.types.test"); - String expectedPrefix = "t"; + Module bar = TestUtils.findModule(modules, "bar"); + URI expectedNamespace = URI.create("urn:opendaylight.bar"); + String expectedPrefix = "bar"; - ContainerSchemaNode interfaces = (ContainerSchemaNode) test.getDataChildByName("interfaces"); + ContainerSchemaNode interfaces = (ContainerSchemaNode) bar.getDataChildByName("interfaces"); ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry"); // test SchemaNode args - QName expectedQName = new QName(expectedNamespace, typesRev, expectedPrefix, "ifEntry"); + QName expectedQName = QName.create(expectedNamespace, barRev, "ifEntry"); assertEquals(expectedQName, ifEntry.getQName()); - SchemaPath expectedPath = TestUtils.createPath(true, expectedNamespace, typesRev, expectedPrefix, "interfaces", + SchemaPath expectedPath = TestUtils.createPath(true, expectedNamespace, barRev, expectedPrefix, "interfaces", "ifEntry"); assertEquals(expectedPath, ifEntry.getPath()); assertNull(ifEntry.getDescription()); @@ -209,8 +157,8 @@ public class YangParserTest { Set availableAugmentations = ifEntry.getAvailableAugmentations(); assertEquals(2, availableAugmentations.size()); // test ListSchemaNode args - List expectedKey = new ArrayList(); - expectedKey.add(new QName(expectedNamespace, typesRev, expectedPrefix, "ifIndex")); + List expectedKey = new ArrayList<>(); + expectedKey.add(QName.create(expectedNamespace, barRev, "ifIndex")); assertEquals(expectedKey, ifEntry.getKeyDefinition()); assertFalse(ifEntry.isUserOrdered()); // test DataNodeContainer args @@ -220,6 +168,7 @@ public class YangParserTest { assertEquals(0, ifEntry.getUses().size()); LeafSchemaNode ifIndex = (LeafSchemaNode) ifEntry.getDataChildByName("ifIndex"); + assertEquals(ifEntry.getKeyDefinition().get(0), ifIndex.getQName()); assertTrue(ifIndex.getType() instanceof Uint32); LeafSchemaNode ifMtu = (LeafSchemaNode) ifEntry.getDataChildByName("ifMtu"); assertTrue(ifMtu.getType() instanceof Int32); @@ -227,15 +176,14 @@ public class YangParserTest { @Test public void testTypedefRangesResolving() throws ParseException { - Module testModule = TestUtils.findModule(modules, "nodes"); - LeafSchemaNode int32Leaf = (LeafSchemaNode) testModule.getDataChildByName("int32-leaf"); + Module foo = TestUtils.findModule(modules, "foo"); + LeafSchemaNode int32Leaf = (LeafSchemaNode) foo.getDataChildByName("int32-leaf"); ExtendedType leafType = (ExtendedType) int32Leaf.getType(); QName leafTypeQName = leafType.getQName(); assertEquals("int32-ext2", leafTypeQName.getLocalName()); - assertEquals("n", leafTypeQName.getPrefix()); - assertEquals(nodesNS, leafTypeQName.getNamespace()); - assertEquals(nodesRev, leafTypeQName.getRevision()); + assertEquals(fooNS, leafTypeQName.getNamespace()); + assertEquals(fooRev, leafTypeQName.getRevision()); assertNull(leafType.getUnits()); assertNull(leafType.getDefaultValue()); assertTrue(leafType.getLengthConstraints().isEmpty()); @@ -243,15 +191,14 @@ public class YangParserTest { List ranges = leafType.getRangeConstraints(); assertEquals(1, ranges.size()); RangeConstraint range = ranges.get(0); - assertEquals(12L, range.getMin()); - assertEquals(20L, range.getMax()); + assertEquals(BigInteger.valueOf(12), range.getMin()); + assertEquals(BigInteger.valueOf(20), range.getMax()); ExtendedType baseType = (ExtendedType) leafType.getBaseType(); QName baseTypeQName = baseType.getQName(); assertEquals("int32-ext2", baseTypeQName.getLocalName()); - assertEquals("t", baseTypeQName.getPrefix()); - assertEquals(typesNS, baseTypeQName.getNamespace()); - assertEquals(typesRev, baseTypeQName.getRevision()); + assertEquals(barNS, baseTypeQName.getNamespace()); + assertEquals(barRev, baseTypeQName.getRevision()); assertEquals("mile", baseType.getUnits()); assertEquals("11", baseType.getDefaultValue()); assertTrue(leafType.getLengthConstraints().isEmpty()); @@ -259,18 +206,17 @@ public class YangParserTest { List baseTypeRanges = baseType.getRangeConstraints(); assertEquals(2, baseTypeRanges.size()); RangeConstraint baseTypeRange1 = baseTypeRanges.get(0); - assertEquals(3L, baseTypeRange1.getMin()); - assertEquals(9L, baseTypeRange1.getMax()); + assertEquals(BigInteger.valueOf(3), baseTypeRange1.getMin()); + assertEquals(BigInteger.valueOf(9), baseTypeRange1.getMax()); RangeConstraint baseTypeRange2 = baseTypeRanges.get(1); - assertEquals(11L, baseTypeRange2.getMin()); - assertEquals(20L, baseTypeRange2.getMax()); + assertEquals(BigInteger.valueOf(11), baseTypeRange2.getMin()); + assertEquals(BigInteger.valueOf(20), baseTypeRange2.getMax()); ExtendedType base = (ExtendedType) baseType.getBaseType(); QName baseQName = base.getQName(); assertEquals("int32-ext1", baseQName.getLocalName()); - assertEquals("t", baseQName.getPrefix()); - assertEquals(typesNS, baseQName.getNamespace()); - assertEquals(typesRev, baseQName.getRevision()); + assertEquals(barNS, baseQName.getNamespace()); + assertEquals(barRev, baseQName.getRevision()); assertNull(base.getUnits()); assertNull(base.getDefaultValue()); assertTrue(leafType.getLengthConstraints().isEmpty()); @@ -278,140 +224,199 @@ public class YangParserTest { List baseRanges = base.getRangeConstraints(); assertEquals(1, baseRanges.size()); RangeConstraint baseRange = baseRanges.get(0); - assertEquals(2L, baseRange.getMin()); - assertEquals(20L, baseRange.getMax()); + assertEquals(BigInteger.valueOf(2), baseRange.getMin()); + assertEquals(BigInteger.valueOf(20), baseRange.getMax()); assertTrue(base.getBaseType() instanceof Int32); } @Test public void testTypedefPatternsResolving() { - Module testModule = TestUtils.findModule(modules, "nodes"); - LeafSchemaNode stringleaf = (LeafSchemaNode) testModule.getDataChildByName("string-leaf"); + Module foo = TestUtils.findModule(modules, "foo"); + LeafSchemaNode stringleaf = (LeafSchemaNode) foo.getDataChildByName("string-leaf"); ExtendedType type = (ExtendedType) stringleaf.getType(); QName typeQName = type.getQName(); assertEquals("string-ext4", typeQName.getLocalName()); - assertEquals("t", typeQName.getPrefix()); - assertEquals(typesNS, typeQName.getNamespace()); - assertEquals(typesRev, typeQName.getRevision()); + assertEquals(barNS, typeQName.getNamespace()); + assertEquals(barRev, typeQName.getRevision()); assertNull(type.getUnits()); assertNull(type.getDefaultValue()); List patterns = type.getPatternConstraints(); assertEquals(1, patterns.size()); PatternConstraint pattern = patterns.iterator().next(); - assertEquals("[e-z]*", pattern.getRegularExpression()); + assertEquals("^[e-z]*$", pattern.getRegularExpression()); assertTrue(type.getLengthConstraints().isEmpty()); assertTrue(type.getRangeConstraints().isEmpty()); ExtendedType baseType1 = (ExtendedType) type.getBaseType(); QName baseType1QName = baseType1.getQName(); assertEquals("string-ext3", baseType1QName.getLocalName()); - assertEquals("t", baseType1QName.getPrefix()); - assertEquals(typesNS, baseType1QName.getNamespace()); - assertEquals(typesRev, baseType1QName.getRevision()); + assertEquals(barNS, baseType1QName.getNamespace()); + assertEquals(barRev, baseType1QName.getRevision()); assertNull(baseType1.getUnits()); assertNull(baseType1.getDefaultValue()); patterns = baseType1.getPatternConstraints(); assertEquals(1, patterns.size()); pattern = patterns.iterator().next(); - assertEquals("[b-u]*", pattern.getRegularExpression()); + assertEquals("^[b-u]*$", pattern.getRegularExpression()); assertTrue(baseType1.getLengthConstraints().isEmpty()); assertTrue(baseType1.getRangeConstraints().isEmpty()); ExtendedType baseType2 = (ExtendedType) baseType1.getBaseType(); QName baseType2QName = baseType2.getQName(); assertEquals("string-ext2", baseType2QName.getLocalName()); - assertEquals("t", baseType2QName.getPrefix()); - assertEquals(typesNS, baseType2QName.getNamespace()); - assertEquals(typesRev, baseType2QName.getRevision()); + assertEquals(barNS, baseType2QName.getNamespace()); + assertEquals(barRev, baseType2QName.getRevision()); assertNull(baseType2.getUnits()); assertNull(baseType2.getDefaultValue()); assertTrue(baseType2.getPatternConstraints().isEmpty()); List baseType2Lengths = baseType2.getLengthConstraints(); assertEquals(1, baseType2Lengths.size()); LengthConstraint length = baseType2Lengths.get(0); - assertEquals(6L, length.getMin()); - assertEquals(10L, length.getMax()); + assertEquals(BigInteger.valueOf(6), length.getMin()); + assertEquals(BigInteger.TEN, length.getMax()); assertTrue(baseType2.getRangeConstraints().isEmpty()); ExtendedType baseType3 = (ExtendedType) baseType2.getBaseType(); QName baseType3QName = baseType3.getQName(); assertEquals("string-ext1", baseType3QName.getLocalName()); - assertEquals("t", baseType3QName.getPrefix()); - assertEquals(typesNS, baseType3QName.getNamespace()); - assertEquals(typesRev, baseType3QName.getRevision()); + assertEquals(barNS, baseType3QName.getNamespace()); + assertEquals(barRev, baseType3QName.getRevision()); assertNull(baseType3.getUnits()); assertNull(baseType3.getDefaultValue()); patterns = baseType3.getPatternConstraints(); assertEquals(1, patterns.size()); pattern = patterns.iterator().next(); - assertEquals("[a-k]*", pattern.getRegularExpression()); + assertEquals("^[a-k]*$", pattern.getRegularExpression()); List baseType3Lengths = baseType3.getLengthConstraints(); assertEquals(1, baseType3Lengths.size()); length = baseType3Lengths.get(0); - assertEquals(5L, length.getMin()); - assertEquals(11L, length.getMax()); + assertEquals(BigInteger.valueOf(5), length.getMin()); + assertEquals(BigInteger.valueOf(11), length.getMax()); assertTrue(baseType3.getRangeConstraints().isEmpty()); assertTrue(baseType3.getBaseType() instanceof StringType); } + @Test + public void testTypedefInvalidPatternsResolving() { + Module foo = TestUtils.findModule(modules, "foo"); + final LeafSchemaNode invalidPatternStringLeaf = (LeafSchemaNode) foo.getDataChildByName("invalid-pattern-string-leaf"); + ExtendedType type = (ExtendedType) invalidPatternStringLeaf.getType(); + QName typeQName = type.getQName(); + assertEquals("invalid-string-pattern", typeQName.getLocalName()); + assertEquals(barNS, typeQName.getNamespace()); + assertEquals(barRev, typeQName.getRevision()); + assertNull(type.getUnits()); + assertNull(type.getDefaultValue()); + List patterns = type.getPatternConstraints(); + assertTrue(patterns.isEmpty()); + + final LeafSchemaNode invalidDirectStringPatternDefLeaf = (LeafSchemaNode) foo.getDataChildByName("invalid-direct-string-pattern-def-leaf"); + type = (ExtendedType) invalidDirectStringPatternDefLeaf.getType(); + typeQName = type.getQName(); + assertEquals("string", typeQName.getLocalName()); + assertEquals(fooNS, typeQName.getNamespace()); + assertEquals(fooRev, typeQName.getRevision()); + assertNull(type.getUnits()); + assertNull(type.getDefaultValue()); + patterns = type.getPatternConstraints(); + assertTrue(patterns.isEmpty()); + + final LeafSchemaNode multiplePatternStringLeaf = (LeafSchemaNode) foo.getDataChildByName("multiple-pattern-string-leaf"); + type = (ExtendedType) multiplePatternStringLeaf.getType(); + typeQName = type.getQName(); + assertEquals("multiple-pattern-string", typeQName.getLocalName()); + assertEquals(barNS, typeQName.getNamespace()); + assertEquals(barRev, typeQName.getRevision()); + assertNull(type.getUnits()); + assertNull(type.getDefaultValue()); + patterns = type.getPatternConstraints(); + assertTrue(!patterns.isEmpty()); + assertEquals(1, patterns.size()); + PatternConstraint pattern = patterns.iterator().next(); + assertEquals("^[e-z]*$", pattern.getRegularExpression()); + assertTrue(type.getLengthConstraints().isEmpty()); + assertTrue(type.getRangeConstraints().isEmpty()); + + final LeafSchemaNode multiplePatternDirectStringDefLeaf = (LeafSchemaNode) foo.getDataChildByName("multiple-pattern-direct-string-def-leaf"); + type = (ExtendedType) multiplePatternDirectStringDefLeaf.getType(); + typeQName = type.getQName(); + assertEquals("string", typeQName.getLocalName()); + assertEquals(fooNS, typeQName.getNamespace()); + assertEquals(fooRev, typeQName.getRevision()); + assertNull(type.getUnits()); + assertNull(type.getDefaultValue()); + patterns = type.getPatternConstraints(); + assertTrue(!patterns.isEmpty()); + assertEquals(2, patterns.size()); + + boolean isEZPattern = false; + boolean isADPattern = false; + for (final PatternConstraint patternConstraint : patterns) { + if (patternConstraint.getRegularExpression().equals("^[e-z]*$")) { + isEZPattern = true; + } else if (patternConstraint.getRegularExpression().equals("^[a-d]*$")) { + isADPattern = true; + } + } + assertTrue(isEZPattern); + assertTrue( isADPattern); + } + @Test public void testTypedefLengthsResolving() { - Module testModule = TestUtils.findModule(modules, "nodes"); + Module foo = TestUtils.findModule(modules, "foo"); - LeafSchemaNode lengthLeaf = (LeafSchemaNode) testModule.getDataChildByName("length-leaf"); + LeafSchemaNode lengthLeaf = (LeafSchemaNode) foo.getDataChildByName("length-leaf"); ExtendedType type = (ExtendedType) lengthLeaf.getType(); QName typeQName = type.getQName(); assertEquals("string-ext2", typeQName.getLocalName()); - assertEquals("n", typeQName.getPrefix()); - assertEquals(nodesNS, typeQName.getNamespace()); - assertEquals(nodesRev, typeQName.getRevision()); + assertEquals(fooNS, typeQName.getNamespace()); + assertEquals(fooRev, typeQName.getRevision()); assertNull(type.getUnits()); assertNull(type.getDefaultValue()); assertTrue(type.getPatternConstraints().isEmpty()); List typeLengths = type.getLengthConstraints(); assertEquals(1, typeLengths.size()); LengthConstraint length = typeLengths.get(0); - assertEquals(7L, length.getMin()); - assertEquals(10L, length.getMax()); + assertEquals(BigInteger.valueOf(7), length.getMin()); + assertEquals(BigInteger.TEN, length.getMax()); assertTrue(type.getRangeConstraints().isEmpty()); ExtendedType baseType1 = (ExtendedType) type.getBaseType(); QName baseType1QName = baseType1.getQName(); assertEquals("string-ext2", baseType1QName.getLocalName()); - assertEquals("t", baseType1QName.getPrefix()); - assertEquals(typesNS, baseType1QName.getNamespace()); - assertEquals(typesRev, baseType1QName.getRevision()); + assertEquals(barNS, baseType1QName.getNamespace()); + assertEquals(barRev, baseType1QName.getRevision()); assertNull(baseType1.getUnits()); assertNull(baseType1.getDefaultValue()); assertTrue(baseType1.getPatternConstraints().isEmpty()); List baseType2Lengths = baseType1.getLengthConstraints(); assertEquals(1, baseType2Lengths.size()); length = baseType2Lengths.get(0); - assertEquals(6L, length.getMin()); - assertEquals(10L, length.getMax()); + assertEquals(BigInteger.valueOf(6), length.getMin()); + assertEquals(BigInteger.TEN, length.getMax()); assertTrue(baseType1.getRangeConstraints().isEmpty()); ExtendedType baseType2 = (ExtendedType) baseType1.getBaseType(); QName baseType2QName = baseType2.getQName(); assertEquals("string-ext1", baseType2QName.getLocalName()); - assertEquals("t", baseType2QName.getPrefix()); - assertEquals(typesNS, baseType2QName.getNamespace()); - assertEquals(typesRev, baseType2QName.getRevision()); + assertEquals(barNS, baseType2QName.getNamespace()); + assertEquals(barRev, baseType2QName.getRevision()); assertNull(baseType2.getUnits()); assertNull(baseType2.getDefaultValue()); List patterns = baseType2.getPatternConstraints(); assertEquals(1, patterns.size()); PatternConstraint pattern = patterns.iterator().next(); - assertEquals("[a-k]*", pattern.getRegularExpression()); + assertEquals("^[a-k]*$", pattern.getRegularExpression()); List baseType3Lengths = baseType2.getLengthConstraints(); assertEquals(1, baseType3Lengths.size()); length = baseType3Lengths.get(0); - assertEquals(5L, length.getMin()); - assertEquals(11L, length.getMax()); + assertEquals(BigInteger.valueOf(5), length.getMin()); + assertEquals(BigInteger.valueOf(11), length.getMax()); assertTrue(baseType2.getRangeConstraints().isEmpty()); assertTrue(baseType2.getBaseType() instanceof StringType); @@ -419,15 +424,14 @@ public class YangParserTest { @Test public void testTypedefDecimal1() { - Module testModule = TestUtils.findModule(modules, "nodes"); - LeafSchemaNode testleaf = (LeafSchemaNode) testModule.getDataChildByName("decimal-leaf"); + Module foo = TestUtils.findModule(modules, "foo"); + LeafSchemaNode testleaf = (LeafSchemaNode) foo.getDataChildByName("decimal-leaf"); ExtendedType type = (ExtendedType) testleaf.getType(); QName typeQName = type.getQName(); assertEquals("my-decimal-type", typeQName.getLocalName()); - assertEquals("n", typeQName.getPrefix()); - assertEquals(nodesNS, typeQName.getNamespace()); - assertEquals(nodesRev, typeQName.getRevision()); + assertEquals(fooNS, typeQName.getNamespace()); + assertEquals(fooRev, typeQName.getRevision()); assertNull(type.getUnits()); assertNull(type.getDefaultValue()); assertEquals(4, (int) type.getFractionDigits()); @@ -438,9 +442,8 @@ public class YangParserTest { ExtendedType typeBase = (ExtendedType) type.getBaseType(); QName typeBaseQName = typeBase.getQName(); assertEquals("my-decimal-type", typeBaseQName.getLocalName()); - assertEquals("t", typeBaseQName.getPrefix()); - assertEquals(typesNS, typeBaseQName.getNamespace()); - assertEquals(typesRev, typeBaseQName.getRevision()); + assertEquals(barNS, typeBaseQName.getNamespace()); + assertEquals(barRev, typeBaseQName.getRevision()); assertNull(typeBase.getUnits()); assertNull(typeBase.getDefaultValue()); assertNull(typeBase.getFractionDigits()); @@ -454,15 +457,14 @@ public class YangParserTest { @Test public void testTypedefDecimal2() { - Module testModule = TestUtils.findModule(modules, "nodes"); - LeafSchemaNode testleaf = (LeafSchemaNode) testModule.getDataChildByName("decimal-leaf2"); + Module foo = TestUtils.findModule(modules, "foo"); + LeafSchemaNode testleaf = (LeafSchemaNode) foo.getDataChildByName("decimal-leaf2"); ExtendedType type = (ExtendedType) testleaf.getType(); QName typeQName = type.getQName(); assertEquals("my-decimal-type", typeQName.getLocalName()); - assertEquals("t", typeQName.getPrefix()); - assertEquals(typesNS, typeQName.getNamespace()); - assertEquals(typesRev, typeQName.getRevision()); + assertEquals(barNS, typeQName.getNamespace()); + assertEquals(barRev, typeQName.getRevision()); assertNull(type.getUnits()); assertNull(type.getDefaultValue()); assertNull(type.getFractionDigits()); @@ -476,15 +478,14 @@ public class YangParserTest { @Test public void testTypedefUnion() { - Module testModule = TestUtils.findModule(modules, "nodes"); - LeafSchemaNode unionleaf = (LeafSchemaNode) testModule.getDataChildByName("union-leaf"); + Module foo = TestUtils.findModule(modules, "foo"); + LeafSchemaNode unionleaf = (LeafSchemaNode) foo.getDataChildByName("union-leaf"); ExtendedType type = (ExtendedType) unionleaf.getType(); QName typeQName = type.getQName(); assertEquals("my-union-ext", typeQName.getLocalName()); - assertEquals("t", typeQName.getPrefix()); - assertEquals(typesNS, typeQName.getNamespace()); - assertEquals(typesRev, typeQName.getRevision()); + assertEquals(barNS, typeQName.getNamespace()); + assertEquals(barRev, typeQName.getRevision()); assertNull(type.getUnits()); assertNull(type.getDefaultValue()); assertNull(type.getFractionDigits()); @@ -495,9 +496,8 @@ public class YangParserTest { ExtendedType baseType = (ExtendedType) type.getBaseType(); QName baseTypeQName = baseType.getQName(); assertEquals("my-union", baseTypeQName.getLocalName()); - assertEquals("t", baseTypeQName.getPrefix()); - assertEquals(typesNS, baseTypeQName.getNamespace()); - assertEquals(typesRev, baseTypeQName.getRevision()); + assertEquals(barNS, baseTypeQName.getNamespace()); + assertEquals(barRev, baseTypeQName.getRevision()); assertNull(baseType.getUnits()); assertNull(baseType.getDefaultValue()); assertNull(baseType.getFractionDigits()); @@ -512,9 +512,8 @@ public class YangParserTest { ExtendedType unionType1 = (ExtendedType) unionTypes.get(0); QName unionType1QName = baseType.getQName(); assertEquals("my-union", unionType1QName.getLocalName()); - assertEquals("t", unionType1QName.getPrefix()); - assertEquals(typesNS, unionType1QName.getNamespace()); - assertEquals(typesRev, unionType1QName.getRevision()); + assertEquals(barNS, unionType1QName.getNamespace()); + assertEquals(barRev, unionType1QName.getRevision()); assertNull(unionType1.getUnits()); assertNull(unionType1.getDefaultValue()); assertNull(unionType1.getFractionDigits()); @@ -523,8 +522,8 @@ public class YangParserTest { List ranges = unionType1.getRangeConstraints(); assertEquals(1, ranges.size()); RangeConstraint range = ranges.get(0); - assertEquals(1L, range.getMin()); - assertEquals(100L, range.getMax()); + assertEquals(BigInteger.ONE, range.getMin()); + assertEquals(BigInteger.valueOf(100), range.getMax()); assertTrue(unionType1.getBaseType() instanceof Int16); assertTrue(unionTypes.get(1) instanceof Int32); @@ -532,14 +531,13 @@ public class YangParserTest { @Test public void testNestedUnionResolving() { - Module testModule = TestUtils.findModule(modules, "nodes"); - LeafSchemaNode testleaf = (LeafSchemaNode) testModule.getDataChildByName("custom-union-leaf"); + Module foo = TestUtils.findModule(modules, "foo"); + LeafSchemaNode testleaf = (LeafSchemaNode) foo.getDataChildByName("custom-union-leaf"); ExtendedType type = (ExtendedType) testleaf.getType(); QName testleafTypeQName = type.getQName(); - assertEquals(customNS, testleafTypeQName.getNamespace()); - assertEquals(customRev, testleafTypeQName.getRevision()); - assertEquals("c", testleafTypeQName.getPrefix()); + assertEquals(bazNS, testleafTypeQName.getNamespace()); + assertEquals(bazRev, testleafTypeQName.getRevision()); assertEquals("union1", testleafTypeQName.getLocalName()); assertNull(type.getUnits()); assertNull(type.getDefaultValue()); @@ -550,9 +548,8 @@ public class YangParserTest { ExtendedType typeBase = (ExtendedType) type.getBaseType(); QName typeBaseQName = typeBase.getQName(); - assertEquals(customNS, typeBaseQName.getNamespace()); - assertEquals(customRev, typeBaseQName.getRevision()); - assertEquals("c", typeBaseQName.getPrefix()); + assertEquals(bazNS, typeBaseQName.getNamespace()); + assertEquals(bazRev, typeBaseQName.getRevision()); assertEquals("union2", typeBaseQName.getLocalName()); assertNull(typeBase.getUnits()); assertNull(typeBase.getDefaultValue()); @@ -569,9 +566,8 @@ public class YangParserTest { ExtendedType unionType1 = (ExtendedType) unionTypes.get(1); QName uniontType1QName = unionType1.getQName(); - assertEquals(typesNS, uniontType1QName.getNamespace()); - assertEquals(typesRev, uniontType1QName.getRevision()); - assertEquals("t", uniontType1QName.getPrefix()); + assertEquals(barNS, uniontType1QName.getNamespace()); + assertEquals(barRev, uniontType1QName.getRevision()); assertEquals("nested-union2", uniontType1QName.getLocalName()); assertNull(unionType1.getUnits()); assertNull(unionType1.getDefaultValue()); @@ -588,9 +584,8 @@ public class YangParserTest { ExtendedType myUnionExt = (ExtendedType) nestedUnion2Types.get(1); QName myUnionExtQName = myUnionExt.getQName(); - assertEquals(typesNS, myUnionExtQName.getNamespace()); - assertEquals(typesRev, myUnionExtQName.getRevision()); - assertEquals("t", myUnionExtQName.getPrefix()); + assertEquals(barNS, myUnionExtQName.getNamespace()); + assertEquals(barRev, myUnionExtQName.getRevision()); assertEquals("my-union-ext", myUnionExtQName.getLocalName()); assertNull(myUnionExt.getUnits()); assertNull(myUnionExt.getDefaultValue()); @@ -601,9 +596,8 @@ public class YangParserTest { ExtendedType myUnion = (ExtendedType) myUnionExt.getBaseType(); QName myUnionQName = myUnion.getQName(); - assertEquals(typesNS, myUnionQName.getNamespace()); - assertEquals(typesRev, myUnionQName.getRevision()); - assertEquals("t", myUnionQName.getPrefix()); + assertEquals(barNS, myUnionQName.getNamespace()); + assertEquals(barRev, myUnionQName.getRevision()); assertEquals("my-union", myUnionQName.getLocalName()); assertNull(myUnion.getUnits()); assertNull(myUnion.getDefaultValue()); @@ -620,9 +614,8 @@ public class YangParserTest { ExtendedType int16Ext = (ExtendedType) myUnionBaseTypes.get(0); QName int16ExtQName = int16Ext.getQName(); - assertEquals(typesNS, int16ExtQName.getNamespace()); - assertEquals(typesRev, int16ExtQName.getRevision()); - assertEquals("t", int16ExtQName.getPrefix()); + assertEquals(barNS, int16ExtQName.getNamespace()); + assertEquals(barRev, int16ExtQName.getRevision()); assertEquals("int16", int16ExtQName.getLocalName()); assertNull(int16Ext.getUnits()); assertNull(int16Ext.getDefaultValue()); @@ -632,16 +625,16 @@ public class YangParserTest { List ranges = int16Ext.getRangeConstraints(); assertEquals(1, ranges.size()); RangeConstraint range = ranges.get(0); - assertEquals(1L, range.getMin()); - assertEquals(100L, range.getMax()); + assertEquals(BigInteger.ONE, range.getMin()); + assertEquals(BigInteger.valueOf(100), range.getMax()); assertTrue(int16Ext.getBaseType() instanceof Int16); } @Test public void testChoice() { - Module testModule = TestUtils.findModule(modules, "nodes"); - ContainerSchemaNode transfer = (ContainerSchemaNode) testModule.getDataChildByName("transfer"); + Module foo = TestUtils.findModule(modules, "foo"); + ContainerSchemaNode transfer = (ContainerSchemaNode) foo.getDataChildByName("transfer"); ChoiceNode how = (ChoiceNode) transfer.getDataChildByName("how"); Set cases = how.getCases(); assertEquals(5, cases.size()); @@ -662,17 +655,16 @@ public class YangParserTest { @Test public void testDeviation() { - Module testModule = TestUtils.findModule(modules, "nodes"); - Set deviations = testModule.getDeviations(); + Module foo = TestUtils.findModule(modules, "foo"); + Set deviations = foo.getDeviations(); assertEquals(1, deviations.size()); Deviation dev = deviations.iterator().next(); - assertEquals("system/user ref", dev.getReference()); - List path = new ArrayList(); - path.add(new QName(typesNS, typesRev, "t", "interfaces")); - path.add(new QName(typesNS, typesRev, "t", "ifEntry")); - SchemaPath expectedPath = new SchemaPath(path, true); + List path = new ArrayList<>(); + path.add(QName.create(barNS, barRev, "interfaces")); + path.add(QName.create(barNS, barRev, "ifEntry")); + SchemaPath expectedPath = SchemaPath.create(path, true); assertEquals(expectedPath, dev.getTargetPath()); assertEquals(Deviate.ADD, dev.getDeviate()); @@ -680,8 +672,8 @@ public class YangParserTest { @Test public void testUnknownNode() { - Module testModule = TestUtils.findModule(modules, "custom"); - ContainerSchemaNode network = (ContainerSchemaNode) testModule.getDataChildByName("network"); + Module baz = TestUtils.findModule(modules, "baz"); + ContainerSchemaNode network = (ContainerSchemaNode) baz.getDataChildByName("network"); List unknownNodes = network.getUnknownSchemaNodes(); assertEquals(1, unknownNodes.size()); UnknownSchemaNode unknownNode = unknownNodes.get(0); @@ -691,34 +683,36 @@ public class YangParserTest { @Test public void testFeature() { - Module testModule = TestUtils.findModule(modules, "custom"); - Set features = testModule.getFeatures(); + Module baz = TestUtils.findModule(modules, "baz"); + Set features = baz.getFeatures(); assertEquals(1, features.size()); } @Test public void testExtension() { - Module testModule = TestUtils.findModule(modules, "custom"); - List extensions = testModule.getExtensionSchemaNodes(); + Module baz = TestUtils.findModule(modules, "baz"); + List extensions = baz.getExtensionSchemaNodes(); assertEquals(1, extensions.size()); ExtensionDefinition extension = extensions.get(0); assertEquals("name", extension.getArgument()); + assertEquals("Takes as argument a name string. Makes the code generator use the given name in the #define.", + extension.getDescription()); assertTrue(extension.isYinElement()); } @Test public void testNotification() { - Module testModule = TestUtils.findModule(modules, "custom"); + Module baz = TestUtils.findModule(modules, "baz"); String expectedPrefix = "c"; - Set notifications = testModule.getNotifications(); + Set notifications = baz.getNotifications(); assertEquals(1, notifications.size()); NotificationDefinition notification = notifications.iterator().next(); // test SchemaNode args - QName expectedQName = new QName(customNS, customRev, expectedPrefix, "event"); + QName expectedQName = QName.create(bazNS, bazRev, "event"); assertEquals(expectedQName, notification.getQName()); - SchemaPath expectedPath = TestUtils.createPath(true, customNS, customRev, expectedPrefix, "event"); + SchemaPath expectedPath = TestUtils.createPath(true, bazNS, bazRev, expectedPrefix, "event"); assertEquals(expectedPath, notification.getPath()); assertNull(notification.getDescription()); assertNull(notification.getReference()); @@ -732,48 +726,40 @@ public class YangParserTest { LeafSchemaNode eventClass = (LeafSchemaNode) notification.getDataChildByName("event-class"); assertTrue(eventClass.getType() instanceof StringType); - AnyXmlSchemaNode reportingEntity = (AnyXmlSchemaNode) notification.getDataChildByName("reporting-entity"); - assertNotNull(reportingEntity); LeafSchemaNode severity = (LeafSchemaNode) notification.getDataChildByName("severity"); assertTrue(severity.getType() instanceof StringType); } @Test public void testRpc() { - Module testModule = TestUtils.findModule(modules, "custom"); + Module baz = TestUtils.findModule(modules, "baz"); - Set rpcs = testModule.getRpcs(); + Set rpcs = baz.getRpcs(); assertEquals(1, rpcs.size()); RpcDefinition rpc = rpcs.iterator().next(); assertEquals("Retrieve all or part of a specified configuration.", rpc.getDescription()); assertEquals("RFC 6241, Section 7.1", rpc.getReference()); - - ContainerSchemaNode input = rpc.getInput(); - assertNotNull(input.getDataChildByName("source")); - assertNotNull(input.getDataChildByName("filter")); - ContainerSchemaNode output = rpc.getOutput(); - assertNotNull(output.getDataChildByName("data")); } @Test public void testTypePath() throws ParseException { - Module test = TestUtils.findModule(modules, "types"); - Set> types = test.getTypeDefinitions(); + Module bar = TestUtils.findModule(modules, "bar"); + Set> types = bar.getTypeDefinitions(); // int32-ext1 ExtendedType int32ext1 = (ExtendedType) TestUtils.findTypedef(types, "int32-ext1"); QName int32TypedefQName = int32ext1.getQName(); - assertEquals(typesNS, int32TypedefQName.getNamespace()); - assertEquals(typesRev, int32TypedefQName.getRevision()); - assertEquals("t", int32TypedefQName.getPrefix()); + assertEquals(barNS, int32TypedefQName.getNamespace()); + assertEquals(barRev, int32TypedefQName.getRevision()); assertEquals("int32-ext1", int32TypedefQName.getLocalName()); SchemaPath typeSchemaPath = int32ext1.getPath(); - List typePath = typeSchemaPath.getPath(); - assertEquals(1, typePath.size()); - assertEquals(int32TypedefQName, typePath.get(0)); + Iterable typePath = typeSchemaPath.getPathFromRoot(); + Iterator typePathIt = typePath.iterator(); + assertEquals(int32TypedefQName, typePathIt.next()); + assertFalse(typePathIt.hasNext()); // int32-ext1/int32 Int32 int32 = (Int32) int32ext1.getBaseType(); @@ -782,22 +768,22 @@ public class YangParserTest { @Test public void testTypePath2() throws ParseException { - Module test = TestUtils.findModule(modules, "types"); - Set> types = test.getTypeDefinitions(); + Module bar = TestUtils.findModule(modules, "bar"); + Set> types = bar.getTypeDefinitions(); // my-decimal-type ExtendedType myDecType = (ExtendedType) TestUtils.findTypedef(types, "my-decimal-type"); QName myDecTypeQName = myDecType.getQName(); - assertEquals(typesNS, myDecTypeQName.getNamespace()); - assertEquals(typesRev, myDecTypeQName.getRevision()); - assertEquals("t", myDecTypeQName.getPrefix()); + assertEquals(barNS, myDecTypeQName.getNamespace()); + assertEquals(barRev, myDecTypeQName.getRevision()); assertEquals("my-decimal-type", myDecTypeQName.getLocalName()); SchemaPath typeSchemaPath = myDecType.getPath(); - List typePath = typeSchemaPath.getPath(); - assertEquals(1, typePath.size()); - assertEquals(myDecTypeQName, typePath.get(0)); + Iterable typePath = typeSchemaPath.getPathFromRoot(); + Iterator typePathIt = typePath.iterator(); + assertEquals(myDecTypeQName, typePathIt.next()); + assertFalse(typePathIt.hasNext()); // my-base-int32-type/int32 Decimal64 dec64 = (Decimal64) myDecType.getBaseType(); @@ -805,14 +791,193 @@ public class YangParserTest { assertEquals(URI.create("urn:ietf:params:xml:ns:yang:1"), dec64QName.getNamespace()); assertNull(dec64QName.getRevision()); - assertEquals("", dec64QName.getPrefix()); assertEquals("decimal64", dec64QName.getLocalName()); SchemaPath dec64SchemaPath = dec64.getPath(); - List dec64Path = dec64SchemaPath.getPath(); - assertEquals(2, dec64Path.size()); - assertEquals(myDecTypeQName, dec64Path.get(0)); - assertEquals(dec64QName, dec64Path.get(1)); + Iterable dec64Path = dec64SchemaPath.getPathFromRoot(); + Iterator dec64PathIt = dec64Path.iterator(); + assertEquals(myDecTypeQName, dec64PathIt.next()); + assertEquals(dec64QName, dec64PathIt.next()); + assertFalse(dec64PathIt.hasNext()); + } + + @Test + public void testParseMethod1() throws Exception { + File yangFile = new File(getClass().getResource("/parse-methods/m1.yang").toURI()); + File dependenciesDir = new File(getClass().getResource("/parse-methods").toURI()); + YangContextParser parser = new YangParserImpl(); + modules = parser.parseFile(yangFile, dependenciesDir).getModules(); + assertEquals(6, modules.size()); + } + + @Test + public void testParseMethod2() throws Exception { + File yangFile = new File(getClass().getResource("/parse-methods/m1.yang").toURI()); + File dependenciesDir = new File(getClass().getResource("/parse-methods/dependencies").toURI()); + YangContextParser parser = new YangParserImpl(); + modules = parser.parseFile(yangFile, dependenciesDir).getModules(); + assertEquals(6, modules.size()); + } + + @Test + public void testSorting() throws Exception { + // Correct order: m2, m4, m6, m8, m7, m6, m3, m1 + File yangFile = new File(getClass().getResource("/sorting-test/m1.yang").toURI()); + File dependenciesDir = new File(getClass().getResource("/sorting-test").toURI()); + YangContextParser parser = new YangParserImpl(); + modules = parser.parseFile(yangFile, dependenciesDir).getModules(); + SchemaContext ctx = new SchemaContextImpl(modules, Collections. emptyMap()); + checkOrder(modules); + assertSetEquals(modules, ctx.getModules()); + + // ########## + parser = new YangParserImpl(); + final File testDir = dependenciesDir; + final String[] fileList = testDir.list(); + final List testFiles = new ArrayList<>(); + if (fileList == null) { + throw new FileNotFoundException(dependenciesDir.getAbsolutePath()); + } + for (String fileName : fileList) { + testFiles.add(new File(testDir, fileName)); + } + Set newModules = parser.parseFiles(testFiles).getModules(); + assertSetEquals(newModules, modules); + ctx = new SchemaContextImpl(newModules, Collections. emptyMap()); + assertSetEquals(newModules, ctx.getModules()); + // ########## + newModules = parser.parseFiles(testFiles, null).getModules(); + assertSetEquals(newModules, modules); + ctx = new SchemaContextImpl(newModules, Collections. emptyMap()); + assertSetEquals(newModules, ctx.getModules()); + // ########## + List streams = new ArrayList<>(); + for (File f : testFiles) { + streams.add(new FileInputStream(f)); + } + newModules = parser.parseSources(BuilderUtils.filesToByteSources(testFiles)).getModules(); + assertSetEquals(newModules, modules); + ctx = new SchemaContextImpl(newModules, Collections. emptyMap()); + assertSetEquals(newModules, ctx.getModules()); + // ########## + streams.clear(); + for (File f : testFiles) { + streams.add(new FileInputStream(f)); + } + newModules = parser.parseSources(BuilderUtils.filesToByteSources(testFiles), null).getModules(); + assertSetEquals(newModules, modules); + ctx = new SchemaContextImpl(newModules, Collections. emptyMap()); + assertSetEquals(newModules, ctx.getModules()); + // ########## + Map mapped = parser.parseYangModelsMapped(testFiles); + newModules = new LinkedHashSet<>(mapped.values()); + assertSetEquals(newModules, modules); + ctx = new SchemaContextImpl(newModules, Collections. emptyMap()); + assertSetEquals(newModules, ctx.getModules()); + // ########## + streams.clear(); + for (File f : testFiles) { + streams.add(new FileInputStream(f)); + } + Map mappedStreams = parser.parseYangModelsFromStreamsMapped(streams); + newModules = new LinkedHashSet<>(mappedStreams.values()); + assertSetEquals(newModules, modules); + ctx = new SchemaContextImpl(newModules, Collections. emptyMap()); + assertSetEquals(newModules, ctx.getModules()); + } + + private void checkOrder(final Collection modules) { + Iterator it = modules.iterator(); + Module m = it.next(); + assertEquals("m2", m.getName()); + m = it.next(); + assertEquals("m4", m.getName()); + m = it.next(); + assertEquals("m6", m.getName()); + m = it.next(); + assertEquals("m8", m.getName()); + m = it.next(); + assertEquals("m7", m.getName()); + m = it.next(); + assertEquals("m5", m.getName()); + m = it.next(); + assertEquals("m3", m.getName()); + m = it.next(); + assertEquals("m1", m.getName()); + } + + private void assertSetEquals(final Set s1, final Set s2) { + assertEquals(s1, s2); + Iterator it = s1.iterator(); + for (Module m : s2) { + assertEquals(m, it.next()); + } + } + + @Test + public void testSubmodules() throws Exception { + Module foo = TestUtils.findModule(modules, "foo"); + + DataSchemaNode id = foo.getDataChildByName("id"); + assertNotNull(id); + DataSchemaNode subExt = foo.getDataChildByName("sub-ext"); + assertNotNull(subExt); + DataSchemaNode subTransfer = foo.getDataChildByName("sub-transfer"); + assertNotNull(subTransfer); + + assertEquals(2, foo.getExtensionSchemaNodes().size()); + assertEquals(2, foo.getAugmentations().size()); + } + + @Test + public void unknownStatementInSubmoduleHeaderTest() throws IOException, URISyntaxException { + + File yang = new File(getClass().getResource("/yang-grammar-test/submodule-header-extension.yang").toURI()); + + try { + YangParserImpl.getInstance().parseFile(yang, yang.getParentFile()); + } catch (YangSyntaxErrorException | YangParseException e) { + e.printStackTrace(); + fail("YangSyntaxErrorException or YangParseException should not be thrown"); + } + } + @Test + public void unknownStatementBetweenRevisionsTest() throws IOException, URISyntaxException { + + File yangModul = new File(getClass().getResource("/yang-grammar-test/revisions-extension.yang").toURI()); + File yangSubmodul = new File(getClass().getResource("/yang-grammar-test/submodule-header-extension.yang") + .toURI()); + + List yangs = new ArrayList(); + yangs.add(yangModul); + yangs.add(yangSubmodul); + + try { + YangParserImpl.getInstance().parseFiles(yangs); + } catch (YangParseException e) { + e.printStackTrace(); + fail("YangParseException should not be thrown"); + } + } + + @Test + public void unknownStatementsInStatementsTest() throws IOException, URISyntaxException { + File yangModule1 = new File(getClass().getResource("/yang-grammar-test/stmtsep-in-statements.yang").toURI()); + File yangModule2 = new File(getClass().getResource("/yang-grammar-test/stmtsep-in-statements2.yang").toURI()); + File yangSubModule = new File(getClass().getResource("/yang-grammar-test/stmtsep-in-statements-sub.yang").toURI()); + + List yangs = new ArrayList(); + yangs.add(yangModule1); + yangs.add(yangModule2); + yangs.add(yangSubModule); + + try { + YangParserImpl.getInstance().parseFiles(yangs); + } catch (YangParseException e) { + e.printStackTrace(); + fail("YangParseException should not be thrown"); + } + } }