Remove deprecated Yin/YangStatementSourceImpl
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / YangParserNegativeTest.java
index 45c74ba6b8b036c1ca74d5cf83c35250d1efeaaf..dbc00013ddcf7e1349f843fd21faff35461ca9b7 100644 (file)
@@ -7,26 +7,21 @@
  */
 package org.opendaylight.yangtools.yang.stmt;
 
-import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
 import com.google.common.base.Throwables;
 import java.io.ByteArrayOutputStream;
-import java.io.File;
-import java.io.InputStream;
+import java.io.IOException;
 import java.io.PrintStream;
 import java.io.UnsupportedEncodingException;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
+import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
-import org.opendaylight.yangtools.yang.parser.util.NamedFileInputStream;
 
 public class YangParserNegativeTest {
 
@@ -45,13 +40,10 @@ public class YangParserNegativeTest {
     }
 
     @Test
-    public void testInvalidImport() throws Exception {
-        final File yang = new File(getClass().getResource("/negative-scenario/testfile1.yang").toURI());
+    public void testInvalidImport() throws IOException, ReactorException, YangSyntaxErrorException {
         try {
-            try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) {
-                TestUtils.loadModule(stream);
-                fail("SomeModifiersUnresolvedException should be thrown");
-            }
+            TestUtils.loadModuleResources(getClass(), "/negative-scenario/testfile1.yang");
+            fail("SomeModifiersUnresolvedException should be thrown");
         } catch (final SomeModifiersUnresolvedException e) {
             final Throwable rootCause = Throwables.getRootCause(e);
             assertTrue(rootCause instanceof InferenceException);
@@ -61,13 +53,10 @@ public class YangParserNegativeTest {
     }
 
     @Test
-    public void testTypeNotFound() throws Exception {
-        final File yang = new File(getClass().getResource("/negative-scenario/testfile2.yang").toURI());
+    public void testTypeNotFound() throws IOException, ReactorException, YangSyntaxErrorException {
         try {
-            try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) {
-                TestUtils.loadModule(stream);
-                fail("InferenceException should be thrown");
-            }
+            TestUtils.loadModuleResources(getClass(), "/negative-scenario/testfile2.yang");
+            fail("InferenceException should be thrown");
         } catch (final SomeModifiersUnresolvedException e) {
             final Throwable rootCause = Throwables.getRootCause(e);
             assertTrue(rootCause instanceof InferenceException);
@@ -77,20 +66,12 @@ public class YangParserNegativeTest {
     }
 
     @Test
-    public void testInvalidAugmentTarget() throws Exception {
-        final File yang1 = new File(getClass().getResource("/negative-scenario/testfile0.yang").toURI());
-        final File yang2 = new File(getClass().getResource("/negative-scenario/testfile3.yang").toURI());
+    public void testInvalidAugmentTarget() throws IOException, ReactorException, YangSyntaxErrorException {
         try {
-            final List<InputStream> streams = new ArrayList<>(2);
-            try (InputStream testFile0 = new NamedFileInputStream(yang1, yang1.getPath())) {
-                streams.add(testFile0);
-                try (InputStream testFile3 = new NamedFileInputStream(yang2, yang2.getPath())) {
-                    streams.add(testFile3);
-                    assertEquals("Expected loaded files count is 2", 2, streams.size());
-                    TestUtils.loadModules(streams);
-                    fail("SomeModifiersUnresolvedException should be thrown");
-                }
-            }
+            TestUtils.loadModuleResources(getClass(),
+                "/negative-scenario/testfile0.yang",
+                "/negative-scenario/testfile3.yang");
+            fail("SomeModifiersUnresolvedException should be thrown");
         } catch (final SomeModifiersUnresolvedException e) {
             final Throwable rootCause = Throwables.getRootCause(e);
             assertTrue(rootCause instanceof InferenceException);
@@ -100,13 +81,10 @@ public class YangParserNegativeTest {
     }
 
     @Test
-    public void testInvalidRefine() throws Exception {
-        final File yang = new File(getClass().getResource("/negative-scenario/testfile4.yang").toURI());
+    public void testInvalidRefine() throws IOException, ReactorException, YangSyntaxErrorException {
         try {
-            try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) {
-                TestUtils.loadModule(stream);
-                fail("SourceException should be thrown");
-            }
+            TestUtils.loadModuleResources(getClass(), "/negative-scenario/testfile4.yang");
+            fail("ReactorException should be thrown");
         } catch (final ReactorException e) {
             assertTrue(e.getCause().getMessage().contains("Error in module 'test4' in the refine of uses " +
                     "'Relative{path=[(urn:simple.container.demo?revision=1970-01-01)node]}': can not perform refine of 'PRESENCE' for" +
@@ -115,39 +93,30 @@ public class YangParserNegativeTest {
     }
 
     @Test
-    public void testInvalidLength() throws Exception {
-        final File yang = new File(getClass().getResource("/negative-scenario/testfile5.yang").toURI());
+    public void testInvalidLength() throws IOException, YangSyntaxErrorException {
         try {
-            try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) {
-                TestUtils.loadModule(stream);
-                fail("YangParseException should be thrown");
-            }
+            TestUtils.loadModuleResources(getClass(), "/negative-scenario/testfile5.yang");
+            fail("ReactorException should be thrown");
         } catch (final ReactorException e) {
             assertTrue(e.getCause().getMessage().contains("Invalid length constraint: <4, 10>"));
         }
     }
 
     @Test
-    public void testInvalidRange() throws Exception {
-        final File yang = new File(getClass().getResource("/negative-scenario/testfile6.yang").toURI());
+    public void testInvalidRange() throws IOException, YangSyntaxErrorException {
         try {
-            try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) {
-                TestUtils.loadModule(stream);
-                fail("Exception should be thrown");
-            }
+            TestUtils.loadModuleResources(getClass(), "/negative-scenario/testfile6.yang");
+            fail("ReactorException should be thrown");
         } catch (final ReactorException e) {
             assertTrue(e.getCause().getMessage().contains("Invalid range constraint: <5, 20>"));
         }
     }
 
     @Test
-    public void testDuplicateContainer() throws Exception {
-        final File yang = new File(getClass().getResource("/negative-scenario/duplicity/container.yang").toURI());
+    public void testDuplicateContainer() throws IOException, YangSyntaxErrorException {
         try {
-            try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) {
-                TestUtils.loadModule(stream);
-                fail("SourceException should be thrown");
-            }
+            TestUtils.loadModuleResources(getClass(), "/negative-scenario/duplicity/container.yang");
+            fail("SourceException should be thrown");
         } catch (final ReactorException e) {
             final String expected = "Error in module 'container': cannot add '(urn:simple.container" +
                     ".demo?revision=1970-01-01)foo'. Node name collision: '(urn:simple.container" +
@@ -157,13 +126,10 @@ public class YangParserNegativeTest {
     }
 
     @Test
-    public void testDuplicateContainerList() throws Exception {
-        final File yang = new File(getClass().getResource("/negative-scenario/duplicity/container-list.yang").toURI());
+    public void testDuplicateContainerList() throws IOException, YangSyntaxErrorException {
         try {
-            try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) {
-                TestUtils.loadModule(stream);
-                fail("SourceException should be thrown");
-            }
+            TestUtils.loadModuleResources(getClass(), "/negative-scenario/duplicity/container-list.yang");
+            fail("SourceException should be thrown");
         } catch (final ReactorException e) {
             final String expected = "Error in module 'container-list': cannot add '(urn:simple.container" +
                     ".demo?revision=1970-01-01)foo'. Node name collision: '(urn:simple.container" +
@@ -173,13 +139,10 @@ public class YangParserNegativeTest {
     }
 
     @Test
-    public void testDuplicateContainerLeaf() throws Exception {
-        final File yang = new File(getClass().getResource("/negative-scenario/duplicity/container-leaf.yang").toURI());
+    public void testDuplicateContainerLeaf() throws IOException, YangSyntaxErrorException {
         try {
-            try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) {
-                TestUtils.loadModule(stream);
-                fail("SourceException should be thrown");
-            }
+            TestUtils.loadModuleResources(getClass(), "/negative-scenario/duplicity/container-leaf.yang");
+            fail("SourceException should be thrown");
         } catch (final ReactorException e) {
             final String expected = "Error in module 'container-leaf': cannot add '(urn:simple.container" +
                     ".demo?revision=1970-01-01)foo'. Node name collision: '(urn:simple.container" +
@@ -189,13 +152,10 @@ public class YangParserNegativeTest {
     }
 
     @Test
-    public void testDuplicateTypedef() throws Exception {
-        final File yang = new File(getClass().getResource("/negative-scenario/duplicity/typedef.yang").toURI());
+    public void testDuplicateTypedef() throws IOException, YangSyntaxErrorException {
         try {
-            try (InputStream stream = new NamedFileInputStream(yang, yang.getPath())) {
-                TestUtils.loadModule(stream);
-                fail("SourceException should be thrown");
-            }
+            TestUtils.loadModuleResources(getClass(), "/negative-scenario/duplicity/typedef.yang");
+            fail("SourceException should be thrown");
         } catch (final ReactorException e) {
             assertTrue(e.getCause().getMessage().startsWith(
                 "Duplicate name for typedef (urn:simple.container.demo?revision=1970-01-01)int-ext [at"));
@@ -203,55 +163,42 @@ public class YangParserNegativeTest {
     }
 
     @Test
-    public void testDuplicityInAugmentTarget1() throws Exception {
-        final File yang1 = new File(getClass().getResource("/negative-scenario/duplicity/augment0.yang").toURI());
-        final File yang2 = new File(getClass().getResource("/negative-scenario/duplicity/augment1.yang").toURI());
-        try (InputStream stream1 = new NamedFileInputStream(yang1, yang1.getPath());
-            InputStream stream2 = new NamedFileInputStream(yang2, yang2.getPath())) {
-            TestUtils.loadModules(Arrays.asList(stream1, stream2));
-            testLog = output.toString();
-            assertTrue(testLog.contains("An augment cannot add node named 'id' because this name is already used in target"));
-        }
+    public void testDuplicityInAugmentTarget1() throws IOException, ReactorException, YangSyntaxErrorException {
+        TestUtils.loadModuleResources(getClass(),
+            "/negative-scenario/duplicity/augment0.yang",
+                "/negative-scenario/duplicity/augment1.yang");
+        testLog = output.toString();
+        assertTrue(testLog.contains("An augment cannot add node named 'id' because this name is already used in target"));
     }
 
     @Test
-    public void testDuplicityInAugmentTarget2() throws Exception {
-        final File yang1 = new File(getClass().getResource("/negative-scenario/duplicity/augment0.yang").toURI());
-        final File yang2 = new File(getClass().getResource("/negative-scenario/duplicity/augment2.yang").toURI());
-        try (InputStream stream1 = new NamedFileInputStream(yang1, yang1.getPath());
-             InputStream stream2 = new NamedFileInputStream(yang2, yang2.getPath())) {
-            TestUtils.loadModules(Arrays.asList(stream1, stream2));
-            testLog = output.toString();
-            assertTrue(testLog.contains("An augment cannot add node named 'delta' because this name is already used in target"));
-        }
+    public void testDuplicityInAugmentTarget2() throws IOException, ReactorException, YangSyntaxErrorException {
+        TestUtils.loadModuleResources(getClass(),
+            "/negative-scenario/duplicity/augment0.yang",
+                "/negative-scenario/duplicity/augment2.yang");
+        testLog = output.toString();
+        assertTrue(testLog.contains("An augment cannot add node named 'delta' because this name is already used in target"));
     }
 
     @Test
-    public void testMandatoryInAugment() throws Exception {
-        final File yang1 = new File(getClass().getResource("/negative-scenario/testfile8.yang").toURI());
-        final File yang2 = new File(getClass().getResource("/negative-scenario/testfile7.yang").toURI());
-        try (InputStream stream1 = new NamedFileInputStream(yang1, yang1.getPath());
-             InputStream stream2 = new NamedFileInputStream(yang2, yang2.getPath())) {
-            TestUtils.loadModules(Arrays.asList(stream1, stream2));
-            testLog = output.toString();
-            assertTrue(testLog.contains(
-                    "An augment cannot add node 'linkleaf' because it is mandatory and in module different than target"));
-        }
+    public void testMandatoryInAugment() throws IOException, ReactorException, YangSyntaxErrorException {
+        TestUtils.loadModuleResources(getClass(),
+            "/negative-scenario/testfile8.yang",
+                "/negative-scenario/testfile7.yang");
+        testLog = output.toString();
+        assertTrue(testLog.contains(
+            "An augment cannot add node 'linkleaf' because it is mandatory and in module different than target"));
     }
 
     @Test
-    public void testInvalidListKeyDefinition() throws Exception {
-        final File yang1 = new File(getClass().getResource("/negative-scenario/invalid-list-key-def.yang").toURI());
+    public void testInvalidListKeyDefinition() throws IOException, YangSyntaxErrorException {
         try {
-            try (InputStream stream1 = new NamedFileInputStream(yang1, yang1.getPath())) {
-                TestUtils.loadModule(stream1);
-                fail("InferenceException should be thrown");
-            }
+            TestUtils.loadModuleResources(getClass(), "/negative-scenario/invalid-list-key-def.yang");
+            fail("InferenceException should be thrown");
         } catch (final ReactorException e) {
-            final String expected = "Key 'rib-id' misses node 'rib-id' in list '(invalid:list:key:def?revision=1970-01-01)" +
-                    "application-map'";
+            final String expected = "Key 'rib-id' misses node 'rib-id' in list "
+                    + "'(invalid:list:key:def?revision=1970-01-01)application-map'";
             assertTrue(e.getCause().getMessage().startsWith(expected));
         }
     }
-
 }