Merge "Increase timeout for waiting for broker service in sal-binding-it."
[controller.git] / opendaylight / config / config-persister-file-xml-adapter / src / test / java / org / opendaylight / controller / config / persist / storage / file / xml / FileStorageAdapterTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.config.persist.storage.file.xml;
10
11 import com.google.common.base.Charsets;
12 import java.io.File;
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;
29
30 public class FileStorageAdapterTest {
31
32     private static int i;
33     private File file;
34     private static final String NON_EXISTENT_DIRECTORY = "./nonExistentDir/";
35     private static final String NON_EXISTENT_FILE = "nonExistent.txt";
36
37     @Before
38     public void setUp() throws Exception {
39         file = Files.createTempFile("testFilePersist", ".txt").toFile();
40         if (!file.exists())
41             return;
42         com.google.common.io.Files.write("", file, Charsets.UTF_8);
43         i = 1;
44     }
45
46     @Test
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);
53
54         final ConfigSnapshotHolder holder = new ConfigSnapshotHolder() {
55             @Override
56             public String getConfigSnapshot() {
57                 return createConfig();
58             }
59
60             @Override
61             public SortedSet<String> getCapabilities() {
62                 return createCaps();
63             }
64         };
65         storage.persistConfig(holder);
66
67         storage.persistConfig(holder);
68
69         Assert.assertEquals(storage.toString().replace("\\","/"),"XmlFileStorageAdapter [storage="+NON_EXISTENT_DIRECTORY+NON_EXISTENT_FILE+"]");
70         delete(new File(NON_EXISTENT_DIRECTORY));
71     }
72     @Test
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);
79
80         final ConfigSnapshotHolder holder = new ConfigSnapshotHolder() {
81             @Override
82             public String getConfigSnapshot() {
83                 return createConfig();
84             }
85
86             @Override
87             public SortedSet<String> getCapabilities() {
88                 return createCaps();
89             }
90         };
91         storage.persistConfig(holder);
92
93         storage.persistConfig(holder);
94
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());
102
103         storage = new XmlFileStorageAdapter();
104         storage.setFileStorage(file);
105         storage.setNumberOfBackups(Integer.MAX_VALUE);
106
107         List<ConfigSnapshotHolder> last = storage.loadLastConfigs();
108         Assert.assertEquals(createCaps(), last.get(0).getCapabilities());
109     }
110
111     private SortedSet<String> createCaps() {
112         SortedSet<String> caps = new TreeSet<>();
113
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);
118         return caps;
119     }
120
121     @Test
122     public void testFileAdapterOneBackup() throws Exception {
123         XmlFileStorageAdapter storage = new XmlFileStorageAdapter();
124
125         PropertiesProviderTest pp = new PropertiesProviderTest();
126         pp.addProperty("fileStorage",file.getPath());
127         pp.addProperty("numberOfBackups",Integer.toString(Integer.MAX_VALUE));
128         storage.instantiate(pp);
129
130         final ConfigSnapshotHolder holder = new ConfigSnapshotHolder() {
131             @Override
132             public String getConfigSnapshot() {
133                 return createConfig();
134             }
135
136             @Override
137             public SortedSet<String> getCapabilities() {
138                 return createCaps();
139             }
140         };
141         storage.persistConfig(holder);
142
143         storage.persistConfig(holder);
144
145         assertEquals(27, com.google.common.io.Files.readLines(file, Charsets.UTF_8).size());
146
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", ""));
152     }
153
154     @Test
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() {
160             @Override
161             public String getConfigSnapshot() {
162                 return createConfig();
163             }
164
165             @Override
166             public SortedSet<String> getCapabilities() {
167                 return createCaps();
168             }
169         };
170         storage.persistConfig(holder);
171
172         storage.persistConfig(holder);
173         storage.persistConfig(holder);
174
175         List<String> readLines = com.google.common.io.Files.readLines(file, Charsets.UTF_8);
176         assertEquals(27, readLines.size());
177
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()));
184     }
185
186     @Test
187     public void testNoLastConfig() throws Exception {
188         File file = Files.createTempFile("testFilePersist", ".txt").toFile();
189         if (!file.exists())
190             return;
191         XmlFileStorageAdapter storage = new XmlFileStorageAdapter();
192         storage.setFileStorage(file);
193
194         List<ConfigSnapshotHolder> elementOptional = storage.loadLastConfigs();
195         assertThat(elementOptional.size(), is(0));
196     }
197
198     @Test(expected = NullPointerException.class)
199     public void testNoProperties() throws Exception {
200         XmlFileStorageAdapter storage = new XmlFileStorageAdapter();
201         storage.loadLastConfigs();
202     }
203
204     @Test(expected = NullPointerException.class)
205     public void testNoProperties2() throws Exception {
206         XmlFileStorageAdapter storage = new XmlFileStorageAdapter();
207         storage.persistConfig(new ConfigSnapshotHolder() {
208             @Override
209             public String getConfigSnapshot() {
210                 return Mockito.mock(String.class);
211             }
212
213             @Override
214             public SortedSet<String> getCapabilities() {
215                 return new TreeSet<>();
216             }
217         } );
218     }
219
220     static String createConfig() {
221         return "<config>" + i++ + "</config>";
222     }
223
224     private void delete(File f) throws IOException {
225         if (f.isDirectory()) {
226             for (File c : f.listFiles())
227                 delete(c);
228         }
229         if (!f.delete())
230             throw new FileNotFoundException("Failed to delete file: " + f);
231     }
232 }