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.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.TimeUnit;
19 import org.junit.Test;
20 import org.opendaylight.controller.md.sal.binding.test.AbstractNotificationBrokerTest;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.OpendaylightMdsalListTestListener;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.TwoLevelListChanged;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.TwoLevelListChangedBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelListBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelListKey;
26 import org.opendaylight.yangtools.concepts.ListenerRegistration;
27 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
28 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
30 public class ForwardedNotificationAdapterTest extends AbstractNotificationBrokerTest {
33 protected Iterable<YangModuleInfo> getModuleInfos() throws Exception {
34 return ImmutableSet.of(BindingReflections.getModuleInfo(TwoLevelListChanged.class));
38 private TwoLevelListChanged createTestData() {
39 final TwoLevelListChangedBuilder tb = new TwoLevelListChangedBuilder();
40 tb.setTopLevelList(ImmutableList.of(new TopLevelListBuilder().setKey(new TopLevelListKey("test")).build()));
45 public void testNotifSubscription() throws InterruptedException {
46 final CountDownLatch latch = new CountDownLatch(1);
47 final TwoLevelListChanged testData = createTestData();
49 final TestNotifListener testNotifListener = new TestNotifListener(latch);
50 final ListenerRegistration<TestNotifListener> listenerRegistration = getNotificationService()
51 .registerNotificationListener(testNotifListener);
52 getNotificationPublishService().putNotification(testData);
55 assertTrue(testNotifListener.getReceivedNotifications().size() == 1);
56 assertEquals(testData, testNotifListener.getReceivedNotifications().get(0));
58 listenerRegistration.close();
62 public void testNotifSubscription2() throws InterruptedException {
63 final CountDownLatch latch = new CountDownLatch(1);
64 final TwoLevelListChanged testData = createTestData();
66 final TestNotifListener testNotifListener = new TestNotifListener(latch);
67 final ListenerRegistration<TestNotifListener> listenerRegistration = getNotificationService()
68 .registerNotificationListener(testNotifListener);
69 assertTrue(getNotificationPublishService().offerNotification(testData));
72 assertTrue(testNotifListener.getReceivedNotifications().size() == 1);
73 assertEquals(testData, testNotifListener.getReceivedNotifications().get(0));
75 listenerRegistration.close();
79 public void testNotifSubscription3() throws InterruptedException {
80 final CountDownLatch latch = new CountDownLatch(1);
81 final TwoLevelListChanged testData = createTestData();
83 final TestNotifListener testNotifListener = new TestNotifListener(latch);
84 final ListenerRegistration<TestNotifListener> listenerRegistration = getNotificationService()
85 .registerNotificationListener(testNotifListener);
86 assertTrue(getNotificationPublishService().offerNotification(testData, 5, TimeUnit.SECONDS));
89 assertTrue(testNotifListener.getReceivedNotifications().size() == 1);
90 assertEquals(testData, testNotifListener.getReceivedNotifications().get(0));
92 listenerRegistration.close();
95 private static class TestNotifListener implements OpendaylightMdsalListTestListener {
96 private List<TwoLevelListChanged> receivedNotifications = new ArrayList<>();
97 private CountDownLatch latch;
99 public TestNotifListener(CountDownLatch latch) {
104 public void onTwoLevelListChanged(TwoLevelListChanged notification) {
105 receivedNotifications.add(notification);
109 public List<TwoLevelListChanged> getReceivedNotifications() {
110 return receivedNotifications;