From: Milos Fabian Date: Fri, 29 Nov 2013 11:46:26 +0000 (+0100) Subject: Fixed PersisterAggregatorTest. X-Git-Tag: jenkins-controller-bulk-release-prepare-only-2-1~293^2 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=refs%2Fchanges%2F47%2F3247%2F1 Fixed PersisterAggregatorTest. Change-Id: Iefc264ddc026df33f5a8124c17835f177627cd50 Signed-off-by: Milos Fabian --- diff --git a/opendaylight/netconf/config-persister-impl/pom.xml b/opendaylight/netconf/config-persister-impl/pom.xml index ce25de215e..6b3dfc7173 100644 --- a/opendaylight/netconf/config-persister-impl/pom.xml +++ b/opendaylight/netconf/config-persister-impl/pom.xml @@ -58,6 +58,12 @@ commons-io test + + org.opendaylight.controller + config-persister-directory-adapter + ${parent.version} + test + 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 7add448025..e109ebec88 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 @@ -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 index 0000000000..7a11b9cd6f --- /dev/null +++ b/opendaylight/netconf/config-persister-impl/src/test/java/org/opendaylight/controller/netconf/persist/impl/DummyAdapter.java @@ -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 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/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 1b4031ac57..7141bc27d3 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 @@ -9,109 +9,97 @@ 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 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 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 persisterWithConfigurations = new ArrayList<>(); - PersisterAggregator.PersisterWithConfiguration first = new PersisterAggregator.PersisterWithConfiguration(mock(Persister.class), false); + List 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 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 index 0000000000..93800401d7 --- /dev/null +++ b/opendaylight/netconf/config-persister-impl/src/test/resources/test1.properties @@ -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 index 0000000000..bdf2caaf02 --- /dev/null +++ b/opendaylight/netconf/config-persister-impl/src/test/resources/test2.properties @@ -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 index 0000000000..c6716cea6a --- /dev/null +++ b/opendaylight/netconf/config-persister-impl/src/test/resources/test3.properties @@ -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