X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fconfig-persister-impl%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Fpersist%2Fimpl%2FPersisterAggregatorTest.java;h=bef1237f980116f84f0f0ba8dc0f58b10028850b;hb=de4ef13b0a23eeec25e3cdd6461864d486707023;hp=7141bc27d3b0fbe847b043dd55afa8d3d4efce18;hpb=8ca41be0336a1a9c02fb46ee0961a66f0ab40bf8;p=controller.git 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 7141bc27d3..bef1237f98 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,25 +8,26 @@ package org.opendaylight.controller.netconf.persist.impl; -import com.google.common.base.Optional; +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; -import org.opendaylight.controller.config.persist.storage.file.FileStorageAdapter; +import org.opendaylight.controller.config.persist.storage.file.xml.XmlFileStorageAdapter; import org.opendaylight.controller.netconf.persist.impl.osgi.ConfigPersisterActivator; import org.opendaylight.controller.netconf.persist.impl.osgi.PropertiesProviderBaseImpl; import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Properties; +import static org.hamcrest.CoreMatchers.containsString; +import static org.junit.Assert.assertEquals; 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.junit.Assert.fail; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; import static org.opendaylight.controller.netconf.persist.impl.PersisterAggregator.PersisterWithConfiguration; @@ -36,19 +37,21 @@ public class PersisterAggregatorTest { static class TestingPropertiesProvider extends PropertiesProviderBaseImpl { - private static Properties prop = new Properties(); + private final Properties prop; - public TestingPropertiesProvider() { + public TestingPropertiesProvider(Properties prop) { super(null); + this.prop = prop; } public static TestingPropertiesProvider loadFile(String fileName) { + Properties prop = new Properties(); try { prop.load(TestingPropertiesProvider.class.getClassLoader().getResourceAsStream(fileName)); } catch (IOException e) { throw new RuntimeException(e); } - return new TestingPropertiesProvider(); + return new TestingPropertiesProvider(prop); } @Override @@ -73,26 +76,42 @@ public class PersisterAggregatorTest { List persisters = persisterAggregator.getPersisterWithConfigurations(); assertEquals(1, persisters.size()); PersisterWithConfiguration persister = persisters.get(0); - assertEquals(DummyAdapter.class.getName() ,persister.getStorage().getClass().getName()); + assertEquals(DummyAdapter.class.getName(), persister.getStorage().getClass().getName()); assertFalse(persister.isReadOnly()); persisterAggregator.persistConfig(null); - persisterAggregator.loadLastConfig(); + persisterAggregator.loadLastConfigs(); persisterAggregator.persistConfig(null); - persisterAggregator.loadLastConfig(); + persisterAggregator.loadLastConfigs(); assertEquals(2, DummyAdapter.persist); assertEquals(2, DummyAdapter.load); 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")); List persisters = persisterAggregator.getPersisterWithConfigurations(); assertEquals(1, persisters.size()); PersisterWithConfiguration persister = persisters.get(0); - assertEquals(FileStorageAdapter.class.getName() ,persister.getStorage().getClass().getName()); + assertEquals(XmlFileStorageAdapter.class.getName() ,persister.getStorage().getClass().getName()); assertFalse(persister.isReadOnly()); } @@ -108,29 +127,41 @@ public class PersisterAggregatorTest { } } + private ConfigSnapshotHolder mockHolder(String name){ + ConfigSnapshotHolder result = mock(ConfigSnapshotHolder.class); + doReturn("mock:" + name).when(result).toString(); + return result; + } + + private Persister mockPersister(String name){ + Persister result = mock(Persister.class); + doReturn("mock:" + name).when(result).toString(); + return result; + } + @Test public void loadLastConfig() throws Exception { List persisterWithConfigurations = new ArrayList<>(); PersisterWithConfiguration first = new PersisterWithConfiguration(mock(Persister.class), false); - ConfigSnapshotHolder ignored = mock(ConfigSnapshotHolder.class); - doReturn(Optional.of(ignored)).when(first.getStorage()).loadLastConfig(); // should be ignored + ConfigSnapshotHolder ignored = mockHolder("ignored"); + doReturn(Arrays.asList(ignored)).when(first.getStorage()).loadLastConfigs(); // should be ignored - ConfigSnapshotHolder used = mock(ConfigSnapshotHolder.class); - PersisterWithConfiguration second = new PersisterWithConfiguration(mock(Persister.class), false); - doReturn(Optional.of(used)).when(second.getStorage()).loadLastConfig(); // should be used - PersisterWithConfiguration third = new PersisterWithConfiguration(mock(Persister.class), false); - doReturn(Optional.absent()).when(third.getStorage()).loadLastConfig(); + ConfigSnapshotHolder used = mockHolder("used"); + PersisterWithConfiguration second = new PersisterWithConfiguration(mockPersister("p1"), false); + doReturn(Arrays.asList(used)).when(second.getStorage()).loadLastConfigs(); // should be used + + PersisterWithConfiguration third = new PersisterWithConfiguration(mockPersister("p2"), false); + doReturn(Arrays.asList()).when(third.getStorage()).loadLastConfigs(); persisterWithConfigurations.add(first); persisterWithConfigurations.add(second); persisterWithConfigurations.add(third); PersisterAggregator persisterAggregator = new PersisterAggregator(persisterWithConfigurations); - Optional configSnapshotHolderOptional = persisterAggregator.loadLastConfig(); - assertTrue(configSnapshotHolderOptional.isPresent()); - assertEquals(used, configSnapshotHolderOptional.get()); + List configSnapshotHolderOptional = persisterAggregator.loadLastConfigs(); + assertEquals(1, configSnapshotHolderOptional.size()); + assertEquals(used, configSnapshotHolderOptional.get(0)); } - }