Migrate NotificationIT 91/109791/1
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 16 Jan 2024 01:40:54 +0000 (02:40 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 16 Jan 2024 01:41:45 +0000 (02:41 +0100)
We are just collecting notifications, which is easily catered to by a
simple API -- to the point of not needing a class and getting by with
method handles.

Change-Id: I140c734a1fe882196f174d00eb825ff635481385
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
opendaylight/md-sal/sal-binding-it/src/test/java/org/opendaylight/controller/test/sal/binding/it/NotificationIT.java

index c1d94e65bebf2d11736d4b7b94f1fc706665b631..163163bf2878ced066a12c16672c47d83324e96c 100644 (file)
@@ -10,16 +10,12 @@ package org.opendaylight.controller.test.sal.binding.it;
 import static org.junit.Assert.assertEquals;
 
 import java.util.ArrayList;
-import java.util.List;
 import javax.inject.Inject;
 import org.junit.Test;
 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
 import org.opendaylight.mdsal.binding.api.NotificationService;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.bi.ba.notification.rev150205.OpendaylightTestNotificationListener;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.bi.ba.notification.rev150205.OutOfPixieDustNotification;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.bi.ba.notification.rev150205.OutOfPixieDustNotificationBuilder;
-import org.opendaylight.yangtools.concepts.ListenerRegistration;
-import org.opendaylight.yangtools.yang.binding.NotificationListener;
 import org.opendaylight.yangtools.yang.common.Uint16;
 import org.ops4j.pax.exam.util.Filter;
 import org.slf4j.Logger;
@@ -44,71 +40,53 @@ public class NotificationIT extends AbstractIT {
      */
     @Test
     public void notificationTest() throws Exception {
-        NotificationTestListener listener1 = new NotificationTestListener();
-        ListenerRegistration<NotificationListener> listener1Reg =
-                notificationService.registerNotificationListener(listener1);
-
-        LOG.info("The notification of type FlowAdded with cookie ID 0 is created. The "
-                + "delay 100ms to make sure that the notification was delivered to "
-                + "listener.");
-        notificationPublishService.putNotification(noDustNotification("rainy day", 42));
-        Thread.sleep(100);
-
-        /**
-         * Check that one notification was delivered and has correct cookie.
-         */
-        assertEquals(1, listener1.notificationBag.size());
-        assertEquals("rainy day", listener1.notificationBag.get(0).getReason());
-        assertEquals(42, listener1.notificationBag.get(0).getDaysTillNewDust().intValue());
-
-        LOG.info("The registration of the Consumer 2. SalFlowListener is registered "
+        final var bag1 = new ArrayList<OutOfPixieDustNotification>();
+        try (var reg1 = notificationService.registerListener(OutOfPixieDustNotification.class, bag1::add)) {
+            LOG.info("""
+                The notification of type FlowAdded with cookie ID 0 is created. The\s\
+                delay 100ms to make sure that the notification was delivered to\s\
+                listener.""");
+            notificationPublishService.putNotification(noDustNotification("rainy day", 42));
+            Thread.sleep(100);
+
+            // Check that one notification was delivered and has correct cookie.
+            assertEquals(1, bag1.size());
+            assertEquals("rainy day", bag1.get(0).getReason());
+            assertEquals(42, bag1.get(0).getDaysTillNewDust().intValue());
+
+            LOG.info("The registration of the Consumer 2. SalFlowListener is registered "
                 + "registered as notification listener.");
 
-        NotificationTestListener listener2 = new NotificationTestListener();
-        final ListenerRegistration<NotificationListener> listener2Reg =
-                notificationService.registerNotificationListener(listener2);
-
-        LOG.info("3 notifications are published");
-        notificationPublishService.putNotification(noDustNotification("rainy day", 5));
-        notificationPublishService.putNotification(noDustNotification("rainy day", 10));
-        notificationPublishService.putNotification(noDustNotification("tax collector", 2));
-
-        /**
-         * The delay 100ms to make sure that the notifications were delivered to
-         * listeners.
-         */
-        Thread.sleep(100);
-
-        /**
-         * Check that 3 notification was delivered to both listeners (first one
-         * received 4 in total, second 3 in total).
-         */
-        assertEquals(4, listener1.notificationBag.size());
-        assertEquals(3, listener2.notificationBag.size());
-
-        /**
-         * The second listener is closed (unregistered)
-         *
-         */
-        listener2Reg.close();
-
-        LOG.info("The notification 5 is published");
-        notificationPublishService.putNotification(noDustNotification("entomologist hunt", 10));
-
-        /**
-         * The delay 100ms to make sure that the notification was delivered to
-         * listener.
-         */
-        Thread.sleep(100);
-
-        /**
-         * Check that first consumer received 5 notifications in total, second
-         * consumer received only three. Last notification was never received by
-         * second consumer because its listener was unregistered.
-         *
-         */
-        assertEquals(5, listener1.notificationBag.size());
-        assertEquals(3, listener2.notificationBag.size());
+            final var bag2 = new ArrayList<OutOfPixieDustNotification>();
+            try (var reg2 = notificationService.registerListener(OutOfPixieDustNotification.class, bag2::add)) {
+                LOG.info("3 notifications are published");
+                notificationPublishService.putNotification(noDustNotification("rainy day", 5));
+                notificationPublishService.putNotification(noDustNotification("rainy day", 10));
+                notificationPublishService.putNotification(noDustNotification("tax collector", 2));
+
+                // The delay 100ms to make sure that the notifications were delivered to listeners.
+                Thread.sleep(100);
+
+                // Check that 3 notification was delivered to both listeners (first one  received 4 in total, second 3
+                // in total).
+                assertEquals(4, bag1.size());
+                assertEquals(3, bag2.size());
+
+                // The second listener is closed (unregistered)
+                reg2.close();
+
+                LOG.info("The notification 5 is published");
+                notificationPublishService.putNotification(noDustNotification("entomologist hunt", 10));
+
+                // The delay 100ms to make sure that the notification was delivered to listener.
+                Thread.sleep(100);
+
+                // Check that first consumer received 5 notifications in total, second  consumer received only three.
+                // Last notification was never received by second consumer because its listener was unregistered.
+                assertEquals(5, bag1.size());
+                assertEquals(3, bag2.size());
+            }
+        }
     }
 
     /**
@@ -121,17 +99,4 @@ public class NotificationIT extends AbstractIT {
         ret.setReason(reason).setDaysTillNewDust(Uint16.valueOf(days));
         return ret.build();
     }
-
-    /**
-     * Implements {@link OpendaylightTestNotificationListener} and contains attributes which keep lists of objects of
-     * the type {@link OutOfPixieDustNotification}.
-     */
-    public static class NotificationTestListener implements OpendaylightTestNotificationListener {
-        List<OutOfPixieDustNotification> notificationBag = new ArrayList<>();
-
-        @Override
-        public void onOutOfPixieDustNotification(final OutOfPixieDustNotification arg0) {
-            notificationBag.add(arg0);
-        }
-    }
 }