Changed $Yang* generation package and name
[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.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.TwoLevelListChanged;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.TwoLevelListChangedBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListKey;
28 import org.opendaylight.yang.svc.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.YangModuleInfoImpl;
29 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
30 import org.opendaylight.yangtools.yang.binding.util.BindingMap;
31
32 public class ForwardedNotificationAdapterTest extends AbstractNotificationBrokerTest {
33     @Override
34     protected Set<YangModuleInfo> getModuleInfos() throws Exception {
35         return Set.of(YangModuleInfoImpl.getInstance());
36     }
37
38     @Test
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);
44
45             final var received = listener.awaitNotifications();
46             assertEquals(1, received.size());
47             assertSame(testData, received.get(0));
48         }
49     }
50
51     @Test
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();
56
57             getNotificationPublishService().offerNotification(testData).get(1, TimeUnit.SECONDS);
58
59             final var received = listener.awaitNotifications();
60             assertEquals(1, received.size());
61             assertSame(testData, received.get(0));
62         }
63     }
64
65     @Test
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();
70
71             assertNotSame(NotificationPublishService.REJECTED,
72                 getNotificationPublishService().offerNotification(testData, 5, TimeUnit.SECONDS));
73
74             final var received = listener.awaitNotifications();
75             assertEquals(1, received.size());
76             assertSame(testData, received.get(0));
77         }
78     }
79
80     private static @NonNull TwoLevelListChanged createTestData() {
81         return new TwoLevelListChangedBuilder()
82             .setTopLevelList(BindingMap.of(new TopLevelListBuilder().withKey(new TopLevelListKey("test")).build()))
83             .build();
84     }
85
86     private static  final class NotifListener implements Listener<TwoLevelListChanged> {
87         private final List<TwoLevelListChanged> receivedNotifications = new ArrayList<>();
88         private final CountDownLatch latch;
89
90         NotifListener(final int expectedCount) {
91             latch = new CountDownLatch(expectedCount);
92         }
93
94         void receiveNotification(final TwoLevelListChanged notification) {
95             receivedNotifications.add(notification);
96             latch.countDown();
97         }
98
99         List<TwoLevelListChanged> awaitNotifications() throws InterruptedException {
100             latch.await();
101             return receivedNotifications;
102         }
103
104         @Override
105         public void onNotification(final TwoLevelListChanged notification) {
106             receiveNotification(notification);
107         }
108     }
109 }