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