From d8088b4a8d5000c8e5c6e442a0e8ac18e4614c82 Mon Sep 17 00:00:00 2001 From: Maros Marsalek Date: Fri, 5 Sep 2014 11:21:01 +0200 Subject: [PATCH] BUG-1521 Add missing unit tests for config-persister-impl Change-Id: Id9b6f024da20ea71daa6b29353e39fbdb81e0fb2 Signed-off-by: Maros Marsalek --- .../ConfigPersisterNotificationHandler.java | 26 +++--- .../persist/impl/PersisterAggregator.java | 4 +- ...onfigPersisterNotificationHandlerTest.java | 66 +++++++++++++++ ...nfigPersisterNotificationListenerTest.java | 84 +++++++++++++++++++ .../persist/impl/PersisterAggregatorTest.java | 18 ++++ 5 files changed, 184 insertions(+), 14 deletions(-) create mode 100644 opendaylight/netconf/config-persister-impl/src/test/java/org/opendaylight/controller/netconf/persist/impl/ConfigPersisterNotificationHandlerTest.java create mode 100644 opendaylight/netconf/config-persister-impl/src/test/java/org/opendaylight/controller/netconf/persist/impl/ConfigPersisterNotificationListenerTest.java diff --git a/opendaylight/netconf/config-persister-impl/src/main/java/org/opendaylight/controller/netconf/persist/impl/ConfigPersisterNotificationHandler.java b/opendaylight/netconf/config-persister-impl/src/main/java/org/opendaylight/controller/netconf/persist/impl/ConfigPersisterNotificationHandler.java index 40a15be706..d72c26cb77 100644 --- a/opendaylight/netconf/config-persister-impl/src/main/java/org/opendaylight/controller/netconf/persist/impl/ConfigPersisterNotificationHandler.java +++ b/opendaylight/netconf/config-persister-impl/src/main/java/org/opendaylight/controller/netconf/persist/impl/ConfigPersisterNotificationHandler.java @@ -34,18 +34,20 @@ public class ConfigPersisterNotificationHandler implements Closeable { private static final Logger logger = LoggerFactory.getLogger(ConfigPersisterNotificationHandler.class); private final MBeanServerConnection mBeanServerConnection; - private final ConfigPersisterNotificationListener listener; + private final NotificationListener listener; - public ConfigPersisterNotificationHandler(MBeanServerConnection mBeanServerConnection, - Persister persisterAggregator) { + public ConfigPersisterNotificationHandler(final MBeanServerConnection mBeanServerConnection, final Persister persisterAggregator) { + this(mBeanServerConnection, new ConfigPersisterNotificationListener(persisterAggregator)); + } + + public ConfigPersisterNotificationHandler(final MBeanServerConnection mBeanServerConnection, final NotificationListener notificationListener) { this.mBeanServerConnection = mBeanServerConnection; - listener = new ConfigPersisterNotificationListener(persisterAggregator); + this.listener = notificationListener; registerAsJMXListener(mBeanServerConnection, listener); - } - private static void registerAsJMXListener(MBeanServerConnection mBeanServerConnection, ConfigPersisterNotificationListener listener) { + private static void registerAsJMXListener(final MBeanServerConnection mBeanServerConnection, final NotificationListener listener) { logger.trace("Called registerAsJMXListener"); try { mBeanServerConnection.addNotificationListener(DefaultCommitOperationMXBean.OBJECT_NAME, listener, null, null); @@ -57,12 +59,12 @@ public class ConfigPersisterNotificationHandler implements Closeable { @Override public synchronized void close() { // unregister from JMX - ObjectName on = DefaultCommitOperationMXBean.OBJECT_NAME; + final ObjectName on = DefaultCommitOperationMXBean.OBJECT_NAME; try { if (mBeanServerConnection.isRegistered(on)) { mBeanServerConnection.removeNotificationListener(on, listener); } - } catch (Exception e) { + } catch (final Exception e) { logger.warn("Unable to unregister {} as listener for {}", listener, on, e); } } @@ -73,12 +75,12 @@ class ConfigPersisterNotificationListener implements NotificationListener { private final Persister persisterAggregator; - ConfigPersisterNotificationListener(Persister persisterAggregator) { + ConfigPersisterNotificationListener(final Persister persisterAggregator) { this.persisterAggregator = persisterAggregator; } @Override - public void handleNotification(Notification notification, Object handback) { + public void handleNotification(final Notification notification, final Object handback) { if (!(notification instanceof NetconfJMXNotification)) return; @@ -89,7 +91,7 @@ class ConfigPersisterNotificationListener implements NotificationListener { if (notification instanceof CommitJMXNotification) { try { handleAfterCommitNotification((CommitJMXNotification) notification); - } catch (Exception e) { + } catch (final Exception e) { // log exceptions from notification Handler here since // notificationBroadcastSupport logs only DEBUG level logger.warn("Failed to handle notification {}", notification, e); @@ -105,7 +107,7 @@ class ConfigPersisterNotificationListener implements NotificationListener { persisterAggregator.persistConfig(new CapabilityStrippingConfigSnapshotHolder(notification.getConfigSnapshot(), notification.getCapabilities())); logger.trace("Configuration persisted successfully"); - } catch (IOException e) { + } catch (final IOException e) { throw new RuntimeException("Unable to persist configuration snapshot", e); } } diff --git a/opendaylight/netconf/config-persister-impl/src/main/java/org/opendaylight/controller/netconf/persist/impl/PersisterAggregator.java b/opendaylight/netconf/config-persister-impl/src/main/java/org/opendaylight/controller/netconf/persist/impl/PersisterAggregator.java index a0e7974b94..7e68ac1875 100644 --- a/opendaylight/netconf/config-persister-impl/src/main/java/org/opendaylight/controller/netconf/persist/impl/PersisterAggregator.java +++ b/opendaylight/netconf/config-persister-impl/src/main/java/org/opendaylight/controller/netconf/persist/impl/PersisterAggregator.java @@ -147,8 +147,8 @@ public final class PersisterAggregator implements Persister { public void persistConfig(ConfigSnapshotHolder holder) throws IOException { for (PersisterWithConfiguration persisterWithConfiguration: persisterWithConfigurations){ if (!persisterWithConfiguration.readOnly){ - logger.debug("Calling {}.persistConfig",persisterWithConfiguration.storage); - persisterWithConfiguration.storage.persistConfig(holder); + logger.debug("Calling {}.persistConfig", persisterWithConfiguration.getStorage()); + persisterWithConfiguration.getStorage().persistConfig(holder); } } } diff --git a/opendaylight/netconf/config-persister-impl/src/test/java/org/opendaylight/controller/netconf/persist/impl/ConfigPersisterNotificationHandlerTest.java b/opendaylight/netconf/config-persister-impl/src/test/java/org/opendaylight/controller/netconf/persist/impl/ConfigPersisterNotificationHandlerTest.java new file mode 100644 index 0000000000..f16083e3f3 --- /dev/null +++ b/opendaylight/netconf/config-persister-impl/src/test/java/org/opendaylight/controller/netconf/persist/impl/ConfigPersisterNotificationHandlerTest.java @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.controller.netconf.persist.impl; + +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyObject; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import javax.management.MBeanServerConnection; + +import javax.management.NotificationFilter; +import javax.management.NotificationListener; +import javax.management.ObjectName; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.opendaylight.controller.config.persist.api.Persister; + +public class ConfigPersisterNotificationHandlerTest { + + @Mock + private MBeanServerConnection mBeanServer; + @Mock + private Persister notificationListener; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + doNothing().when(mBeanServer).addNotificationListener(any(ObjectName.class), any(NotificationListener.class), + any(NotificationFilter.class), anyObject()); + } + + @Test + public void testNotificationHandler() throws Exception { + doReturn(true).when(mBeanServer).isRegistered(any(ObjectName.class)); + doThrow(Exception.class).when(mBeanServer).removeNotificationListener(any(ObjectName.class), any(NotificationListener.class)); + + final ConfigPersisterNotificationHandler testedHandler = new ConfigPersisterNotificationHandler(mBeanServer, notificationListener); + verify(mBeanServer).addNotificationListener(any(ObjectName.class), any(NotificationListener.class), + any(NotificationFilter.class), anyObject()); + + testedHandler.close(); + verify(mBeanServer).removeNotificationListener(any(ObjectName.class), any(NotificationListener.class)); + } + + @Test + public void testNotificationHandlerCloseNotRegistered() throws Exception { + doReturn(false).when(mBeanServer).isRegistered(any(ObjectName.class)); + + final ConfigPersisterNotificationHandler testedHandler = new ConfigPersisterNotificationHandler(mBeanServer, notificationListener); + + testedHandler.close(); + verify(mBeanServer, times(0)).removeNotificationListener(any(ObjectName.class), any(NotificationListener.class)); + } +} diff --git a/opendaylight/netconf/config-persister-impl/src/test/java/org/opendaylight/controller/netconf/persist/impl/ConfigPersisterNotificationListenerTest.java b/opendaylight/netconf/config-persister-impl/src/test/java/org/opendaylight/controller/netconf/persist/impl/ConfigPersisterNotificationListenerTest.java new file mode 100644 index 0000000000..bf031f192a --- /dev/null +++ b/opendaylight/netconf/config-persister-impl/src/test/java/org/opendaylight/controller/netconf/persist/impl/ConfigPersisterNotificationListenerTest.java @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.controller.netconf.persist.impl; + +import java.util.Collections; + +import javax.management.Notification; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Matchers; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder; +import org.opendaylight.controller.config.persist.api.Persister; +import org.opendaylight.controller.netconf.api.jmx.CommitJMXNotification; +import org.opendaylight.controller.netconf.api.jmx.NetconfJMXNotification; +import org.opendaylight.controller.netconf.util.xml.XmlUtil; + +import com.google.common.collect.Lists; + +public class ConfigPersisterNotificationListenerTest { + + @Mock + private Persister mockPersister; + private PersisterAggregator persisterAggregator; + + @Mock + private NetconfJMXNotification unknownNetconfNotif; + @Mock + private CommitJMXNotification commitNetconfNotif; + @Mock + private Notification unknownNotif; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + + Mockito.doNothing().when(mockPersister).persistConfig(Matchers.any(ConfigSnapshotHolder.class)); + Mockito.doReturn("persister").when(mockPersister).toString(); + final PersisterAggregator.PersisterWithConfiguration withCfg = new PersisterAggregator.PersisterWithConfiguration(mockPersister, false); + persisterAggregator = new PersisterAggregator(Lists.newArrayList(withCfg)); + + Mockito.doReturn("netconfUnknownNotification").when(unknownNetconfNotif).toString(); + Mockito.doReturn("netconfCommitNotification").when(commitNetconfNotif).toString(); + + Mockito.doReturn(XmlUtil.readXmlToElement("")).when(commitNetconfNotif).getConfigSnapshot(); + Mockito.doReturn(Collections.emptySet()).when(commitNetconfNotif).getCapabilities(); + + } + + @Test + public void testNotificationListenerUnknownNotification() throws Exception { + final ConfigPersisterNotificationListener testeListener = new ConfigPersisterNotificationListener(persisterAggregator); + testeListener.handleNotification(unknownNotif, null); + Mockito.verifyZeroInteractions(mockPersister); + } + + @Test + public void testNotificationListenerUnknownNetconfNotification() throws Exception { + final ConfigPersisterNotificationListener testeListener = new ConfigPersisterNotificationListener(persisterAggregator); + try { + testeListener.handleNotification(unknownNetconfNotif, null); + Assert.fail("Unknown netconf notification should fail"); + } catch (final IllegalStateException e) { + Mockito.verifyZeroInteractions(mockPersister); + } + } + + @Test + public void testNotificationListenerCommitNetconfNotification() throws Exception { + final ConfigPersisterNotificationListener testeListener = new ConfigPersisterNotificationListener(persisterAggregator); + testeListener.handleNotification(commitNetconfNotif, null); + Mockito.verify(mockPersister).persistConfig(Matchers.any(ConfigSnapshotHolder.class)); + } +} diff --git a/opendaylight/netconf/config-persister-impl/src/test/java/org/opendaylight/controller/netconf/persist/impl/PersisterAggregatorTest.java b/opendaylight/netconf/config-persister-impl/src/test/java/org/opendaylight/controller/netconf/persist/impl/PersisterAggregatorTest.java index acea75a743..cd646aeb07 100644 --- a/opendaylight/netconf/config-persister-impl/src/test/java/org/opendaylight/controller/netconf/persist/impl/PersisterAggregatorTest.java +++ b/opendaylight/netconf/config-persister-impl/src/test/java/org/opendaylight/controller/netconf/persist/impl/PersisterAggregatorTest.java @@ -8,6 +8,8 @@ package org.opendaylight.controller.netconf.persist.impl; +import com.google.common.collect.Lists; + import org.junit.Test; import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder; import org.opendaylight.controller.config.persist.api.Persister; @@ -87,6 +89,22 @@ public class PersisterAggregatorTest { assertEquals(1, DummyAdapter.props); } + @Test + public void testNoopAdapter() throws Exception { + final NoOpStorageAdapter noOpStorageAdapter = new NoOpStorageAdapter(); + final PersisterAggregator persisterAggregator = + new PersisterAggregator(Lists.newArrayList(new PersisterWithConfiguration(noOpStorageAdapter, false))); + + noOpStorageAdapter.instantiate(null); + + persisterAggregator.persistConfig(null); + persisterAggregator.loadLastConfigs(); + persisterAggregator.persistConfig(null); + persisterAggregator.loadLastConfigs(); + + noOpStorageAdapter.close(); + } + @Test public void testLoadFromPropertyFile() throws Exception { PersisterAggregator persisterAggregator = PersisterAggregator.createFromProperties(loadFile("test2.properties")); -- 2.36.6