Do not mask single exceptions 85/99185/3
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 3 Jan 2022 12:54:16 +0000 (13:54 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 4 Jan 2022 13:11:30 +0000 (14:11 +0100)
If we end up with a single exception, do not wrap it in
InferenceException, as we want to correctly identify whether it is a
problem with the source or inference.

Change-Id: I9963ec5067145b5886a2bf20cba4bd8104794afe
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
parser/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/SourceSpecificContext.java
parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/AugmentToExtensionTest.java
parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug6240Test.java
parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug7480Test.java
parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/DeviationResolutionTest.java
parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/openconfigver/OpenconfigVersionBorderCaseTest.java
parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/openconfigver/OpenconfigVersionDefaultsTest.java
parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/openconfigver/OpenconfigVersionImportTest.java
parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/openconfigver/OpenconfigVersionMultipleImportTest.java
parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/openconfigver/OpenconfigVersionTest.java
parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/openconfigver/yin/YinOpenconfigVersionTest.java

index 0d1b58214025541f9257f342aee06daa7dd69ab6..d34b4e19af1e6e313f630a9aa25da07f97ebeb97 100644 (file)
@@ -386,15 +386,17 @@ final class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeha
             }
         }
 
-        if (exceptions.isEmpty()) {
-            return Optional.empty();
+        switch (exceptions.size()) {
+            case 0:
+                return Optional.empty();
+            case 1:
+                return Optional.of(exceptions.get(0));
+            default:
+                final String message = String.format("Yang model processing phase %s failed", identifier);
+                final InferenceException ex = new InferenceException(message, root, exceptions.get(0));
+                exceptions.listIterator(1).forEachRemaining(ex::addSuppressed);
+                return Optional.of(ex);
         }
-
-        final String message = String.format("Yang model processing phase %s failed", identifier);
-        final InferenceException e = new InferenceException(message, root, exceptions.get(0));
-        exceptions.listIterator(1).forEachRemaining(e::addSuppressed);
-
-        return Optional.of(e);
     }
 
     void loadStatements() {
index bf8edd5e59426a7eaa0d349e40e2438b73938f69..c9e9392f0f15bec0f7c3d375afa022af90e1cf3c 100644 (file)
@@ -7,9 +7,7 @@
  */
 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.assertTrue;
 
 import org.junit.Test;
@@ -18,18 +16,12 @@ import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.UsesNode;
-import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
 
 public class AugmentToExtensionTest extends AbstractYangTest {
     @Test
     public void testIncorrectPath() throws Exception {
-        // FIXME: this should not be here, or why do we need encapsulation? Add asserts for that
-        final var cause = assertInferenceExceptionDir("/augment-to-extension-test/incorrect-path",
-            startsWith("Yang model processing phase EFFECTIVE_MODEL failed [at "));
-
-        final var firstCause = cause.getCause();
-        assertThat(firstCause, instanceOf(InferenceException.class));
-        assertThat(firstCause.getMessage(), startsWith("Augment target "
+        assertInferenceExceptionDir("/augment-to-extension-test/incorrect-path",
+            startsWith("Augment target "
             + "'Descendant{qnames=[(uri:augment-module?revision=2014-10-07)my-extension-name-a, input]}'"
             + " not found [at "));
     }
index e10dcfc724025f80e2ef114de76cf2d6ce534d66..f44361f384a8770ac9d1cc52e62b0baf183e2639 100644 (file)
@@ -7,29 +7,30 @@
  */
 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.assertTrue;
+import static org.junit.Assert.assertThrows;
 
-import java.util.Collection;
 import org.junit.Test;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.Module;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
+import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
 
-public class Bug6240Test {
+public class Bug6240Test extends AbstractYangTest {
     private static final String NS = "bar";
     private static final String REV = "2016-07-19";
 
     @Test
     public void testModels() throws Exception {
-        final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug6240/correct");
-        assertNotNull(context);
+        final var context = assertEffectiveModelDir("/bugs/bug6240/correct");
 
-        final Collection<? extends Module> modules = context.getModules();
+        final var modules = context.getModules();
         assertEquals(2, modules.size());
 
         Module bar = null;
@@ -41,27 +42,28 @@ public class Bug6240Test {
         }
 
         assertNotNull(bar);
-        assertTrue(bar.getDataChildByName(QName.create(NS, REV, "foo-grp-con")) instanceof ContainerSchemaNode);
-        assertTrue(bar.getDataChildByName(QName.create(NS, REV, "sub-foo-grp-con")) instanceof ContainerSchemaNode);
+        assertThat(bar.getDataChildByName(QName.create(NS, REV, "foo-grp-con")), instanceOf(ContainerSchemaNode.class));
+        assertThat(bar.getDataChildByName(QName.create(NS, REV, "sub-foo-grp-con")),
+            instanceOf(ContainerSchemaNode.class));
 
         assertEquals(1, bar.getSubmodules().size());
 
         final DataSchemaNode dataChildByName = bar.getDataChildByName(QName.create(NS, REV, "sub-bar-con"));
-        assertTrue(dataChildByName instanceof ContainerSchemaNode);
+        assertThat(dataChildByName, instanceOf(ContainerSchemaNode.class));
         final ContainerSchemaNode subBarCon = (ContainerSchemaNode) dataChildByName;
 
-        assertTrue(subBarCon.getDataChildByName(QName.create(NS, REV, "foo-grp-con")) instanceof ContainerSchemaNode);
-        assertTrue(subBarCon.getDataChildByName(QName.create(NS, REV, "sub-foo-grp-con"))
-            instanceof ContainerSchemaNode);
+        assertThat(subBarCon.getDataChildByName(QName.create(NS, REV, "foo-grp-con")),
+            instanceOf(ContainerSchemaNode.class));
+        assertThat(subBarCon.getDataChildByName(QName.create(NS, REV, "sub-foo-grp-con")),
+            instanceOf(ContainerSchemaNode.class));
     }
 
     @Test
-    public void testInvalidModels() throws Exception {
-        try {
-            StmtTestUtils.parseYangSources("/bugs/bug6240/incorrect");
-        } catch (final SomeModifiersUnresolvedException e) {
-            assertTrue(e.getCause().getCause().getMessage().startsWith(
-                "Grouping '(bar?revision=2016-07-19)foo-imp-grp' was not resolved."));
-        }
+    public void testInvalidModels() {
+        final var ex = assertThrows(SomeModifiersUnresolvedException.class,
+            () -> StmtTestUtils.parseYangSources("/bugs/bug6240/incorrect"));
+        final var cause = ex.getCause();
+        assertThat(cause, instanceOf(SourceException.class));
+        assertThat(cause.getMessage(), startsWith("Grouping '(bar?revision=2016-07-19)foo-imp-grp' was not resolved."));
     }
 }
index bea668a07d1c488ce580ed5dd66e8bb424cb401a..f2224c3c868348341e0d9923d3d1338a0a064f5f 100644 (file)
@@ -54,7 +54,7 @@ public class Bug7480Test {
         final var ex = assertThrows(SomeModifiersUnresolvedException.class,
             () -> parseYangSources("/bugs/bug7480/files-2", "/bugs/bug7480/lib-2"));
         final String message = ex.getSuppressed().length > 0
-            ? ex.getSuppressed()[0].getCause().getMessage() : ex.getCause().getCause().getMessage();
+            ? ex.getSuppressed()[0].getMessage() : ex.getCause().getMessage();
         assertThat(message, startsWith("Imported module [missing-lib] was not found."));
     }
 
index 954b74c878ca6a8c8501db364dca1ecf0b3f8d38..648f644f36a0158fbe20cba11bb558ed403befac 100644 (file)
@@ -268,7 +268,7 @@ public class DeviationResolutionTest {
         final ReactorException ex = assertThrows(ReactorException.class, () -> TestUtils.parseYangSource(
             "/deviation-resolution-test/foo-invalid-deviation-path.yang",
             "/deviation-resolution-test/bar.yang"));
-        final Throwable cause = ex.getCause().getCause();
+        final Throwable cause = ex.getCause();
         assertThat(cause, instanceOf(InferenceException.class));
         assertThat(cause.getMessage(), startsWith(
             "Deviation target 'Absolute{qnames=[(bar?revision=2017-01-20)invalid, path]}' not found"));
index 4e40aa701d6ebcc91d7ed93547cf7d22f71971b2..d645e185f266358e75b8df643d32379660aada7f 100644 (file)
@@ -80,7 +80,7 @@ public class OpenconfigVersionBorderCaseTest {
     public void borderCaseInvalidMajorTest() throws Exception {
         final ReactorException ex = assertThrows(ReactorException.class,
             () -> StmtTestUtils.parseYangSources("/openconfig-version/border-case/border-case-invalid-major", SEMVER));
-        assertThat(ex.getCause().getCause().getMessage(),
+        assertThat(ex.getCause().getMessage(),
             startsWith("Unable to find module compatible with requested import [bar(5.5.5)]."));
     }
 
@@ -88,7 +88,7 @@ public class OpenconfigVersionBorderCaseTest {
     public void borderCaseInvalidMinorTest() {
         final ReactorException ex = assertThrows(ReactorException.class,
             () -> StmtTestUtils.parseYangSources("/openconfig-version/border-case/border-case-invalid-minor", SEMVER));
-        assertThat(ex.getCause().getCause().getMessage(),
+        assertThat(ex.getCause().getMessage(),
             startsWith("Unable to find module compatible with requested import [bar(5.5.5)]."));
     }
 
@@ -96,7 +96,7 @@ public class OpenconfigVersionBorderCaseTest {
     public void borderCaseInvalidPatchTest() throws Exception {
         final ReactorException ex = assertThrows(ReactorException.class,
             () -> StmtTestUtils.parseYangSources("/openconfig-version/border-case/border-case-invalid-patch", SEMVER));
-        assertThat(ex.getCause().getCause().getMessage(),
+        assertThat(ex.getCause().getMessage(),
             startsWith("Unable to find module compatible with requested import [bar(5.5.5)]."));
     }
 }
index 4254595aa7b14499a5e1e563001f27039721a3ec..1f1e23ceb2e1045be416559e19b39f43e7d355c0 100644 (file)
@@ -7,17 +7,17 @@
  */
 package org.opendaylight.yangtools.yang.stmt.openconfigver;
 
+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.assertTrue;
-import static org.junit.Assert.fail;
+import static org.junit.Assert.assertThrows;
 
 import java.util.Optional;
 import org.junit.Test;
 import org.opendaylight.yangtools.concepts.SemVer;
 import org.opendaylight.yangtools.yang.common.XMLNamespace;
 import org.opendaylight.yangtools.yang.model.api.Module;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.parser.api.ImportResolutionMode;
 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
@@ -30,7 +30,7 @@ public class OpenconfigVersionDefaultsTest {
 
     @Test
     public void defaultsTest() throws Exception {
-        SchemaContext context = StmtTestUtils.parseYangSources("/openconfig-version/defaults/defaults", SEMVER);
+        final var context = StmtTestUtils.parseYangSources("/openconfig-version/defaults/defaults", SEMVER);
         assertNotNull(context);
 
         Module foo = context.findModules(XMLNamespace.of("foo")).iterator().next();
@@ -42,7 +42,7 @@ public class OpenconfigVersionDefaultsTest {
 
     @Test
     public void defaultMajorValidTest() throws Exception {
-        SchemaContext context = StmtTestUtils.parseYangSources("/openconfig-version/defaults/default-major-valid",
+        final var context = StmtTestUtils.parseYangSources("/openconfig-version/defaults/default-major-valid",
             SEMVER);
         assertNotNull(context);
 
@@ -55,12 +55,9 @@ public class OpenconfigVersionDefaultsTest {
 
     @Test
     public void defaultMajorInvalidTest() throws Exception {
-        try {
-            StmtTestUtils.parseYangSources("/openconfig-version/defaults/default-major-invalid", SEMVER);
-            fail("Test should fail due to invalid openconfig version");
-        } catch (ReactorException e) {
-            assertTrue(e.getCause().getCause().getMessage()
-                    .startsWith("Unable to find module compatible with requested import [bar(0.0.1)]."));
-        }
+        final var ex = assertThrows(ReactorException.class,
+            () -> StmtTestUtils.parseYangSources("/openconfig-version/defaults/default-major-invalid", SEMVER));
+        assertThat(ex.getCause().getMessage(),
+            startsWith("Unable to find module compatible with requested import [bar(0.0.1)]."));
     }
 }
index 02457ddf77914e4aad518b8586ac9c11cba26c3d..d9ea7a976e54054136125f26e7f2118f5c674938 100644 (file)
@@ -43,7 +43,7 @@ public class OpenconfigVersionImportTest {
     public void importInvalidDeprecatedTest1() {
         ReactorException ex = assertThrows(ReactorException.class,
             () -> StmtTestUtils.parseYangSources("/openconfig-version/import/import-invalid-deprecated-1", SEMVER));
-        assertThat(ex.getCause().getCause().getMessage(), startsWith(
+        assertThat(ex.getCause().getMessage(), startsWith(
             "Unable to find module compatible with requested import [openconfig-extensions(1.0.0)]."));
     }
 
@@ -51,7 +51,7 @@ public class OpenconfigVersionImportTest {
     public void importInvalidDeprecatedTest2() {
         ReactorException ex = assertThrows(ReactorException.class,
             () -> StmtTestUtils.parseYangSources("/openconfig-version/import/import-invalid-deprecated-2", SEMVER));
-        assertThat(ex.getCause().getCause().getMessage(), startsWith(
+        assertThat(ex.getCause().getMessage(), startsWith(
             "Unable to find module compatible with requested import [openconfig-extensions(0.9.9)]."));
     }
 
@@ -59,7 +59,7 @@ public class OpenconfigVersionImportTest {
     public void importInvalidNotsufficientTest1() {
         ReactorException ex = assertThrows(ReactorException.class,
             () -> StmtTestUtils.parseYangSources("/openconfig-version/import/import-invalid-notsufficient-1", SEMVER));
-        assertThat(ex.getCause().getCause().getMessage(), startsWith(
+        assertThat(ex.getCause().getMessage(), startsWith(
             "Unable to find module compatible with requested import [openconfig-extensions(2.0.0)]."));
     }
 
@@ -67,7 +67,7 @@ public class OpenconfigVersionImportTest {
     public void importInvalidNotsufficientTest2() {
         ReactorException ex = assertThrows(ReactorException.class,
             () -> StmtTestUtils.parseYangSources("/openconfig-version/import/import-invalid-notsufficient-2", SEMVER));
-        assertThat(ex.getCause().getCause().getMessage(), startsWith(
+        assertThat(ex.getCause().getMessage(), startsWith(
             "Unable to find module compatible with requested import [openconfig-extensions(2.0.5)]."));
     }
 }
index 7c0209356e1df2574b8decea0a89e8885edaf7ba..a9febc3c41daa91b731c9a22d3ca110d956a0efb 100644 (file)
@@ -30,18 +30,18 @@ public class OpenconfigVersionMultipleImportTest {
         .build();
 
     @Test
-    public void multipleInvalidDeprecatedTest() throws Exception {
+    public void multipleInvalidDeprecatedTest() {
         ReactorException ex = assertThrows(ReactorException.class,
             () -> StmtTestUtils.parseYangSources("/openconfig-version/multiple/multiple-invalid-deprecated", SEMVER));
-        assertThat(ex.getCause().getCause().getMessage(),
+        assertThat(ex.getCause().getMessage(),
             startsWith("Unable to find module compatible with requested import [bar(1.0.0)]."));
     }
 
     @Test
-    public void multipleInvalidNosufficientTest() throws Exception {
+    public void multipleInvalidNosufficientTest() {
         ReactorException ex = assertThrows(ReactorException.class,
             () -> StmtTestUtils.parseYangSources("/openconfig-version/multiple/multiple-invalid-nosufficient", SEMVER));
-        assertThat(ex.getCause().getCause().getMessage(),
+        assertThat(ex.getCause().getMessage(),
             startsWith("Unable to find module compatible with requested import [bar(2.5.5)]."));
     }
 
index 577649e4002e0fa896d3cf57b6086da002ab918b..639c36a1e13ba1d459d3a6d407b93784a4ead68e 100644 (file)
@@ -108,18 +108,18 @@ public class OpenconfigVersionTest {
     }
 
     @Test
-    public void basicImportErrTest1() throws Exception {
+    public void basicImportErrTest1() {
         ReactorException ex = assertThrows(ReactorException.class,
             () -> StmtTestUtils.parseYangSources("/openconfig-version/basic-import-invalid-1", SEMVER));
-        assertThat(ex.getCause().getCause().getMessage(),
+        assertThat(ex.getCause().getMessage(),
             startsWith("Unable to find module compatible with requested import [bar(0.1.2)]."));
     }
 
     @Test
-    public void basicImportErrTest2() throws Exception {
+    public void basicImportErrTest2() {
         ReactorException ex = assertThrows(ReactorException.class,
             () -> StmtTestUtils.parseYangSources("/openconfig-version/basic-import-invalid-2", SEMVER));
-        assertThat(ex.getCause().getCause().getMessage(),
+        assertThat(ex.getCause().getMessage(),
             startsWith("Unable to find module compatible with requested import [bar(0.1.2)]."));
     }
 
index b2e8469a1cbab58a5bcfe90cedba853fb67e2ed3..99ef80647313e177ec2def92d86d3c68f4665ddd 100644 (file)
@@ -62,10 +62,10 @@ public class YinOpenconfigVersionTest {
     }
 
     @Test
-    public void basicImportErrTest1() throws URISyntaxException, SAXException, IOException {
+    public void basicImportErrTest1() {
         ReactorException ex = assertThrows(ReactorException.class,
             () -> StmtTestUtils.parseYinSources("/openconfig-version/yin-input/basic-import-invalid", SEMVER));
-        assertThat(ex.getCause().getCause().getMessage(),
+        assertThat(ex.getCause().getMessage(),
             startsWith("Unable to find module compatible with requested import [bar(0.1.2)]."));
     }
 }