f16083e3f37071ffab6385a7b2d58e99b201c48a
[controller.git] / opendaylight / netconf / config-persister-impl / src / test / java / org / opendaylight / controller / netconf / persist / impl / ConfigPersisterNotificationHandlerTest.java
1 /*
2  * Copyright (c) 2014 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
9 package org.opendaylight.controller.netconf.persist.impl;
10
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Matchers.anyObject;
13 import static org.mockito.Mockito.doNothing;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.doThrow;
16 import static org.mockito.Mockito.times;
17 import static org.mockito.Mockito.verify;
18
19 import javax.management.MBeanServerConnection;
20
21 import javax.management.NotificationFilter;
22 import javax.management.NotificationListener;
23 import javax.management.ObjectName;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.Mock;
27 import org.mockito.MockitoAnnotations;
28 import org.opendaylight.controller.config.persist.api.Persister;
29
30 public class ConfigPersisterNotificationHandlerTest {
31
32     @Mock
33     private MBeanServerConnection mBeanServer;
34     @Mock
35     private Persister notificationListener;
36
37     @Before
38     public void setUp() throws Exception {
39         MockitoAnnotations.initMocks(this);
40         doNothing().when(mBeanServer).addNotificationListener(any(ObjectName.class), any(NotificationListener.class),
41                 any(NotificationFilter.class), anyObject());
42     }
43
44     @Test
45     public void testNotificationHandler() throws Exception {
46         doReturn(true).when(mBeanServer).isRegistered(any(ObjectName.class));
47         doThrow(Exception.class).when(mBeanServer).removeNotificationListener(any(ObjectName.class), any(NotificationListener.class));
48
49         final ConfigPersisterNotificationHandler testedHandler = new ConfigPersisterNotificationHandler(mBeanServer, notificationListener);
50         verify(mBeanServer).addNotificationListener(any(ObjectName.class), any(NotificationListener.class),
51                 any(NotificationFilter.class), anyObject());
52
53         testedHandler.close();
54         verify(mBeanServer).removeNotificationListener(any(ObjectName.class), any(NotificationListener.class));
55     }
56
57     @Test
58     public void testNotificationHandlerCloseNotRegistered() throws Exception {
59         doReturn(false).when(mBeanServer).isRegistered(any(ObjectName.class));
60
61         final ConfigPersisterNotificationHandler testedHandler = new ConfigPersisterNotificationHandler(mBeanServer, notificationListener);
62
63         testedHandler.close();
64         verify(mBeanServer, times(0)).removeNotificationListener(any(ObjectName.class), any(NotificationListener.class));
65     }
66 }