<build>
<plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-jar-plugin</artifactId>
+ </plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
--- /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.test;
+
+import java.util.HashMap;
+import java.util.Map;
+import org.opendaylight.controller.config.persist.api.PropertiesProvider;
+
+public class PropertiesProviderTest implements PropertiesProvider {
+ private final Map<String,String> properties = new HashMap();
+
+ public void addProperty(String key,String value){
+ properties.put(key,value);
+ }
+ @Override
+ public String getProperty(String key) {
+ return properties.get(key);
+ }
+
+ @Override
+ public String getFullKeyForReporting(String key) {
+ return null;
+ }
+}
<artifactId>mockito-configuration</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>${project.groupId}</groupId>
+ <artifactId>config-persister-api</artifactId>
+ <type>test-jar</type>
+ <scope>test</scope>
+ </dependency>
</dependencies>
package org.opendaylight.controller.config.persist.storage.directory;
-import org.apache.commons.io.IOUtils;
-import org.junit.Test;
-import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
-
import java.io.File;
import java.util.Collections;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
-
+import org.apache.commons.io.IOUtils;
+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.test.PropertiesProviderTest;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
public class DirectoryStorageAdapterTest {
- DirectoryPersister tested;
+ Persister tested;
@Test
public void testEmptyDirectory() throws Exception {
File folder = new File("target/emptyFolder");
folder.mkdir();
- tested = new DirectoryPersister((folder));
+
+ PropertiesProviderTest pp = new PropertiesProviderTest();
+ pp.addProperty("directoryStorage",folder.getPath());
+ DirectoryStorageAdapter dsa = new DirectoryStorageAdapter();
+ tested = dsa.instantiate(pp);
assertEquals(Collections.<ConfigSnapshotHolder>emptyList(), tested.loadLastConfigs());
try {
@Test
public void testOneFile() throws Exception {
File folder = getFolder("oneFile");
- tested = new DirectoryPersister((folder));
+ PropertiesProviderTest pp = new PropertiesProviderTest();
+ pp.addProperty("directoryStorage",folder.getPath());
+ DirectoryStorageAdapter dsa = new DirectoryStorageAdapter();
+ tested = dsa.instantiate(pp);
+
List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
assertEquals(1, results.size());
ConfigSnapshotHolder result = results.get(0);
@Test
public void testTwoFiles() throws Exception {
File folder = getFolder("twoFiles");
- tested = new DirectoryPersister((folder));
+ PropertiesProviderTest pp = new PropertiesProviderTest();
+ pp.addProperty("directoryStorage",folder.getPath());
+ DirectoryStorageAdapter dsa = new DirectoryStorageAdapter();
+ tested = dsa.instantiate(pp);
+
List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
assertEquals(2, results.size());
assertSnapshot(results.get(0), "twoFilesExpected1");
<artifactId>mockito-configuration</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>${project.groupId}</groupId>
+ <artifactId>config-persister-api</artifactId>
+ <type>test-jar</type>
+ <scope>test</scope>
+ </dependency>
</dependencies>
package org.opendaylight.controller.config.persist.storage.directory.xml;
-import org.junit.Test;
-import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
-
import java.io.File;
import java.util.Collections;
import java.util.List;
import java.util.SortedSet;
-
+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.test.PropertiesProviderTest;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
public class DirectoryStorageAdapterTest {
- XmlDirectoryPersister tested;
+
+ Persister tested;
@Test
public void testEmptyDirectory() throws Exception {
@Test
public void testOneFile() throws Exception {
File folder = getFolder("oneFile");
- tested = new XmlDirectoryPersister((folder));
+
+ PropertiesProviderTest pp = new PropertiesProviderTest();
+ pp.addProperty("directoryStorage",folder.getPath());
+ XmlDirectoryStorageAdapter xmlDsa = new XmlDirectoryStorageAdapter();
+ tested = xmlDsa.instantiate(pp);
+
List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
assertEquals(1, results.size());
ConfigSnapshotHolder result = results.get(0);
@Test
public void testTwoFiles() throws Exception {
File folder = getFolder("twoFiles");
- tested = new XmlDirectoryPersister((folder));
+
+ PropertiesProviderTest pp = new PropertiesProviderTest();
+ pp.addProperty("directoryStorage",folder.getPath());
+ XmlDirectoryStorageAdapter xmlDsa = new XmlDirectoryStorageAdapter();
+ tested = xmlDsa.instantiate(pp);
+
List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
assertEquals(2, results.size());
<artifactId>mockito-configuration</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>${project.groupId}</groupId>
+ <artifactId>config-persister-api</artifactId>
+ <type>test-jar</type>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.io.Files;
-import org.apache.commons.lang3.StringUtils;
-import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
-import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolderImpl;
-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.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
+import org.apache.commons.lang3.StringUtils;
+import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
+import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolderImpl;
+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.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
- * StorageAdapter that stores configuration in a plan file.
+ * StorageAdapter that stores configuration in a plain file.
*/
public class FileStorageAdapter implements StorageAdapter, Persister {
private static final Logger logger = LoggerFactory.getLogger(FileStorageAdapter.class);
import com.google.common.base.Charsets;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
-import org.junit.Before;
-import org.junit.Test;
-import org.mockito.Mockito;
-import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
-
import java.io.File;
import java.nio.file.Files;
import java.util.Collection;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
-
+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.api.Persister;
+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;
i = 1;
}
+
+ @Test
+ public void testFileAdapterAsPersister() throws Exception {
+ FileStorageAdapter storage = new FileStorageAdapter();
+ PropertiesProviderTest pp = new PropertiesProviderTest();
+ pp.addProperty("fileStorage",file.getPath());
+ pp.addProperty("numberOfBackups",Integer.toString(Integer.MAX_VALUE));
+
+ Persister configPersister = storage.instantiate(pp);
+ final ConfigSnapshotHolder holder = new ConfigSnapshotHolder() {
+ @Override
+ public String getConfigSnapshot() {
+ return createConfig();
+ }
+
+ @Override
+ public SortedSet<String> getCapabilities() {
+ return createCaps();
+ }
+ };
+ configPersister.persistConfig(holder);
+
+ configPersister.persistConfig(holder);
+
+ Collection<String> readLines = Collections2.filter(com.google.common.io.Files.readLines(file, Charsets.UTF_8),
+ new Predicate<String>() {
+
+ @Override
+ public boolean apply(String input) {
+ if (input.equals(""))
+ return false;
+ return true;
+ }
+ });
+ assertEquals(14, readLines.size());
+
+ List<ConfigSnapshotHolder> lastConf = storage.loadLastConfigs();
+ assertEquals(1, lastConf.size());
+ ConfigSnapshotHolder configSnapshotHolder = lastConf.get(0);
+ assertEquals("<config>2</config>",
+ configSnapshotHolder.getConfigSnapshot().replaceAll("\\s", ""));
+ assertEquals(createCaps(), configSnapshotHolder.getCapabilities());
+ }
@Test
public void testFileAdapter() throws Exception {
FileStorageAdapter storage = new FileStorageAdapter();
<artifactId>config-persister-api</artifactId>
<version>${config.version}</version>
</dependency>
+ <dependency>
+ <groupId>${project.groupId}</groupId>
+ <artifactId>config-persister-api</artifactId>
+ <version>${config.version}</version>
+ <type>test-jar</type>
+ </dependency>
<dependency>
<groupId>org.opendaylight.bgpcep</groupId>
<artifactId>mockito-configuration</artifactId>