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.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;
37 public class ForwardedNotificationAdapterTest extends AbstractNotificationBrokerTest {
38 private static final Logger LOG = LoggerFactory.getLogger(ForwardedNotificationAdapterTest.class);
41 protected Set<YangModuleInfo> getModuleInfos() throws Exception {
42 return Set.of(BindingReflections.getModuleInfo(TwoLevelListChanged.class));
46 public void testPutSubscription() throws InterruptedException {
47 final var listener = new TestNotifListener(1);
48 try (var reg = getNotificationService().registerNotificationListener(listener)) {
49 assertPutSubscription(listener);
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);
61 private void assertPutSubscription(final AbstractNotifListener listener) throws InterruptedException {
62 final var testData = createTestData();
63 getNotificationPublishService().putNotification(testData);
65 final var received = listener.awaitNotifications();
66 assertEquals(1, received.size());
67 assertSame(testData, received.get(0));
71 public void testOfferSubscription() throws InterruptedException {
72 final var listener = new TestNotifListener(1);
73 try (var reg = getNotificationService().registerNotificationListener(listener)) {
74 assertOfferNotification(listener);
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);
86 private void assertOfferNotification(final AbstractNotifListener listener) throws InterruptedException {
87 final var testData = createTestData();
90 getNotificationPublishService().offerNotification(testData).get(1, TimeUnit.SECONDS);
91 } catch (ExecutionException | TimeoutException e) {
92 throw new AssertionError("Notification should be delivered", e);
95 final var received = listener.awaitNotifications();
96 assertEquals(1, received.size());
97 assertSame(testData, received.get(0));
101 public void testOfferTimedNotification() throws InterruptedException {
102 final var listener = new TestNotifListener(1);
103 try (var reg = getNotificationService().registerNotificationListener(listener)) {
104 assertOfferTimedNotification(listener);
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);
116 private void assertOfferTimedNotification(final AbstractNotifListener listener) throws InterruptedException {
117 final var testData = createTestData();
119 assertNotSame(NotificationPublishService.REJECTED,
120 getNotificationPublishService().offerNotification(testData, 5, TimeUnit.SECONDS));
122 final var received = listener.awaitNotifications();
123 assertEquals(1, received.size());
124 assertSame(testData, received.get(0));
128 private static @NonNull TwoLevelListChanged createTestData() {
129 return new TwoLevelListChangedBuilder()
130 .setTopLevelList(BindingMap.of(new TopLevelListBuilder().withKey(new TopLevelListKey("test")).build()))
134 private abstract static class AbstractNotifListener {
135 private final List<TwoLevelListChanged> receivedNotifications = new ArrayList<>();
136 private final CountDownLatch latch;
138 AbstractNotifListener(final int expectedCount) {
139 latch = new CountDownLatch(expectedCount);
142 final void receiveNotification(final TwoLevelListChanged notification) {
143 receivedNotifications.add(notification);
147 final List<TwoLevelListChanged> awaitNotifications() throws InterruptedException {
149 return receivedNotifications;
153 private static class SimpleNotifListener extends AbstractNotifListener implements Listener<TwoLevelListChanged> {
154 SimpleNotifListener(final int expectedCount) {
155 super(expectedCount);
159 public void onNotification(final TwoLevelListChanged notification) {
160 receiveNotification(notification);
164 private static class TestNotifListener extends AbstractNotifListener
165 implements OpendaylightMdsalBindingTestListener {
166 TestNotifListener(final int expectedCount) {
167 super(expectedCount);
171 public void onTwoLevelListChanged(final TwoLevelListChanged notification) {
172 receiveNotification(notification);