Merge "Fixed PersisterAggregatorTest."
authorEd Warnicke <eaw@cisco.com>
Sat, 30 Nov 2013 16:33:07 +0000 (16:33 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Sat, 30 Nov 2013 16:33:07 +0000 (16:33 +0000)
opendaylight/netconf/config-persister-impl/pom.xml
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/DummyAdapter.java [new file with mode: 0644]
opendaylight/netconf/config-persister-impl/src/test/java/org/opendaylight/controller/netconf/persist/impl/PersisterAggregatorTest.java
opendaylight/netconf/config-persister-impl/src/test/resources/test1.properties [new file with mode: 0644]
opendaylight/netconf/config-persister-impl/src/test/resources/test2.properties [new file with mode: 0644]
opendaylight/netconf/config-persister-impl/src/test/resources/test3.properties [new file with mode: 0644]

index dce858fce63dbad06159d7109f77fb40005c301e..c6caf417a17484aca119f7c2e5e655ce08102440 100644 (file)
             <artifactId>commons-io</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.opendaylight.controller</groupId>
+            <artifactId>config-persister-directory-adapter</artifactId>
+            <version>${parent.version}</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
     <build>
index 7add448025b51794ef6ed0cc261e6dbd4cb50479..e109ebec887b8e3c2a1579e5e2292fc970b25f1e 100644 (file)
@@ -61,7 +61,7 @@ public final class PersisterAggregator implements Persister {
 
     public static class PersisterWithConfiguration {
 
-        public final Persister storage;
+        private final Persister storage;
         private final boolean readOnly;
 
         public PersisterWithConfiguration(Persister storage, boolean readOnly) {
@@ -69,6 +69,16 @@ public final class PersisterAggregator implements Persister {
             this.readOnly = readOnly;
         }
 
+        @VisibleForTesting
+        public Persister getStorage() {
+            return storage;
+        }
+
+        @VisibleForTesting
+        public boolean isReadOnly() {
+            return readOnly;
+        }
+
         @Override
         public String toString() {
             return "PersisterWithConfiguration{" +
diff --git a/opendaylight/netconf/config-persister-impl/src/test/java/org/opendaylight/controller/netconf/persist/impl/DummyAdapter.java b/opendaylight/netconf/config-persister-impl/src/test/java/org/opendaylight/controller/netconf/persist/impl/DummyAdapter.java
new file mode 100644 (file)
index 0000000..7a11b9c
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2013 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 com.google.common.base.Optional;
+import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
+import org.opendaylight.controller.config.persist.api.Persister;
+import org.opendaylight.controller.config.persist.api.PropertiesProvider;
+import org.opendaylight.controller.config.persist.api.StorageAdapter;
+
+import java.io.IOException;
+
+public class DummyAdapter implements StorageAdapter, Persister {
+
+    static int persist = 0;
+
+    @Override
+    public void persistConfig(ConfigSnapshotHolder holder) throws IOException {
+        persist++;
+    }
+
+    static int load = 0;
+
+    @Override
+    public Optional<ConfigSnapshotHolder> loadLastConfig() throws IOException {
+        load++;
+        return Optional.absent();
+    }
+
+    static int props = 0;
+
+    @Override
+    public Persister instantiate(PropertiesProvider propertiesProvider) {
+        props++;
+        return this;
+    }
+
+    @Override
+    public void close() {
+    }
+
+}
index 1b4031ac57d3e3d4c2dd4331f012850a47f5962f..7141bc27d3b0fbe847b043dd55afa8d3d4efce18 100644 (file)
 package org.opendaylight.controller.netconf.persist.impl;
 
 import com.google.common.base.Optional;
-import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
-import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
 import org.opendaylight.controller.config.persist.api.Persister;
-import org.opendaylight.controller.config.persist.api.PropertiesProvider;
-import org.opendaylight.controller.config.persist.api.StorageAdapter;
 import org.opendaylight.controller.config.persist.storage.file.FileStorageAdapter;
 import org.opendaylight.controller.netconf.persist.impl.osgi.ConfigPersisterActivator;
 import org.opendaylight.controller.netconf.persist.impl.osgi.PropertiesProviderBaseImpl;
 
-import java.io.File;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Properties;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertThat;
-import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.fail;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertEquals;
 import static org.junit.matchers.JUnitMatchers.containsString;
-import static org.mockito.Matchers.anyString;
-import static org.mockito.Mockito.doCallRealMethod;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
+import static org.opendaylight.controller.netconf.persist.impl.PersisterAggregator.PersisterWithConfiguration;
+import static org.opendaylight.controller.netconf.persist.impl.PersisterAggregatorTest.TestingPropertiesProvider.loadFile;
 
 public class PersisterAggregatorTest {
-    @Mock
-    TestingPropertiesProvider propertiesProvider;
 
-    class TestingPropertiesProvider extends PropertiesProviderBaseImpl {
-        TestingPropertiesProvider() {
+    static class TestingPropertiesProvider extends PropertiesProviderBaseImpl {
+
+        private static Properties prop = new Properties();
+
+        public TestingPropertiesProvider() {
             super(null);
         }
 
+        public static TestingPropertiesProvider loadFile(String fileName) {
+            try {
+                prop.load(TestingPropertiesProvider.class.getClassLoader().getResourceAsStream(fileName));
+            } catch (IOException e) {
+                throw new RuntimeException(e);
+            }
+            return new TestingPropertiesProvider();
+        }
+
         @Override
         public String getFullKeyForReporting(String key) {
-            return "prefix." + key;
+            return ConfigPersisterActivator.NETCONF_CONFIG_PERSISTER + "." + key;
         }
 
         @Override
         public String getProperty(String key) {
-            throw new UnsupportedOperationException("should be mocked");
+            return prop.getProperty(getFullKeyForReporting(key));
         }
-    }
 
-    @Before
-    public void setUpMocks() {
-        MockitoAnnotations.initMocks(this);
-        doCallRealMethod().when(propertiesProvider).getFullKeyForReporting(anyString());
+        @Override
+        public String getPropertyWithoutPrefix(String fullKey){
+            return prop.getProperty(fullKey);
+        }
     }
 
-    @Ignore
     @Test
-    public void testFromProperties() throws Exception {
-        doReturn("").when(propertiesProvider).getProperty(ConfigPersisterActivator.NETCONF_CONFIG_PERSISTER);
-        doReturn(MockAdapter.class.getName()).when(propertiesProvider).getProperty(
-                ConfigPersisterActivator.STORAGE_ADAPTER_CLASS_PROP_SUFFIX);
-        doReturn("false").when(propertiesProvider).getProperty("readOnly");
+    public void testDummyAdapter() throws Exception {
+        PersisterAggregator persisterAggregator = PersisterAggregator.createFromProperties(loadFile("test1.properties"));
+        List<PersisterWithConfiguration> persisters = persisterAggregator.getPersisterWithConfigurations();
+        assertEquals(1, persisters.size());
+        PersisterWithConfiguration persister = persisters.get(0);
+        assertEquals(DummyAdapter.class.getName() ,persister.getStorage().getClass().getName());
+        assertFalse(persister.isReadOnly());
 
-        PersisterAggregator persisterAggregator = PersisterAggregator.createFromProperties(propertiesProvider);
         persisterAggregator.persistConfig(null);
         persisterAggregator.loadLastConfig();
         persisterAggregator.persistConfig(null);
         persisterAggregator.loadLastConfig();
 
-        assertEquals(2, MockAdapter.persist);
-        assertEquals(2, MockAdapter.load);
-        assertEquals(1, MockAdapter.props);
+        assertEquals(2, DummyAdapter.persist);
+        assertEquals(2, DummyAdapter.load);
+        assertEquals(1, DummyAdapter.props);
     }
 
-
-    @Ignore
     @Test
-    public void testFromProperties2() throws Exception {
-        String prefix = "";
-        doReturn(prefix).when(propertiesProvider).getProperty(ConfigPersisterActivator.NETCONF_CONFIG_PERSISTER);
-        doReturn(FileStorageAdapter.class.getName()).when(propertiesProvider).getProperty(
-                ConfigPersisterActivator.STORAGE_ADAPTER_CLASS_PROP_SUFFIX);
-
-        doReturn("target" + File.separator + "generated-test-sources" + File.separator + "testFile").when(
-                propertiesProvider).getProperty("prefix.properties.fileStorage");
-        doReturn("propertiesProvider").when(propertiesProvider).toString();
-        doReturn(null).when(propertiesProvider).getProperty("prefix.properties.numberOfBackups");
-
-        PersisterAggregator persisterAggregator = PersisterAggregator.createFromProperties(propertiesProvider);
+    public void testLoadFromPropertyFile() throws Exception {
+        PersisterAggregator persisterAggregator = PersisterAggregator.createFromProperties(loadFile("test2.properties"));
+        List<PersisterWithConfiguration> persisters = persisterAggregator.getPersisterWithConfigurations();
+        assertEquals(1, persisters.size());
+        PersisterWithConfiguration persister = persisters.get(0);
+        assertEquals(FileStorageAdapter.class.getName() ,persister.getStorage().getClass().getName());
+        assertFalse(persister.isReadOnly());
     }
 
-    @Ignore
     @Test
-    public void testFromProperties3() throws Exception {
-        doReturn("").when(propertiesProvider).getProperty(ConfigPersisterActivator.NETCONF_CONFIG_PERSISTER);
-        doReturn(FileStorageAdapter.class.getName()).when(propertiesProvider).getProperty(
-                ConfigPersisterActivator.STORAGE_ADAPTER_CLASS_PROP_SUFFIX);
-        doReturn("target" + File.separator + "generated-test-sources" + File.separator + "testFile").when(
-                propertiesProvider).getProperty("prefix.properties.fileStorage");
-        doReturn("false").when(propertiesProvider).getProperty("readOnly");
-        doReturn("propertiesProvider").when(propertiesProvider).toString();
-        doReturn("0").when(propertiesProvider).getProperty("prefix.properties.numberOfBackups");
+    public void testFileStorageNumberOfBackups() throws Exception {
         try {
-            PersisterAggregator.createFromProperties(propertiesProvider);
+            PersisterAggregator.createFromProperties(loadFile("test3.properties"));
             fail();
         } catch (RuntimeException e) {
             assertThat(
@@ -122,18 +110,18 @@ public class PersisterAggregatorTest {
 
     @Test
     public void loadLastConfig() throws Exception {
-        List<PersisterAggregator.PersisterWithConfiguration> persisterWithConfigurations = new ArrayList<>();
-        PersisterAggregator.PersisterWithConfiguration first = new PersisterAggregator.PersisterWithConfiguration(mock(Persister.class), false);
+        List<PersisterWithConfiguration> persisterWithConfigurations = new ArrayList<>();
+        PersisterWithConfiguration first = new PersisterWithConfiguration(mock(Persister.class), false);
 
         ConfigSnapshotHolder ignored = mock(ConfigSnapshotHolder.class);
-        doReturn(Optional.of(ignored)).when(first.storage).loadLastConfig(); // should be ignored
+        doReturn(Optional.of(ignored)).when(first.getStorage()).loadLastConfig(); // should be ignored
 
         ConfigSnapshotHolder used = mock(ConfigSnapshotHolder.class);
-        PersisterAggregator.PersisterWithConfiguration second = new PersisterAggregator.PersisterWithConfiguration(mock(Persister.class), false);
-        doReturn(Optional.of(used)).when(second.storage).loadLastConfig(); // should be used
+        PersisterWithConfiguration second = new PersisterWithConfiguration(mock(Persister.class), false);
+        doReturn(Optional.of(used)).when(second.getStorage()).loadLastConfig(); // should be used
 
-        PersisterAggregator.PersisterWithConfiguration third = new PersisterAggregator.PersisterWithConfiguration(mock(Persister.class), false);
-        doReturn(Optional.absent()).when(third.storage).loadLastConfig();
+        PersisterWithConfiguration third = new PersisterWithConfiguration(mock(Persister.class), false);
+        doReturn(Optional.absent()).when(third.getStorage()).loadLastConfig();
 
         persisterWithConfigurations.add(first);
         persisterWithConfigurations.add(second);
@@ -145,50 +133,4 @@ public class PersisterAggregatorTest {
         assertEquals(used, configSnapshotHolderOptional.get());
     }
 
-    @Ignore
-    @Test
-    public void test() throws Exception {
-//        Persister storage = mock(Persister.class);
-//        doReturn(null).when(storage).loadLastConfig();
-//        doNothing().when(storage).persistConfig(any(ConfigSnapshotHolder.class));
-//
-//        PersisterAggregator persister = new PersisterAggregator(storage);
-//        persister.loadLastConfig();
-//        persister.persistConfig(null);
-//
-//        verify(storage).loadLastConfig();
-//        verify(storage).persistConfig(any(ConfigSnapshotHolder.class));
-    }
-
-    public static class MockAdapter implements StorageAdapter, Persister {
-
-        static int persist = 0;
-
-        @Override
-        public void persistConfig(ConfigSnapshotHolder holder) throws IOException {
-            persist++;
-        }
-
-        static int load = 0;
-
-        @Override
-        public Optional<ConfigSnapshotHolder> loadLastConfig() throws IOException {
-            load++;
-            return Optional.absent();
-        }
-
-        static int props = 0;
-
-        @Override
-        public Persister instantiate(PropertiesProvider propertiesProvider) {
-            props++;
-            return this;
-        }
-
-        @Override
-        public void close() {
-        }
-
-    }
-
 }
diff --git a/opendaylight/netconf/config-persister-impl/src/test/resources/test1.properties b/opendaylight/netconf/config-persister-impl/src/test/resources/test1.properties
new file mode 100644 (file)
index 0000000..9380040
--- /dev/null
@@ -0,0 +1,3 @@
+netconf.config.persister.active=1
+netconf.config.persister.1.storageAdapterClass=org.opendaylight.controller.netconf.persist.impl.DummyAdapter
+netconf.config.persister.1.properties.fileStorage=configuration/initial/
\ No newline at end of file
diff --git a/opendaylight/netconf/config-persister-impl/src/test/resources/test2.properties b/opendaylight/netconf/config-persister-impl/src/test/resources/test2.properties
new file mode 100644 (file)
index 0000000..bdf2caa
--- /dev/null
@@ -0,0 +1,9 @@
+netconf.config.persister.active=2
+# read startup configuration
+netconf.config.persister.1.storageAdapterClass=org.opendaylight.controller.config.persist.storage.directory.DirectoryStorageAdapter
+netconf.config.persister.1.properties.directoryStorage=configuration/initial/
+netconf.config.persister.1.readonly=true
+
+netconf.config.persister.2.storageAdapterClass=org.opendaylight.controller.config.persist.storage.file.FileStorageAdapter
+netconf.config.persister.2.properties.fileStorage=configuration/current/controller.config.2.txt
+netconf.config.persister.2.properties.numberOfBackups=3
\ No newline at end of file
diff --git a/opendaylight/netconf/config-persister-impl/src/test/resources/test3.properties b/opendaylight/netconf/config-persister-impl/src/test/resources/test3.properties
new file mode 100644 (file)
index 0000000..c6716ce
--- /dev/null
@@ -0,0 +1,4 @@
+netconf.config.persister.active=3
+netconf.config.persister.3.storageAdapterClass=org.opendaylight.controller.config.persist.storage.file.FileStorageAdapter
+netconf.config.persister.3.properties.fileStorage=configuration/current/controller.config.2.txt
+netconf.config.persister.3.properties.numberOfBackups=0
\ No newline at end of file