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

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