6a8dc5fde960400ea07aa68e44b0a23375d6df30
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / test / java / org / opendaylight / controller / md / sal / binding / impl / test / BackwardsCompatibleNotificationBrokerTest.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.assertTrue;
12
13 import org.opendaylight.controller.md.sal.binding.compat.HeliumNotificationProviderServiceAdapter;
14
15 import com.google.common.collect.ImmutableList;
16 import java.util.ArrayList;
17 import java.util.List;
18 import java.util.concurrent.CountDownLatch;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
22 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
23 import org.opendaylight.controller.md.sal.binding.test.AbstractNotificationBrokerTest;
24 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.OpendaylightMdsalListTestListener;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.TwoLevelListChanged;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.TwoLevelListChangedBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelListBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelListKey;
30 import org.opendaylight.yangtools.concepts.ListenerRegistration;
31 import org.opendaylight.yangtools.yang.binding.NotificationListener;
32
33 public class BackwardsCompatibleNotificationBrokerTest extends AbstractNotificationBrokerTest {
34
35     private NotificationProviderService notificationProviderService;
36
37     @Before
38     public void initTest() {
39         final NotificationService notificationService = getNotificationService();
40         final NotificationPublishService notificationPublishService = getNotificationPublishService();
41         notificationProviderService = new HeliumNotificationProviderServiceAdapter(notificationPublishService, notificationService);
42     }
43
44     private TwoLevelListChanged createTestData() {
45         final TwoLevelListChangedBuilder tb = new TwoLevelListChangedBuilder();
46         tb.setTopLevelList(ImmutableList.of(new TopLevelListBuilder().setKey(new TopLevelListKey("test")).build()));
47         return tb.build();
48     }
49
50     @Test
51     public void testNotifSubscriptionForwarded() throws InterruptedException {
52         final CountDownLatch latch = new CountDownLatch(1);
53         final TwoLevelListChanged testData = createTestData();
54
55         final NotifTestListener testNotifListener = new NotifTestListener(latch);
56         final ListenerRegistration<NotificationListener> listenerRegistration =
57                 notificationProviderService.registerNotificationListener(testNotifListener);
58         notificationProviderService.publish(testData);
59
60         latch.await();
61         assertTrue(testNotifListener.getReceivedNotifications().size() == 1);
62         assertEquals(testData, testNotifListener.getReceivedNotifications().get(0));
63         listenerRegistration.close();
64     }
65
66     private static class NotifTestListener implements OpendaylightMdsalListTestListener {
67         private List<TwoLevelListChanged> receivedNotifications = new ArrayList<>();
68         private CountDownLatch latch;
69
70         public NotifTestListener(CountDownLatch latch) {
71             this.latch = latch;
72         }
73
74         @Override
75         public void onTwoLevelListChanged(TwoLevelListChanged notification) {
76             receivedNotifications.add(notification);
77             latch.countDown();
78         }
79
80         public List<TwoLevelListChanged> getReceivedNotifications() {
81             return receivedNotifications;
82         }
83     }
84
85 }