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.controller.md.sal.binding.impl.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;
19 import java.util.concurrent.CountDownLatch;
20 import java.util.concurrent.ExecutionException;
21 import java.util.concurrent.TimeUnit;
22 import java.util.concurrent.TimeoutException;
23 import org.junit.Assert;
24 import org.junit.Test;
25 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
26 import org.opendaylight.controller.md.sal.binding.test.AbstractNotificationBrokerTest;
27 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.OpendaylightMdsalListTestListener;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.TwoLevelListChanged;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.TwoLevelListChangedBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelListBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelListKey;
33 import org.opendaylight.yangtools.concepts.ListenerRegistration;
34 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 public class ForwardedNotificationAdapterTest extends AbstractNotificationBrokerTest {
40 private static final Logger LOG = LoggerFactory.getLogger(ForwardedNotificationAdapterTest.class);
43 protected Set<YangModuleInfo> getModuleInfos() throws Exception {
44 return ImmutableSet.of(BindingReflections.getModuleInfo(TwoLevelListChanged.class));
48 private static TwoLevelListChanged createTestData() {
49 final TwoLevelListChangedBuilder tb = new TwoLevelListChangedBuilder();
50 tb.setTopLevelList(ImmutableList.of(new TopLevelListBuilder().withKey(new TopLevelListKey("test")).build()));
55 public void testNotifSubscription() throws InterruptedException {
56 final CountDownLatch latch = new CountDownLatch(1);
57 final TwoLevelListChanged testData = createTestData();
59 final TestNotifListener testNotifListener = new TestNotifListener(latch);
60 final ListenerRegistration<TestNotifListener> listenerRegistration = getNotificationService()
61 .registerNotificationListener(testNotifListener);
62 getNotificationPublishService().putNotification(testData);
65 assertTrue(testNotifListener.getReceivedNotifications().size() == 1);
66 assertEquals(testData, testNotifListener.getReceivedNotifications().get(0));
68 listenerRegistration.close();
72 public void testNotifSubscription2() throws InterruptedException {
73 final CountDownLatch latch = new CountDownLatch(1);
74 final TwoLevelListChanged testData = createTestData();
76 final TestNotifListener testNotifListener = new TestNotifListener(latch);
77 final ListenerRegistration<TestNotifListener> listenerRegistration = getNotificationService()
78 .registerNotificationListener(testNotifListener);
80 getNotificationPublishService().offerNotification(testData).get(1, TimeUnit.SECONDS);
81 } catch (ExecutionException | TimeoutException e) {
82 LOG.error("Notification delivery failed", e);
83 Assert.fail("notification should be delivered");
87 assertTrue(testNotifListener.getReceivedNotifications().size() == 1);
88 assertEquals(testData, testNotifListener.getReceivedNotifications().get(0));
90 listenerRegistration.close();
94 public void testNotifSubscription3() throws InterruptedException {
95 final CountDownLatch latch = new CountDownLatch(1);
96 final TwoLevelListChanged testData = createTestData();
98 final TestNotifListener testNotifListener = new TestNotifListener(latch);
99 final ListenerRegistration<TestNotifListener> listenerRegistration = getNotificationService()
100 .registerNotificationListener(testNotifListener);
101 assertNotSame(NotificationPublishService.REJECTED,
102 getNotificationPublishService().offerNotification(testData, 5, TimeUnit.SECONDS));
105 assertTrue(testNotifListener.getReceivedNotifications().size() == 1);
106 assertEquals(testData, testNotifListener.getReceivedNotifications().get(0));
108 listenerRegistration.close();
111 private static class TestNotifListener implements OpendaylightMdsalListTestListener {
112 private final List<TwoLevelListChanged> receivedNotifications = new ArrayList<>();
113 private final CountDownLatch latch;
115 TestNotifListener(final CountDownLatch latch) {
120 public void onTwoLevelListChanged(final TwoLevelListChanged notification) {
121 receivedNotifications.add(notification);
125 public List<TwoLevelListChanged> getReceivedNotifications() {
126 return receivedNotifications;