Hide BindingReflections.getModuleInfo()
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / test / java / org / opendaylight / mdsal / binding / dom / adapter / 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;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotSame;
12 import static org.junit.Assert.assertSame;
13
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.Set;
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.eclipse.jdt.annotation.NonNull;
22 import org.junit.Test;
23 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
24 import org.opendaylight.mdsal.binding.api.NotificationService.Listener;
25 import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractNotificationBrokerTest;
26 import org.opendaylight.mdsal.binding.runtime.spi.BindingRuntimeHelpers;
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.yang.binding.YangModuleInfo;
33 import org.opendaylight.yangtools.yang.binding.util.BindingMap;
34
35 public class ForwardedNotificationAdapterTest extends AbstractNotificationBrokerTest {
36     @Override
37     protected Set<YangModuleInfo> getModuleInfos() throws Exception {
38         return Set.of(BindingRuntimeHelpers.getYangModuleInfo(TwoLevelListChanged.class));
39     }
40
41     @Test
42     public void testPutSubscription() throws InterruptedException {
43         final var listener = new TestNotifListener(1);
44         try (var reg = getNotificationService().registerNotificationListener(listener)) {
45             assertPutSubscription(listener);
46         }
47     }
48
49     @Test
50     public void testPutSubscriptionSimple() throws InterruptedException {
51         final var listener = new SimpleNotifListener(1);
52         try (var reg = getNotificationService().registerListener(TwoLevelListChanged.class, listener)) {
53             assertPutSubscription(listener);
54         }
55     }
56
57     private void assertPutSubscription(final AbstractNotifListener listener) throws InterruptedException {
58         final var testData = createTestData();
59         getNotificationPublishService().putNotification(testData);
60
61         final var received = listener.awaitNotifications();
62         assertEquals(1, received.size());
63         assertSame(testData, received.get(0));
64     }
65
66     @Test
67     public void testOfferSubscription() throws InterruptedException {
68         final var listener = new TestNotifListener(1);
69         try (var reg = getNotificationService().registerNotificationListener(listener)) {
70             assertOfferNotification(listener);
71         }
72     }
73
74     @Test
75     public void testOfferSubscriptionSimple() throws InterruptedException {
76         final var listener = new SimpleNotifListener(1);
77         try (var reg = getNotificationService().registerListener(TwoLevelListChanged.class, listener)) {
78             assertOfferNotification(listener);
79         }
80     }
81
82     private void assertOfferNotification(final AbstractNotifListener listener) throws InterruptedException {
83         final var testData = createTestData();
84
85         try {
86             getNotificationPublishService().offerNotification(testData).get(1, TimeUnit.SECONDS);
87         } catch (ExecutionException | TimeoutException e) {
88             throw new AssertionError("Notification should be delivered", e);
89         }
90
91         final var received = listener.awaitNotifications();
92         assertEquals(1, received.size());
93         assertSame(testData, received.get(0));
94     }
95
96     @Test
97     public void testOfferTimedNotification() throws InterruptedException {
98         final var listener = new TestNotifListener(1);
99         try (var reg = getNotificationService().registerNotificationListener(listener)) {
100             assertOfferTimedNotification(listener);
101         }
102     }
103
104     @Test
105     public void testOfferTimedNotificationSimple() throws InterruptedException {
106         final var listener = new SimpleNotifListener(1);
107         try (var reg = getNotificationService().registerListener(TwoLevelListChanged.class, listener)) {
108             assertOfferTimedNotification(listener);
109         }
110     }
111
112     private void assertOfferTimedNotification(final AbstractNotifListener listener) throws InterruptedException {
113         final var testData = createTestData();
114
115         assertNotSame(NotificationPublishService.REJECTED,
116             getNotificationPublishService().offerNotification(testData, 5, TimeUnit.SECONDS));
117
118         final var received = listener.awaitNotifications();
119         assertEquals(1, received.size());
120         assertSame(testData, received.get(0));
121     }
122
123
124     private static @NonNull TwoLevelListChanged createTestData() {
125         return new TwoLevelListChangedBuilder()
126             .setTopLevelList(BindingMap.of(new TopLevelListBuilder().withKey(new TopLevelListKey("test")).build()))
127             .build();
128     }
129
130     private abstract static class AbstractNotifListener {
131         private final List<TwoLevelListChanged> receivedNotifications = new ArrayList<>();
132         private final CountDownLatch latch;
133
134         AbstractNotifListener(final int expectedCount) {
135             latch = new CountDownLatch(expectedCount);
136         }
137
138         final void receiveNotification(final TwoLevelListChanged notification) {
139             receivedNotifications.add(notification);
140             latch.countDown();
141         }
142
143         final List<TwoLevelListChanged> awaitNotifications() throws InterruptedException {
144             latch.await();
145             return receivedNotifications;
146         }
147     }
148
149     private static class SimpleNotifListener extends AbstractNotifListener implements Listener<TwoLevelListChanged> {
150         SimpleNotifListener(final int expectedCount) {
151             super(expectedCount);
152         }
153
154         @Override
155         public void onNotification(final TwoLevelListChanged notification) {
156             receiveNotification(notification);
157         }
158     }
159
160     private static class TestNotifListener extends AbstractNotifListener
161             implements OpendaylightMdsalBindingTestListener {
162         TestNotifListener(final int expectedCount) {
163             super(expectedCount);
164         }
165
166         @Override
167         public void onTwoLevelListChanged(final TwoLevelListChanged notification) {
168             receiveNotification(notification);
169         }
170     }
171 }