2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.dom.adapter;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotSame;
12 import static org.junit.Assert.assertSame;
14 import java.util.ArrayList;
15 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.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;
35 public class ForwardedNotificationAdapterTest extends AbstractNotificationBrokerTest {
37 protected Set<YangModuleInfo> getModuleInfos() throws Exception {
38 return Set.of(BindingRuntimeHelpers.getYangModuleInfo(TwoLevelListChanged.class));
42 public void testPutSubscription() throws InterruptedException {
43 final var listener = new TestNotifListener(1);
44 try (var reg = getNotificationService().registerNotificationListener(listener)) {
45 assertPutSubscription(listener);
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);
57 private void assertPutSubscription(final AbstractNotifListener listener) throws InterruptedException {
58 final var testData = createTestData();
59 getNotificationPublishService().putNotification(testData);
61 final var received = listener.awaitNotifications();
62 assertEquals(1, received.size());
63 assertSame(testData, received.get(0));
67 public void testOfferSubscription() throws InterruptedException {
68 final var listener = new TestNotifListener(1);
69 try (var reg = getNotificationService().registerNotificationListener(listener)) {
70 assertOfferNotification(listener);
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);
82 private void assertOfferNotification(final AbstractNotifListener listener) throws InterruptedException {
83 final var testData = createTestData();
86 getNotificationPublishService().offerNotification(testData).get(1, TimeUnit.SECONDS);
87 } catch (ExecutionException | TimeoutException e) {
88 throw new AssertionError("Notification should be delivered", e);
91 final var received = listener.awaitNotifications();
92 assertEquals(1, received.size());
93 assertSame(testData, received.get(0));
97 public void testOfferTimedNotification() throws InterruptedException {
98 final var listener = new TestNotifListener(1);
99 try (var reg = getNotificationService().registerNotificationListener(listener)) {
100 assertOfferTimedNotification(listener);
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);
112 private void assertOfferTimedNotification(final AbstractNotifListener listener) throws InterruptedException {
113 final var testData = createTestData();
115 assertNotSame(NotificationPublishService.REJECTED,
116 getNotificationPublishService().offerNotification(testData, 5, TimeUnit.SECONDS));
118 final var received = listener.awaitNotifications();
119 assertEquals(1, received.size());
120 assertSame(testData, received.get(0));
124 private static @NonNull TwoLevelListChanged createTestData() {
125 return new TwoLevelListChangedBuilder()
126 .setTopLevelList(BindingMap.of(new TopLevelListBuilder().withKey(new TopLevelListKey("test")).build()))
130 private abstract static class AbstractNotifListener {
131 private final List<TwoLevelListChanged> receivedNotifications = new ArrayList<>();
132 private final CountDownLatch latch;
134 AbstractNotifListener(final int expectedCount) {
135 latch = new CountDownLatch(expectedCount);
138 final void receiveNotification(final TwoLevelListChanged notification) {
139 receivedNotifications.add(notification);
143 final List<TwoLevelListChanged> awaitNotifications() throws InterruptedException {
145 return receivedNotifications;
149 private static class SimpleNotifListener extends AbstractNotifListener implements Listener<TwoLevelListChanged> {
150 SimpleNotifListener(final int expectedCount) {
151 super(expectedCount);
155 public void onNotification(final TwoLevelListChanged notification) {
156 receiveNotification(notification);
160 private static class TestNotifListener extends AbstractNotifListener
161 implements OpendaylightMdsalBindingTestListener {
162 TestNotifListener(final int expectedCount) {
163 super(expectedCount);
167 public void onTwoLevelListChanged(final TwoLevelListChanged notification) {
168 receiveNotification(notification);