updated test coverage for AutodetectDirectoryPersister
[controller.git] / opendaylight / config / config-persister-directory-autodetect-adapter / src / test / java / org / opendaylight / controller / config / persist / storage / directory / autodetect / AutodetectDirectoryPersisterTest.java
index 7e423914c216c02c88c92f58e367d6818b0fa86c..bcade93352670fcae086cf75f776242d0886276e 100644 (file)
@@ -7,21 +7,29 @@
  */
 package org.opendaylight.controller.config.persist.storage.directory.autodetect;
 
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
 import junit.framework.Assert;
 import org.junit.Test;
 import org.junit.matchers.JUnitMatchers;
 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
-
-import java.io.File;
-import java.util.List;
+import org.opendaylight.controller.config.persist.test.PropertiesProviderTest;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.fail;
 
 public class AutodetectDirectoryPersisterTest {
 
     @Test
     public void testCombined() throws Exception {
         File resourcePath = FileTypeTest.getResourceAsFile("/combined/1controller.txt.config");
+        File parentFile = resourcePath.getParentFile();
 
-        AutodetectDirectoryPersister persister = new AutodetectDirectoryPersister(resourcePath.getParentFile());
+        AutodetectDirectoryStorageAdapter adapter = new AutodetectDirectoryStorageAdapter();
+
+        PropertiesProviderTest pp = new PropertiesProviderTest();
+        pp.addProperty("directoryStorage",parentFile.getPath());
+        AutodetectDirectoryPersister persister = (AutodetectDirectoryPersister) adapter.instantiate(pp);
         List<ConfigSnapshotHolder> configs = persister.loadLastConfigs();
 
         Assert.assertEquals(2, configs.size());
@@ -34,4 +42,50 @@ public class AutodetectDirectoryPersisterTest {
 
         Assert.assertEquals(configs.get(0).getCapabilities(), configs.get(1).getCapabilities());
     }
+
+    @Test
+    public void testInvalidXml() throws Exception {
+        File resourcePath = FileTypeTest.getResourceAsFile("/bad_controller.xml.config");
+        File parentFile = resourcePath.getParentFile();
+
+        AutodetectDirectoryStorageAdapter adapter = new AutodetectDirectoryStorageAdapter();
+
+        PropertiesProviderTest pp = new PropertiesProviderTest();
+        pp.addProperty("directoryStorage",parentFile.getPath());
+        AutodetectDirectoryPersister persister = (AutodetectDirectoryPersister) adapter.instantiate(pp);
+        try {
+            List<ConfigSnapshotHolder> configs = persister.loadLastConfigs();
+            fail("An exception of type " + IllegalStateException.class + " was expected");
+        } catch (IllegalStateException ise){
+            String message = ise.getMessage();
+            assertThat(message, JUnitMatchers.containsString("Unable to restore configuration snapshot from "));
+        }
+
+    }
+
+    @Test
+    public void testPersistConfig() throws Exception {
+        File resourcePath = FileTypeTest.getResourceAsFile("/combined/1controller.txt.config");
+        File parentFile = resourcePath.getParentFile();
+
+        AutodetectDirectoryStorageAdapter adapter = new AutodetectDirectoryStorageAdapter();
+
+        PropertiesProviderTest pp = new PropertiesProviderTest();
+        pp.addProperty("directoryStorage",parentFile.getPath());
+        AutodetectDirectoryPersister persister = (AutodetectDirectoryPersister) adapter.instantiate(pp);
+        List<ConfigSnapshotHolder> configs = null;
+        try {
+            configs = persister.loadLastConfigs();
+        } catch (IOException e) {
+            fail("An exception of type " + UnsupportedOperationException.class + " was expected");
+        }
+        Assert.assertEquals(2, configs.size());
+        try {
+            persister.persistConfig(configs.get(0));
+        } catch (UnsupportedOperationException uoe){
+            String message = uoe.getMessage();
+            assertThat(message,JUnitMatchers.containsString("This adapter is read only. Please set readonly=true on class"));
+        }
+    }
+
 }