Fix followerDistributedDataStore tear down
[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 @Deprecated
39 public class ForwardedNotificationAdapterTest extends AbstractNotificationBrokerTest {
40
41     private static final Logger LOG = LoggerFactory.getLogger(ForwardedNotificationAdapterTest.class);
42
43     @Override
44     protected Set<YangModuleInfo> getModuleInfos() throws Exception {
45         return ImmutableSet.of(BindingReflections.getModuleInfo(TwoLevelListChanged.class));
46
47     }
48
49     private static TwoLevelListChanged createTestData() {
50         final TwoLevelListChangedBuilder tb = new TwoLevelListChangedBuilder();
51         tb.setTopLevelList(ImmutableList.of(new TopLevelListBuilder().withKey(new TopLevelListKey("test")).build()));
52         return tb.build();
53     }
54
55     @Test
56     public void testNotifSubscription() throws InterruptedException {
57         final CountDownLatch latch = new CountDownLatch(1);
58         final TwoLevelListChanged testData = createTestData();
59
60         final TestNotifListener testNotifListener = new TestNotifListener(latch);
61         final ListenerRegistration<TestNotifListener> listenerRegistration = getNotificationService()
62                 .registerNotificationListener(testNotifListener);
63         getNotificationPublishService().putNotification(testData);
64
65         latch.await();
66         assertTrue(testNotifListener.getReceivedNotifications().size() == 1);
67         assertEquals(testData, testNotifListener.getReceivedNotifications().get(0));
68
69         listenerRegistration.close();
70     }
71
72     @Test
73     public void testNotifSubscription2() throws InterruptedException {
74         final CountDownLatch latch = new CountDownLatch(1);
75         final TwoLevelListChanged testData = createTestData();
76
77         final TestNotifListener testNotifListener = new TestNotifListener(latch);
78         final ListenerRegistration<TestNotifListener> listenerRegistration = getNotificationService()
79                 .registerNotificationListener(testNotifListener);
80         try {
81             getNotificationPublishService().offerNotification(testData).get(1, TimeUnit.SECONDS);
82         } catch (ExecutionException | TimeoutException e) {
83             LOG.error("Notification delivery failed", e);
84             Assert.fail("notification should be delivered");
85         }
86
87         latch.await();
88         assertTrue(testNotifListener.getReceivedNotifications().size() == 1);
89         assertEquals(testData, testNotifListener.getReceivedNotifications().get(0));
90
91         listenerRegistration.close();
92     }
93
94     @Test
95     public void testNotifSubscription3() throws InterruptedException {
96         final CountDownLatch latch = new CountDownLatch(1);
97         final TwoLevelListChanged testData = createTestData();
98
99         final TestNotifListener testNotifListener = new TestNotifListener(latch);
100         final ListenerRegistration<TestNotifListener> listenerRegistration = getNotificationService()
101                 .registerNotificationListener(testNotifListener);
102         assertNotSame(NotificationPublishService.REJECTED,
103                 getNotificationPublishService().offerNotification(testData, 5, TimeUnit.SECONDS));
104
105         latch.await();
106         assertTrue(testNotifListener.getReceivedNotifications().size() == 1);
107         assertEquals(testData, testNotifListener.getReceivedNotifications().get(0));
108
109         listenerRegistration.close();
110     }
111
112     private static class TestNotifListener implements OpendaylightMdsalListTestListener {
113         private final List<TwoLevelListChanged> receivedNotifications = new ArrayList<>();
114         private final CountDownLatch latch;
115
116         TestNotifListener(final CountDownLatch latch) {
117             this.latch = latch;
118         }
119
120         @Override
121         public void onTwoLevelListChanged(final TwoLevelListChanged notification) {
122             receivedNotifications.add(notification);
123             latch.countDown();
124         }
125
126         public List<TwoLevelListChanged> getReceivedNotifications() {
127             return receivedNotifications;
128         }
129     }
130 }