7c65043156075a31a61c4496616e3f7b9629e053
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / test / java / org / opendaylight / controller / md / sal / binding / impl / test / 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.controller.md.sal.binding.impl.test;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotSame;
12 import static org.junit.Assert.assertTrue;
13
14 import com.google.common.collect.ImmutableList;
15 import com.google.common.collect.ImmutableSet;
16 import java.util.ArrayList;
17 import java.util.List;
18 import java.util.concurrent.CountDownLatch;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.TimeUnit;
21 import java.util.concurrent.TimeoutException;
22 import org.junit.Assert;
23 import org.junit.Test;
24 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
25 import org.opendaylight.controller.md.sal.binding.test.AbstractNotificationBrokerTest;
26 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.OpendaylightMdsalListTestListener;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.TwoLevelListChanged;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.TwoLevelListChangedBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelListBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelListKey;
32 import org.opendaylight.yangtools.concepts.ListenerRegistration;
33 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 public class ForwardedNotificationAdapterTest extends AbstractNotificationBrokerTest {
38
39     private static final Logger LOG = LoggerFactory.getLogger(ForwardedNotificationAdapterTest.class);
40
41     @Override
42     protected Iterable<YangModuleInfo> getModuleInfos() throws Exception {
43         return ImmutableSet.of(BindingReflections.getModuleInfo(TwoLevelListChanged.class));
44
45     }
46
47     private static TwoLevelListChanged createTestData() {
48         final TwoLevelListChangedBuilder tb = new TwoLevelListChangedBuilder();
49         tb.setTopLevelList(ImmutableList.of(new TopLevelListBuilder().withKey(new TopLevelListKey("test")).build()));
50         return tb.build();
51     }
52
53     @Test
54     public void testNotifSubscription() throws InterruptedException {
55         final CountDownLatch latch = new CountDownLatch(1);
56         final TwoLevelListChanged testData = createTestData();
57
58         final TestNotifListener testNotifListener = new TestNotifListener(latch);
59         final ListenerRegistration<TestNotifListener> listenerRegistration = getNotificationService()
60                 .registerNotificationListener(testNotifListener);
61         getNotificationPublishService().putNotification(testData);
62
63         latch.await();
64         assertTrue(testNotifListener.getReceivedNotifications().size() == 1);
65         assertEquals(testData, testNotifListener.getReceivedNotifications().get(0));
66
67         listenerRegistration.close();
68     }
69
70     @Test
71     public void testNotifSubscription2() throws InterruptedException {
72         final CountDownLatch latch = new CountDownLatch(1);
73         final TwoLevelListChanged testData = createTestData();
74
75         final TestNotifListener testNotifListener = new TestNotifListener(latch);
76         final ListenerRegistration<TestNotifListener> listenerRegistration = getNotificationService()
77                 .registerNotificationListener(testNotifListener);
78         try {
79             getNotificationPublishService().offerNotification(testData).get(1, TimeUnit.SECONDS);
80         } catch (ExecutionException | TimeoutException e) {
81             LOG.error("Notification delivery failed", e);
82             Assert.fail("notification should be delivered");
83         }
84
85         latch.await();
86         assertTrue(testNotifListener.getReceivedNotifications().size() == 1);
87         assertEquals(testData, testNotifListener.getReceivedNotifications().get(0));
88
89         listenerRegistration.close();
90     }
91
92     @Test
93     public void testNotifSubscription3() throws InterruptedException {
94         final CountDownLatch latch = new CountDownLatch(1);
95         final TwoLevelListChanged testData = createTestData();
96
97         final TestNotifListener testNotifListener = new TestNotifListener(latch);
98         final ListenerRegistration<TestNotifListener> listenerRegistration = getNotificationService()
99                 .registerNotificationListener(testNotifListener);
100         assertNotSame(NotificationPublishService.REJECTED,
101                 getNotificationPublishService().offerNotification(testData, 5, TimeUnit.SECONDS));
102
103         latch.await();
104         assertTrue(testNotifListener.getReceivedNotifications().size() == 1);
105         assertEquals(testData, testNotifListener.getReceivedNotifications().get(0));
106
107         listenerRegistration.close();
108     }
109
110     private static class TestNotifListener implements OpendaylightMdsalListTestListener {
111         private final List<TwoLevelListChanged> receivedNotifications = new ArrayList<>();
112         private final CountDownLatch latch;
113
114         TestNotifListener(final CountDownLatch latch) {
115             this.latch = latch;
116         }
117
118         @Override
119         public void onTwoLevelListChanged(final TwoLevelListChanged notification) {
120             receivedNotifications.add(notification);
121             latch.countDown();
122         }
123
124         public List<TwoLevelListChanged> getReceivedNotifications() {
125             return receivedNotifications;
126         }
127     }
128 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.