Merge "BUG-2288: deprecate old binding Notification API elements"
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / test / java / org / opendaylight / controller / md / sal / binding / impl / test / ForwardedNotificationAdapterTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.md.sal.binding.impl.test;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12
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;
29
30 public class ForwardedNotificationAdapterTest extends AbstractNotificationBrokerTest {
31
32     @Override
33     protected Iterable<YangModuleInfo> getModuleInfos() throws Exception {
34         return ImmutableSet.of(BindingReflections.getModuleInfo(TwoLevelListChanged.class));
35
36     }
37
38     private TwoLevelListChanged createTestData() {
39         final TwoLevelListChangedBuilder tb = new TwoLevelListChangedBuilder();
40         tb.setTopLevelList(ImmutableList.of(new TopLevelListBuilder().setKey(new TopLevelListKey("test")).build()));
41         return tb.build();
42     }
43
44     @Test
45     public void testNotifSubscription() throws InterruptedException {
46         final CountDownLatch latch = new CountDownLatch(1);
47         final TwoLevelListChanged testData = createTestData();
48
49         final TestNotifListener testNotifListener = new TestNotifListener(latch);
50         final ListenerRegistration<TestNotifListener> listenerRegistration = getNotificationService()
51                 .registerNotificationListener(testNotifListener);
52         getNotificationPublishService().putNotification(testData);
53
54         latch.await();
55         assertTrue(testNotifListener.getReceivedNotifications().size() == 1);
56         assertEquals(testData, testNotifListener.getReceivedNotifications().get(0));
57
58         listenerRegistration.close();
59     }
60
61     @Test
62     public void testNotifSubscription2() throws InterruptedException {
63         final CountDownLatch latch = new CountDownLatch(1);
64         final TwoLevelListChanged testData = createTestData();
65
66         final TestNotifListener testNotifListener = new TestNotifListener(latch);
67         final ListenerRegistration<TestNotifListener> listenerRegistration = getNotificationService()
68                 .registerNotificationListener(testNotifListener);
69         assertTrue(getNotificationPublishService().offerNotification(testData));
70
71         latch.await();
72         assertTrue(testNotifListener.getReceivedNotifications().size() == 1);
73         assertEquals(testData, testNotifListener.getReceivedNotifications().get(0));
74
75         listenerRegistration.close();
76     }
77
78     @Test
79     public void testNotifSubscription3() throws InterruptedException {
80         final CountDownLatch latch = new CountDownLatch(1);
81         final TwoLevelListChanged testData = createTestData();
82
83         final TestNotifListener testNotifListener = new TestNotifListener(latch);
84         final ListenerRegistration<TestNotifListener> listenerRegistration = getNotificationService()
85                 .registerNotificationListener(testNotifListener);
86         assertTrue(getNotificationPublishService().offerNotification(testData, 5, TimeUnit.SECONDS));
87
88         latch.await();
89         assertTrue(testNotifListener.getReceivedNotifications().size() == 1);
90         assertEquals(testData, testNotifListener.getReceivedNotifications().get(0));
91
92         listenerRegistration.close();
93     }
94
95     private static class TestNotifListener implements OpendaylightMdsalListTestListener {
96         private List<TwoLevelListChanged> receivedNotifications = new ArrayList<>();
97         private CountDownLatch latch;
98
99         public TestNotifListener(CountDownLatch latch) {
100             this.latch = latch;
101         }
102
103         @Override
104         public void onTwoLevelListChanged(TwoLevelListChanged notification) {
105             receivedNotifications.add(notification);
106             latch.countDown();
107         }
108
109         public List<TwoLevelListChanged> getReceivedNotifications() {
110             return receivedNotifications;
111         }
112     }
113 }