Rename DOMDataTreeChangeService
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / test / java / org / opendaylight / mdsal / binding / dom / adapter / ForwardedNotificationAdapterTest.java
index 90e3ed96b358f8a56c2cafc87b6d0830dbccf8b8..ff0f48a9ff18ea310a96d96c5f4719f245041e85 100644 (file)
@@ -9,122 +9,101 @@ package org.opendaylight.mdsal.binding.dom.adapter;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotSame;
-import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertSame;
 
-import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.ImmutableSet;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Set;
 import java.util.concurrent.CountDownLatch;
-import java.util.concurrent.ExecutionException;
 import java.util.concurrent.TimeUnit;
-import java.util.concurrent.TimeoutException;
-import org.junit.Assert;
+import org.eclipse.jdt.annotation.NonNull;
 import org.junit.Test;
 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
+import org.opendaylight.mdsal.binding.api.NotificationService.Listener;
 import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractNotificationBrokerTest;
-import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.OpendaylightMdsalBindingTestListener;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.TwoLevelListChanged;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.TwoLevelListChangedBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListKey;
-import org.opendaylight.yangtools.concepts.ListenerRegistration;
+import org.opendaylight.yang.svc.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.YangModuleInfoImpl;
 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.opendaylight.yangtools.yang.binding.util.BindingMap;
 
 public class ForwardedNotificationAdapterTest extends AbstractNotificationBrokerTest {
-
-    private static final Logger LOG = LoggerFactory.getLogger(ForwardedNotificationAdapterTest.class);
-
     @Override
     protected Set<YangModuleInfo> getModuleInfos() throws Exception {
-        return ImmutableSet.of(BindingReflections.getModuleInfo(TwoLevelListChanged.class));
-
-    }
-
-    private static TwoLevelListChanged createTestData() {
-        final TopLevelListKey key = new TopLevelListKey("test");
-        return new TwoLevelListChangedBuilder()
-                .setTopLevelList(ImmutableMap.of(key, new TopLevelListBuilder().withKey(key).build()))
-                .build();
+        return Set.of(YangModuleInfoImpl.getInstance());
     }
 
     @Test
-    public void testNotifSubscription() throws InterruptedException {
-        final CountDownLatch latch = new CountDownLatch(1);
-        final TwoLevelListChanged testData = createTestData();
-
-        final TestNotifListener testNotifListener = new TestNotifListener(latch);
-        final ListenerRegistration<TestNotifListener> listenerRegistration = getNotificationService()
-                .registerNotificationListener(testNotifListener);
-        getNotificationPublishService().putNotification(testData);
-
-        latch.await();
-        assertTrue(testNotifListener.getReceivedNotifications().size() == 1);
-        assertEquals(testData, testNotifListener.getReceivedNotifications().get(0));
-
-        listenerRegistration.close();
+    public void testPutSubscription() throws Exception {
+        final var listener = new NotifListener(1);
+        try (var reg = getNotificationService().registerListener(TwoLevelListChanged.class, listener)) {
+            final var testData = createTestData();
+            getNotificationPublishService().putNotification(testData);
+
+            final var received = listener.awaitNotifications();
+            assertEquals(1, received.size());
+            assertSame(testData, received.get(0));
+        }
     }
 
     @Test
-    public void testNotifSubscription2() throws InterruptedException {
-        final CountDownLatch latch = new CountDownLatch(1);
-        final TwoLevelListChanged testData = createTestData();
-
-        final TestNotifListener testNotifListener = new TestNotifListener(latch);
-        final ListenerRegistration<TestNotifListener> listenerRegistration = getNotificationService()
-                .registerNotificationListener(testNotifListener);
-        try {
-            getNotificationPublishService().offerNotification(testData).get(1, TimeUnit.SECONDS);
-        } catch (ExecutionException | TimeoutException e) {
-            LOG.error("Notification delivery failed", e);
-            Assert.fail("notification should be delivered");
-        }
+    public void testOfferSubscription() throws Exception {
+        final var listener = new NotifListener(1);
+        try (var reg = getNotificationService().registerListener(TwoLevelListChanged.class, listener)) {
+            final var testData = createTestData();
 
-        latch.await();
-        assertTrue(testNotifListener.getReceivedNotifications().size() == 1);
-        assertEquals(testData, testNotifListener.getReceivedNotifications().get(0));
+            getNotificationPublishService().offerNotification(testData).get(1, TimeUnit.SECONDS);
 
-        listenerRegistration.close();
+            final var received = listener.awaitNotifications();
+            assertEquals(1, received.size());
+            assertSame(testData, received.get(0));
+        }
     }
 
     @Test
-    public void testNotifSubscription3() throws InterruptedException {
-        final CountDownLatch latch = new CountDownLatch(1);
-        final TwoLevelListChanged testData = createTestData();
-
-        final TestNotifListener testNotifListener = new TestNotifListener(latch);
-        final ListenerRegistration<TestNotifListener> listenerRegistration = getNotificationService()
-                .registerNotificationListener(testNotifListener);
-        assertNotSame(NotificationPublishService.REJECTED,
+    public void testOfferTimedNotification() throws Exception {
+        final var listener = new NotifListener(1);
+        try (var reg = getNotificationService().registerListener(TwoLevelListChanged.class, listener)) {
+            final var testData = createTestData();
+
+            assertNotSame(NotificationPublishService.REJECTED,
                 getNotificationPublishService().offerNotification(testData, 5, TimeUnit.SECONDS));
 
-        latch.await();
-        assertTrue(testNotifListener.getReceivedNotifications().size() == 1);
-        assertEquals(testData, testNotifListener.getReceivedNotifications().get(0));
+            final var received = listener.awaitNotifications();
+            assertEquals(1, received.size());
+            assertSame(testData, received.get(0));
+        }
+    }
 
-        listenerRegistration.close();
+    private static @NonNull TwoLevelListChanged createTestData() {
+        return new TwoLevelListChangedBuilder()
+            .setTopLevelList(BindingMap.of(new TopLevelListBuilder().withKey(new TopLevelListKey("test")).build()))
+            .build();
     }
 
-    private static class TestNotifListener implements OpendaylightMdsalBindingTestListener {
+    private static  final class NotifListener implements Listener<TwoLevelListChanged> {
         private final List<TwoLevelListChanged> receivedNotifications = new ArrayList<>();
         private final CountDownLatch latch;
 
-        TestNotifListener(final CountDownLatch latch) {
-            this.latch = latch;
+        NotifListener(final int expectedCount) {
+            latch = new CountDownLatch(expectedCount);
         }
 
-        @Override
-        public void onTwoLevelListChanged(final TwoLevelListChanged notification) {
+        void receiveNotification(final TwoLevelListChanged notification) {
             receivedNotifications.add(notification);
             latch.countDown();
         }
 
-        public List<TwoLevelListChanged> getReceivedNotifications() {
+        List<TwoLevelListChanged> awaitNotifications() throws InterruptedException {
+            latch.await();
             return receivedNotifications;
         }
+
+        @Override
+        public void onNotification(final TwoLevelListChanged notification) {
+            receiveNotification(notification);
+        }
     }
 }