Move BindingReflections to mdsal-binding-spec-util
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / test / java / org / opendaylight / mdsal / binding / dom / adapter / 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.mdsal.binding.dom.adapter.test;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotSame;
12 import static org.junit.Assert.assertTrue;
13
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.mdsal.binding.api.NotificationPublishService;
25 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.OpendaylightMdsalBindingTestListener;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.TwoLevelListChanged;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.TwoLevelListChangedBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListKey;
31 import org.opendaylight.yangtools.concepts.ListenerRegistration;
32 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class ForwardedNotificationAdapterTest extends AbstractNotificationBrokerTest {
37
38     private static final Logger LOG = LoggerFactory.getLogger(ForwardedNotificationAdapterTest.class);
39
40     @Override
41     protected Iterable<YangModuleInfo> getModuleInfos() throws Exception {
42         return ImmutableSet.of(BindingReflections.getModuleInfo(TwoLevelListChanged.class));
43
44     }
45
46     private static TwoLevelListChanged createTestData() {
47         final TwoLevelListChangedBuilder tb = new TwoLevelListChangedBuilder();
48         tb.setTopLevelList(ImmutableList.of(new TopLevelListBuilder().withKey(new TopLevelListKey("test")).build()));
49         return tb.build();
50     }
51
52     @Test
53     public void testNotifSubscription() throws InterruptedException {
54         final CountDownLatch latch = new CountDownLatch(1);
55         final TwoLevelListChanged testData = createTestData();
56
57         final TestNotifListener testNotifListener = new TestNotifListener(latch);
58         final ListenerRegistration<TestNotifListener> listenerRegistration = getNotificationService()
59                 .registerNotificationListener(testNotifListener);
60         getNotificationPublishService().putNotification(testData);
61
62         latch.await();
63         assertTrue(testNotifListener.getReceivedNotifications().size() == 1);
64         assertEquals(testData, testNotifListener.getReceivedNotifications().get(0));
65
66         listenerRegistration.close();
67     }
68
69     @Test
70     public void testNotifSubscription2() throws InterruptedException {
71         final CountDownLatch latch = new CountDownLatch(1);
72         final TwoLevelListChanged testData = createTestData();
73
74         final TestNotifListener testNotifListener = new TestNotifListener(latch);
75         final ListenerRegistration<TestNotifListener> listenerRegistration = getNotificationService()
76                 .registerNotificationListener(testNotifListener);
77         try {
78             getNotificationPublishService().offerNotification(testData).get(1, TimeUnit.SECONDS);
79         } catch (ExecutionException | TimeoutException e) {
80             LOG.error("Notification delivery failed", e);
81             Assert.fail("notification should be delivered");
82         }
83
84         latch.await();
85         assertTrue(testNotifListener.getReceivedNotifications().size() == 1);
86         assertEquals(testData, testNotifListener.getReceivedNotifications().get(0));
87
88         listenerRegistration.close();
89     }
90
91     @Test
92     public void testNotifSubscription3() throws InterruptedException {
93         final CountDownLatch latch = new CountDownLatch(1);
94         final TwoLevelListChanged testData = createTestData();
95
96         final TestNotifListener testNotifListener = new TestNotifListener(latch);
97         final ListenerRegistration<TestNotifListener> listenerRegistration = getNotificationService()
98                 .registerNotificationListener(testNotifListener);
99         assertNotSame(NotificationPublishService.REJECTED,
100                 getNotificationPublishService().offerNotification(testData, 5, TimeUnit.SECONDS));
101
102         latch.await();
103         assertTrue(testNotifListener.getReceivedNotifications().size() == 1);
104         assertEquals(testData, testNotifListener.getReceivedNotifications().get(0));
105
106         listenerRegistration.close();
107     }
108
109     private static class TestNotifListener implements OpendaylightMdsalBindingTestListener {
110         private final List<TwoLevelListChanged> receivedNotifications = new ArrayList<>();
111         private final CountDownLatch latch;
112
113         TestNotifListener(final CountDownLatch latch) {
114             this.latch = latch;
115         }
116
117         @Override
118         public void onTwoLevelListChanged(final TwoLevelListChanged notification) {
119             receivedNotifications.add(notification);
120             latch.countDown();
121         }
122
123         public List<TwoLevelListChanged> getReceivedNotifications() {
124             return receivedNotifications;
125         }
126     }
127 }