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

©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.