e96b547169c6dda0abbe5a4c1a40ff57e9a2ac70
[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 import javax.management.NotificationFilter;
21 import javax.management.NotificationListener;
22 import javax.management.ObjectName;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.mockito.Mock;
26 import org.mockito.MockitoAnnotations;
27 import org.opendaylight.controller.config.persist.api.Persister;
28
29 public class ConfigPersisterNotificationHandlerTest {
30
31     @Mock
32     private MBeanServerConnection mBeanServer;
33     @Mock
34     private Persister notificationListener;
35
36     @Before
37     public void setUp() throws Exception {
38         MockitoAnnotations.initMocks(this);
39         doNothing().when(mBeanServer).addNotificationListener(any(ObjectName.class), any(NotificationListener.class),
40                 any(NotificationFilter.class), anyObject());
41     }
42
43     @Test
44     public void testNotificationHandler() throws Exception {
45         doReturn(true).when(mBeanServer).isRegistered(any(ObjectName.class));
46         doThrow(Exception.class).when(mBeanServer).removeNotificationListener(any(ObjectName.class), any(NotificationListener.class));
47
48         final ConfigPersisterNotificationHandler testedHandler = new ConfigPersisterNotificationHandler(mBeanServer, notificationListener);
49         verify(mBeanServer).addNotificationListener(any(ObjectName.class), any(NotificationListener.class),
50                 any(NotificationFilter.class), anyObject());
51
52         testedHandler.close();
53         verify(mBeanServer).removeNotificationListener(any(ObjectName.class), any(NotificationListener.class));
54     }
55
56     @Test
57     public void testNotificationHandlerCloseNotRegistered() throws Exception {
58         doReturn(false).when(mBeanServer).isRegistered(any(ObjectName.class));
59
60         final ConfigPersisterNotificationHandler testedHandler = new ConfigPersisterNotificationHandler(mBeanServer, notificationListener);
61
62         testedHandler.close();
63         verify(mBeanServer, times(0)).removeNotificationListener(any(ObjectName.class), any(NotificationListener.class));
64     }
65 }