Do not catch instantiation exceptions during augment
[yangtools.git] / yang / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / Bug6669Test.java
index 2e520c20db91ef51882a06e73a7a318748fd0b08..20bdd12f10a514420105ff99699c984443594fcb 100644 (file)
@@ -7,8 +7,11 @@
  */
 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.assertNotNull;
-import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.assertTrue;
 
 import org.junit.Test;
@@ -19,6 +22,8 @@ import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
+import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
+import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
 
 public class Bug6669Test {
     private static final String REV = "2016-09-08";
@@ -29,36 +34,35 @@ public class Bug6669Test {
     private static final QName BAR_1 = QName.create(BAR_NS, REV, "bar1");
     private static final QName BAR_2 = QName.create(BAR_NS, REV, "bar2");
     private static final QName M = QName.create(BAR_NS, REV, "m");
-    private static final QName L = QName.create(BAR_NS, REV, "l");
 
     @Test
-    public void testInvalidAugment() throws Exception {
-        final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug6669/invalid/test1");
-        assertNotNull(context);
-
-        final SchemaNode findDataSchemaNode = SchemaContextUtil.findDataSchemaNode(context,
-                SchemaPath.create(true, ROOT, BAR, BAR_1, M));
-        assertNull(findDataSchemaNode);
+    public void testInvalidAugment() {
+        final ReactorException ex = assertThrows(ReactorException.class,
+            () -> StmtTestUtils.parseYangSources("/bugs/bug6669/invalid/test1"));
+        final Throwable cause = ex.getCause();
+        assertThat(cause, instanceOf(InferenceException.class));
+        assertThat(cause.getMessage(), startsWith(
+            "An augment cannot add node 'm' because it is mandatory and in module different than target"));
     }
 
     @Test
-    public void testInvalidAugment2() throws Exception {
-        final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug6669/invalid/test2");
-        assertNotNull(context);
-
-        final SchemaNode findDataSchemaNode = SchemaContextUtil.findDataSchemaNode(context,
-                SchemaPath.create(true, ROOT, BAR, BAR_1, BAR_2, M));
-        assertNull(findDataSchemaNode);
+    public void testInvalidAugment2() {
+        final ReactorException ex = assertThrows(ReactorException.class,
+            () -> StmtTestUtils.parseYangSources("/bugs/bug6669/invalid/test2"));
+        final Throwable cause = ex.getCause();
+        assertThat(cause, instanceOf(InferenceException.class));
+        assertThat(cause.getMessage(), startsWith(
+            "An augment cannot add node 'm' because it is mandatory and in module different than target"));
     }
 
     @Test
-    public void testInvalidAugment3() throws Exception {
-        final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug6669/invalid/test3");
-        assertNotNull(context);
-
-        final SchemaNode findDataSchemaNode = SchemaContextUtil.findDataSchemaNode(context,
-                SchemaPath.create(true, ROOT, BAR, BAR_1, BAR_2, L));
-        assertNull(findDataSchemaNode);
+    public void testInvalidAugment3() {
+        final ReactorException ex = assertThrows(ReactorException.class,
+            () ->  StmtTestUtils.parseYangSources("/bugs/bug6669/invalid/test3"));
+        final Throwable cause = ex.getCause();
+        assertThat(cause, instanceOf(InferenceException.class));
+        assertThat(cause.getMessage(), startsWith(
+            "An augment cannot add node 'l' because it is mandatory and in module different than target"));
     }
 
     @Test
@@ -87,7 +91,7 @@ public class Bug6669Test {
         assertNotNull(context);
 
         final SchemaNode findDataSchemaNode = SchemaContextUtil.findDataSchemaNode(context,
-                SchemaPath.create(true, ROOT, BAR, BAR_1, BAR_2, L));
+                SchemaPath.create(true, ROOT, BAR, BAR_1, BAR_2, QName.create(BAR_NS, REV, "l")));
         assertTrue(findDataSchemaNode instanceof ListSchemaNode);
     }
 }