Fix resource leaks in test cases
[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 static junit.framework.Assert.assertTrue;
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.assertFalse;
16 import static org.junit.Assert.assertThat;
17 import static org.mockito.Mockito.doReturn;
18 import static org.mockito.Mockito.mock;
19
20 import com.google.common.base.Charsets;
21 import com.google.common.collect.Sets;
22 import java.io.File;
23 import java.io.FileNotFoundException;
24 import java.io.IOException;
25 import java.nio.file.Files;
26 import java.util.List;
27 import java.util.SortedSet;
28 import java.util.TreeSet;
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
33 import org.opendaylight.controller.config.persist.test.PropertiesProviderTest;
34
35 public class FileStorageAdapterTest {
36
37     private static int i;
38     private File file;
39     private static final String NON_EXISTENT_DIRECTORY = "./nonExistentDir/";
40     private static final String NON_EXISTENT_FILE = "nonExistent.txt";
41     private XmlFileStorageAdapter storage;
42
43     @Before
44     public void setUp() throws Exception {
45         file = Files.createTempFile("testFilePersist", ".txt").toFile();
46         file.deleteOnExit();
47         if (!file.exists()) {
48             return;
49         }
50         com.google.common.io.Files.write("", file, Charsets.UTF_8);
51         i = 1;
52         storage = new XmlFileStorageAdapter();
53     }
54
55     @After
56     public void tearDown() throws Exception {
57         storage.reset();
58     }
59
60     @Test
61     public void testNewFile() throws Exception {
62         PropertiesProviderTest pp = new PropertiesProviderTest();
63         pp.addProperty("fileStorage",NON_EXISTENT_DIRECTORY+NON_EXISTENT_FILE);
64         pp.addProperty("numberOfBackups",Integer.toString(Integer.MAX_VALUE));
65         storage.instantiate(pp);
66
67         final ConfigSnapshotHolder holder = new ConfigSnapshotHolder() {
68             @Override
69             public String getConfigSnapshot() {
70                 return createConfig();
71             }
72
73             @Override
74             public SortedSet<String> getCapabilities() {
75                 return createCaps();
76             }
77         };
78         storage.persistConfig(holder);
79
80         storage.persistConfig(holder);
81
82         assertEquals(storage.toString().replace("\\","/"),"XmlFileStorageAdapter [storage="+NON_EXISTENT_DIRECTORY+NON_EXISTENT_FILE+"]");
83         delete(new File(NON_EXISTENT_DIRECTORY));
84     }
85
86     @Test
87     public void testFileAdapter() throws Exception {
88         PropertiesProviderTest pp = new PropertiesProviderTest();
89         pp.addProperty("fileStorage",file.getPath());
90         pp.addProperty("numberOfBackups",Integer.toString(Integer.MAX_VALUE));
91         storage.instantiate(pp);
92
93         final ConfigSnapshotHolder holder = new ConfigSnapshotHolder() {
94             @Override
95             public String getConfigSnapshot() {
96                 return createConfig();
97             }
98
99             @Override
100             public SortedSet<String> getCapabilities() {
101                 return createCaps();
102             }
103         };
104         storage.persistConfig(holder);
105
106         storage.persistConfig(holder);
107
108         assertEquals(29, com.google.common.io.Files.readLines(file, Charsets.UTF_8).size());
109         List<ConfigSnapshotHolder> lastConf = storage.loadLastConfigs();
110         assertEquals(1, lastConf.size());
111         ConfigSnapshotHolder configSnapshotHolder = lastConf.get(0);
112         assertXMLEqual("<config>2</config>", configSnapshotHolder.getConfigSnapshot());
113         assertEquals(createCaps(), configSnapshotHolder.getCapabilities());
114
115         storage = new XmlFileStorageAdapter();
116         storage.setFileStorage(file);
117         storage.setNumberOfBackups(Integer.MAX_VALUE);
118
119         List<ConfigSnapshotHolder> last = storage.loadLastConfigs();
120         assertEquals(createCaps(), last.get(0).getCapabilities());
121     }
122
123     private SortedSet<String> createCaps() {
124         SortedSet<String> caps = new TreeSet<>();
125
126         caps.add("cap1" + i);
127         caps.add("cap2" + i);
128         caps.add("urn:opendaylight:params:xml:ns:yang:controller:netty?module=netty&revision=2013-11-19" + i);
129         caps.add("capaaaa as dasfasdf s2" + i);
130         return caps;
131     }
132
133     @Test
134     public void testFileAdapterOneBackup() throws Exception {
135         PropertiesProviderTest pp = new PropertiesProviderTest();
136         pp.addProperty("fileStorage",file.getPath());
137         pp.addProperty("numberOfBackups",Integer.toString(Integer.MAX_VALUE));
138         storage.instantiate(pp);
139
140         final ConfigSnapshotHolder holder = new ConfigSnapshotHolder() {
141             @Override
142             public String getConfigSnapshot() {
143                 return createConfig();
144             }
145
146             @Override
147             public SortedSet<String> getCapabilities() {
148                 return createCaps();
149             }
150         };
151         storage.persistConfig(holder);
152
153         storage.persistConfig(holder);
154
155         assertEquals(29, com.google.common.io.Files.readLines(file, Charsets.UTF_8).size());
156
157         List<ConfigSnapshotHolder> lastConf = storage.loadLastConfigs();
158         assertEquals(1, lastConf.size());
159         ConfigSnapshotHolder configSnapshotHolder = lastConf.get(0);
160         assertXMLEqual("<config>2</config>", configSnapshotHolder.getConfigSnapshot());
161     }
162
163     @Test
164     public void testWithFeatures() throws Exception {
165         PropertiesProviderTest pp = new PropertiesProviderTest();
166         pp.addProperty("fileStorage",file.getPath());
167         pp.addProperty("numberOfBackups",Integer.toString(Integer.MAX_VALUE));
168         storage.instantiate(pp);
169
170         final ConfigSnapshotHolder holder = new ConfigSnapshotHolder() {
171             @Override
172             public String getConfigSnapshot() {
173                 return createConfig();
174             }
175
176             @Override
177             public SortedSet<String> getCapabilities() {
178                 return createCaps();
179             }
180         };
181         final FeatureListProvider mock = mock(FeatureListProvider.class);
182
183         doReturn(Sets.newHashSet("f1-11", "f2-22")).when(mock).listFeatures();
184         storage.setFeaturesService(mock);
185         storage.persistConfig(holder);
186
187         assertEquals(20, com.google.common.io.Files.readLines(file, Charsets.UTF_8).size());
188
189         List<ConfigSnapshotHolder> lastConf = storage.loadLastConfigs();
190         assertEquals(1, lastConf.size());
191         ConfigSnapshotHolder configSnapshotHolder = lastConf.get(0);
192         assertXMLEqual("<config>1</config>", configSnapshotHolder.getConfigSnapshot());
193         assertEquals(Sets.newHashSet("f1-11", "f2-22"), storage.getPersistedFeatures());
194     }
195
196     @Test
197     public void testNoFeaturesStored() throws Exception {
198         PropertiesProviderTest pp = new PropertiesProviderTest();
199         pp.addProperty("fileStorage",file.getPath());
200         pp.addProperty("numberOfBackups",Integer.toString(Integer.MAX_VALUE));
201         storage.instantiate(pp);
202
203         com.google.common.io.Files.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
204                 "<persisted-snapshots>\n" +
205                 "   <snapshots>\n" +
206                 "      <snapshot>\n" +
207                 "         <required-capabilities>\n" +
208                 "            <capability>cap12</capability>\n" +
209                 "         </required-capabilities>\n" +
210                 "         <configuration>\n" +
211                 "            <config>1</config>\n" +
212                 "         </configuration>\n" +
213                 "      </snapshot>\n" +
214                 "   </snapshots>\n" +
215                 "</persisted-snapshots>", file, Charsets.UTF_8);
216
217         List<ConfigSnapshotHolder> lastConf = storage.loadLastConfigs();
218         assertEquals(1, lastConf.size());
219         ConfigSnapshotHolder configSnapshotHolder = lastConf.get(0);
220         assertXMLEqual("<config>1</config>", configSnapshotHolder.getConfigSnapshot());
221         assertTrue(storage.getPersistedFeatures().isEmpty());
222     }
223
224     @Test
225     public void testFileAdapterOnlyTwoBackups() throws Exception {
226         storage.setFileStorage(file);
227         storage.setNumberOfBackups(2);
228         final ConfigSnapshotHolder holder = new ConfigSnapshotHolder() {
229             @Override
230             public String getConfigSnapshot() {
231                 return createConfig();
232             }
233
234             @Override
235             public SortedSet<String> getCapabilities() {
236                 return createCaps();
237             }
238         };
239         storage.persistConfig(holder);
240
241         storage.persistConfig(holder);
242         storage.persistConfig(holder);
243
244         List<String> readLines = com.google.common.io.Files.readLines(file, Charsets.UTF_8);
245         assertEquals(29, readLines.size());
246
247         List<ConfigSnapshotHolder> lastConf = storage.loadLastConfigs();
248         assertEquals(1, lastConf.size());
249         ConfigSnapshotHolder configSnapshotHolder = lastConf.get(0);
250         assertXMLEqual("<config>3</config>", configSnapshotHolder.getConfigSnapshot());
251         assertFalse(readLines.contains(holder.getConfigSnapshot()));
252         assertTrue(storage.getPersistedFeatures().isEmpty());
253     }
254
255     @Test
256     public void testNoLastConfig() throws Exception {
257         File file = Files.createTempFile("testFilePersist", ".txt").toFile();
258         file.deleteOnExit();
259         if (!file.exists()) {
260             return;
261         }
262         try (XmlFileStorageAdapter storage = new XmlFileStorageAdapter()) {
263             storage.setFileStorage(file);
264
265             List<ConfigSnapshotHolder> elementOptional = storage.loadLastConfigs();
266             assertThat(elementOptional.size(), is(0));
267         }
268     }
269
270     @Test(expected = NullPointerException.class)
271     public void testNoProperties() throws Exception {
272         try (XmlFileStorageAdapter storage = new XmlFileStorageAdapter()) {
273             storage.loadLastConfigs();
274         }
275     }
276
277     @Test(expected = NullPointerException.class)
278     public void testNoProperties2() throws Exception {
279         try (XmlFileStorageAdapter storage = new XmlFileStorageAdapter()) {
280             storage.persistConfig(new ConfigSnapshotHolder() {
281                 @Override
282                 public String getConfigSnapshot() {
283                     return mock(String.class);
284                 }
285
286                 @Override
287                 public SortedSet<String> getCapabilities() {
288                     return new TreeSet<>();
289                 }
290             });
291         }
292     }
293
294     static String createConfig() {
295         return "<config>" + i++ + "</config>";
296     }
297
298     private void delete(final File f) throws IOException {
299         if (f.isDirectory()) {
300             for (File c : f.listFiles()) {
301                 delete(c);
302             }
303         }
304         if (!f.delete()) {
305             throw new FileNotFoundException("Failed to delete file: " + f);
306         }
307     }
308 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.