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;
13 import com.google.common.collect.ImmutableList;
14 import com.google.common.collect.ImmutableSet;
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.concurrent.CountDownLatch;
18 import java.util.concurrent.ExecutionException;
19 import java.util.concurrent.TimeUnit;
20 import java.util.concurrent.TimeoutException;
21 import org.junit.Assert;
22 import org.junit.Test;
23 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.OpendaylightMdsalBindingTestListener;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.TwoLevelListChanged;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.TwoLevelListChangedBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListKey;
29 import org.opendaylight.yangtools.concepts.ListenerRegistration;
30 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
31 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 public class ForwardedNotificationAdapterTest extends AbstractNotificationBrokerTest {
37 private static final Logger LOG = LoggerFactory.getLogger(ForwardedNotificationAdapterTest.class);
40 protected Iterable<YangModuleInfo> getModuleInfos() throws Exception {
41 return ImmutableSet.of(BindingReflections.getModuleInfo(TwoLevelListChanged.class));
45 private TwoLevelListChanged createTestData() {
46 final TwoLevelListChangedBuilder tb = new TwoLevelListChangedBuilder();
47 tb.setTopLevelList(ImmutableList.of(new TopLevelListBuilder().setKey(new TopLevelListKey("test")).build()));
52 public void testNotifSubscription() throws InterruptedException {
53 final CountDownLatch latch = new CountDownLatch(1);
54 final TwoLevelListChanged testData = createTestData();
56 final TestNotifListener testNotifListener = new TestNotifListener(latch);
57 final ListenerRegistration<TestNotifListener> listenerRegistration = getNotificationService()
58 .registerNotificationListener(testNotifListener);
59 getNotificationPublishService().putNotification(testData);
62 assertTrue(testNotifListener.getReceivedNotifications().size() == 1);
63 assertEquals(testData, testNotifListener.getReceivedNotifications().get(0));
65 listenerRegistration.close();
69 public void testNotifSubscription2() throws InterruptedException {
70 final CountDownLatch latch = new CountDownLatch(1);
71 final TwoLevelListChanged testData = createTestData();
73 final TestNotifListener testNotifListener = new TestNotifListener(latch);
74 final ListenerRegistration<TestNotifListener> listenerRegistration = getNotificationService()
75 .registerNotificationListener(testNotifListener);
77 getNotificationPublishService().offerNotification(testData).get(1, TimeUnit.SECONDS);
78 } catch (ExecutionException | TimeoutException e) {
79 LOG.error("Notification delivery failed", e);
80 Assert.fail("notification should be delivered");
84 assertTrue(testNotifListener.getReceivedNotifications().size() == 1);
85 assertEquals(testData, testNotifListener.getReceivedNotifications().get(0));
87 listenerRegistration.close();
91 public void testNotifSubscription3() throws InterruptedException {
92 final CountDownLatch latch = new CountDownLatch(1);
93 final TwoLevelListChanged testData = createTestData();
95 final TestNotifListener testNotifListener = new TestNotifListener(latch);
96 final ListenerRegistration<TestNotifListener> listenerRegistration = getNotificationService()
97 .registerNotificationListener(testNotifListener);
98 assertNotSame(NotificationPublishService.REJECTED,
99 getNotificationPublishService().offerNotification(testData, 5, TimeUnit.SECONDS));
102 assertTrue(testNotifListener.getReceivedNotifications().size() == 1);
103 assertEquals(testData, testNotifListener.getReceivedNotifications().get(0));
105 listenerRegistration.close();
108 private static class TestNotifListener implements OpendaylightMdsalBindingTestListener {
109 private final List<TwoLevelListChanged> receivedNotifications = new ArrayList<>();
110 private final CountDownLatch latch;
112 public TestNotifListener(final CountDownLatch latch) {
117 public void onTwoLevelListChanged(final TwoLevelListChanged notification) {
118 receivedNotifications.add(notification);
122 public List<TwoLevelListChanged> getReceivedNotifications() {
123 return receivedNotifications;