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;
*/
@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());
+ }
+ }
}
/**
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);
- }
- }
}