BUG-1521 Add missing unit tests for config-persister-impl 42/10842/1
authorMaros Marsalek <mmarsale@cisco.com>
Fri, 5 Sep 2014 09:21:01 +0000 (11:21 +0200)
committerMaros Marsalek <mmarsale@cisco.com>
Fri, 5 Sep 2014 09:21:01 +0000 (11:21 +0200)
Change-Id: Id9b6f024da20ea71daa6b29353e39fbdb81e0fb2
Signed-off-by: Maros Marsalek <mmarsale@cisco.com>
opendaylight/netconf/config-persister-impl/src/main/java/org/opendaylight/controller/netconf/persist/impl/ConfigPersisterNotificationHandler.java
opendaylight/netconf/config-persister-impl/src/main/java/org/opendaylight/controller/netconf/persist/impl/PersisterAggregator.java
opendaylight/netconf/config-persister-impl/src/test/java/org/opendaylight/controller/netconf/persist/impl/ConfigPersisterNotificationHandlerTest.java [new file with mode: 0644]
opendaylight/netconf/config-persister-impl/src/test/java/org/opendaylight/controller/netconf/persist/impl/ConfigPersisterNotificationListenerTest.java [new file with mode: 0644]
opendaylight/netconf/config-persister-impl/src/test/java/org/opendaylight/controller/netconf/persist/impl/PersisterAggregatorTest.java

index 40a15be70616217d81529b003f899236865d8ac8..d72c26cb775dd76f34645d1d5bc533eb4bc082fe 100644 (file)
@@ -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);
         }
     }
index a0e7974b942951c521305ae4726baa4a5a74d74a..7e68ac18757e194d3c4b405f69c6a7f2453c9044 100644 (file)
@@ -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 (file)
index 0000000..f16083e
--- /dev/null
@@ -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 (file)
index 0000000..bf031f1
--- /dev/null
@@ -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("<config-snapshot/>")).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));
+    }
+}
index acea75a7432d37dbede7c219ed961784b1f63b86..cd646aeb079100a72a230f9d62624504a72a5ad2 100644 (file)
@@ -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"));