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