2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.mdsal.binding.dom.adapter.test;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotSame;
12 import static org.junit.Assert.assertTrue;
14 import com.google.common.collect.ImmutableList;
15 import com.google.common.collect.ImmutableSet;
16 import java.util.ArrayList;
17 import java.util.List;
18 import java.util.concurrent.CountDownLatch;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.TimeUnit;
21 import java.util.concurrent.TimeoutException;
22 import org.junit.Assert;
23 import org.junit.Test;
24 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
25 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.OpendaylightMdsalBindingTestListener;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.TwoLevelListChanged;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.TwoLevelListChangedBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListKey;
31 import org.opendaylight.yangtools.concepts.ListenerRegistration;
32 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 public class ForwardedNotificationAdapterTest extends AbstractNotificationBrokerTest {
38 private static final Logger LOG = LoggerFactory.getLogger(ForwardedNotificationAdapterTest.class);
41 protected Iterable<YangModuleInfo> getModuleInfos() throws Exception {
42 return ImmutableSet.of(BindingReflections.getModuleInfo(TwoLevelListChanged.class));
46 private static TwoLevelListChanged createTestData() {
47 final TwoLevelListChangedBuilder tb = new TwoLevelListChangedBuilder();
48 tb.setTopLevelList(ImmutableList.of(new TopLevelListBuilder().withKey(new TopLevelListKey("test")).build()));
53 public void testNotifSubscription() throws InterruptedException {
54 final CountDownLatch latch = new CountDownLatch(1);
55 final TwoLevelListChanged testData = createTestData();
57 final TestNotifListener testNotifListener = new TestNotifListener(latch);
58 final ListenerRegistration<TestNotifListener> listenerRegistration = getNotificationService()
59 .registerNotificationListener(testNotifListener);
60 getNotificationPublishService().putNotification(testData);
63 assertTrue(testNotifListener.getReceivedNotifications().size() == 1);
64 assertEquals(testData, testNotifListener.getReceivedNotifications().get(0));
66 listenerRegistration.close();
70 public void testNotifSubscription2() throws InterruptedException {
71 final CountDownLatch latch = new CountDownLatch(1);
72 final TwoLevelListChanged testData = createTestData();
74 final TestNotifListener testNotifListener = new TestNotifListener(latch);
75 final ListenerRegistration<TestNotifListener> listenerRegistration = getNotificationService()
76 .registerNotificationListener(testNotifListener);
78 getNotificationPublishService().offerNotification(testData).get(1, TimeUnit.SECONDS);
79 } catch (ExecutionException | TimeoutException e) {
80 LOG.error("Notification delivery failed", e);
81 Assert.fail("notification should be delivered");
85 assertTrue(testNotifListener.getReceivedNotifications().size() == 1);
86 assertEquals(testData, testNotifListener.getReceivedNotifications().get(0));
88 listenerRegistration.close();
92 public void testNotifSubscription3() throws InterruptedException {
93 final CountDownLatch latch = new CountDownLatch(1);
94 final TwoLevelListChanged testData = createTestData();
96 final TestNotifListener testNotifListener = new TestNotifListener(latch);
97 final ListenerRegistration<TestNotifListener> listenerRegistration = getNotificationService()
98 .registerNotificationListener(testNotifListener);
99 assertNotSame(NotificationPublishService.REJECTED,
100 getNotificationPublishService().offerNotification(testData, 5, TimeUnit.SECONDS));
103 assertTrue(testNotifListener.getReceivedNotifications().size() == 1);
104 assertEquals(testData, testNotifListener.getReceivedNotifications().get(0));
106 listenerRegistration.close();
109 private static class TestNotifListener implements OpendaylightMdsalBindingTestListener {
110 private final List<TwoLevelListChanged> receivedNotifications = new ArrayList<>();
111 private final CountDownLatch latch;
113 TestNotifListener(final CountDownLatch latch) {
118 public void onTwoLevelListChanged(final TwoLevelListChanged notification) {
119 receivedNotifications.add(notification);
123 public List<TwoLevelListChanged> getReceivedNotifications() {
124 return receivedNotifications;