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