2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.config.persist.impl;
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Matchers.anyObject;
13 import static org.mockito.Matchers.anyString;
14 import static org.mockito.Mockito.doNothing;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.doThrow;
17 import static org.mockito.Mockito.times;
18 import static org.mockito.Mockito.verify;
20 import javax.management.MBeanServerConnection;
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.facade.xml.ConfigSubsystemFacade;
29 import org.opendaylight.controller.config.facade.xml.ConfigSubsystemFacadeFactory;
30 import org.opendaylight.controller.config.persist.api.Persister;
31 import org.opendaylight.controller.config.util.ConfigRegistryClient;
33 public class ConfigPersisterNotificationHandlerTest {
36 private MBeanServerConnection mBeanServer;
38 private Persister notificationListener;
40 private ConfigSubsystemFacadeFactory facadeFactory;
42 private ConfigSubsystemFacade facade;
44 private ConfigRegistryClient configRegistryClient;
47 public void setUp() throws Exception {
48 MockitoAnnotations.initMocks(this);
50 doReturn(facade).when(facadeFactory).createFacade(anyString());
52 doNothing().when(mBeanServer).addNotificationListener(any(ObjectName.class), any(NotificationListener.class),
53 any(NotificationFilter.class), anyObject());
57 public void testNotificationHandler() throws Exception {
58 doReturn(true).when(mBeanServer).isRegistered(any(ObjectName.class));
59 doThrow(Exception.class).when(mBeanServer).removeNotificationListener(any(ObjectName.class), any(NotificationListener.class));
61 final ConfigPersisterNotificationHandler testedHandler = new ConfigPersisterNotificationHandler(mBeanServer, notificationListener, facadeFactory);
62 verify(mBeanServer).addNotificationListener(any(ObjectName.class), any(NotificationListener.class),
63 any(NotificationFilter.class), anyObject());
65 testedHandler.close();
66 verify(mBeanServer).removeNotificationListener(any(ObjectName.class), any(NotificationListener.class));
70 public void testNotificationHandlerCloseNotRegistered() throws Exception {
71 doReturn(false).when(mBeanServer).isRegistered(any(ObjectName.class));
73 final ConfigPersisterNotificationHandler testedHandler = new ConfigPersisterNotificationHandler(mBeanServer, notificationListener, facadeFactory);
75 testedHandler.close();
76 verify(mBeanServer, times(0)).removeNotificationListener(any(ObjectName.class), any(NotificationListener.class));