Cleanup Bug6897Test 20/97720/4
authorRobert Varga <robert.varga@pantheon.tech>
Sat, 2 Oct 2021 22:17:20 +0000 (00:17 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 5 Oct 2021 08:36:41 +0000 (10:36 +0200)
Migrate to AbstractYangTest and perform general housekeeping.

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

index 584b1487aef4c6a4092e5e8d677849555f6084c1..0864ba0787a59b7160e34706342238c481747c16 100644 (file)
@@ -7,14 +7,14 @@
  */
 package org.opendaylight.yangtools.yang.parser.stmt.rfc7950;
 
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.CoreMatchers.startsWith;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.collection.IsIn.in;
 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 java.util.Collection;
-import java.util.HashSet;
-import java.util.Set;
+import java.util.stream.Collectors;
 import org.junit.Test;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
@@ -24,27 +24,24 @@ import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
 import org.opendaylight.yangtools.yang.model.api.NotificationNodeContainer;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
-import org.opendaylight.yangtools.yang.stmt.StmtTestUtils;
+import org.opendaylight.yangtools.yang.stmt.AbstractYangTest;
 
-public class Bug6897Test {
+public class Bug6897Test extends AbstractYangTest {
     private static final String FOO_NS = "foo";
 
     @Test
     public void notificationsInDataContainersTest() throws Exception {
-        final SchemaContext schemaContext = StmtTestUtils
-                .parseYangSource("/rfc7950/notifications-in-data-nodes/foo.yang");
-        assertNotNull(schemaContext);
+        final var context = assertEffectiveModel("/rfc7950/notifications-in-data-nodes/foo.yang");
 
-        assertContainsNotifications(schemaContext, "root", "grp-notification", "aug-notification");
-        assertContainsNotifications(schemaContext, "top-list", "top-list-notification");
-        assertContainsNotifications(schemaContext, "top", "top-notification");
+        assertContainsNotifications(context, "root", "grp-notification", "aug-notification");
+        assertContainsNotifications(context, "top-list", "top-list-notification");
+        assertContainsNotifications(context, "top", "top-notification");
 
-        final Collection<? extends GroupingDefinition> groupings = schemaContext.getGroupings();
+        final Collection<? extends GroupingDefinition> groupings = context.getGroupings();
         assertEquals(1, groupings.size());
         assertContainsNotifications(groupings.iterator().next(), "grp-notification");
 
-        final Collection<? extends Module> modules = schemaContext.getModules();
+        final Collection<? extends Module> modules = context.getModules();
         assertEquals(1, modules.size());
         final Module foo = modules.iterator().next();
         final Collection<? extends AugmentationSchemaNode> augmentations = foo.getAugmentations();
@@ -56,65 +53,50 @@ public class Bug6897Test {
             final String... notificationNames) {
         final DataSchemaNode dataChildByName = schemaContext.getDataChildByName(
             QName.create(FOO_NS, dataContainerName));
-        assertTrue(dataChildByName instanceof NotificationNodeContainer);
+        assertThat(dataChildByName, instanceOf(NotificationNodeContainer.class));
         assertContainsNotifications((NotificationNodeContainer) dataChildByName, notificationNames);
     }
 
     private static void assertContainsNotifications(final NotificationNodeContainer notificationContainer,
             final String... notificationNames) {
-        final Collection<? extends NotificationDefinition> notifications = notificationContainer.getNotifications();
+        final var notifications = notificationContainer.getNotifications();
         assertEquals(notificationNames.length, notifications.size());
 
-        final Set<QName> notificationQNames = new HashSet<>();
-        notifications.forEach(n -> notificationQNames.add(n.getQName()));
+        final var notificationQNames = notifications.stream()
+            .map(NotificationDefinition::getQName)
+            .collect(Collectors.toUnmodifiableSet());
 
         for (final String notificationName : notificationNames) {
-            assertTrue(notificationQNames.contains(QName.create(FOO_NS, notificationName)));
+            assertThat(QName.create(FOO_NS, notificationName), in(notificationQNames));
         }
     }
 
     @Test
-    public void invalid10Test() throws Exception {
-        try {
-            StmtTestUtils.parseYangSource("/rfc7950/notifications-in-data-nodes/foo10.yang");
-            fail("Test should fail due to invalid Yang 1.0");
-        } catch (final SomeModifiersUnresolvedException e) {
-            assertTrue(e.getCause().getMessage().startsWith("NOTIFICATION is not valid for"));
-        }
+    public void invalid10Test() {
+        assertInvalidSubstatementException(startsWith("NOTIFICATION is not valid for"),
+            "/rfc7950/notifications-in-data-nodes/foo10.yang");
     }
 
     @Test
-    public void invalid11Test() throws Exception {
-        try {
-            StmtTestUtils.parseYangSource("/rfc7950/notifications-in-data-nodes/foo-invalid.yang");
-            fail("Test should fail due to invalid Yang 1.1");
-        } catch (final SomeModifiersUnresolvedException e) {
-            assertTrue(e.getCause().getMessage().startsWith(
-                "Notification (foo)grp-notification is defined within an rpc, action, or another notification"));
-        }
+    public void invalid11Test() {
+        assertSourceException(
+            startsWith("Notification (foo)grp-notification is defined within an rpc, action, or another notification"),
+            "/rfc7950/notifications-in-data-nodes/foo-invalid.yang");
     }
 
     @Test
-    public void testNotificationWithinListWithoutKey() throws Exception {
-        try {
-            StmtTestUtils.parseYangSource("/rfc7950/notifications-in-data-nodes/bar-invalid.yang");
-            fail("Test should fail due to invalid Yang 1.1");
-        } catch (final SomeModifiersUnresolvedException e) {
-            assertTrue(e.getCause().getMessage().startsWith(
-                "Notification (bar-namespace?revision=2016-12-08)my-notification is defined within a list "
-                        + "that has no key statement"));
-        }
+    public void testNotificationWithinListWithoutKey() {
+        assertSourceException(
+            startsWith("Notification (bar-namespace?revision=2016-12-08)my-notification is defined within a list that "
+                + "has no key statement"),
+            "/rfc7950/notifications-in-data-nodes/bar-invalid.yang");
     }
 
     @Test
-    public void testNotificationInUsedGroupingWithinCase() throws Exception {
-        try {
-            StmtTestUtils.parseYangSource("/rfc7950/notifications-in-data-nodes/baz-invalid.yang");
-            fail("Test should fail due to invalid Yang 1.1");
-        } catch (final SomeModifiersUnresolvedException e) {
-            assertTrue(e.getCause().getMessage().startsWith(
-                "Notification (baz-namespace?revision=2016-12-08)notification-in-grouping is defined within a case "
-                        + "statement"));
-        }
+    public void testNotificationInUsedGroupingWithinCase() {
+        assertSourceException(
+            startsWith("Notification (baz-namespace?revision=2016-12-08)notification-in-grouping is defined within a "
+                + "case statement"),
+            "/rfc7950/notifications-in-data-nodes/baz-invalid.yang");
     }
 }