9c965736f718305511f51d9ce3e3e9e3101deb1e
[netconf.git] / plugins / netconf-client-mdsal / src / test / java / org / opendaylight / netconf / client / mdsal / spi / NetconfDeviceNotificationServiceTest.java
1 /*
2  * Copyright (c) 2016 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.netconf.client.mdsal.spi;
9
10 import static org.mockito.Mockito.doReturn;
11 import static org.mockito.Mockito.never;
12 import static org.mockito.Mockito.times;
13 import static org.mockito.Mockito.verify;
14
15 import org.junit.Assert;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19 import org.mockito.Mock;
20 import org.mockito.junit.MockitoJUnitRunner;
21 import org.opendaylight.mdsal.dom.api.DOMNotification;
22 import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
23 import org.opendaylight.yangtools.concepts.ListenerRegistration;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
26
27 @RunWith(MockitoJUnitRunner.StrictStubs.class)
28 public class NetconfDeviceNotificationServiceTest {
29     @Mock
30     private DOMNotificationListener listener1;
31     @Mock
32     private DOMNotificationListener listener2;
33     @Mock
34     private DOMNotification notification1;
35     @Mock
36     private DOMNotification notification2;
37
38     private NetconfDeviceNotificationService service;
39     private ListenerRegistration<DOMNotificationListener> registration;
40
41
42     @Before
43     public void setUp() throws Exception {
44         final Absolute path1 = Absolute.of(QName.create("namespace1", "path1"));
45         final Absolute path2 = Absolute.of(QName.create("namespace2", "path2"));
46         service = new NetconfDeviceNotificationService();
47         service.registerNotificationListener(listener1, path1);
48         registration = service.registerNotificationListener(listener2, path2);
49
50         doReturn(path1).when(notification1).getType();
51         doReturn(path2).when(notification2).getType();
52     }
53
54     @Test
55     public void testPublishNotification() throws Exception {
56
57         service.publishNotification(notification1);
58         verify(listener1).onNotification(notification1);
59         verify(listener2, never()).onNotification(notification1);
60
61         service.publishNotification(notification2);
62         verify(listener2).onNotification(notification2);
63         verify(listener1, never()).onNotification(notification2);
64     }
65
66     @Test
67     public void testCloseRegistration() throws Exception {
68         service.publishNotification(notification2);
69         Assert.assertEquals(listener2, registration.getInstance());
70         registration.close();
71         service.publishNotification(notification2);
72         verify(listener2, times(1)).onNotification(notification2);
73     }
74 }