Cleanup SubstatementValidatorTest 07/97707/2
authorRobert Varga <robert.varga@pantheon.tech>
Sat, 2 Oct 2021 15:35:18 +0000 (17:35 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Sun, 3 Oct 2021 18:47:54 +0000 (20:47 +0200)
We have a ton of missing asserts and try/catch checking, clean all of
that up.

Change-Id: I9a3fcc050656ddba33e375c84956cfdb2f7ca693
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/SubstatementValidatorTest.java
parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/TestUtils.java

index be3cc46eaea1471ce95f0160069f530f0d714ff2..9b5f9215fc03f5e27d759c6ee71946cda3d2ce34 100644 (file)
@@ -7,82 +7,66 @@
  */
 package org.opendaylight.yangtools.yang.stmt;
 
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.CoreMatchers.startsWith;
+import static org.hamcrest.MatcherAssert.assertThat;
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertThrows;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
 
-import java.io.ByteArrayOutputStream;
-import java.io.PrintStream;
-import java.io.UnsupportedEncodingException;
-import java.nio.charset.StandardCharsets;
-import java.util.Collection;
-import org.junit.After;
-import org.junit.Before;
 import org.junit.Test;
-import org.opendaylight.yangtools.yang.model.api.Module;
-import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
+import org.opendaylight.yangtools.yang.parser.spi.meta.InvalidSubstatementException;
+import org.opendaylight.yangtools.yang.parser.spi.meta.MissingSubstatementException;
 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
+import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
 
 public class SubstatementValidatorTest {
-    @SuppressWarnings("checkstyle:regexpSinglelineJava")
-    private final PrintStream stdout = System.out;
-    private final ByteArrayOutputStream output = new ByteArrayOutputStream();
-
-    @Before
-    public void setUp() throws UnsupportedEncodingException {
-        System.setOut(new PrintStream(output, true, StandardCharsets.UTF_8));
-    }
-
-    @After
-    public void cleanUp() {
-        System.setOut(stdout);
-    }
-
     @Test
     public void noException() throws Exception {
-        assertNotNull(TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-augment").toURI()));
+        assertEquals(3, TestUtils.loadModules("/augment-test/augment-in-augment").getModules().size());
     }
 
     @Test
     public void undesirableElementException() throws Exception {
-        try {
-            TestUtils.loadModules(getClass().getResource("/substatement-validator/undesirable-element").toURI());
-            fail("Unexpected success");
-        } catch (ReactorException ex) {
-            assertNotNull(ex.getCause());
-            assertTrue(ex.getCause().getMessage().contains("TYPE is not valid for REVISION"));
-        }
+        final var ex = assertThrows(SomeModifiersUnresolvedException.class,
+            () -> TestUtils.loadModules("/substatement-validator/undesirable-element"));
+        final var cause = ex.getCause();
+        assertThat(cause, instanceOf(InvalidSubstatementException.class));
+        assertThat(cause.getMessage(), startsWith("TYPE is not valid for REVISION. Error in module undesirable "
+            + "(QNameModule{ns=urn:opendaylight.undesirable, rev=2015-11-11}) [at "));
     }
 
     @Test
     public void maximalElementCountException() throws Exception {
-        try {
-            TestUtils.loadModules(getClass().getResource("/substatement-validator/maximal-element").toURI());
-            fail("Unexpected success");
-        } catch (ReactorException ex) {
-            assertNotNull(ex.getCause());
-            assertTrue(ex.getCause().getMessage().contains("Maximal count of DESCRIPTION for AUGMENT is 1"));
-        }
+        final var ex = assertThrows(SomeModifiersUnresolvedException.class,
+            () -> TestUtils.loadModules("/substatement-validator/maximal-element"));
+        final var cause = ex.getCause();
+        assertThat(cause, instanceOf(InvalidSubstatementException.class));
+        assertThat(cause.getMessage(), startsWith("Maximal count of DESCRIPTION for AUGMENT is 1, detected 2. Error in "
+            + "module baz (QNameModule{ns=urn:opendaylight.baz, rev=2015-11-11}) [at "));
     }
 
     @Test
     public void missingElementException() {
-        assertThrows(SomeModifiersUnresolvedException.class, () -> TestUtils.loadModules(
-            SubstatementValidatorTest.class.getResource("/substatement-validator/missing-element").toURI()));
+        final var ex = assertThrows(SomeModifiersUnresolvedException.class,
+            () -> TestUtils.loadModules("/substatement-validator/missing-element"));
+        final var cause = ex.getCause();
+        // FIXME: should be MissingSubstatementException?
+        assertThat(cause, instanceOf(SourceException.class));
+        assertThat(cause.getMessage(), startsWith("Missing prefix statement [at "));
     }
 
     @Test
     public void bug6173Test() throws Exception {
-        final Collection<? extends Module> loadModules = TestUtils.loadModules(getClass()
-            .getResource("/substatement-validator/empty-element").toURI()).getModules();
-        assertEquals(1, loadModules.size());
+        assertEquals(1, TestUtils.loadModules("/substatement-validator/empty-element").getModules().size());
     }
 
     @Test
     public void bug4310test() throws Exception {
-        assertThrows(SomeModifiersUnresolvedException.class, () -> TestUtils.loadModules(
-            SubstatementValidatorTest.class.getResource("/substatement-validator/bug-4310").toURI()));
+        final var ex = assertThrows(SomeModifiersUnresolvedException.class,
+            () -> TestUtils.loadModules("/substatement-validator/bug-4310"));
+        final var cause = ex.getCause();
+        assertThat(cause, instanceOf(MissingSubstatementException.class));
+        assertThat(cause.getMessage(), startsWith("TYPE is missing TYPE. Minimal count is 1. Error in module bug4310 "
+            + "(QNameModule{ns=urn:opendaylight.bug4310}) [at "));
     }
-}
\ No newline at end of file
+}
index fd55a88966bd7b6eecc5c14fdee44ad15f617550..98bbe2ec7e870664baf8a056f3fbc04aa48b45e4 100644 (file)
@@ -48,9 +48,14 @@ public final class TestUtils {
         // Hidden on purpose
     }
 
-    public static @NonNull List<StatementStreamSource> loadSources(final String yangFilesDirectoryPath)
+    public static @NonNull List<StatementStreamSource> loadSources(final String resourceDirectory)
             throws Exception {
-        final var files = new File(TestUtils.class.getResource(yangFilesDirectoryPath).toURI())
+        return loadSources(TestUtils.class, resourceDirectory);
+    }
+
+    public static @NonNull List<StatementStreamSource> loadSources(final Class<?> cls, final String resourceDirectory)
+            throws Exception {
+        final var files = new File(cls.getResource(resourceDirectory).toURI())
             .listFiles(StmtTestUtils.YANG_FILE_FILTER);
         final var sources = new ArrayList<StatementStreamSource>(files.length);
         for (var file : files) {
@@ -59,6 +64,15 @@ public final class TestUtils {
         return sources;
     }
 
+    public static EffectiveModelContext loadModules(final String resourceDirectory) throws Exception {
+        return loadModules(TestUtils.class, resourceDirectory);
+    }
+
+    public static EffectiveModelContext loadModules(final Class<?> cls, final String resourceDirectory)
+            throws Exception {
+        return loadModules(cls.getResource(resourceDirectory).toURI());
+    }
+
     public static EffectiveModelContext loadModules(final URI resourceDirectory)
             throws ReactorException, IOException, YangSyntaxErrorException {
         final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();