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;
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.controller.md.sal.binding.api.NotificationPublishService;
25 import org.opendaylight.controller.md.sal.binding.test.AbstractNotificationBrokerTest;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.OpendaylightMdsalListTestListener;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.TwoLevelListChanged;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.TwoLevelListChangedBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelListBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelListKey;
31 import org.opendaylight.yangtools.concepts.ListenerRegistration;
32 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
33 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 public class ForwardedNotificationAdapterTest extends AbstractNotificationBrokerTest {
39 private static final Logger LOG = LoggerFactory.getLogger(ForwardedNotificationAdapterTest.class);
42 protected Iterable<YangModuleInfo> getModuleInfos() throws Exception {
43 return ImmutableSet.of(BindingReflections.getModuleInfo(TwoLevelListChanged.class));
47 private TwoLevelListChanged createTestData() {
48 final TwoLevelListChangedBuilder tb = new TwoLevelListChangedBuilder();
49 tb.setTopLevelList(ImmutableList.of(new TopLevelListBuilder().setKey(new TopLevelListKey("test")).build()));
54 public void testNotifSubscription() throws InterruptedException {
55 final CountDownLatch latch = new CountDownLatch(1);
56 final TwoLevelListChanged testData = createTestData();
58 final TestNotifListener testNotifListener = new TestNotifListener(latch);
59 final ListenerRegistration<TestNotifListener> listenerRegistration = getNotificationService()
60 .registerNotificationListener(testNotifListener);
61 getNotificationPublishService().putNotification(testData);
64 assertTrue(testNotifListener.getReceivedNotifications().size() == 1);
65 assertEquals(testData, testNotifListener.getReceivedNotifications().get(0));
67 listenerRegistration.close();
71 public void testNotifSubscription2() throws InterruptedException {
72 final CountDownLatch latch = new CountDownLatch(1);
73 final TwoLevelListChanged testData = createTestData();
75 final TestNotifListener testNotifListener = new TestNotifListener(latch);
76 final ListenerRegistration<TestNotifListener> listenerRegistration = getNotificationService()
77 .registerNotificationListener(testNotifListener);
79 getNotificationPublishService().offerNotification(testData).get(1, TimeUnit.SECONDS);
80 } catch (ExecutionException | TimeoutException e) {
81 LOG.error("Notification delivery failed", e);
82 Assert.fail("notification should be delivered");
86 assertTrue(testNotifListener.getReceivedNotifications().size() == 1);
87 assertEquals(testData, testNotifListener.getReceivedNotifications().get(0));
89 listenerRegistration.close();
93 public void testNotifSubscription3() throws InterruptedException {
94 final CountDownLatch latch = new CountDownLatch(1);
95 final TwoLevelListChanged testData = createTestData();
97 final TestNotifListener testNotifListener = new TestNotifListener(latch);
98 final ListenerRegistration<TestNotifListener> listenerRegistration = getNotificationService()
99 .registerNotificationListener(testNotifListener);
100 assertNotSame(NotificationPublishService.REJECTED,
101 getNotificationPublishService().offerNotification(testData, 5, TimeUnit.SECONDS));
104 assertTrue(testNotifListener.getReceivedNotifications().size() == 1);
105 assertEquals(testData, testNotifListener.getReceivedNotifications().get(0));
107 listenerRegistration.close();
110 private static class TestNotifListener implements OpendaylightMdsalListTestListener {
111 private List<TwoLevelListChanged> receivedNotifications = new ArrayList<>();
112 private CountDownLatch latch;
114 public TestNotifListener(CountDownLatch latch) {
119 public void onTwoLevelListChanged(TwoLevelListChanged notification) {
120 receivedNotifications.add(notification);
124 public List<TwoLevelListChanged> getReceivedNotifications() {
125 return receivedNotifications;