Merge "BUG-997 Implement Filesystem source cache for schema repository"
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / impl / YangParserNegativeTest.java
index b66fb83e92d186742999bb1741faf27f7eed8b84..41d76c39972cd6792379edbdcc114ef909ade167 100644 (file)
@@ -7,26 +7,28 @@
  */
 package org.opendaylight.yangtools.yang.parser.impl;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
+import java.io.File;
 import java.io.FileInputStream;
-import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
-
 import org.junit.Test;
+import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
 import org.opendaylight.yangtools.yang.parser.util.YangValidationException;
 
 public class YangParserNegativeTest {
 
     @Test
-    public void testInvalidImport() throws IOException {
+    public void testInvalidImport() throws Exception {
+        File yang = new File(getClass().getResource("/negative-scenario/testfile1.yang").toURI());
         try {
-            try (InputStream stream = new FileInputStream(getClass().getResource("/negative-scenario/testfile1.yang")
-                    .getPath())) {
+            try (InputStream stream = new FileInputStream(yang)) {
                 TestUtils.loadModule(stream);
                 fail("ValidationException should by thrown");
             }
@@ -36,10 +38,10 @@ public class YangParserNegativeTest {
     }
 
     @Test
-    public void testTypeNotFound() throws IOException {
+    public void testTypeNotFound() throws Exception {
+        File yang = new File(getClass().getResource("/negative-scenario/testfile2.yang").toURI());
         try {
-            try (InputStream stream = new FileInputStream(getClass().getResource("/negative-scenario/testfile2.yang")
-                    .getPath())) {
+            try (InputStream stream = new FileInputStream(yang)) {
                 TestUtils.loadModule(stream);
                 fail("YangParseException should by thrown");
             }
@@ -49,14 +51,14 @@ public class YangParserNegativeTest {
     }
 
     @Test
-    public void testInvalidAugmentTarget() throws IOException {
+    public void testInvalidAugmentTarget() throws Exception {
+        File yang1 = new File(getClass().getResource("/negative-scenario/testfile0.yang").toURI());
+        File yang2 = new File(getClass().getResource("/negative-scenario/testfile3.yang").toURI());
         try {
             final List<InputStream> streams = new ArrayList<>(2);
-            try (InputStream testFile0 = new FileInputStream(getClass()
-                    .getResource("/negative-scenario/testfile0.yang").getPath())) {
+            try (InputStream testFile0 = new FileInputStream(yang1)) {
                 streams.add(testFile0);
-                try (InputStream testFile3 = new FileInputStream(getClass().getResource(
-                        "/negative-scenario/testfile3.yang").getPath())) {
+                try (InputStream testFile3 = new FileInputStream(yang2)) {
                     streams.add(testFile3);
                     assertEquals("Expected loaded files count is 2", 2, streams.size());
                     TestUtils.loadModules(streams);
@@ -64,16 +66,17 @@ public class YangParserNegativeTest {
                 }
             }
         } catch (YangParseException e) {
-            assertEquals("Error in module 'test3' at line 10: Error in augment parsing: failed to find augment target",
+            assertEquals(
+                    "Error in module 'test3' at line 10: Error in augment parsing: failed to find augment target: augment /data:unknown",
                     e.getMessage());
         }
     }
 
     @Test
-    public void testInvalidRefine() throws IOException {
+    public void testInvalidRefine() throws Exception {
+        File yang = new File(getClass().getResource("/negative-scenario/testfile4.yang").toURI());
         try {
-            try (InputStream stream = new FileInputStream(getClass().getResource("/negative-scenario/testfile4.yang")
-                    .getPath())) {
+            try (InputStream stream = new FileInputStream(yang)) {
                 TestUtils.loadModule(stream);
                 fail("YangParseException should by thrown");
             }
@@ -83,10 +86,10 @@ public class YangParserNegativeTest {
     }
 
     @Test
-    public void testInvalidLength() throws IOException {
+    public void testInvalidLength() throws Exception {
+        File yang = new File(getClass().getResource("/negative-scenario/testfile5.yang").toURI());
         try {
-            try (InputStream stream = new FileInputStream(getClass().getResource("/negative-scenario/testfile5.yang")
-                    .getPath())) {
+            try (InputStream stream = new FileInputStream(yang)) {
                 TestUtils.loadModule(stream);
                 fail("YangParseException should by thrown");
             }
@@ -96,10 +99,10 @@ public class YangParserNegativeTest {
     }
 
     @Test
-    public void testInvalidRange() throws IOException {
+    public void testInvalidRange() throws Exception {
+        File yang = new File(getClass().getResource("/negative-scenario/testfile6.yang").toURI());
         try {
-            try (InputStream stream = new FileInputStream(getClass().getResource("/negative-scenario/testfile6.yang")
-                    .getPath())) {
+            try (InputStream stream = new FileInputStream(yang)) {
                 TestUtils.loadModule(stream);
                 fail("YangParseException should by thrown");
             }
@@ -109,10 +112,10 @@ public class YangParserNegativeTest {
     }
 
     @Test
-    public void testDuplicateContainer() throws IOException {
+    public void testDuplicateContainer() throws Exception {
+        File yang = new File(getClass().getResource("/negative-scenario/duplicity/container.yang").toURI());
         try {
-            try (InputStream stream = new FileInputStream(getClass().getResource(
-                    "/negative-scenario/duplicity/container.yang").getPath())) {
+            try (InputStream stream = new FileInputStream(yang)) {
                 TestUtils.loadModule(stream);
                 fail("YangParseException should by thrown");
             }
@@ -123,10 +126,10 @@ public class YangParserNegativeTest {
     }
 
     @Test
-    public void testDuplicateContainerList() throws IOException {
+    public void testDuplicateContainerList() throws Exception {
+        File yang = new File(getClass().getResource("/negative-scenario/duplicity/container-list.yang").toURI());
         try {
-            try (InputStream stream = new FileInputStream(getClass().getResource(
-                    "/negative-scenario/duplicity/container-list.yang").getPath())) {
+            try (InputStream stream = new FileInputStream(yang)) {
                 TestUtils.loadModule(stream);
                 fail("YangParseException should by thrown");
             }
@@ -137,10 +140,10 @@ public class YangParserNegativeTest {
     }
 
     @Test
-    public void testDuplicateContainerLeaf() throws IOException {
+    public void testDuplicateContainerLeaf() throws Exception {
+        File yang = new File(getClass().getResource("/negative-scenario/duplicity/container-leaf.yang").toURI());
         try {
-            try (InputStream stream = new FileInputStream(getClass().getResource(
-                    "/negative-scenario/duplicity/container-leaf.yang").getPath())) {
+            try (InputStream stream = new FileInputStream(yang)) {
                 TestUtils.loadModule(stream);
                 fail("YangParseException should by thrown");
             }
@@ -151,10 +154,10 @@ public class YangParserNegativeTest {
     }
 
     @Test
-    public void testDuplicateTypedef() throws IOException {
+    public void testDuplicateTypedef() throws Exception {
+        File yang = new File(getClass().getResource("/negative-scenario/duplicity/typedef.yang").toURI());
         try {
-            try (InputStream stream = new FileInputStream(getClass().getResource(
-                    "/negative-scenario/duplicity/typedef.yang").getPath())) {
+            try (InputStream stream = new FileInputStream(yang)) {
                 TestUtils.loadModule(stream);
                 fail("YangParseException should by thrown");
             }
@@ -166,32 +169,88 @@ public class YangParserNegativeTest {
 
     @Test
     public void testDuplicityInAugmentTarget1() throws Exception {
+        File yang1 = new File(getClass().getResource("/negative-scenario/duplicity/augment0.yang").toURI());
+        File yang2 = new File(getClass().getResource("/negative-scenario/duplicity/augment1.yang").toURI());
         try {
-            try (InputStream stream1 = new FileInputStream(getClass().getResource(
-                    "/negative-scenario/duplicity/augment0.yang").getPath());
-                    InputStream stream2 = new FileInputStream(getClass().getResource(
-                            "/negative-scenario/duplicity/augment1.yang").getPath())) {
+            try (InputStream stream1 = new FileInputStream(yang1); InputStream stream2 = new FileInputStream(yang2)) {
                 TestUtils.loadModules(Arrays.asList(stream1, stream2));
                 fail("YangParseException should by thrown");
             }
         } catch (YangParseException e) {
-            String expected = "Error in module 'augment1' at line 10: Failed to perform augmentation: Error in module 'augment0' at line 8: Can not add 'leaf id' to 'container bar' in module 'augment0': node with same name already declared at line 9";
-            assertEquals(expected, e.getMessage());
+            assertTrue(e.getMessage().contains("Error in module 'augment1'"));
+            assertTrue(e.getMessage().contains("Failed to perform augmentation:"));
+
         }
     }
 
     @Test
     public void testDuplicityInAugmentTarget2() throws Exception {
+        File yang1 = new File(getClass().getResource("/negative-scenario/duplicity/augment0.yang").toURI());
+        File yang2 = new File(getClass().getResource("/negative-scenario/duplicity/augment2.yang").toURI());
         try {
-            try (InputStream stream1 = new FileInputStream(getClass().getResource(
-                    "/negative-scenario/duplicity/augment0.yang").getPath());
-                    InputStream stream2 = new FileInputStream(getClass().getResource(
-                            "/negative-scenario/duplicity/augment2.yang").getPath())) {
+            try (InputStream stream1 = new FileInputStream(yang1); InputStream stream2 = new FileInputStream(yang2)) {
                 TestUtils.loadModules(Arrays.asList(stream1, stream2));
                 fail("YangParseException should by thrown");
             }
         } catch (YangParseException e) {
-            String expected = "Error in module 'augment0' at line 17: Can not add 'anyxml delta' to node 'choice-ext' in module 'augment0': case with same name already declared at line 18";
+            assertTrue(e.getMessage().contains("Error in module "));
+            assertTrue(e.getMessage().contains("case with same name already declared "));
+        }
+    }
+
+    @Test
+    public void testMandatoryInAugment() throws Exception {
+        File yang1 = new File(getClass().getResource("/negative-scenario/testfile8.yang").toURI());
+        File yang2 = new File(getClass().getResource("/negative-scenario/testfile7.yang").toURI());
+        try {
+            try (InputStream stream1 = new FileInputStream(yang1); InputStream stream2 = new FileInputStream(yang2)) {
+                TestUtils.loadModules(Arrays.asList(stream1, stream2));
+                fail("YangParseException should by thrown");
+            }
+        } catch (YangParseException e) {
+            String expected = "Error in module 'testfile7' at line 18: Error in augment parsing: cannot augment mandatory node linkleaf";
+            assertEquals(expected, e.getMessage());
+        }
+    }
+
+    @Test
+    public void testWrongDependenciesDir() throws Exception {
+        try {
+            File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").toURI());
+            File dependenciesDir = new File("/invalid");
+            YangContextParser parser = new YangParserImpl();
+            parser.parseFile(yangFile, dependenciesDir);
+            fail("Exception should by thrown");
+        } catch (IllegalStateException e) {
+            String expected = File.separator + "invalid does not exists";
+            assertEquals(expected, e.getMessage());
+        }
+    }
+
+    @Test
+    public void testWrongDependenciesDir2() throws Exception {
+        try {
+            File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").toURI());
+            File dependenciesDir = new File(getClass().getResource("/model").toURI());
+            YangContextParser parser = new YangParserImpl();
+            parser.parseFile(yangFile, dependenciesDir);
+            fail("Exception should by thrown");
+        } catch (YangValidationException e) {
+            String expected = "Not existing module imported";
+            assertTrue(e.getMessage().contains(expected));
+        }
+    }
+
+    @Test
+    public void testInvalidListKeyDefinition() throws Exception {
+        File yang1 = new File(getClass().getResource("/negative-scenario/invalid-list-key-def.yang").toURI());
+        try {
+            try (InputStream stream1 = new FileInputStream(yang1)) {
+                TestUtils.loadModule(stream1);
+                fail("YangParseException should by thrown");
+            }
+        } catch (YangParseException e) {
+            String expected = "Error in module 'invalid-list-key-def' at line 6: Failed to resolve list key for name rib-id";
             assertEquals(expected, e.getMessage());
         }
     }