X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fconfig%2Fconfig-persister-file-xml-adapter%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fconfig%2Fpersist%2Fstorage%2Ffile%2Fxml%2FFileStorageAdapterTest.java;h=dc1fc78a243a2af4658ce3cc180f82cdfe4de671;hp=5322f6357a01b0a3e01e1d58a8d943113b942562;hb=2fb20e0091de8945147ca984721b23d28161aa8d;hpb=acd7a3cdef79a6b1fdcf0f68d3beab7422f4e9c7 diff --git a/opendaylight/config/config-persister-file-xml-adapter/src/test/java/org/opendaylight/controller/config/persist/storage/file/xml/FileStorageAdapterTest.java b/opendaylight/config/config-persister-file-xml-adapter/src/test/java/org/opendaylight/controller/config/persist/storage/file/xml/FileStorageAdapterTest.java index 5322f6357a..dc1fc78a24 100644 --- a/opendaylight/config/config-persister-file-xml-adapter/src/test/java/org/opendaylight/controller/config/persist/storage/file/xml/FileStorageAdapterTest.java +++ b/opendaylight/config/config-persister-file-xml-adapter/src/test/java/org/opendaylight/controller/config/persist/storage/file/xml/FileStorageAdapterTest.java @@ -8,7 +8,17 @@ package org.opendaylight.controller.config.persist.storage.file.xml; +import static junit.framework.Assert.assertTrue; +import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; + import com.google.common.base.Charsets; +import com.google.common.collect.Sets; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; @@ -16,16 +26,11 @@ import java.nio.file.Files; import java.util.List; import java.util.SortedSet; import java.util.TreeSet; -import junit.framework.Assert; +import org.junit.After; import org.junit.Before; import org.junit.Test; -import org.mockito.Mockito; import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder; import org.opendaylight.controller.config.persist.test.PropertiesProviderTest; -import static junit.framework.Assert.assertFalse; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThat; public class FileStorageAdapterTest { @@ -33,19 +38,27 @@ public class FileStorageAdapterTest { private File file; private static final String NON_EXISTENT_DIRECTORY = "./nonExistentDir/"; private static final String NON_EXISTENT_FILE = "nonExistent.txt"; + private XmlFileStorageAdapter storage; @Before public void setUp() throws Exception { file = Files.createTempFile("testFilePersist", ".txt").toFile(); - if (!file.exists()) + file.deleteOnExit(); + if (!file.exists()) { return; + } com.google.common.io.Files.write("", file, Charsets.UTF_8); i = 1; + storage = new XmlFileStorageAdapter(); + } + + @After + public void tearDown() throws Exception { + storage.reset(); } @Test public void testNewFile() throws Exception { - XmlFileStorageAdapter storage = new XmlFileStorageAdapter(); PropertiesProviderTest pp = new PropertiesProviderTest(); pp.addProperty("fileStorage",NON_EXISTENT_DIRECTORY+NON_EXISTENT_FILE); pp.addProperty("numberOfBackups",Integer.toString(Integer.MAX_VALUE)); @@ -66,12 +79,12 @@ public class FileStorageAdapterTest { storage.persistConfig(holder); - Assert.assertEquals(storage.toString().replace("\\","/"),"XmlFileStorageAdapter [storage="+NON_EXISTENT_DIRECTORY+NON_EXISTENT_FILE+"]"); + assertEquals(storage.toString().replace("\\","/"),"XmlFileStorageAdapter [storage="+NON_EXISTENT_DIRECTORY+NON_EXISTENT_FILE+"]"); delete(new File(NON_EXISTENT_DIRECTORY)); } + @Test public void testFileAdapter() throws Exception { - XmlFileStorageAdapter storage = new XmlFileStorageAdapter(); PropertiesProviderTest pp = new PropertiesProviderTest(); pp.addProperty("fileStorage",file.getPath()); pp.addProperty("numberOfBackups",Integer.toString(Integer.MAX_VALUE)); @@ -92,12 +105,11 @@ public class FileStorageAdapterTest { storage.persistConfig(holder); - assertEquals(27, com.google.common.io.Files.readLines(file, Charsets.UTF_8).size()); + assertEquals(29, com.google.common.io.Files.readLines(file, Charsets.UTF_8).size()); List lastConf = storage.loadLastConfigs(); assertEquals(1, lastConf.size()); ConfigSnapshotHolder configSnapshotHolder = lastConf.get(0); - assertEquals("2", - configSnapshotHolder.getConfigSnapshot().replaceAll("\\s", "")); + assertXMLEqual("2", configSnapshotHolder.getConfigSnapshot()); assertEquals(createCaps(), configSnapshotHolder.getCapabilities()); storage = new XmlFileStorageAdapter(); @@ -105,7 +117,7 @@ public class FileStorageAdapterTest { storage.setNumberOfBackups(Integer.MAX_VALUE); List last = storage.loadLastConfigs(); - Assert.assertEquals(createCaps(), last.get(0).getCapabilities()); + assertEquals(createCaps(), last.get(0).getCapabilities()); } private SortedSet createCaps() { @@ -120,8 +132,6 @@ public class FileStorageAdapterTest { @Test public void testFileAdapterOneBackup() throws Exception { - XmlFileStorageAdapter storage = new XmlFileStorageAdapter(); - PropertiesProviderTest pp = new PropertiesProviderTest(); pp.addProperty("fileStorage",file.getPath()); pp.addProperty("numberOfBackups",Integer.toString(Integer.MAX_VALUE)); @@ -142,18 +152,77 @@ public class FileStorageAdapterTest { storage.persistConfig(holder); - assertEquals(27, com.google.common.io.Files.readLines(file, Charsets.UTF_8).size()); + assertEquals(29, com.google.common.io.Files.readLines(file, Charsets.UTF_8).size()); List lastConf = storage.loadLastConfigs(); assertEquals(1, lastConf.size()); ConfigSnapshotHolder configSnapshotHolder = lastConf.get(0); - assertEquals("2", - configSnapshotHolder.getConfigSnapshot().replaceAll("\\s", "")); + assertXMLEqual("2", configSnapshotHolder.getConfigSnapshot()); + } + + @Test + public void testWithFeatures() throws Exception { + PropertiesProviderTest pp = new PropertiesProviderTest(); + pp.addProperty("fileStorage",file.getPath()); + pp.addProperty("numberOfBackups",Integer.toString(Integer.MAX_VALUE)); + storage.instantiate(pp); + + final ConfigSnapshotHolder holder = new ConfigSnapshotHolder() { + @Override + public String getConfigSnapshot() { + return createConfig(); + } + + @Override + public SortedSet getCapabilities() { + return createCaps(); + } + }; + final FeatureListProvider mock = mock(FeatureListProvider.class); + + doReturn(Sets.newHashSet("f1-11", "f2-22")).when(mock).listFeatures(); + storage.setFeaturesService(mock); + storage.persistConfig(holder); + + assertEquals(20, com.google.common.io.Files.readLines(file, Charsets.UTF_8).size()); + + List lastConf = storage.loadLastConfigs(); + assertEquals(1, lastConf.size()); + ConfigSnapshotHolder configSnapshotHolder = lastConf.get(0); + assertXMLEqual("1", configSnapshotHolder.getConfigSnapshot()); + assertEquals(Sets.newHashSet("f1-11", "f2-22"), storage.getPersistedFeatures()); + } + + @Test + public void testNoFeaturesStored() throws Exception { + PropertiesProviderTest pp = new PropertiesProviderTest(); + pp.addProperty("fileStorage",file.getPath()); + pp.addProperty("numberOfBackups",Integer.toString(Integer.MAX_VALUE)); + storage.instantiate(pp); + + com.google.common.io.Files.write("\n" + + "\n" + + " \n" + + " \n" + + " \n" + + " cap12\n" + + " \n" + + " \n" + + " 1\n" + + " \n" + + " \n" + + " \n" + + "", file, Charsets.UTF_8); + + List lastConf = storage.loadLastConfigs(); + assertEquals(1, lastConf.size()); + ConfigSnapshotHolder configSnapshotHolder = lastConf.get(0); + assertXMLEqual("1", configSnapshotHolder.getConfigSnapshot()); + assertTrue(storage.getPersistedFeatures().isEmpty()); } @Test public void testFileAdapterOnlyTwoBackups() throws Exception { - XmlFileStorageAdapter storage = new XmlFileStorageAdapter(); storage.setFileStorage(file); storage.setNumberOfBackups(2); final ConfigSnapshotHolder holder = new ConfigSnapshotHolder() { @@ -173,60 +242,67 @@ public class FileStorageAdapterTest { storage.persistConfig(holder); List readLines = com.google.common.io.Files.readLines(file, Charsets.UTF_8); - assertEquals(27, readLines.size()); + assertEquals(29, readLines.size()); List lastConf = storage.loadLastConfigs(); assertEquals(1, lastConf.size()); ConfigSnapshotHolder configSnapshotHolder = lastConf.get(0); - assertEquals("3", - configSnapshotHolder.getConfigSnapshot().replaceAll("\\s", "")); + assertXMLEqual("3", configSnapshotHolder.getConfigSnapshot()); assertFalse(readLines.contains(holder.getConfigSnapshot())); + assertTrue(storage.getPersistedFeatures().isEmpty()); } @Test public void testNoLastConfig() throws Exception { File file = Files.createTempFile("testFilePersist", ".txt").toFile(); - if (!file.exists()) + file.deleteOnExit(); + if (!file.exists()) { return; - XmlFileStorageAdapter storage = new XmlFileStorageAdapter(); - storage.setFileStorage(file); + } + try (XmlFileStorageAdapter storage = new XmlFileStorageAdapter()) { + storage.setFileStorage(file); - List elementOptional = storage.loadLastConfigs(); - assertThat(elementOptional.size(), is(0)); + List elementOptional = storage.loadLastConfigs(); + assertThat(elementOptional.size(), is(0)); + } } @Test(expected = NullPointerException.class) public void testNoProperties() throws Exception { - XmlFileStorageAdapter storage = new XmlFileStorageAdapter(); - storage.loadLastConfigs(); + try (XmlFileStorageAdapter storage = new XmlFileStorageAdapter()) { + storage.loadLastConfigs(); + } } @Test(expected = NullPointerException.class) public void testNoProperties2() throws Exception { - XmlFileStorageAdapter storage = new XmlFileStorageAdapter(); - storage.persistConfig(new ConfigSnapshotHolder() { - @Override - public String getConfigSnapshot() { - return Mockito.mock(String.class); - } - - @Override - public SortedSet getCapabilities() { - return new TreeSet<>(); - } - } ); + try (XmlFileStorageAdapter storage = new XmlFileStorageAdapter()) { + storage.persistConfig(new ConfigSnapshotHolder() { + @Override + public String getConfigSnapshot() { + return mock(String.class); + } + + @Override + public SortedSet getCapabilities() { + return new TreeSet<>(); + } + }); + } } static String createConfig() { return "" + i++ + ""; } - private void delete(File f) throws IOException { + private void delete(final File f) throws IOException { if (f.isDirectory()) { - for (File c : f.listFiles()) + for (File c : f.listFiles()) { delete(c); + } } - if (!f.delete()) + if (!f.delete()) { throw new FileNotFoundException("Failed to delete file: " + f); + } } }