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;
39 public class ForwardedNotificationAdapterTest extends AbstractNotificationBrokerTest {
41 private static final Logger LOG = LoggerFactory.getLogger(ForwardedNotificationAdapterTest.class);
44 protected Set<YangModuleInfo> getModuleInfos() throws Exception {
45 return ImmutableSet.of(BindingReflections.getModuleInfo(TwoLevelListChanged.class));
49 private static TwoLevelListChanged createTestData() {
50 final TwoLevelListChangedBuilder tb = new TwoLevelListChangedBuilder();
51 tb.setTopLevelList(ImmutableList.of(new TopLevelListBuilder().withKey(new TopLevelListKey("test")).build()));
56 public void testNotifSubscription() throws InterruptedException {
57 final CountDownLatch latch = new CountDownLatch(1);
58 final TwoLevelListChanged testData = createTestData();
60 final TestNotifListener testNotifListener = new TestNotifListener(latch);
61 final ListenerRegistration<TestNotifListener> listenerRegistration = getNotificationService()
62 .registerNotificationListener(testNotifListener);
63 getNotificationPublishService().putNotification(testData);
66 assertTrue(testNotifListener.getReceivedNotifications().size() == 1);
67 assertEquals(testData, testNotifListener.getReceivedNotifications().get(0));
69 listenerRegistration.close();
73 public void testNotifSubscription2() throws InterruptedException {
74 final CountDownLatch latch = new CountDownLatch(1);
75 final TwoLevelListChanged testData = createTestData();
77 final TestNotifListener testNotifListener = new TestNotifListener(latch);
78 final ListenerRegistration<TestNotifListener> listenerRegistration = getNotificationService()
79 .registerNotificationListener(testNotifListener);
81 getNotificationPublishService().offerNotification(testData).get(1, TimeUnit.SECONDS);
82 } catch (ExecutionException | TimeoutException e) {
83 LOG.error("Notification delivery failed", e);
84 Assert.fail("notification should be delivered");
88 assertTrue(testNotifListener.getReceivedNotifications().size() == 1);
89 assertEquals(testData, testNotifListener.getReceivedNotifications().get(0));
91 listenerRegistration.close();
95 public void testNotifSubscription3() throws InterruptedException {
96 final CountDownLatch latch = new CountDownLatch(1);
97 final TwoLevelListChanged testData = createTestData();
99 final TestNotifListener testNotifListener = new TestNotifListener(latch);
100 final ListenerRegistration<TestNotifListener> listenerRegistration = getNotificationService()
101 .registerNotificationListener(testNotifListener);
102 assertNotSame(NotificationPublishService.REJECTED,
103 getNotificationPublishService().offerNotification(testData, 5, TimeUnit.SECONDS));
106 assertTrue(testNotifListener.getReceivedNotifications().size() == 1);
107 assertEquals(testData, testNotifListener.getReceivedNotifications().get(0));
109 listenerRegistration.close();
112 private static class TestNotifListener implements OpendaylightMdsalListTestListener {
113 private final List<TwoLevelListChanged> receivedNotifications = new ArrayList<>();
114 private final CountDownLatch latch;
116 TestNotifListener(final CountDownLatch latch) {
121 public void onTwoLevelListChanged(final TwoLevelListChanged notification) {
122 receivedNotifications.add(notification);
126 public List<TwoLevelListChanged> getReceivedNotifications() {
127 return receivedNotifications;