Implement registerNotificationListeners()
[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.junit.jupiter.api.Assertions.assertEquals;
11 import static org.mockito.Mockito.doReturn;
12 import static org.mockito.Mockito.never;
13 import static org.mockito.Mockito.times;
14 import static org.mockito.Mockito.verify;
15
16 import org.junit.jupiter.api.BeforeEach;
17 import org.junit.jupiter.api.Test;
18 import org.junit.jupiter.api.extension.ExtendWith;
19 import org.mockito.Mock;
20 import org.mockito.junit.jupiter.MockitoExtension;
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 @ExtendWith(MockitoExtension.class)
28 class NetconfDeviceNotificationServiceTest {
29     private static final Absolute PATH1 = Absolute.of(QName.create("namespace1", "path1"));
30     private static final Absolute PATH2 = Absolute.of(QName.create("namespace2", "path2"));
31
32     @Mock
33     private DOMNotificationListener listener1;
34     @Mock
35     private DOMNotificationListener listener2;
36     @Mock
37     private DOMNotification notification1;
38     @Mock
39     private DOMNotification notification2;
40
41     private final NetconfDeviceNotificationService service = new NetconfDeviceNotificationService();
42     private ListenerRegistration<DOMNotificationListener> registration;
43
44     @BeforeEach
45     void beforeEach() throws Exception {
46         service.registerNotificationListener(listener1, PATH1);
47         registration = service.registerNotificationListener(listener2, PATH2);
48
49         doReturn(PATH2).when(notification2).getType();
50     }
51
52     @Test
53     void testPublishNotification() throws Exception {
54         doReturn(PATH1).when(notification1).getType();
55
56         service.publishNotification(notification1);
57         verify(listener1).onNotification(notification1);
58         verify(listener2, never()).onNotification(notification1);
59
60         service.publishNotification(notification2);
61         verify(listener2).onNotification(notification2);
62         verify(listener1, never()).onNotification(notification2);
63     }
64
65     @Test
66     void testCloseRegistration() throws Exception {
67         service.publishNotification(notification2);
68         assertEquals(listener2, registration.getInstance());
69         registration.close();
70         service.publishNotification(notification2);
71         verify(listener2, times(1)).onNotification(notification2);
72     }
73 }