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 org.opendaylight.controller.config.persist.test.PropertiesProviderTest;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.fail;
public class AutodetectDirectoryPersisterTest {
String snapFromXml = configs.get(1).getConfigSnapshot();
org.junit.Assert.assertThat(snapFromXml, JUnitMatchers.containsString("<config>xml</config>"));
- Assert.assertEquals(configs.get(0).getCapabilities(), configs.get(1).getCapabilities()); }
+ 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"));
+ }
+ }
+
}
--- /dev/null
+<snapshot>
+ <required-capabilities>
+ <capability
+ <capability>cap2</capability>
+ <capability>capa a</capability>
+ </required-capabilities>
+ <configuration>
+ <config>xml</config>
+ </configuration>
+</snapshot>
\ No newline at end of file
+++ /dev/null
-/*
- * 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.config.persist.storage.file.xml.model;
-
-import javax.xml.bind.ValidationEventHandler;
-import javax.xml.bind.annotation.DomHandler;
-import javax.xml.transform.Source;
-import javax.xml.transform.stream.StreamResult;
-import javax.xml.transform.stream.StreamSource;
-import java.io.StringReader;
-import java.io.StringWriter;
-
-class CapabilityHandler implements DomHandler<String, StreamResult> {
-
- private static final String START_TAG = "<capability>";
- private static final String END_TAG = "</capability>";
-
- private StringWriter xmlWriter = new StringWriter();
-
- public StreamResult createUnmarshaller(ValidationEventHandler errorHandler) {
- xmlWriter.getBuffer().setLength(0);
- return new StreamResult(xmlWriter);
- }
-
- public String getElement(StreamResult rt) {
- String xml = rt.getWriter().toString();
- int beginIndex = xml.indexOf(START_TAG) + START_TAG.length();
- int endIndex = xml.indexOf(END_TAG);
- return xml.substring(beginIndex, endIndex);
- }
-
- public Source marshal(String n, ValidationEventHandler errorHandler) {
- try {
- String xml = START_TAG + n.trim() + END_TAG;
- StringReader xmlReader = new StringReader(xml);
- return new StreamSource(xmlReader);
- } catch(Exception e) {
- throw new RuntimeException(e);
- }
- }
-
-}
import com.google.common.base.Charsets;
import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
import java.util.SortedSet;
private static int i;
private File file;
+ private static final String NON_EXISTENT_DIRECTORY = "./nonExistentDir/";
+ private static final String NON_EXISTENT_FILE = "nonExistent.txt";
@Before
public void setUp() throws Exception {
i = 1;
}
+ @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));
+ storage.instantiate(pp);
+
+ final ConfigSnapshotHolder holder = new ConfigSnapshotHolder() {
+ @Override
+ public String getConfigSnapshot() {
+ return createConfig();
+ }
+
+ @Override
+ public SortedSet<String> getCapabilities() {
+ return createCaps();
+ }
+ };
+ storage.persistConfig(holder);
+
+ storage.persistConfig(holder);
+
+ Assert.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();
return "<config>" + i++ + "</config>";
}
+ private void delete(File f) throws IOException {
+ if (f.isDirectory()) {
+ for (File c : f.listFiles())
+ delete(c);
+ }
+ if (!f.delete())
+ throw new FileNotFoundException("Failed to delete file: " + f);
+ }
}