2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.config.persist.storage.file.xml;
11 import com.google.common.base.Charsets;
13 import java.io.FileNotFoundException;
14 import java.io.IOException;
15 import java.nio.file.Files;
16 import java.util.List;
17 import java.util.SortedSet;
18 import java.util.TreeSet;
19 import junit.framework.Assert;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.mockito.Mockito;
23 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
24 import org.opendaylight.controller.config.persist.test.PropertiesProviderTest;
25 import static junit.framework.Assert.assertFalse;
26 import static org.hamcrest.CoreMatchers.is;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertThat;
30 public class FileStorageAdapterTest {
34 private static final String NON_EXISTENT_DIRECTORY = "./nonExistentDir/";
35 private static final String NON_EXISTENT_FILE = "nonExistent.txt";
38 public void setUp() throws Exception {
39 file = Files.createTempFile("testFilePersist", ".txt").toFile();
42 com.google.common.io.Files.write("", file, Charsets.UTF_8);
47 public void testNewFile() throws Exception {
48 XmlFileStorageAdapter storage = new XmlFileStorageAdapter();
49 PropertiesProviderTest pp = new PropertiesProviderTest();
50 pp.addProperty("fileStorage",NON_EXISTENT_DIRECTORY+NON_EXISTENT_FILE);
51 pp.addProperty("numberOfBackups",Integer.toString(Integer.MAX_VALUE));
52 storage.instantiate(pp);
54 final ConfigSnapshotHolder holder = new ConfigSnapshotHolder() {
56 public String getConfigSnapshot() {
57 return createConfig();
61 public SortedSet<String> getCapabilities() {
65 storage.persistConfig(holder);
67 storage.persistConfig(holder);
69 Assert.assertEquals(storage.toString().replace("\\","/"),"XmlFileStorageAdapter [storage="+NON_EXISTENT_DIRECTORY+NON_EXISTENT_FILE+"]");
70 delete(new File(NON_EXISTENT_DIRECTORY));
73 public void testFileAdapter() throws Exception {
74 XmlFileStorageAdapter storage = new XmlFileStorageAdapter();
75 PropertiesProviderTest pp = new PropertiesProviderTest();
76 pp.addProperty("fileStorage",file.getPath());
77 pp.addProperty("numberOfBackups",Integer.toString(Integer.MAX_VALUE));
78 storage.instantiate(pp);
80 final ConfigSnapshotHolder holder = new ConfigSnapshotHolder() {
82 public String getConfigSnapshot() {
83 return createConfig();
87 public SortedSet<String> getCapabilities() {
91 storage.persistConfig(holder);
93 storage.persistConfig(holder);
95 assertEquals(27, com.google.common.io.Files.readLines(file, Charsets.UTF_8).size());
96 List<ConfigSnapshotHolder> lastConf = storage.loadLastConfigs();
97 assertEquals(1, lastConf.size());
98 ConfigSnapshotHolder configSnapshotHolder = lastConf.get(0);
99 assertEquals("<config>2</config>",
100 configSnapshotHolder.getConfigSnapshot().replaceAll("\\s", ""));
101 assertEquals(createCaps(), configSnapshotHolder.getCapabilities());
103 storage = new XmlFileStorageAdapter();
104 storage.setFileStorage(file);
105 storage.setNumberOfBackups(Integer.MAX_VALUE);
107 List<ConfigSnapshotHolder> last = storage.loadLastConfigs();
108 Assert.assertEquals(createCaps(), last.get(0).getCapabilities());
111 private SortedSet<String> createCaps() {
112 SortedSet<String> caps = new TreeSet<>();
114 caps.add("cap1" + i);
115 caps.add("cap2" + i);
116 caps.add("urn:opendaylight:params:xml:ns:yang:controller:netty?module=netty&revision=2013-11-19" + i);
117 caps.add("capaaaa as dasfasdf s2" + i);
122 public void testFileAdapterOneBackup() throws Exception {
123 XmlFileStorageAdapter storage = new XmlFileStorageAdapter();
125 PropertiesProviderTest pp = new PropertiesProviderTest();
126 pp.addProperty("fileStorage",file.getPath());
127 pp.addProperty("numberOfBackups",Integer.toString(Integer.MAX_VALUE));
128 storage.instantiate(pp);
130 final ConfigSnapshotHolder holder = new ConfigSnapshotHolder() {
132 public String getConfigSnapshot() {
133 return createConfig();
137 public SortedSet<String> getCapabilities() {
141 storage.persistConfig(holder);
143 storage.persistConfig(holder);
145 assertEquals(27, com.google.common.io.Files.readLines(file, Charsets.UTF_8).size());
147 List<ConfigSnapshotHolder> lastConf = storage.loadLastConfigs();
148 assertEquals(1, lastConf.size());
149 ConfigSnapshotHolder configSnapshotHolder = lastConf.get(0);
150 assertEquals("<config>2</config>",
151 configSnapshotHolder.getConfigSnapshot().replaceAll("\\s", ""));
155 public void testFileAdapterOnlyTwoBackups() throws Exception {
156 XmlFileStorageAdapter storage = new XmlFileStorageAdapter();
157 storage.setFileStorage(file);
158 storage.setNumberOfBackups(2);
159 final ConfigSnapshotHolder holder = new ConfigSnapshotHolder() {
161 public String getConfigSnapshot() {
162 return createConfig();
166 public SortedSet<String> getCapabilities() {
170 storage.persistConfig(holder);
172 storage.persistConfig(holder);
173 storage.persistConfig(holder);
175 List<String> readLines = com.google.common.io.Files.readLines(file, Charsets.UTF_8);
176 assertEquals(27, readLines.size());
178 List<ConfigSnapshotHolder> lastConf = storage.loadLastConfigs();
179 assertEquals(1, lastConf.size());
180 ConfigSnapshotHolder configSnapshotHolder = lastConf.get(0);
181 assertEquals("<config>3</config>",
182 configSnapshotHolder.getConfigSnapshot().replaceAll("\\s", ""));
183 assertFalse(readLines.contains(holder.getConfigSnapshot()));
187 public void testNoLastConfig() throws Exception {
188 File file = Files.createTempFile("testFilePersist", ".txt").toFile();
191 XmlFileStorageAdapter storage = new XmlFileStorageAdapter();
192 storage.setFileStorage(file);
194 List<ConfigSnapshotHolder> elementOptional = storage.loadLastConfigs();
195 assertThat(elementOptional.size(), is(0));
198 @Test(expected = NullPointerException.class)
199 public void testNoProperties() throws Exception {
200 XmlFileStorageAdapter storage = new XmlFileStorageAdapter();
201 storage.loadLastConfigs();
204 @Test(expected = NullPointerException.class)
205 public void testNoProperties2() throws Exception {
206 XmlFileStorageAdapter storage = new XmlFileStorageAdapter();
207 storage.persistConfig(new ConfigSnapshotHolder() {
209 public String getConfigSnapshot() {
210 return Mockito.mock(String.class);
214 public SortedSet<String> getCapabilities() {
215 return new TreeSet<>();
220 static String createConfig() {
221 return "<config>" + i++ + "</config>";
224 private void delete(File f) throws IOException {
225 if (f.isDirectory()) {
226 for (File c : f.listFiles())
230 throw new FileNotFoundException("Failed to delete file: " + f);