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 static org.junit.Assert.assertFalse;
12 import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
13 import static org.hamcrest.CoreMatchers.is;
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertThat;
16 import com.google.common.base.Charsets;
18 import java.io.FileNotFoundException;
19 import java.io.IOException;
20 import java.nio.file.Files;
21 import java.util.List;
22 import java.util.SortedSet;
23 import java.util.TreeSet;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.Mockito;
27 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
28 import org.opendaylight.controller.config.persist.test.PropertiesProviderTest;
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();
43 com.google.common.io.Files.write("", file, Charsets.UTF_8);
48 public void testNewFile() throws Exception {
49 XmlFileStorageAdapter storage = new XmlFileStorageAdapter();
50 PropertiesProviderTest pp = new PropertiesProviderTest();
51 pp.addProperty("fileStorage",NON_EXISTENT_DIRECTORY+NON_EXISTENT_FILE);
52 pp.addProperty("numberOfBackups",Integer.toString(Integer.MAX_VALUE));
53 storage.instantiate(pp);
55 final ConfigSnapshotHolder holder = new ConfigSnapshotHolder() {
57 public String getConfigSnapshot() {
58 return createConfig();
62 public SortedSet<String> getCapabilities() {
66 storage.persistConfig(holder);
68 storage.persistConfig(holder);
70 assertEquals(storage.toString().replace("\\","/"),"XmlFileStorageAdapter [storage="+NON_EXISTENT_DIRECTORY+NON_EXISTENT_FILE+"]");
71 delete(new File(NON_EXISTENT_DIRECTORY));
74 public void testFileAdapter() throws Exception {
75 XmlFileStorageAdapter storage = new XmlFileStorageAdapter();
76 PropertiesProviderTest pp = new PropertiesProviderTest();
77 pp.addProperty("fileStorage",file.getPath());
78 pp.addProperty("numberOfBackups",Integer.toString(Integer.MAX_VALUE));
79 storage.instantiate(pp);
81 final ConfigSnapshotHolder holder = new ConfigSnapshotHolder() {
83 public String getConfigSnapshot() {
84 return createConfig();
88 public SortedSet<String> getCapabilities() {
92 storage.persistConfig(holder);
94 storage.persistConfig(holder);
96 assertEquals(27, com.google.common.io.Files.readLines(file, Charsets.UTF_8).size());
97 List<ConfigSnapshotHolder> lastConf = storage.loadLastConfigs();
98 assertEquals(1, lastConf.size());
99 ConfigSnapshotHolder configSnapshotHolder = lastConf.get(0);
100 assertXMLEqual("<config>2</config>", configSnapshotHolder.getConfigSnapshot());
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 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 assertXMLEqual("<config>2</config>", configSnapshotHolder.getConfigSnapshot());
154 public void testFileAdapterOnlyTwoBackups() throws Exception {
155 XmlFileStorageAdapter storage = new XmlFileStorageAdapter();
156 storage.setFileStorage(file);
157 storage.setNumberOfBackups(2);
158 final ConfigSnapshotHolder holder = new ConfigSnapshotHolder() {
160 public String getConfigSnapshot() {
161 return createConfig();
165 public SortedSet<String> getCapabilities() {
169 storage.persistConfig(holder);
171 storage.persistConfig(holder);
172 storage.persistConfig(holder);
174 List<String> readLines = com.google.common.io.Files.readLines(file, Charsets.UTF_8);
175 assertEquals(27, readLines.size());
177 List<ConfigSnapshotHolder> lastConf = storage.loadLastConfigs();
178 assertEquals(1, lastConf.size());
179 ConfigSnapshotHolder configSnapshotHolder = lastConf.get(0);
180 assertXMLEqual("<config>3</config>", configSnapshotHolder.getConfigSnapshot());
181 assertFalse(readLines.contains(holder.getConfigSnapshot()));
185 public void testNoLastConfig() throws Exception {
186 File file = Files.createTempFile("testFilePersist", ".txt").toFile();
187 if (!file.exists()) {
190 XmlFileStorageAdapter storage = new XmlFileStorageAdapter();
191 storage.setFileStorage(file);
193 List<ConfigSnapshotHolder> elementOptional = storage.loadLastConfigs();
194 assertThat(elementOptional.size(), is(0));
197 @Test(expected = NullPointerException.class)
198 public void testNoProperties() throws Exception {
199 XmlFileStorageAdapter storage = new XmlFileStorageAdapter();
200 storage.loadLastConfigs();
203 @Test(expected = NullPointerException.class)
204 public void testNoProperties2() throws Exception {
205 XmlFileStorageAdapter storage = new XmlFileStorageAdapter();
206 storage.persistConfig(new ConfigSnapshotHolder() {
208 public String getConfigSnapshot() {
209 return Mockito.mock(String.class);
213 public SortedSet<String> getCapabilities() {
214 return new TreeSet<>();
219 static String createConfig() {
220 return "<config>" + i++ + "</config>";
223 private void delete(final File f) throws IOException {
224 if (f.isDirectory()) {
225 for (File c : f.listFiles()) {
230 throw new FileNotFoundException("Failed to delete file: " + f);