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.TimeUnit;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.junit.Test;
21 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
22 import org.opendaylight.mdsal.binding.api.NotificationService.Listener;
23 import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractNotificationBrokerTest;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.$YangModuleInfoImpl;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.TwoLevelListChanged;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.TwoLevelListChangedBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListKey;
29 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
30 import org.opendaylight.yangtools.yang.binding.util.BindingMap;
32 public class ForwardedNotificationAdapterTest extends AbstractNotificationBrokerTest {
34 protected Set<YangModuleInfo> getModuleInfos() throws Exception {
35 return Set.of($YangModuleInfoImpl.getInstance());
39 public void testPutSubscription() throws Exception {
40 final var listener = new NotifListener(1);
41 try (var reg = getNotificationService().registerListener(TwoLevelListChanged.class, listener)) {
42 final var testData = createTestData();
43 getNotificationPublishService().putNotification(testData);
45 final var received = listener.awaitNotifications();
46 assertEquals(1, received.size());
47 assertSame(testData, received.get(0));
52 public void testOfferSubscription() throws Exception {
53 final var listener = new NotifListener(1);
54 try (var reg = getNotificationService().registerListener(TwoLevelListChanged.class, listener)) {
55 final var testData = createTestData();
57 getNotificationPublishService().offerNotification(testData).get(1, TimeUnit.SECONDS);
59 final var received = listener.awaitNotifications();
60 assertEquals(1, received.size());
61 assertSame(testData, received.get(0));
66 public void testOfferTimedNotification() throws Exception {
67 final var listener = new NotifListener(1);
68 try (var reg = getNotificationService().registerListener(TwoLevelListChanged.class, listener)) {
69 final var testData = createTestData();
71 assertNotSame(NotificationPublishService.REJECTED,
72 getNotificationPublishService().offerNotification(testData, 5, TimeUnit.SECONDS));
74 final var received = listener.awaitNotifications();
75 assertEquals(1, received.size());
76 assertSame(testData, received.get(0));
80 private static @NonNull TwoLevelListChanged createTestData() {
81 return new TwoLevelListChangedBuilder()
82 .setTopLevelList(BindingMap.of(new TopLevelListBuilder().withKey(new TopLevelListKey("test")).build()))
86 private static final class NotifListener implements Listener<TwoLevelListChanged> {
87 private final List<TwoLevelListChanged> receivedNotifications = new ArrayList<>();
88 private final CountDownLatch latch;
90 NotifListener(final int expectedCount) {
91 latch = new CountDownLatch(expectedCount);
94 void receiveNotification(final TwoLevelListChanged notification) {
95 receivedNotifications.add(notification);
99 List<TwoLevelListChanged> awaitNotifications() throws InterruptedException {
101 return receivedNotifications;
105 public void onNotification(final TwoLevelListChanged notification) {
106 receiveNotification(notification);