Address FIXME for QueuedNotificationManager
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / impl / YangParserTest.java
index 2868497487cdb70f6f46890c65c594cacf7dbb2a..a8da2ba4f207063f67ed99938e789cb6a3ac189d 100644 (file)
@@ -12,13 +12,15 @@ 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;
@@ -27,16 +29,14 @@ 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.AugmentationSchema;
 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
-import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
+import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
@@ -60,6 +60,7 @@ 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;
@@ -68,6 +69,7 @@ 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 {
     public static final String FS = File.separator;
@@ -129,7 +131,7 @@ public class YangParserTest {
 
         ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
         // test SchemaNode args
-        QName expectedQName = new QName(expectedNamespace, barRev, expectedPrefix, "ifEntry");
+        QName expectedQName = QName.create(expectedNamespace, barRev, "ifEntry");
         assertEquals(expectedQName, ifEntry.getQName());
         SchemaPath expectedPath = TestUtils.createPath(true, expectedNamespace, barRev, expectedPrefix, "interfaces",
                 "ifEntry");
@@ -144,7 +146,7 @@ public class YangParserTest {
         ConstraintDefinition constraints = ifEntry.getConstraints();
         assertNull(constraints.getWhenCondition());
         assertEquals(0, constraints.getMustConstraints().size());
-        assertFalse(constraints.isMandatory());
+        assertTrue(constraints.isMandatory());
         assertEquals(1, (int) constraints.getMinElements());
         assertEquals(11, (int) constraints.getMaxElements());
         // test AugmentationTarget args
@@ -152,7 +154,7 @@ public class YangParserTest {
         assertEquals(2, availableAugmentations.size());
         // test ListSchemaNode args
         List<QName> expectedKey = new ArrayList<>();
-        expectedKey.add(new QName(expectedNamespace, barRev, expectedPrefix, "ifIndex"));
+        expectedKey.add(QName.create(expectedNamespace, barRev, "ifIndex"));
         assertEquals(expectedKey, ifEntry.getKeyDefinition());
         assertFalse(ifEntry.isUserOrdered());
         // test DataNodeContainer args
@@ -239,7 +241,7 @@ public class YangParserTest {
         List<PatternConstraint> 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());
 
@@ -253,7 +255,7 @@ public class YangParserTest {
         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());
 
@@ -282,7 +284,7 @@ public class YangParserTest {
         patterns = baseType3.getPatternConstraints();
         assertEquals(1, patterns.size());
         pattern = patterns.iterator().next();
-        assertEquals("[a-k]*", pattern.getRegularExpression());
+        assertEquals("^[a-k]*$", pattern.getRegularExpression());
         List<LengthConstraint> baseType3Lengths = baseType3.getLengthConstraints();
         assertEquals(1, baseType3Lengths.size());
         length = baseType3Lengths.get(0);
@@ -293,6 +295,72 @@ public class YangParserTest {
         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<PatternConstraint> 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 foo = TestUtils.findModule(modules, "foo");
@@ -339,7 +407,7 @@ public class YangParserTest {
         List<PatternConstraint> patterns = baseType2.getPatternConstraints();
         assertEquals(1, patterns.size());
         PatternConstraint pattern = patterns.iterator().next();
-        assertEquals("[a-k]*", pattern.getRegularExpression());
+        assertEquals("^[a-k]*$", pattern.getRegularExpression());
         List<LengthConstraint> baseType3Lengths = baseType2.getLengthConstraints();
         assertEquals(1, baseType3Lengths.size());
         length = baseType3Lengths.get(0);
@@ -563,7 +631,7 @@ public class YangParserTest {
     public void testChoice() {
         Module foo = TestUtils.findModule(modules, "foo");
         ContainerSchemaNode transfer = (ContainerSchemaNode) foo.getDataChildByName("transfer");
-        ChoiceNode how = (ChoiceNode) transfer.getDataChildByName("how");
+        ChoiceSchemaNode how = (ChoiceSchemaNode) transfer.getDataChildByName("how");
         Set<ChoiceCaseNode> cases = how.getCases();
         assertEquals(5, cases.size());
         ChoiceCaseNode input = null;
@@ -590,8 +658,8 @@ public class YangParserTest {
         assertEquals("system/user ref", dev.getReference());
 
         List<QName> path = new ArrayList<>();
-        path.add(new QName(barNS, barRev, "br", "interfaces"));
-        path.add(new QName(barNS, barRev, "br", "ifEntry"));
+        path.add(QName.create(barNS, barRev, "interfaces"));
+        path.add(QName.create(barNS, barRev, "ifEntry"));
         SchemaPath expectedPath = SchemaPath.create(path, true);
 
         assertEquals(expectedPath, dev.getTargetPath());
@@ -638,7 +706,7 @@ public class YangParserTest {
 
         NotificationDefinition notification = notifications.iterator().next();
         // test SchemaNode args
-        QName expectedQName = new QName(bazNS, bazRev, expectedPrefix, "event");
+        QName expectedQName = QName.create(bazNS, bazRev, "event");
         assertEquals(expectedQName, notification.getQName());
         SchemaPath expectedPath = TestUtils.createPath(true, bazNS, bazRev, expectedPrefix, "event");
         assertEquals(expectedPath, notification.getPath());
@@ -754,7 +822,7 @@ public class YangParserTest {
         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.<ModuleIdentifier, String> emptyMap());
+        SchemaContext ctx = new SchemaContextImpl(modules, Collections.emptyMap());
         checkOrder(modules);
         assertSetEquals(modules, ctx.getModules());
 
@@ -771,12 +839,12 @@ public class YangParserTest {
         }
         Set<Module> newModules = parser.parseFiles(testFiles).getModules();
         assertSetEquals(newModules, modules);
-        ctx = new SchemaContextImpl(newModules, Collections.<ModuleIdentifier, String> emptyMap());
+        ctx = new SchemaContextImpl(newModules, Collections.emptyMap());
         assertSetEquals(newModules, ctx.getModules());
         // ##########
         newModules = parser.parseFiles(testFiles, null).getModules();
         assertSetEquals(newModules, modules);
-        ctx = new SchemaContextImpl(newModules, Collections.<ModuleIdentifier, String> emptyMap());
+        ctx = new SchemaContextImpl(newModules, Collections.emptyMap());
         assertSetEquals(newModules, ctx.getModules());
         // ##########
         List<InputStream> streams = new ArrayList<>();
@@ -785,7 +853,7 @@ public class YangParserTest {
         }
         newModules = parser.parseSources(BuilderUtils.filesToByteSources(testFiles)).getModules();
         assertSetEquals(newModules, modules);
-        ctx = new SchemaContextImpl(newModules, Collections.<ModuleIdentifier, String> emptyMap());
+        ctx = new SchemaContextImpl(newModules, Collections.emptyMap());
         assertSetEquals(newModules, ctx.getModules());
         // ##########
         streams.clear();
@@ -794,27 +862,11 @@ public class YangParserTest {
         }
         newModules = parser.parseSources(BuilderUtils.filesToByteSources(testFiles), null).getModules();
         assertSetEquals(newModules, modules);
-        ctx = new SchemaContextImpl(newModules, Collections.<ModuleIdentifier, String> emptyMap());
-        assertSetEquals(newModules, ctx.getModules());
-        // ##########
-        Map<File, Module> mapped = parser.parseYangModelsMapped(testFiles);
-        newModules = new LinkedHashSet<>(mapped.values());
-        assertSetEquals(newModules, modules);
-        ctx = new SchemaContextImpl(newModules, Collections.<ModuleIdentifier, String> emptyMap());
-        assertSetEquals(newModules, ctx.getModules());
-        // ##########
-        streams.clear();
-        for (File f : testFiles) {
-            streams.add(new FileInputStream(f));
-        }
-        Map<InputStream, Module> mappedStreams = parser.parseYangModelsFromStreamsMapped(streams);
-        newModules = new LinkedHashSet<>(mappedStreams.values());
-        assertSetEquals(newModules, modules);
-        ctx = new SchemaContextImpl(newModules, Collections.<ModuleIdentifier, String> emptyMap());
+        ctx = new SchemaContextImpl(newModules, Collections.emptyMap());
         assertSetEquals(newModules, ctx.getModules());
     }
 
-    private void checkOrder(final Collection<Module> modules) {
+    private static void checkOrder(final Collection<Module> modules) {
         Iterator<Module> it = modules.iterator();
         Module m = it.next();
         assertEquals("m2", m.getName());
@@ -834,7 +886,7 @@ public class YangParserTest {
         assertEquals("m1", m.getName());
     }
 
-    private void assertSetEquals(final Set<Module> s1, final Set<Module> s2) {
+    private static void assertSetEquals(final Set<Module> s1, final Set<Module> s2) {
         assertEquals(s1, s2);
         Iterator<Module> it = s1.iterator();
         for (Module m : s2) {
@@ -857,4 +909,55 @@ public class YangParserTest {
         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<File> 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<File> 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");
+        }
+    }
 }