Minor code refactoring and improvements.
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / test / java / org / opendaylight / controller / yang / parser / impl / YangParserNegativeTest.java
index be44c6ff98f991df55acfaa1188eb5f10559fbb6..fb73bd6e5b7eb6937ce95e9fd70bb4450b8bc955 100644 (file)
@@ -9,7 +9,11 @@ package org.opendaylight.controller.yang.parser.impl;
 
 import static org.junit.Assert.*;
 
+import java.io.FileInputStream;
 import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
 
 import org.junit.Test;
 import org.opendaylight.controller.yang.parser.util.YangParseException;
@@ -20,9 +24,12 @@ public class YangParserNegativeTest {
     @Test
     public void testInvalidImport() throws IOException {
         try {
-            TestUtils.loadModule("/negative-scenario/testfile1.yang");
-            fail("ValidationException should by thrown");
-        } catch(YangValidationException e) {
+            try (InputStream stream = new FileInputStream(getClass().getResource("/negative-scenario/testfile1.yang")
+                    .getPath())) {
+                TestUtils.loadModule(stream);
+                fail("ValidationException should by thrown");
+            }
+        } catch (YangValidationException e) {
             assertTrue(e.getMessage().contains("Not existing module imported"));
         }
     }
@@ -30,19 +37,33 @@ public class YangParserNegativeTest {
     @Test
     public void testTypeNotFound() throws IOException {
         try {
-            TestUtils.loadModule("/negative-scenario/testfile2.yang");
-            fail("YangParseException should by thrown");
-        } catch(YangParseException e) {
-            assertTrue(e.getMessage().contains("Error in module 'test2' on line 24: Referenced type 'int-ext' not found."));
+            try (InputStream stream = new FileInputStream(getClass().getResource("/negative-scenario/testfile2.yang")
+                    .getPath())) {
+                TestUtils.loadModule(stream);
+                fail("YangParseException should by thrown");
+            }
+        } catch (YangParseException e) {
+            assertTrue(e.getMessage().contains(
+                    "Error in module 'test2' on line 24: Referenced type 'int-ext' not found."));
         }
     }
 
     @Test
     public void testInvalidAugmentTarget() throws IOException {
         try {
-            TestUtils.loadModules("/negative-scenario/testfile0.yang", "/negative-scenario/testfile3.yang");
-            fail("YangParseException should by thrown");
-        } catch(YangParseException e) {
+            final List<InputStream> streams = new ArrayList<>(2);
+            try (InputStream testFile0 = new FileInputStream(getClass()
+                    .getResource("/negative-scenario/testfile0.yang").getPath())) {
+                streams.add(testFile0);
+                try (InputStream testFile3 = new FileInputStream(getClass().getResource(
+                        "/negative-scenario/testfile3.yang").getPath())) {
+                    streams.add(testFile3);
+                    assertEquals("Expected loaded files count is 2", 2, streams.size());
+                    TestUtils.loadModules(streams);
+                    fail("YangParseException should by thrown");
+                }
+            }
+        } catch (YangParseException e) {
             assertTrue(e.getMessage().contains("Failed to resolve augments in module 'test3'."));
         }
     }
@@ -50,11 +71,95 @@ public class YangParserNegativeTest {
     @Test
     public void testInvalidRefine() throws IOException {
         try {
-            TestUtils.loadModule("/negative-scenario/testfile4.yang");
-            fail("YangParseException should by thrown");
-        } catch(YangParseException e) {
+            try (InputStream stream = new FileInputStream(getClass().getResource("/negative-scenario/testfile4.yang")
+                    .getPath())) {
+                TestUtils.loadModule(stream);
+                fail("YangParseException should by thrown");
+            }
+        } catch (YangParseException e) {
             assertTrue(e.getMessage().contains("Can not refine 'presence' for 'node'."));
         }
     }
 
+    @Test
+    public void testInvalidLength() throws IOException {
+        try {
+            try (InputStream stream = new FileInputStream(getClass().getResource("/negative-scenario/testfile5.yang")
+                    .getPath())) {
+                TestUtils.loadModule(stream);
+                fail("YangParseException should by thrown");
+            }
+        } catch (YangParseException e) {
+            assertTrue(e.getMessage().contains("Invalid length constraint: <4, 10>"));
+        }
+    }
+
+    @Test
+    public void testInvalidRange() throws IOException {
+        try {
+            try (InputStream stream = new FileInputStream(getClass().getResource("/negative-scenario/testfile6.yang")
+                    .getPath())) {
+                TestUtils.loadModule(stream);
+                fail("YangParseException should by thrown");
+            }
+        } catch (YangParseException e) {
+            assertTrue(e.getMessage().contains("Invalid range constraint: <5, 20>"));
+        }
+    }
+
+    @Test
+    public void testDuplicateContainer() throws IOException {
+        try {
+            try (InputStream stream = new FileInputStream(getClass().getResource(
+                    "/negative-scenario/duplicity/container.yang").getPath())) {
+                TestUtils.loadModule(stream);
+                fail("YangParseException should by thrown");
+            }
+        } catch (YangParseException e) {
+            assertTrue(e.getMessage()
+                    .contains("Error in module 'container' on line 10: Duplicate node found at line 6"));
+        }
+    }
+
+    @Test
+    public void testDuplicateContainerList() throws IOException {
+        try {
+            try (InputStream stream = new FileInputStream(getClass().getResource(
+                    "/negative-scenario/duplicity/container-list.yang").getPath())) {
+                TestUtils.loadModule(stream);
+                fail("YangParseException should by thrown");
+            }
+        } catch (YangParseException e) {
+            assertTrue(e.getMessage().contains(
+                    "Error in module 'container-list' on line 10: Duplicate node found at line 6"));
+        }
+    }
+
+    @Test
+    public void testDuplicateContainerLeaf() throws IOException {
+        try {
+            try (InputStream stream = new FileInputStream(getClass().getResource(
+                    "/negative-scenario/duplicity/container-leaf.yang").getPath())) {
+                TestUtils.loadModule(stream);
+                fail("YangParseException should by thrown");
+            }
+        } catch (YangParseException e) {
+            assertTrue(e.getMessage().contains(
+                    "Error in module 'container-leaf' on line 10: Duplicate node found at line 6"));
+        }
+    }
+
+    @Test
+    public void testDuplicateTypedef() throws IOException {
+        try {
+            try (InputStream stream = new FileInputStream(getClass().getResource(
+                    "/negative-scenario/duplicity/typedef.yang").getPath())) {
+                TestUtils.loadModule(stream);
+                fail("YangParseException should by thrown");
+            }
+        } catch (YangParseException e) {
+            assertTrue(e.getMessage().contains("Error in module 'typedef' on line 10: Duplicate node found at line 6"));
+        }
+    }
+
 }